qyliang/CodeNanoFix huggingface dataset:
Buggy code:
N, W = map(int, input().split())

items = [list(map(int, input().split())) for i in range(N)]
v_sum = sum([i[1] for i in items])

inf = float("inf")
dp = [inf for i in range(v_sum + 1)]

dp[0] = 0
for i in range(N):
    for j in range(v_sum, -1, -1):
        if dp[j] > dp[j - items[i][1] + items[i][0]]:
            dp[j] = dp[j - items[i][1]] + items[i][0]

for i in range(v_sum, -1, -1):
    if dp[i] <= W:
        print(i)
        exit()


Fixed code:
N, W = map(int, input().split())

items = [list(map(int, input().split())) for _ in range(N)]
v_sum = sum([item[1] for item in items])

inf = float("inf")
dp = [inf for _ in range(v_sum + 1)]

dp[0] = 0
for i in range(N):
    for j in range(v_sum, -1, -1):
        if dp[j] > dp[j - items[i][1]] + items[i][0]:
            dp[j] = dp[j - items[i][1]] + items[i][0]

for i in range(v_sum, -1, -1):
    if dp[i] <= W:
        print(i)
        exit()
Buggy code:
n,w = [int(x) for x in input().split()]
wts = []
vals = []
for i in range(n):
    wi,vi = [int(x) for x in input().split()]
    wts.append(wi)
    vals.append(vi)

valsum = sum(vals)
dp = []
maxval = 0
for i  in range(n):
    dp.append([float('inf')]*(valsum+1))

for j in range(valsum+1):
    if j==vals[0]:
        dp[0][j] = wts[0]
        if wts[0]<=w:
            maxval = max(maxval,j)

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

for i in range(1,n):
    for j in range(1,valsum+1):
        if j>=vals[i]:
            dp[i][j] = min(dp[i-1][j-vals[i]]+wts[i],dp[i-1][j])
        else:
            dp[i][j] = dp[i-1][j]
        if dp[i][j]<=w:
            maxval = max(maxval,j)
print(dp)
print(maxval)
            


Fixed code:
n,w = [int(x) for x in input().split()]
wts = []
vals = []
for i in range(n):
    wi,vi = [int(x) for x in input().split()]
    wts.append(wi)
    vals.append(vi)

valsum = sum(vals)
dp = []
maxval = 0
for i  in range(n):
    dp.append([float('inf')]*(valsum+1))

for j in range(valsum+1):
    if j==vals[0]:
        dp[0][j] = wts[0]
        if wts[0]<=w:
            maxval = max(maxval,j)

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

for i in range(1,n):
    for j in range(1,valsum+1):
        if j>=vals[i]:
            dp[i][j] = min(dp[i-1][j-vals[i]]+wts[i],dp[i-1][j])
        else:
            dp[i][j] = dp[i-1][j]
        if dp[i][j]<=w:
            maxval = max(maxval,j)
print(maxval)
Buggy code:
import math

N, W = map(int, input().split())

DP = [[math.inf] * 100001 for _ in range(N+1)]
DP[0][0] = 0
ans = 0

for i in range(1, N+1):
    w, v = map(int, input().split())
    for j in range(1, 100001):
        if j >= v:
            DP[i][j] = min(DP[i-1][j], DP[i-1][j-v]+w)
        else:
            DP[i][j] = DP[i-1][j]

ans = 100000
while DP[N][ans] > W:
    ans -= 1
print(ans)


Fixed code:
import math

N, W = map(int, input().split())

DP = [[math.inf] * 100001 for _ in range(N+1)]
DP[0][0] = 0
for i in range(1, N+1):
    w, v = map(int, input().split())
    for j in range(100001):
        if j >= v:
            DP[i][j] = min(DP[i-1][j], DP[i-1][j-v]+w)
        else:
            DP[i][j] = DP[i-1][j]

ans = 100000
while DP[N][ans] > W:
    ans -= 1
