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


n, w = map(int, input().split())
info = [list(map(int, input().split())) for i in range(n)]

dp = [0]*(w + 1)
for i in range(n):
    for j in range(w + 1)[::-1]:
        if j - info[i][0] < 0:
            break
        else:
            dp[j] = max(dp[j], dp[j - info[i][0]] + info[i][1])
print(dp[w])

Fixed submission:
n,w = map(int,input().split())
info = [[0,0]]+[list(map(int,input().split())) for i in range(n)]

## dp[i][j]:i番目までのアイテムを重さjのナップサックに詰めるときの最大価値
dp = [[0]*(w+1) for i in range(n+1)]
for i in range(n+1):
  for j in range(w+1):
    if j-info[i][0]>=0:
      dp[i][j] = max(dp[i-1][j],dp[i-1][j-info[i][0]]+info[i][1])
    else:
      dp[i][j] = dp[i-1][j]

print(dp[n][w])

Buggy python submission (verdict: Time Limit Exceeded):
n, w = map(int, input().split())
info = [list(map(int, input().split())) for i in range(n)]

dp = [0]*(w + 1)
for i in range(n):
    for j in range(w + 1)[::-1]:
        if j - info[i][0] < 0:
            break
        else:
            dp[j] = max(dp[j], dp[j - info[i][0]] + info[i][1])
print(dp[w])

Fixed submission:
n,w = map(int,input().split())
info = [[0,0]]+[list(map(int,input().split())) for i in range(n)]

## dp[i][j]:i番目までのアイテムを重さjのナップサックに詰めるときの最大価値
dp = [[0]*(w+1) for i in range(n+1)]
for i in range(n+1):
  for j in range(w+1):
    if j-info[i][0]>=0:
      dp[i][j] = max(dp[i-1][j],dp[i-1][j-info[i][0]]+info[i][1])
    else:
      dp[i][j] = dp[i-1][j]

print(dp[n][w])

Buggy python submission (verdict: Runtime Error):
n,w = map(int,input().split())
info = [[0,0]]+[list(map(int,input().split())) for i in range(n)]
v = 0
for i in range(n+1):
  v += info[i][1]
n = n+1
## dp[i][j]:i番目までのアイテムを詰めたときに総価値jになるときの最小重さ
dp = [[float("inf")]*(v) for i in range(n)]

for i in range(n):
  dp[i][0] = 0

for i in range(1,n):
  for j in range(1,v):
    if j-info[i][1]>=0:
      dp[i][j] = min(dp[i-1][j],dp[i-1][j-info[i][1]]+info[i][0])
    else:
      dp[i][j] = dp[i-1][j]

for j in range(v):
  if dp[n-1][j] <= w:
    ans = j
print(ans)

Fixed submission:
n,w = map(int,input().split())
info = [[0,0]]+[list(map(int,input().split())) for i in range(n)]

## dp[i][j]:i番目までのアイテムを重さjのナップサックに詰めるときの最大価値
dp = [[0]*(w+1) for i in range(n+1)]
for i in range(n+1):
  for j in range(w+1):
    if j-info[i][0]>=0:
      dp[i][j] = max(dp[i-1][j],dp[i-1][j-info[i][0]]+info[i][1])
    else:
      dp[i][j] = dp[i-1][j]

print(dp[n][w])

Buggy python submission (verdict: Time Limit Exceeded):
n,max_w = map(int,input().split())
w=[]
v=[]

for i in range(n):
  tmp_w,tmp_v = map(int,input().split()) 
  w.append(tmp_w)
  v.append(tmp_v)

dp =[[-1]*(max_w+1) for i in range(n+1)]
  
#i番目以降の品物から、重さの総和がj以下となるように選ぶ
def knapsac(i,j):
  if dp[i][j]>=0:
    return dp[i][j]
  if i == n:
    #品物は残っていない
    dp[i][j] = 0
  elif j < w[i]:
    #品物が入らない
    dp[i][j] = knapsac(i+1,j)
  else:
    #品物が入る→品物を入れる場合と入れない場合を両方試す
    dp[i][j] = max(knapsac(i+1,j),knapsac(i+1,j-w[i])+v[i])
  return dp[i][j]

print(knapsac(0,max_w))

Fixed submission:
n,w = map(int,input().split())
info = [[0,0]]+[list(map(int,input().split())) for i in range(n)]

## dp[i][j]:i番目までのアイテムを重さjのナップサックに詰めるときの最大価値
dp = [[0]*(w+1) for i in range(n+1)]
for i in range(n+1):
  for j in range(w+1):
    if j-info[i][0]>=0:
      dp[i][j] = max(dp[i-1][j],dp[i-1][j-info[i][0]]+info[i][1])
    else:
      dp[i][j] = dp[i-1][j]

print(dp[n][w])

Buggy python submission (verdict: Time Limit Exceeded):
n,max_w = map(int,input().split())
w=[]
v=[]

for i in range(n):
  tmp_w,tmp_v = map(int,input().split()) 
  w.append(tmp_w)
  v.append(tmp_v)
  
dp=[[0]*(max_w+1) for i in range(n)]

#dp[i番目の品まで入れられる][重さがj以下]
for j in range(max_w+1):
  if j < w[0]:
    dp[0][j]=0
  else:
    dp[0][j]=v[0]
    
for i in range(0,n-1):
  for j in range(max_w+1):
    if j < w[i+1]:
      dp[i+1][j] = dp[i][j]
    else:
      dp[i+1][j] = max(dp[i][j],dp[i][j-w[i+1]]+v[i+1])
print(dp[n-1][max_w])

Fixed submission:
n,w = map(int,input().split())
info = [[0,0]]+[list(map(int,input().split())) for i in range(n)]

## dp[i][j]:i番目までのアイテムを重さjのナップサックに詰めるときの最大価値
dp = [[0]*(w+1) for i in range(n+1)]
for i in range(n+1):
  for j in range(w+1):
    if j-info[i][0]>=0:
      dp[i][j] = max(dp[i-1][j],dp[i-1][j-info[i][0]]+info[i][1])
    else:
      dp[i][j] = dp[i-1][j]

print(dp[n][w])