Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline
n,t=map(int,input().split())
dp=[[[0,0] for i in range(t)] for j in range(n+1)]
food=[list(map(int,input().split())) for i in range(n)]
foodmax=[0]*n
food.sort(key=lambda x:x[1],reverse=True)
food.sort(key=lambda x:x[0])
foodmax[-1]=food[-1][1]
for i in range(n-1)[::-1]:
  foodmax[i]=max(foodmax[i+1],food[i][1])
foodmax+=[0]
for i in range(n):
  for j in range(t):
    if j<food[i][0]:
      dp[i+1][j]=dp[i][j]
    else:
      dp[i+1][j][0]=max(dp[i][j][0],dp[i][j-food[i][0]][0]+food[i][1])
      if dp[i][j][0]>=dp[i][j-food[i][0]][0]+food[i][1]:
        dp[i+1][j][1]=dp[i][j][1]
      else:
        dp[i+1][j][1]=i
ans=0
for i in range(n+1):
  if ans<dp[i][t-1][0]+foodmax[dp[i][t-1][1]+1]:
    ans=dp[i][t-1][0]+foodmax[dp[i][t-1][1]+1]
print(ans)

Fixed submission:
import sys
input=sys.stdin.readline
n,t=map(int,input().split())
dp=[[0 for i in range(t)] for j in range(n+1)]
food=[list(map(int,input().split())) for i in range(n)]
foodmax=[0]*n
food.sort(key=lambda x:x[1],reverse=True)
food.sort(key=lambda x:x[0])
foodmax[-1]=food[-1][1]
for i in range(n-1)[::-1]:
  foodmax[i]=max(foodmax[i+1],food[i][1])
foodmax+=[0]
for i in range(n):
  for j in range(t):
    if j<food[i][0]:
      dp[i+1][j]=dp[i][j]
    else:
      dp[i+1][j]=max(dp[i][j],dp[i][j-food[i][0]]+food[i][1])
      
ans=0
for i in range(n):
  if ans<dp[i+1][t-1]+foodmax[i+1]:
    ans=dp[i+1][t-1]+foodmax[i+1]
print(ans)

Buggy python submission (verdict: Runtime Error):
n,t=map(int,input().split())
dp=[[[0,0] for i in range(t)] for j in range(n+1)]
food=[list(map(int,input().split())) for i in range(n)]
foodmax=[0]*n
food.sort(key=lambda x:x[1],reverse=True)
food.sort(key=lambda x:x[0])
foodmax[-1]=food[-1][1]
for i in range(n-1)[::-1]:
  foodmax[i]=max(foodmax[i+1],food[i][1])
for i in range(n):
  for j in range(t):
    if j<food[i][0]:
      dp[i+1][j]=dp[i][j]
    else:
      dp[i+1][j][0]=max(dp[i][j][0],dp[i][j-food[i][0]][0]+food[i][1])
      if dp[i][j][0]>=dp[i][j-food[i][0]][0]+food[i][1]:
        dp[i+1][j][1]=dp[i][j][1]
      else:
        dp[i+1][j][1]=i
ans=0
for i in range(n+1):
  if ans<dp[i][t-1][0]+foodmax[dp[i][t-1][1]+1]:
    ans=dp[i][t-1][0]+foodmax[dp[i][t-1][1]+1]
print(ans)

Fixed submission:
import sys
input=sys.stdin.readline
n,t=map(int,input().split())
dp=[[0 for i in range(t)] for j in range(n+1)]
food=[list(map(int,input().split())) for i in range(n)]
foodmax=[0]*n
food.sort(key=lambda x:x[1],reverse=True)
food.sort(key=lambda x:x[0])
foodmax[-1]=food[-1][1]
for i in range(n-1)[::-1]:
  foodmax[i]=max(foodmax[i+1],food[i][1])
foodmax+=[0]
for i in range(n):
  for j in range(t):
    if j<food[i][0]:
      dp[i+1][j]=dp[i][j]
    else:
      dp[i+1][j]=max(dp[i][j],dp[i][j-food[i][0]]+food[i][1])
      
ans=0
for i in range(n):
  if ans<dp[i+1][t-1]+foodmax[i+1]:
    ans=dp[i+1][t-1]+foodmax[i+1]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,t=map(int,input().split())
dp=[[[0,0] for i in range(t)] for j in range(n+1)]
food=[list(map(int,input().split())) for i in range(n)]
food.sort(key=lambda x:x[1],reverse=True)
food.sort(key=lambda x:x[0])
for i in range(n):
  for j in range(t):
    if j<food[i][0]:
      dp[i+1][j]=dp[i][j]
    else:
      dp[i+1][j][0]=max(dp[i][j][0],dp[i][j-food[i][0]][0]+food[i][1])
      if dp[i][j][0]>=dp[i][j-food[i][0]][0]+food[i][1]:
        dp[i+1][j][1]=dp[i][j][1]
      else:
        dp[i+1][j][1]=i
ans=0
for i in range(n+1):
  last=0
  for j in range(dp[i][t-1][1]+1,n):
    if food[j][1]>last:last=food[j][1]
  if ans<dp[i][t-1][0]+last:
    ans=dp[i][t-1][0]+last
print(ans)

Fixed submission:
import sys
input=sys.stdin.readline
n,t=map(int,input().split())
dp=[[0 for i in range(t)] for j in range(n+1)]
food=[list(map(int,input().split())) for i in range(n)]
foodmax=[0]*n
food.sort(key=lambda x:x[1],reverse=True)
food.sort(key=lambda x:x[0])
foodmax[-1]=food[-1][1]
for i in range(n-1)[::-1]:
  foodmax[i]=max(foodmax[i+1],food[i][1])
foodmax+=[0]
for i in range(n):
  for j in range(t):
    if j<food[i][0]:
      dp[i+1][j]=dp[i][j]
    else:
      dp[i+1][j]=max(dp[i][j],dp[i][j-food[i][0]]+food[i][1])
      
ans=0
for i in range(n):
  if ans<dp[i+1][t-1]+foodmax[i+1]:
    ans=dp[i+1][t-1]+foodmax[i+1]
print(ans)