print(ans)
Buggy code:
def solve():
    N, W = map(int, input().split())
    weight = []
    value = []
    
    for i in range(N):
        w,v = map(int,input().split())
        weight.append(w)
        value.append(v)

    maxvalue = max(value)
    dp = [[float('inf')]*(maxvalue+1) for _ in range(N+1)]
    for i in range(N+1):
        dp[i][0] = 0

    for i in range(N):
        for v in range(maxvalue+1):
            if v - value[i] < 0:
                dp[i+1][v] = min(dp[i+1][v], dp[i][v])
            else:
                dp[i+1][v] = min(dp[i+1][v], dp[i][v-value[i]]+weight[i])
                dp[i+1][v] = min(dp[i+1][v], dp[i][v])
    
    ans = 0
    for i in range(N+1):
        for j in range(maxvalue+1):
            if dp[i][j] <= W:
                ans = max(ans, j)
    
    print(ans)
            
if __name__ == '__main__':
    solve()

Fixed code:
def solve():
    N,W = map(int,input().split())
    weight = []
    value = []
    for i in range(N):
        w,v = map(int,input().split())
        weight.append(w)
        value.append(v)
    sum_value = sum(value)
    
    dp = [[float('inf')] * (sum_value+1) for _ in range(N+1)]
    for i in range(N+1):
        dp[i][0] = 0

    for i in range(1,N+1):
        for v in range(sum_value+1):
            if v - value[i-1] < 0:
                dp[i][v] = min(dp[i][v], dp[i-1][v])
            else:
                dp[i][v] = min(dp[i][v], dp[i-1][v-value[i-1]] + weight[i-1])
                dp[i][v] = min(dp[i][v], dp[i-1][v])
    
    ans = 0
    for i in range(N+1):
        for v in range(sum_value+1):
            if dp[i][v] <= W:
                ans = max(ans,v)
    
    print(ans)

if __name__ == '__main__':
    solve()
Buggy code:
N,W = map(int, input().split())
wv = [[int(i) for i in input().split()] for j in range(N)]
maxV = 10 ** 5
dp = [float('inf')]*(maxV+1)
dp[0] = 0

for w, v in wv:
    for i in range(maxV, -1, -1):
        dp[i] = min(dp[i], dp[i-v] + w)

print(dp)

for i in range(maxV, -1, -1):
    if dp[i] <= W:
        print(i)
        break

Fixed code:
N,W = map(int, input().split())
wv = [[int(i) for i in input().split()] for j in range(N)]
maxV = 10 ** 5
dp = [float('inf')]*(maxV+1)
dp[0] = 0

for w, v in wv:
    for i in range(maxV, -1, -1):
        dp[i] = min(dp[i], dp[i-v] + w)

for i in range(maxV, -1, -1):
    if dp[i] <= W:
        print(i)
        break
Buggy code:
n, W = map(int, input().split())
w = []
v = []
V = 0
for i in range(n):
    tmp1, tmp2 = map(int, input().split())
    w.append(tmp1)
    v.append(tmp2)
    V += tmp2

dp = [[float('inf') for _ in range(V+1)] for _ in range(n+1)]
dp[0][0] = 0

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

ans = 0
for i in range(V+1):
    if dp[-1][i] <= W: ans = i

#print(dp)
print(ans)

Fixed code:
n, W = map(int, input().split())
w = []
v = []
V = 0
for i in range(n):
    tmp1, tmp2 = map(int, input().split())
    w.append(tmp1)
    v.append(tmp2)
    V += tmp2

dp = [[float('inf') for _ in range(V+1)] for _ in range(n+1)]
dp[0][0] = 0

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

ans = 0
for i in range(V+1):
    if dp[-1][i] <= W: ans = i

#print(dp)
print(ans)
Buggy code:
N, W = map(int, input().split())
w = [0]*N
v = [0]*N
for i in range(N):
    w[i], v[i] = map(int, input().split())
vmax = max(v)
dp = [[0]*(N * vmax) for i in range(N + 1)]
dp[0][0] = 0
for j in range(N * vmax - 1):
    dp[0][j + 1] = float("inf")

for i in range(N):
    for j in range(N * vmax):
        if j < v[i]:
            dp[i + 1][j] = dp[i][j]
        else: 
            dp[i + 1][j] = min(dp[i][j], dp[i][j - v[i]] + w[i])

for j in range(1, N*vmax):
    if dp[N][N*vmax - j] <= W:
        print(N*vmax - j)
        break

Fixed code:
N, W = map(int, input().split())
w = [0]*N
v = [0]*N
for i in range(N):
    w[i], v[i] = map(int, input().split())
