Problem p03164 (python)

Fixed submission:
INF = 1000000000000
n, W = map(int, input().split())
values = []
weights = []
total = 10
for _ in range(n):
    w, v = map(int, input().split())
    weights.append(w)
    values.append(v)
    total += v
dp = [[INF for j in range(total)] for i 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(1,total):
        if v < values[i-1]:
            dp[i][v] = dp[i-1][v]
        else:
            dp[i][v] = min(dp[i-1][v],dp[i-1][v-values[i-1]]+weights[i-1]) 
res = 0
for i in range(total):
    if dp[n][i] <= W:
        res = i
print(res)

Buggy submission (verdict: Runtime Error):
import math
n, W = map(int, input().strip().split())
values = []
weights = []
total = 10
for _ in range(n):
    w, v = map(int, input().strip().split())
    weights.append(w)
    values.append(v)
    total += v
dp = [[math.inf]*total for i 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(1,total):
        if v < values[i-1]:
            dp[i][v] = dp[i-1][v]
        else:
            dp[i][v] = min(dp[i-1][v],dp[i-1][v-values[i-1]]+weights[i-1]) 
res = 0
for i in range(total):
    if dp[n][i] <= W:
        res = i
print(res)

Buggy submission (verdict: Runtime Error):
import math
n, W = map(int, input().strip().split())
 
values = []
weights = []
total = 10
for _ in range(n):
    w, v = map(int, input().strip().split())
    weights.append(w)
    values.append(v)
    total += v

dp = [[math.inf for j in range(total)] for i 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(1,total):
        if v < values[i-1]:
            dp[i][v] = dp[i-1][v]
        else:
            dp[i][v] = min(dp[i-1][v],dp[i-1][v-values[i-1]]+weights[i-1]) 
res = 0
for i in range(total):
    if dp[n][i] <= W:
        res = i
print(res)

Buggy submission (verdict: Runtime Error):
import math
n, W = map(int, input().strip().split())

values = []
weights = []
total = 10
for _ in range(n):
    w, v = map(int, input().strip().split())
    weights.append(w)
    values.append(v)
    total += v

dp = [[[0, math.inf] for j in range(total)] for i in range(n+1)]
for i in range(n+1):
    dp[i][0] = [1, 0]
for i in range(1,n+1):
    for v in range(1,total):
        exclude = dp[i-1][v]
        include = [0, math.inf]
        if v >= values[i-1]:
            if dp[i-1][v-values[i-1]][1] + weights[i-1] <= W:
                include = dp[i-1][v-values[i-1]]
        dp[i][v] = [exclude[0] or include[0], min(exclude[1], weights[i-1] + include[1])]
res = 0
for i in range(total):
    if dp[n][i][0] == 1:
        res = i
print(res)

Buggy submission (verdict: Runtime Error):
import math
n, W = map(int, input().strip().split())

values = []
weights = []

for _ in range(n):
    w, v = map(int, input().strip().split())
    weights.append(w)
    values.append(v)

dp = [[[0, math.inf] for j in range(1001)] for i in range(n+1)]
for i in range(n+1):
    dp[i][0] = [1, 0]
for i in range(1,n+1):
    for v in range(1,1001):
        exclude = dp[i-1][v]
        include = [0, math.inf]
        if v >= values[i-1]:
            if dp[i-1][v-values[i-1]][1] + weights[i-1] <= W:
                include = dp[i-1][v-values[i-1]]
        dp[i][v] = [exclude[0] or include[0], min(exclude[1], weights[i-1] + include[1])]
res = 0
for i in range(1001):
    if dp[n][i][0] == 1:
        res = i
print(res)