n = int(input()) blocks = [] for _ in range(n): w, s = map(int, input().split()) blocks.append((s + w, w, s)) # Sort blocks by the sum of s_i and w_i in ascending order blocks.sort() # Initialize DP array where dp[k] is the minimal cumulative weight for k blocks dp = [float('inf')] * (n + 1) dp[0] = 0 for sw, w, s in blocks: # Update in reverse to prevent using the same block multiple times for k in range(n, 0, -1): if dp[k-1] <= s: # Check if adding this block to k-1 blocks is better dp[k] = min(dp[k], dp[k-1] + w) # Find the maximum number of blocks that can be stacked max_blocks = 0 for k in range(n, 0, -1): if dp[k] < float('inf'): max_blocks = k break print(max_blocks)