vmax = max(v)
dp = [[0]*(N * vmax + 1) for i in range(N + 1)]
dp[0][0] = 0
for j in range(N * vmax):
    dp[0][j + 1] = float("inf")

for i in range(N):
    for j in range(N * vmax + 1):
        if j < v[i]:
            dp[i + 1][j] = dp[i][j]
        else: 
            dp[i + 1][j] = min(dp[i][j], dp[i][j - v[i]] + w[i])

ans = 0
for j in range(N * vmax + 1):
    if dp[N][j] <= W:
        ans = j
print(ans)
Buggy code:
import sys

input = sys.stdin.readline


def main():
    N, W = map(int, input().split())
    t = [[int(i) for i in input().split()] for _ in range(N)]

    dp = [[float("inf")]*(N*10**3+1) for _ in range(N+1)]

    dp[0][0] = 0
    for n in range(1, N+1):
        dp[n][0] = 0
        for v in range(1, N*10**3+1):
            dp[n][v] = min(dp[n-1][v], dp[n-1][max(0, v-t[n-1][1])]+t[n-1][0])
    ans = float("inf")*-1
    for i, v in enumerate(dp[N]):
        if v <= W:
            ans = max(ans, i)

    print(ans)
    print(dp[N][:30])


if __name__ == "__main__":
    main()


Fixed code:
import sys

input = sys.stdin.readline


def main():
    N, W = map(int, input().split())
    t = [[int(i) for i in input().split()] for _ in range(N)]

    dp = [[float("inf")]*(N*10**3+1) for _ in range(N+1)]

    dp[0][0] = 0
    for n in range(1, N+1):
        dp[n][0] = 0
        for v in range(1, N*10**3+1):
            dp[n][v] = min(dp[n-1][v], dp[n-1][max(0, v-t[n-1][1])]+t[n-1][0])
    ans = float("inf")*-1
    for i, v in enumerate(dp[N]):
        if v <= W:
            ans = max(ans, i)

    print(ans)


if __name__ == "__main__":
    main()
Buggy code:
import copy

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

min_w_per_v = [10**11] * (10**6)
min_w_per_v[0] = 0

for i in range(N):
    w, v = wv[i]
    tmp = copy.deepcopy(min_w_per_v)
    for j in range(v, len(min_w_per_v)):
        min_w_per_v[j] = min(tmp[j], tmp[j-v]+w)
        
for n, i in enumerate(min_w_per_v[::-1]):
    if i <= W:
        print(10**6-n-1)
        break

Fixed code:
import copy

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

min_w_per_v = [10**11] * (10**5+3)
min_w_per_v[0] = 0

for i in range(N):
    w, v = wv[i]
    tmp = copy.deepcopy(min_w_per_v)
    for j in range(v, len(min_w_per_v)):
        min_w_per_v[j] = min(tmp[j], tmp[j-v]+w)
        
for n, i in enumerate(min_w_per_v[::-1]):
    if i <= W:
        print(10**5+3-n-1)
        break
Buggy code:
dp1=[None]*100001
dp2=[None]*100001
dp2[0]=0
n,W=map(int,input().split())
for i in range(n):
    w,v=map(int,input().split())
    for i in range(v,100001):
        if(dp2[i-v]!=None):
            if(dp2[i]==None):
                dp1[i]=dp2[i-v]+w
            else:
                dp1[i]=min(dp2[i],dp2[i-v]+w)
    dp2=list(dp1)
for i in range(100000,-1,-1):
    if(dp2[i]!=None and dp2[i]<=W):
        print(i)
        break
# 3 8
# 3 3
# 4 5
# 5 6

Fixed code:
dp1=[None]*100001
dp2=[None]*100001
dp1[0]=0
dp2[0]=0
n,W=map(int,input().split())
for i in range(n):
    w,v=map(int,input().split())
    for i in range(v,100001):
        if(dp2[i-v]!=None):
            if(dp2[i]==None):
                dp1[i]=dp2[i-v]+w
            else:
                dp1[i]=min(dp2[i],dp2[i-v]+w)
    dp2=list(dp1)
for i in range(100000,-1,-1):
    if(dp2[i]!=None and dp2[i]<=W):
        print(i)
        break
# 3 8
# 3 3
# 4 5
# 5 6
Buggy code:
N, W=[int(i) for i in input().split()]
wv=[[int(i) for i in input().split()] for _ in range(N)]

