Problem p03806 (python)

Fixed submission:
import sys



n, ma, mb = map(int, input().split())
abc = []

for _ in range(n):
    a, b, cost = map(int, input().split())
    abc.append((a, b, cost))

def main():
    sa = 0
    sb = 0
    for i in range(n):
        sa += abc[i][0]
        sb += abc[i][1]

    res = [[float('infinity')] * (sb + 1) for _ in range(sa + 1)]
    res[0][0] = 0

    for a, b, c in abc:

        for j in range(sa, -1, -1):
            for k in range(sb, -1, -1):
                if j >= a and k >= b:
                    res[j][k] = min(res[j][k], res[j-a][k-b] + c)

    ans = float('infinity')
    for i in range(1, min(sa//ma, sb//mb) + 1):
        ans = min(ans, res[ma*i][mb*i])

    if ans == float('infinity'):
        return -1
    else:
        return ans

if __name__ == '__main__':
    ans = main()
    print(ans)

Buggy submission (verdict: Runtime Error):
N, A, B = map(int, input().split())

items = []
A_MAX = 0
B_MAX = 0
for _ in range(N):
    a, b, cost = map(int, input().split())
    items.append((a, b, cost))
    A_MAX += a
    B_MAX += b

dp = [[float('infinity')] * (A_MAX + 1) for _ in range(B_MAX + 1)]

dp[0][0] = 0

for a, b, cost in items:
    for i in range(B_MAX, -1, -1):
        for j in range(A_MAX, -1, -1):
            if i - b >= 0 and j - a >= 0:
                dp[i][j] = min(dp[i][j], dp[i - b][j - a] + cost)

ans = float('infinity')
for k in range(1, B_MAX + 1):
    for l in range(1, A_MAX + 1):
        if dp[k][l] == float('infinity'):
            continue
        if A * dp[k] == dp[l] * B:
            ans = min(ans, dp[k][l])
if ans == float('infinity'):
    print(-1)
else:
    print(ans)

Buggy submission (verdict: Runtime Error):
N, A, B = map(int, input().split())

items = []
A_MAX = 0
B_MAX = 0
for _ in range(N):
    a, b, cost = map(int, input().split())
    items.append((a, b, cost))
    A_MAX += a
    B_MAX += b

A_MAX = int(A_MAX)
B_MAX = int(B_MAX)

dp = [[float('infinity')] * (A_MAX + 1) for _ in range(B_MAX + 1)]

dp[0][0] = 0
for a, b, cost in items:
    dp[b][a] = cost

for a, b, cost in items:
    for i in range(B_MAX + 1):
        for j in range(A_MAX + 1):
            if i - b >= 0 and j - a >= 0:
                dp[i][j] = min(dp[i][j], dp[i - b][j - a] + cost)

ans = float('infinity')
for k in range(1, B_MAX + 1):
    for l in range(1, A_MAX + 1):
        if dp[k][l] == float('infinity'):
            continue
        if A * dp[k] == dp[l] * B:
            ans = min(ans, dp[k][l])
if ans == float('infinity'):
    print(-1)
else:
    print(ans)

Buggy submission (verdict: Runtime Error):
N, A, B = map(int, input().split())

items = []
A_MAX = 0
B_MAX = 0
MAX_cost = 0
for _ in range(N):
    a, b, cost = map(int, input().split())
    items.append((a, b, cost))
    A_MAX += a
    B_MAX += b
    MAX_cost += cost

dp = [[float('infinity')] * (A_MAX + 1) for _ in range(B_MAX + 1)]

dp[0][0] = 0
for a, b, cost in items:
    dp[b][a] = cost

for a, b, cost in items:
    for i in range(1, B_MAX + 1):
        for j in range(1, A_MAX + 1):
            if j - a > 0 and i - b > 0:
                dp[i][j] = min(dp[i][j], dp[i - b][j - a] + cost)

ans = float('infinity')
for k in range(1, B_MAX + 1):
    for l in range(1, A_MAX + 1):
        if dp[k][l] == float('infinity'):
            continue
        if A * dp[k] == dp[l] * B:
            ans = min(ans, dp[k][l])
if ans == float('infinity'):
    print(-1)
else:
    print(ans)

Buggy submission (verdict: Runtime Error):
N, A, B = map(int, input().split())

items = []
A_MAX = 0
B_MAX = 0
MAX_cost = 0
for _ in range(N):
    a, b, cost = map(int, input().split())
    items.append((a, b, cost))
    A_MAX += a
    B_MAX += b
    MAX_cost += cost

dp = [[float('infinity')] * (A_MAX + 1) for _ in range(B_MAX + 1)]

# dp[0][0] = 0
for a, b, cost in items:
    dp[b][a] = cost

for a, b, cost in items:
    for i in range(B_MAX + 1):
        for j in range(A_MAX + 1):
            if j - a > 0 and i - b > 0:
                dp[i][j] = min(dp[i][j], dp[i - b][j - a] + cost)

ans = float('infinity')
for k in range(B_MAX):
    for l in range(A_MAX):
        if dp[k][l] == float('infinity'):
            continue
        if A * dp[k] == dp[l] * B:
            ans = min(ans, dp[k][l])
if ans == float('infinity'):
    print(-1)
else:
    print(ans)

Buggy submission (verdict: Runtime Error):
N, A, B = map(int, input().split())

items = []
A_MAX = 0
B_MAX = 0
MAX_cost = 0
for _ in range(N):
    a, b, cost = map(int, input().split())
    items.append((a, b, cost))
    A_MAX += a
    B_MAX += b
    MAX_cost += cost

dp = [[float('infinity')] * (A_MAX + 1) for _ in range(B_MAX + 1)]

# dp[0][0] = 0
for a, b, cost in items:
    dp[b][a] = cost

for a, b, cost in items:
    for i in range(B_MAX):
        for j in range(A_MAX):
            if j - a >= 0 and i - b >= 0:
                dp[i][j] = min(dp[i][j], dp[i - b][j - a] + cost)

ans = float('infinity')
for k in range(B_MAX):
    for l in range(A_MAX):
        if dp[k][l] == float('infinity'):
            continue
        if A * dp[k] == dp[l] * B:
            ans = min(ans, dp[k][l])
if ans == float('infinity'):
    print(-1)
else:
    print(ans)