from bisect import bisect_right



N = int(eval(input()))

HPs = [tuple(map(int, input().split())) for i in range(N)]



HPs.sort(key=lambda x: x[0] + x[1])



# i人目まで考慮して、j人積んだ場合の積まれている高さの最小値

# 計算量削減のため、dp[j]=∞であるjのうち、最小のj以外は省略する

INF = float('inf')

dp = [0] + [INF]

for H, P in HPs:

    iH = bisect_right(dp, H)

    dp0 = [0] + [hgt + P for hgt in dp[:iH]] + [INF] * (len(dp) - iH)

    dp = [x if x <= y else y for x, y in zip(dp0, dp)]



    if dp[-1] != INF:

        dp += [INF]



print((len(dp) - 2))

from bisect import bisect_right



N = int(eval(input()))

HPs = [tuple(map(int, input().split())) for i in range(N)]



HPs.sort(key=lambda x: x[0] + x[1])



# i人目まで考慮して、j人積んだ場合の積まれている高さの最小値

# 計算量削減のため、dp[j]=∞であるjのうち、最小のj以外は省略する

INF = float('inf')

dp = [0] + [INF]

lenDP = 2

num = 0

for i, (H, P) in enumerate(HPs, 1):

    iH = bisect_right(dp, H)

    x = lenDP - N + i - 2



    if x <= 0:

        dp0 = [0] + [hgt + P for hgt in dp[:iH]] + [INF] * (len(dp) - iH)

        dp = [x if x <= y else y for x, y in zip(dp0, dp)]

    else:

        # 最終的な答えに影響を与えなくなった要素を省略する

        dp0 = [hgt + P for hgt in dp[x-1:iH]] + [INF] * (len(dp) - iH)

        dp = [x if x <= y else y for x, y in zip(dp0, dp[x:])]

        lenDP -= x

        num += x



    if dp[-1] != INF:

        dp += [INF]

        lenDP += 1



print((num + len(dp) - 2))

from bisect import bisect_right

N = int(eval(input()))
HPs = [tuple(map(int, input().split())) for i in range(N)]
HPs.sort(key=lambda x: x[0] + x[1])
# i人目まで考慮して、j人積んだ場合の積まれている高さの最小値
# 計算量削減のため、dp[j]=∞であるjのうち、最小のj以外は省略する
INF = float("inf")
dp = [0] + [INF]
for H, P in HPs:
    iH = bisect_right(dp, H)
    dp0 = [0] + [hgt + P for hgt in dp[:iH]] + [INF] * (len(dp) - iH)
    dp = [x if x <= y else y for x, y in zip(dp0, dp)]
    if dp[-1] != INF:
        dp += [INF]
print((len(dp) - 2))

from bisect import bisect_right

N = int(eval(input()))
HPs = [tuple(map(int, input().split())) for i in range(N)]
HPs.sort(key=lambda x: x[0] + x[1])
# i人目まで考慮して、j人積んだ場合の積まれている高さの最小値
# 計算量削減のため、dp[j]=∞であるjのうち、最小のj以外は省略する
INF = float("inf")
dp = [0] + [INF]
lenDP = 2
num = 0
for i, (H, P) in enumerate(HPs, 1):
    iH = bisect_right(dp, H)
    x = lenDP - N + i - 2
    if x <= 0:
        dp0 = [0] + [hgt + P for hgt in dp[:iH]] + [INF] * (len(dp) - iH)
        dp = [x if x <= y else y for x, y in zip(dp0, dp)]
    else:
        # 最終的な答えに影響を与えなくなった要素を省略する
        dp0 = [hgt + P for hgt in dp[x - 1 : iH]] + [INF] * (len(dp) - iH)
        dp = [x if x <= y else y for x, y in zip(dp0, dp[x:])]
        lenDP -= x
        num += x
    if dp[-1] != INF:
        dp += [INF]
        lenDP += 1
print((num + len(dp) - 2))

-for H, P in HPs: +lenDP = 2 +num = 0 +for i, (H, P) in enumerate(HPs, 1): -    dp0 = [0] + [hgt + P for hgt in dp[:iH]] + [INF] * (len(dp) - iH) -    dp = [x if x <= y else y for x, y in zip(dp0, dp)] +    x = lenDP - N + i - 2 +    if x <= 0: +        dp0 = [0] + [hgt + P for hgt in dp[:iH]] + [INF] * (len(dp) - iH) +        dp = [x if x <= y else y for x, y in zip(dp0, dp)] +    else: +        # 最終的な答えに影響を与えなくなった要素を省略する +        dp0 = [hgt + P for hgt in dp[x - 1 : iH]] + [INF] * (len(dp) - iH) +        dp = [x if x <= y else y for x, y in zip(dp0, dp[x:])] +        lenDP -= x +        num += x -print((len(dp) - 2)) +        lenDP += 1 +print((num + len(dp) - 2))