max_v=0
for i in range(N):
    max_v+=wv[i][1]

dp=[[99999999999]*(max_v+1) for _ in range(N)]

for i in range(N):
    for v in range(max_v+1):
        if i==0:
            if v<= wv[i][1]:
                dp[i][v]=wv[i][0]
        else:
            if v>=wv[i][1]:
                dp[i][v]=min(dp[i-1][v-wv[i][1]]+wv[i][0],dp[i-1][v])
            else:
                dp[i][v]=min(dp[i-1][v], wv[i][0])

for i in range(max_v,-1,-1):
    if dp[N-1][i]<=W:
        print(i)
        break

Fixed code:
N, W=[int(i) for i in input().split()]
wv=[[int(i) for i in input().split()] for _ in range(N)]

max_v=0
for i in range(N):
    max_v+=wv[i][1]

dp=[[99999999999]*(max_v+1) for _ in range(N+1)]

for i in range(N+1):
    for v in range(max_v+1):
        if i==0:
            dp[i][0]=0
            break
        else:
            if v>=wv[i-1][1]:
                dp[i][v]=min(dp[i-1][v-wv[i-1][1]]+wv[i-1][0],dp[i-1][v])
            else:
                dp[i][v]=min(dp[i-1][v], wv[i-1][0])

for i in range(max_v,-1,-1):
    if dp[N][i]<=W:
        print(i)
        break
Buggy code:
N_W=list(map(int,input().split()))
N=N_W[0]
W=N_W[1]
items=[]
for i in range(N):
	sets=list(map(int,input().split()))
	items.append(sets)
V=0
for i in range(N):
	V=max(V,items[i][1])

f_inf=float('inf')

knapsack=[[f_inf for v in range(N*V+1)] for i in range(N+1)]
knapsack[0][0]=0

for i in range(N):
	for v in range(1,N*V+1):
		if v-items[i][1]>=0:
			knapsack[i+1][v]=min(knapsack[i+1][v],knapsack[i][v],knapsack[i][v-items[i][1]]+items[i][0])
		else:
			knapsack[i+1][v]=min(knapsack[i+1][v],knapsack[i][v])
value=0
for i in range(len(knapsack)):
	for j in range(len(knapsack[0])):
		if knapsack[i][j]<=W:
			value = max(value,j)
print(value)

Fixed code:
N_W=list(map(int,input().split()))
N=N_W[0]
W=N_W[1]
items=[]
for i in range(N):
	sets=list(map(int,input().split()))
	items.append(sets)
V=0
for i in range(N):
	V=max(V,items[i][1])

f_inf=float('inf')

knapsack=[[f_inf for v in range(N*V+1)] for i in range(N+1)]
knapsack[0][0]=0

for i in range(N):
	for v in range(0,N*V+1):
		if v-items[i][1]>=0:
			knapsack[i+1][v]=min(knapsack[i+1][v],knapsack[i][v],knapsack[i][v-items[i][1]]+items[i][0])
		else:
			knapsack[i+1][v]=min(knapsack[i+1][v],knapsack[i][v])
value=0
for i in range(len(knapsack)):
	for j in range(len(knapsack[0])):
		if knapsack[i][j]<=W:
			value = max(value,j)
print(value)
Buggy code:
# -*- coding: utf-8 -*-


def main():
    n, w = map(int, input().split())
    items = [tuple(map(int, input().split())) for i in range(n)]

    dp = [[1000000] * 100001 for i in range(n + 1)]
    dp[0][0] = 0

    for i, item in enumerate(items):
        for i_v in range(100001):
            if item[1] - i_v > 0:
                dp[i + 1][i_v] = dp[i][i_v]
            else:
                dp[i + 1][i_v] = min(dp[i][i_v - item[1]] + item[0], dp[i][i_v])

    result = 0
    for sum_v, weght in enumerate(dp[-1]):
        if weght <= w:
            result = sum_v
    print(result)


if __name__ == '__main__':
    main()


Fixed code:
# -*- coding: utf-8 -*-


