N = int(eval(input()))

HP = [list(map(int,input().split())) for _ in range(N)]

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

ceil = max(HP,key=lambda x: x[0])[0]

#dp[i][j] -> i人目まで見る。j人積むときの、座布団の枚数の最小値

dp = [[ceil+1]*(N+1) for _ in range(N+1)]

ans = 0

for i in range(N+1):

    dp[i][0] = 0

for i in range(1,N+1):

    h,p = HP[i-1]

    for j in range(1,N+1):

        if dp[i-1][j-1]<=h:

            dp[i][j] = min(dp[i-1][j],dp[i-1][j-1]+p)

            ans = max(ans,j)

        else:

            dp[i][j] = dp[i-1][j]



print(ans)
N = int(eval(input()))

"""

dp[i][j] -> i番目の人間までみる。j人が積むときの最小値。



"""

HP = [list(map(int,input().split())) for _ in range(N)]

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

dp = [float("INF")]*(N+1)

dp[0] = 0

for i in range(1,N+1):

    for j in range(N,0,-1):

        if dp[j-1] <= HP[i-1][0]:

            dp[j] = min(dp[j],dp[j-1]+HP[i-1][1])

for j in range(N,-1,-1):

    if dp[j] != float("INF"):

        print(j)

        break

N = int(eval(input()))
HP = [list(map(int, input().split())) for _ in range(N)]
HP.sort(key=lambda x: x[0] + x[1])
ceil = max(HP, key=lambda x: x[0])[0]
# dp[i][j] -> i人目まで見る。j人積むときの、座布団の枚数の最小値
dp = [[ceil + 1] * (N + 1) for _ in range(N + 1)]
ans = 0
for i in range(N + 1):
    dp[i][0] = 0
for i in range(1, N + 1):
    h, p = HP[i - 1]
    for j in range(1, N + 1):
        if dp[i - 1][j - 1] <= h:
            dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - 1] + p)
            ans = max(ans, j)
        else:
            dp[i][j] = dp[i - 1][j]
print(ans)

N = int(eval(input()))
"""
dp[i][j] -> i番目の人間までみる。j人が積むときの最小値。
"""
HP = [list(map(int, input().split())) for _ in range(N)]
HP.sort(key=lambda x: x[0] + x[1])
dp = [float("INF")] * (N + 1)
dp[0] = 0
for i in range(1, N + 1):
    for j in range(N, 0, -1):
        if dp[j - 1] <= HP[i - 1][0]:
            dp[j] = min(dp[j], dp[j - 1] + HP[i - 1][1])
for j in range(N, -1, -1):
    if dp[j] != float("INF"):
        print(j)
        break

+""" +dp[i][j] -> i番目の人間までみる。j人が積むときの最小値。 +""" -ceil = max(HP, key=lambda x: x[0])[0] -# dp[i][j] -> i人目まで見る。j人積むときの、座布団の枚数の最小値 -dp = [[ceil + 1] * (N + 1) for _ in range(N + 1)] -ans = 0 -for i in range(N + 1): -    dp[i][0] = 0 +dp = [float("INF")] * (N + 1) +dp[0] = 0 -    h, p = HP[i - 1] -    for j in range(1, N + 1): -        if dp[i - 1][j - 1] <= h: -            dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - 1] + p) -            ans = max(ans, j) -        else: -            dp[i][j] = dp[i - 1][j] -print(ans) +    for j in range(N, 0, -1): +        if dp[j - 1] <= HP[i - 1][0]: +            dp[j] = min(dp[j], dp[j - 1] + HP[i - 1][1]) +for j in range(N, -1, -1): +    if dp[j] != float("INF"): +        print(j) +        break