Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

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

DP = [[[0] * 2 for _ in range(T)] for _ in range(N)]

a, b = AB[0]
if a < T:
  DP[0][a][0] = b
DP[0][0][1] = b

for i in range(1, N):
  a, b = AB[i]
  if a < T:
    DP[i][a][0] = b
  DP[i][0][1] = b
  for j in range(T):
    DP[i][j][0] = max(DP[i][j][0], DP[i - 1][j][0])
    DP[i][j][1] = max(DP[i][j][1], DP[i - 1][j][1])
    if j + a < T: 
      DP[i][j + a][0] = max(DP[i][j + a][0], DP[i - 1][j][0] + b)
      DP[i][j + a][1] = max(DP[i][j + a][1], DP[i - 1][j][1] + b)
    DP[i][j][1] = max(DP[i][j][1], DP[i - 1][j][1])
    DP[i][j][1] = max(DP[i][j][1], DP[i - 1][j][0] + b)

ans = 0
for i in range(T):
  ans = max(ans, DP[-1][i][0])
  ans = max(ans, DP[-1][i][1])

print(ans)

Fixed submission:
N, T = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(N)]


AB.sort(reverse = True)
DP = [[0] * T for _ in range(N)]
DP[0][0] = AB[0][1]
for i in range(1, N):
  a, b = AB[i]
  DP[i][0] = max(DP[i - 1][0], b)
  for j in range(T):
    DP[i][j] = max(DP[i][j], DP[i - 1][j])
    if j + a < T:
      DP[i][j + a] = DP[i - 1][j] + b

print(max(DP[-1]))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

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

DP = [[[0] * 2 for _ in range(T)] for _ in range(N)]

a, b = AB[0]
if a < T:
  DP[0][a][0] = b
DP[0][0][1] = b

for i in range(1, N):
  a, b = AB[i]
  if a < T:
    DP[i][a][0] = b
  DP[i][0][1] = b
  for j in range(T):
    DP[i][j][0] = max(DP[i][j][0], DP[i - 1][j][0])
    DP[i][j][1] = max(DP[i][j][1], DP[i - 1][j][1])
    if j + a < T: 
      DP[i][j + a][0] = max(DP[i][j + a][0], DP[i - 1][j][0] + b)
      DP[i][j + a][1] = max(DP[i][j + a][1], DP[i - 1][j][1] + b)
    DP[i][j][1] = max(DP[i][j][1], DP[i - 1][j][1])
    DP[i][j][1] = max(DP[i][j][1], DP[i - 1][j][0] + b)

ans = 0
for i in range(T):
  ans = max(ans, DP[-1][i][0])
  ans = max(ans, DP[-1][i][1])

print(ans)

Fixed submission:
N, T = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(N)]


AB.sort(reverse = True)
DP = [[0] * T for _ in range(N)]
DP[0][0] = AB[0][1]
for i in range(1, N):
  a, b = AB[i]
  DP[i][0] = max(DP[i - 1][0], b)
  for j in range(T):
    DP[i][j] = max(DP[i][j], DP[i - 1][j])
    if j + a < T:
      DP[i][j + a] = DP[i - 1][j] + b

print(max(DP[-1]))

Buggy python submission (verdict: Time Limit Exceeded):
N, T = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(N)]

DP = [[[0] * 2 for _ in range(T)] for _ in range(N)]

a, b = AB[0]
if a < T:
  DP[0][a][0] = b
DP[0][0][1] = b

for i in range(1, N):
  a, b = AB[i]
  if a < T:
    DP[i][a][0] = b
  DP[i][0][1] = b
  for j in range(T):
    DP[i][j][0] = max(DP[i][j][0], DP[i - 1][j][0])
    DP[i][j][1] = max(DP[i][j][1], DP[i - 1][j][1])
    if j + a < T: 
      DP[i][j + a][0] = max(DP[i][j + a][0], DP[i - 1][j][0] + b)
      DP[i][j + a][1] = max(DP[i][j + a][1], DP[i - 1][j][1] + b)
    DP[i][j][1] = max(DP[i][j][1], DP[i - 1][j][1])
    DP[i][j][1] = max(DP[i][j][1], DP[i - 1][j][0] + b)

ans = 0
for i in range(T):
  ans = max(ans, DP[-1][i][0])
  ans = max(ans, DP[-1][i][1])

print(ans)

Fixed submission:
N, T = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(N)]


AB.sort(reverse = True)
DP = [[0] * T for _ in range(N)]
DP[0][0] = AB[0][1]
for i in range(1, N):
  a, b = AB[i]
  DP[i][0] = max(DP[i - 1][0], b)
  for j in range(T):
    DP[i][j] = max(DP[i][j], DP[i - 1][j])
    if j + a < T:
      DP[i][j + a] = DP[i - 1][j] + b

print(max(DP[-1]))

Buggy python submission (verdict: Runtime Error):
N, T = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(N)]

ans = 0

DP = [0] * T

for i in range(N):
  a, b = AB[i]
  for j in range(T - 1, -1, -1):
    if j + a >= T:
      ans = max(ans, DP[j] + b)
    else:
      DP[j + a] = max(DP[j + a], DP[j] + b)

for i in range(N):
  ans = max(ans, DP[i])
print(ans)

Fixed submission:
N, T = list(map(int, input().split()))
AB = [list(map(int, input().split())) for _ in range(N)]


AB.sort(reverse = True)
DP = [[0] * T for _ in range(N)]
DP[0][0] = AB[0][1]
for i in range(1, N):
  a, b = AB[i]
  DP[i][0] = max(DP[i - 1][0], b)
  for j in range(T):
    DP[i][j] = max(DP[i][j], DP[i - 1][j])
    if j + a < T:
      DP[i][j + a] = DP[i - 1][j] + b

print(max(DP[-1]))