In the final of CODE FESTIVAL, there are N participants with heights and powers. Ringo is hosting a game where participants add zabuton to a stack. Each participant can add zabuton based on their height and power. The goal is to maximize the number of participants who can add zabuton. Constraints: 1 ≤ N ≤ 5000, 0 ≤ Hi ≤ 10^9, 1 ≤ Pi ≤ 10^9. Input format: N, H1 P1, H2 P2, ..., HN PN. Output: Maximum number of participants who can add zabuton.

# Buggy version:
import bisect

N=int(input())
hito=[]
for i in range(N):
    h,p=map(int,input().split())
    hito.append((h+p,h,p))

hito.sort()
print(hito)
data=[0]
for i in range(0,N):
    gomi,h,p=hito[i]
    for j in range(len(data)-1,-1,-1):
        if j==len(data)-1:
            if h>=data[-1]:
                data.append(data[-1]+p)
            if h>=data[j-1] and j!=0:
                data[j]=min(data[j],data[j-1]+p)
        else:
            if h>=data[j-1] and j!=0:
                data[j]=min(data[j],data[j-1]+p)
    

print(len(data)-1)



# Fixed version:
import bisect

N=int(input())
hito=[]
for i in range(N):
    h,p=map(int,input().split())
    hito.append((h+p,h,p))

hito.sort()
data=[0]
for i in range(0,N):
    gomi,h,p=hito[i]
    for j in range(len(data)-1,-1,-1):
        if j==len(data)-1:
            if h>=data[-1]:
                data.append(data[-1]+p)
            if h>=data[j-1] and j!=0:
                data[j]=min(data[j],data[j-1]+p)
        else:
            if h>=data[j-1] and j!=0:
                data[j]=min(data[j],data[j-1]+p)
    

print(len(data)-1)

# Buggy version:
N = int(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)
ans = 0
for i in range(N+1):
    dp[i][0] = 0
for i in range(N):
    h,p = HP[i]
    for j in range(N,0,-1):
        if dp[j-1]<=h:
            dp[j] = min(dp[j],dp[j-1]+p)
            ans = max(ans,j)

print(ans)

# Fixed version:
N = int(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)

# Buggy version:
n = int(input())
hp = [list(map(int,input().split())) for i in range(n)]
hp.sort(key = lambda x:x[0]+x[1])
dp = [[10**18 for i in range(n+1)] for j in range(n+1)]
dp[0][0] = 0
for i in range(1,n+1):
  dp[i] = dp[i-1][:]
  h,p = hp[i-1]
  for j in range(1,i+1):
    if dp[i-1][j-1] <= h:
      dp[i][j] = min(dp[i][j],dp[i-1][j-1]+p)
for i in range(n+1):
  if dp[-1][i] == 10**18:
    print(i-1)
    break

# Fixed version:
n = int(input())
hp = [list(map(int,input().split())) for i in range(n)]
hp.sort(key = lambda x:x[0]+x[1])
dp = [[10**18 for i in range(j+1)] for j in range(n+1)]
dp[0][0] = 0
for i in range(1,n+1):
  h,p = hp[i-1]
  for j in range(i+1):
    if j < i:
      dp[i][j] = dp[i-1][j]
    if j and dp[i-1][j-1] <= h:
      dp[i][j] = min(dp[i][j],dp[i-1][j-1]+p)
for i in range(n+1):
  if dp[-1][i] == 10**18:
    print(i-1)
    break
else:
  print(n)

# Buggy version:
def main():
    import sys
     input=sys.stdin.readline
    n = int(input())
    hp = sorted([tuple(map(int, input().split()))
                 for _ in [0]*n], key=lambda x: sum(x))
    dp = [10**10]*(n+1)
    dp[0] = 0
    for c, (h, p) in enumerate(hp):
        for k in range(c, -1, -1):
            if dp[k] <= h:
                dp[k+1] = min(dp[k+1], dp[k]+p)
    for i in range(n, -1, -1):
        if dp[i] != 10**10:
            print(i)
            break


if __name__ == '__main__':
    main()


# Fixed version:
def main():
    import sys
    input=sys.stdin.readline
    n=int(input())
    hp=sorted([list(map(int,input().split())) for _ in [0]*n],key=lambda x:sum(x))
    dp=[10**10]*(n+1)
    dp[0]=0
    for c,[h,p] in enumerate(hp):
        for k in range(c,-1,-1):
            if dp[k]<=h:
                dp[k+1]=min(dp[k+1],dp[k]+p)
    for i in range(n,-1,-1):
        if dp[i]!=10**10:
            print(i)
            break
if __name__=='__main__':
    main()