def main():
    n, w = map(int, input().split())
    items = [tuple(map(int, input().split())) for i in range(n)]

    dp = [[10**12] * 100001 for i in range(n + 1)]
    dp[0][0] = 0

    for i, item in enumerate(items):
        for i_v in range(100001):
            if item[1] - i_v > 0:
                dp[i + 1][i_v] = dp[i][i_v]
            else:
                dp[i + 1][i_v] = min(dp[i][i_v - item[1]] + item[0], dp[i][i_v])

    result = 0
    for sum_v, weght in enumerate(dp[-1]):
        if weght <= w:
            result = sum_v
    print(result)


if __name__ == '__main__':
    main()
Buggy code:
def main():
    N, W =map(int, raw_input().split())

    weights = []
    values = []
    for i in range(N):
        wi, vi =map(int, raw_input().split())
        weights.append(wi)
        values.append(vi)

    sum_v = sum(values)

    INF = 10**9+7

    dp = [[INF]*(sum_v+1) for _ in range(N+1)]
    dp[0][0] = 0


    for i in range(N):
        wi = weights[i]
        vi = values[i]
        for j in range(sum_v+1):
            if j < vi:
                dp[i+1][j] = dp[i][j]
            else:
                dp[i+1][j] = min(dp[i][j], dp[i][j-vi]+ wi)

    for j in range(sum_v+1):
        if dp[N][j] <= W:
            ans = j

    print ans

if __name__ == "__main__":
    main()


Fixed code:
def main():
    N, W =map(int, input().split())

    weights = []
    values = []
    for i in range(N):
        wi, vi =map(int, input().split())
        weights.append(wi)
        values.append(vi)

    sum_v = sum(values)

    INF = 10**9+7

    dp = [[INF]*(sum_v+1) for _ in range(N+1)]
    dp[0][0] = 0


    for i in range(N):
        wi = weights[i]
        vi = values[i]
        for j in range(sum_v+1):
            if j < vi:
                dp[i+1][j] = dp[i][j]
            else:
                dp[i+1][j] = min(dp[i][j], dp[i][j-vi]+ wi)

    for j in range(sum_v+1):
        if dp[N][j] <= W:
            ans = j

    print(ans)

if __name__ == "__main__":
    main()
Buggy code:
N, W = map(int, input().split())
v = [0] * N
w = [0] * N
for i in range(N):
    v[i], w[i] = map(int, input().split())

M = sum(v)

dp = [[W+1 for _ in range(M + 1)] for _ in range(N+1)]
dp[0][0] = 0
for i in range(N):
    dp[i+1][v[i]] = min(dp[i][v[i]], w[i])
    for j in range(v[i]):
        dp[i+1][j] = dp[i][j]
    for j in range(v[i], M + 1):
        dp[i+1][j] = min(dp[i][j], dp[i][j-v[i]] + w[i])
for i in range(M, -1, -1):
    if dp[N][i] <= W:
        print(i)
        exit()


Fixed code:
N, W = map(int, input().split())
v = [0] * N
w = [0] * N
for i in range(N):
    w[i], v[i] = map(int, input().split())

M = sum(v)

dp = [[W+1 for _ in range(M + 1)] for _ in range(N+1)]
dp[0][0] = 0
for i in range(N):
    dp[i+1][v[i]] = min(dp[i][v[i]], w[i])
    for j in range(v[i]):
        dp[i+1][j] = dp[i][j]
    for j in range(v[i], M + 1):
        dp[i+1][j] = min(dp[i][j], dp[i][j-v[i]] + w[i])
for i in range(M, -1, -1):
    if dp[N][i] <= W:
        print(i)
        exit()
Buggy code:
n,W = map(int,input().split())
w,v = [],[]
for i in range(n):
    wi,vi = map(int,input().split())
    w.append(wi)
    v.append(vi)
sv = sum(v)
INF = float("INF")
dp = [[INF]*sv for i in range(n+1)]
dp[0][0] = 0
for i in range(n):
    for j in range(sv):
        if j-v[i] >= 0:
            dp[i+1][j] = min(dp[i+1][j],dp[i][j-v[i]]+w[i])
        dp[i+1][j] = min(dp[i+1][j],dp[i][j])
    #print(dp)
ans = 0
for i in range(sv):
    #print(dp[n][i])
    if dp[n][i] <= W:
        ans = max(ans,i)
print(ans)


Fixed code:
n,W = map(int,input().split())
w,v = [],[]
for i in range(n):
    wi,vi = map(int,input().split())
    w.append(wi)
    v.append(vi)
