Buggy python submission (verdict: Time Limit Exceeded):
n, ma, mb = map(int, input().split())
abc = [tuple(int(x) for x in input().split()) for _ in range(n)]

c = n * 100 + 1

dp = [[[99999 for _ in range(1001)] for _ in range(1001)] for _ in range(n + 1)]

ans = 99999
dp[0][0][0] = 0
for i in range(n):
    for j in range(1001):
        for k in range(1001):
            if dp[i][j][k] < 9999:
                dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k])
                dp[i+1][j+abc[i][0]][k+abc[i][1]] = min(dp[i+1][j+abc[i][0]][k+abc[i][1]], dp[i][j][k] + abc[i][2])
                if (j+abc[i][0]) * mb == (k+abc[i][1]) * ma:
                    ans = min(ans, dp[i+1][j+abc[i][0]][k+abc[i][1]])

if ans == 99999:
    print(-1)
else:
    print(ans)

Fixed submission:
n, ma, mb = map(int, input().split())
abc = [tuple(int(x) for x in input().split()) for _ in range(n)]

c = n * 100 + 1

m = 401

dp = [[[99999 for _ in range(m)] for _ in range(m)] for _ in range(n + 1)]

ans = 99999
dp[0][0][0] = 0
for i in range(n):
    for j in range(m):
        for k in range(m):
            if dp[i][j][k] < 9999:
                dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k])
                dp[i+1][j+abc[i][0]][k+abc[i][1]] = min(dp[i+1][j+abc[i][0]][k+abc[i][1]], dp[i][j][k] + abc[i][2])
                if (j+abc[i][0]) * mb == (k+abc[i][1]) * ma:
                    ans = min(ans, dp[i+1][j+abc[i][0]][k+abc[i][1]])

if ans == 99999:
    print(-1)
else:
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, ma, mb = map(int, input().split())
abc = [tuple(int(x) for x in input().split()) for _ in range(n)]

c = n * 100 + 1

dp = [[[] for _ in range(c)] for _ in range(n + 1)]

dp[0][0].append([0,0])

ans = 999999

for i in range(n):
    for j in range(c):
        for a, b in dp[i][j]:
            dp[i+1][j].append([a, b])
            if (abc[i][0] + a) * mb == (abc[i][1] + b) * ma:
                ans = min(ans, abc[i][2] + j)
            dp[i+1][j + abc[i][2]].append([abc[i][0] + a, abc[i][1] + b])

if ans == 999999:
    print(-1)
else:
    print(ans)

Fixed submission:
n, ma, mb = map(int, input().split())
abc = [tuple(int(x) for x in input().split()) for _ in range(n)]

c = n * 100 + 1

m = 401

dp = [[[99999 for _ in range(m)] for _ in range(m)] for _ in range(n + 1)]

ans = 99999
dp[0][0][0] = 0
for i in range(n):
    for j in range(m):
        for k in range(m):
            if dp[i][j][k] < 9999:
                dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k])
                dp[i+1][j+abc[i][0]][k+abc[i][1]] = min(dp[i+1][j+abc[i][0]][k+abc[i][1]], dp[i][j][k] + abc[i][2])
                if (j+abc[i][0]) * mb == (k+abc[i][1]) * ma:
                    ans = min(ans, dp[i+1][j+abc[i][0]][k+abc[i][1]])

if ans == 99999:
    print(-1)
else:
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, ma, mb = map(int, input().split())
abc = [tuple(int(x) for x in input().split()) for _ in range(n)]

m = ma, mb

ans = set()

def res(a, b, c, i):
    if i == n:
        return
    if (a + abc[i][0]) * mb == (b + abc[i][1]) * ma:
        #print(a,b,c,i, abc[i])
        ans.add(c + abc[i][2])
    res(a + abc[i][0], b + abc[i][1], c + abc[i][2], i + 1)
    res(a, b, c, i + 1)

res(0,0,0,0)

#print(ans)
if len(ans) == 0:
    print(-1)
else:
    print(min(ans))

Fixed submission:
n, ma, mb = map(int, input().split())
abc = [tuple(int(x) for x in input().split()) for _ in range(n)]

c = n * 100 + 1

m = 401

dp = [[[99999 for _ in range(m)] for _ in range(m)] for _ in range(n + 1)]

ans = 99999
dp[0][0][0] = 0
for i in range(n):
    for j in range(m):
        for k in range(m):
            if dp[i][j][k] < 9999:
                dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k])
                dp[i+1][j+abc[i][0]][k+abc[i][1]] = min(dp[i+1][j+abc[i][0]][k+abc[i][1]], dp[i][j][k] + abc[i][2])
                if (j+abc[i][0]) * mb == (k+abc[i][1]) * ma:
                    ans = min(ans, dp[i+1][j+abc[i][0]][k+abc[i][1]])

if ans == 99999:
    print(-1)
else:
    print(ans)