Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input():
	return sys.stdin.buffer.readline()[:-1]
n = int(input())
men = [tuple(map(int, input().split())) for _ in range(n)]
men.sort(key=lambda x: (x[0]+x[1], -x[0]))
allowed = [-1 for _ in range(n)]
allowed[n-1] = men[-1][0]
for i in range(n-2, -1, -1):
	allowed[i] = max(allowed[i+1], men[i][0])

INF = 10**10
dp = [[INF for _ in range(n+1)] for _ in range(n+1)]
dp[0][0] = 0
for i in range(n):
	dp[i+1][0] = 0
	for j in range(1, i+2):
		dp[i+1][j] = min(dp[i+1][j], dp[i][j])
		if dp[i][j-1] <= men[i][0]:
			dp[i+1][j] = min(dp[i+1][j], dp[i][j-1] + men[i][1])

#print(dp)
for i in range(n+1):
	if dp[n][i] < INF:
		ans = i
print(ans)

Fixed submission:
import sys
def input():
	return sys.stdin.buffer.readline()[:-1]
n = int(input())
men = [tuple(map(int, input().split())) for _ in range(n)]
men.sort(key=lambda x: (x[0]+x[1], -x[0]))
allowed = [-1 for _ in range(n)]
allowed[n-1] = men[-1][0]
for i in range(n-2, -1, -1):
	allowed[i] = max(allowed[i+1], men[i][0])

INF = 10**10
dp = [INF for _ in range(n+1)]
dp[0] = 0
for i in range(n):
	for j in range(i+1, 0, -1):
		if dp[j-1] <= men[i][0]:
			dp[j] = min(dp[j], dp[j-1] + men[i][1])

#print(dp)
for i in range(n+1):
	if dp[i] < INF:
		ans = i
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def input():
	return sys.stdin.buffer.readline()[:-1]
n = int(input())
men = [list(map(int, input().split())) for _ in range(n)]
men.sort(key=lambda x: (x[0]+x[1], -x[0]))
allowed = [-1 for _ in range(n)]
allowed[n-1] = men[-1][0]
for i in range(n-2, -1, -1):
	allowed[i] = max(allowed[i+1], men[i][0])

INF = 10**10
dp = [[INF for _ in range(n+1)] for _ in range(n+1)]
dp[0][0] = 0
for i in range(n):
	dp[i+1][0] = 0
	for j in range(1, i+2):
		dp[i+1][j] = min(dp[i+1][j], dp[i][j])
		if dp[i][j-1] <= men[i][0]:
			dp[i+1][j] = min(dp[i+1][j], dp[i][j-1] + men[i][1])

#print(dp)
for i in range(n+1):
	if dp[n][i] < INF:
		ans = i
print(ans)

Fixed submission:
import sys
def input():
	return sys.stdin.buffer.readline()[:-1]
n = int(input())
men = [tuple(map(int, input().split())) for _ in range(n)]
men.sort(key=lambda x: (x[0]+x[1], -x[0]))
allowed = [-1 for _ in range(n)]
allowed[n-1] = men[-1][0]
for i in range(n-2, -1, -1):
	allowed[i] = max(allowed[i+1], men[i][0])

INF = 10**10
dp = [INF for _ in range(n+1)]
dp[0] = 0
for i in range(n):
	for j in range(i+1, 0, -1):
		if dp[j-1] <= men[i][0]:
			dp[j] = min(dp[j], dp[j-1] + men[i][1])

#print(dp)
for i in range(n+1):
	if dp[i] < INF:
		ans = i
print(ans)

Buggy python submission (verdict: Runtime Error):
n = int(input())
men = [list(map(int, input().split())) for _ in range(n)]
men.sort(key=lambda x: x[0]+x[1], x[0])
dp = [[0, 0] for _ in range(n+1)]
for i in range(1, n+1):
	for j in range(i):
		if dp[j][0] <= men[i-1][0]:
			if dp[j][1]+1 == dp[i][1]:
				dp[i][0] = min(dp[i][0], dp[j][0] + men[i-1][1])
			elif dp[j][1]+1 > dp[i][1]:
				dp[i][1] = dp[j][1]+1
				dp[i][0] = dp[j][0] + men[i-1][1]
ans = 0
for i in range(1, n+1):
	ans = max(ans, dp[i][1])
print(ans)

Fixed submission:
import sys
def input():
	return sys.stdin.buffer.readline()[:-1]
n = int(input())
men = [tuple(map(int, input().split())) for _ in range(n)]
men.sort(key=lambda x: (x[0]+x[1], -x[0]))
allowed = [-1 for _ in range(n)]
allowed[n-1] = men[-1][0]
for i in range(n-2, -1, -1):
	allowed[i] = max(allowed[i+1], men[i][0])

INF = 10**10
dp = [INF for _ in range(n+1)]
dp[0] = 0
for i in range(n):
	for j in range(i+1, 0, -1):
		if dp[j-1] <= men[i][0]:
			dp[j] = min(dp[j], dp[j-1] + men[i][1])

#print(dp)
for i in range(n+1):
	if dp[i] < INF:
		ans = i
print(ans)