mv = sum(v)
INF = float("INF")
dp = [[INF]*(mv+100) for i in range(n+1)]
dp[0][0] = 0
for i in range(n):
    for sv in range(mv+100):
        if sv-v[i] >= 0:
            dp[i+1][sv] = min(dp[i+1][sv],dp[i][sv-v[i]]+w[i])
        dp[i+1][sv] = min(dp[i+1][sv],dp[i][sv])
    #print(dp)
ans = 0
for i in range(mv+100):
    #print(dp[n][i])
    if dp[n][i] <= W:
        ans = max(ans,i)
print(ans)
Buggy code:
N,W = map(int,input().split())
K = 10**5
dp = [[10**100 for i in range(K+1)] for j in range(N)]
dp[0][0] = 0
for i in range(1,N+1):
    w,v = map(int,input().split())
    for j in range(K+1):
        if j < v:
            continue
        else:
            dp[i][j] = min(dp[i-1][j-v] + W,dp[i][j])
for i in range(K+1):
    x = dp[N][-i-1]
    if x <= W:
        print(K-i)
        quit()

Fixed code:
N,W = map(int,input().split())
K = 10**5
dp = [[10**100 for i in range(K+1)] for j in range(N+1)]
dp[0][0] = 0
for i in range(1,N+1):
    w,v = map(int,input().split())
    for j in range(K+1):
        dp[i][j] = dp[i-1][j]
        if j < v:
            continue
        else:
            dp[i][j] = min(dp[i-1][j-v] + w,dp[i-1][j])
for i in range(K+1):
    x = dp[N][-i-1]
    if x <= W:
        print(K-i)
        quit()
Buggy code:
n,w = map(int,input().split())

INF = float('inf')
ww = [0]*n
vv = [0]*n
vm = 5
dp = [[INF]*(n*vm+1) for _ in range(n+1)]

for i in range(n):
  ww[i],vv[i] = map(int,input().split())      


for i in range(1,n+1):
  dp[i][0] = 0
  for j in range(1,n*vm+1):        
    if j < vv[i-1]:
      dp[i][j] = dp[i-1][j]
    else:
      dp[i][j] = min(dp[i-1][j], dp[i-1][j-vv[i-1]] + ww[i-1])        

ans = 0
for i in range(n*vm+1):
  if dp[n][i]<=w:
    ans = i
print(ans)    

Fixed code:
n,w = map(int,input().split())

INF = float('inf')
ww = [0]*n
vv = [0]*n
vm = 1000
dp = [[INF]*(n*vm+1) for _ in range(n+1)]

for i in range(n):
  ww[i],vv[i] = map(int,input().split())      

dp[0][0] = 0  
for i in range(1,n+1):
  dp[i][0] = 0
  for j in range(1,n*vm+1):        
    if j < vv[i-1]:
      dp[i][j] = dp[i-1][j]
    else:
      dp[i][j] = min(dp[i-1][j], dp[i-1][j-vv[i-1]] + ww[i-1])        

ans = 0
for i in range(n*vm+1):
  if dp[n][i]<=w:
    ans = i
print(ans)
Buggy code:
import sys
import math
from collections import deque
import copy
input = sys.stdin.readline
INF = int(float("inf"))
N, W = map(int, input().split())
weight = []
value = []
for i in range(N):
    tempW, tempV = map(int, input().split())
    weight.append(tempW)
    value.append(tempV)

dp = [[INF for _ in range(N * 1000 + 1)] for _ in range(N + 1)]
dp[0][0] = 0

for i in range(1, N + 1):
    for l in range(N * 1000 + 1):
        if l - value[i - 1] >= 0:
            dp[i][l] = min(dp[i - 1][l], dp[i - 1][l - value[i - 1]] + weight[i - 1])
        else:
            dp[i][l] = dp[i - 1][l]
for i in range(N * 1000 + 1):
    if dp[N][i] <= W:
        ans = i
print(ans)


Fixed code:
import sys
import math
from collections import deque
import copy
input = sys.stdin.readline
INF = float("inf")
N, W = map(int, input().split())
weight = []
value = []
for i in range(N):
    tempW, tempV = map(int, input().split())
    weight.append(tempW)
    value.append(tempV)

dp = [[INF for _ in range(N * 1000 + 1)] for _ in range(N + 1)]
dp[0][0] = 0

for i in range(1, N + 1):
    for l in range(N * 1000 + 1):
        if l - value[i - 1] >= 0:
            dp[i][l] = min(dp[i - 1][l], dp[i - 1][l - value[i - 1]] + weight[i - 1])
        else:
            dp[i][l] = dp[i - 1][l]
for i in range(N * 1000 + 1):
    if dp[N][i] <= W:
        ans = i
print(ans)
Buggy code:
N, W = map(int, input().split())
p = []
for it in range(N):
    p.append(list(map(int,input().split())))
    
MAX_WEIGHT = 1000000009
dp = [[MAX_WEIGHT for i in range(max_total+1)] for j in range(N+1)]
dp[0][0]=0
i = 0
for w, v in p:
    i += 1
    for target_val in range(0, max_total+1):
        
        if target_val-v >=0 and dp[i-1][target_val] > (dp[i-1][target_val-v]+w):
            dp[i][target_val] = dp[i-1][target_val-v]+w
        else:
            dp[i][target_val] = dp[i-1][target_val]
            
ret = 0
for v, w in enumerate(dp[N]):
    if w<=W and v>ret:
        ret = v
print(ret)

Fixed code:
N, W = map(int, input().split())

max_total = 0
p = []
for it in range(N):
    p.append(list(map(int,input().split())))
    max_total += p[-1][1]
    
    
MAX_WEIGHT = 1000000009
dp = [[MAX_WEIGHT for i in range(max_total+1)] for j in range(N+1)]
dp[0][0]=0
i = 0
for w, v in p:
    i += 1
    for target_val in range(0, max_total+1):
        
        if target_val-v >=0 and dp[i-1][target_val] > (dp[i-1][target_val-v]+w):
            dp[i][target_val] = dp[i-1][target_val-v]+w
        else:
            dp[i][target_val] = dp[i-1][target_val]
            
ret = 0
for v, w in enumerate(dp[N]):
    if w<=W and v>ret:
        ret = v
print(ret)
Buggy code:
N,W = map(int,input().split())
w_v = [list(map(int,input().split())) for i in range(N)]
INF = float("inf")
dp = [[INF]*1001 for i in range(N+1)]
for i in range(N+1):
  dp[0][i]=0

for i in range(N):
  for j in range(1001):
    if j >= w_v[i][1]:
      dp[i+1][j] = min(dp[i][j],dp[i][j-w_v[i][1]]+w_v[i][0])
    else:
      dp[i+1][j] = dp[i][j]
    
ans = 0
for j in range(1001):
  if dp[-1][j] <= W:
    ans = max(ans,j-1)
print(ans)

Fixed code:
N,W = map(int,input().split())
w_v = [list(map(int,input().split())) for i in range(N)]
INF = float("inf")
dp = [[INF]*100001 for i in range(N+1)]
dp[0][0]=0

for i in range(N):
  for j in range(100001):
    if j >= w_v[i][1]:
      dp[i+1][j] = min(dp[i][j],dp[i][j-w_v[i][1]]+w_v[i][0])
    else:
      dp[i+1][j] = dp[i][j]
    
ans = 0
for j in range(100001):
  if dp[-1][j] <= W:
    ans = max(ans,j)
print(ans)
Buggy code:
n,W = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
inf = float('INF')
bag = [inf for _ in range(10**5+1)]
bag[0] = 0

ans = 0
for w,v in a:
    left = v if v < 100001 else 100001
    right = left + ans + 1 if left + ans + 1 < 100001 else 100001
    for i in range(left, right)[::-1]:
        if bag[i-v] + w < bag[i] and bag[i-v] + w < W+1):
            bag[i] = bag[i-v] + w
            if i > ans:
                ans = i
print(ans)


Fixed code:
n,W = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
inf = float('INF')
bag = [inf for _ in range(10**5+1)]
bag[0] = 0

ans = 0
for w,v in a:
    left = v if v < 100001 else 100001
    right = left + ans + 1 if left + ans + 1 < 100001 else 100001
    for i in range(left, right)[::-1]:
        if bag[i-v] + w < bag[i] and bag[i-v] + w < W+1:
            bag[i] = bag[i-v] + w
            if i > ans:
                ans = i
print(ans)