Buggy python submission (verdict: Time Limit Exceeded):
N, Ma, Mb = map(int,input().split())
C = [map(int,input().split()) for i in range(N)]
INF = 40000
dp = [[[INF]*401 for i in range(401)] for i in range(N+1)]
dp[0][0][0] = 0
for i in range(1,N+1):
  a,b,c = C[i-1]
  for j in range(401):
      for k in range(401):
        dp[i][j][k] = dp[i-1][j][k]
        if j >= a and k >= b and dp[i-1][j-a][k-b] != INF:
            dp[i][j][k] = min(dp[i][j][k],dp[i-1][j-a][k-b]+c)
ans = INF
Ma2,Mb2 = Ma,Mb
while max(Ma2,Mb2) < 401:
  ans = min(ans,dp[-1][Ma2][Mb2])
  Ma2 += Ma
  Mb2 += Mb    

print(ans if ans != INF else -1)


Fixed submission:
N, Ma, Mb = map(int, input().split())
table = []
for i in range(N):
  a,b,c = map(int, input().split())
  table.append([a,b,c])
inf = 10**9
dp = [[[inf]*401 for i in range(401)] for i in range(N+1)]
dp[0][0][0] = 0

for i in range(1,N+1):
  a,b,c = table[i-1]
  for j in range(401):
    for k in range(401):
      dp[i][j][k] = dp[i-1][j][k]
      if 400>=j-a>=0 and 400>=k-b>=0:
        dp[i][j][k] = min(dp[i-1][j][k],dp[i-1][j-a][k-b]+c)
ans = 10**9
i = 1
while max(Ma,Mb)*i <= 400:
  ans = min(ans,dp[N][Ma*i][Mb*i])
  i += 1
if ans == 10**9:
  ans = -1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, Ma, Mb = map(int, input().split())
table = []
for i in range(N):
  a,b,c = map(int, input().split())
  table.append([a,b,c])
inf = 10**9
dp = [[[inf]*401 for i in range(401)] for i in range(N+1)]
dp[0][0][0] = 0

for i in range(1,N+1):
  a,b,c = table[i-1]
  for j in range(401):
    for k in range(401):
      dp[i][j][k] = dp[i-1][j][k]
      if 400>=j-a>=0 and 400>=k-b>=0:
        dp[i][j][k] = min(dp[i-1][j][k],dp[i-1][j-a][k-b]+c)
ans = 10**9
i = 1
while max(Ma,Mb)*i <= 400:
  ans = min(ans,dp[N][Ma*i][Mb*i])
  i += 1
if ans == 10**9:
  ans = -1
print(ans)

Fixed submission:
N, Ma, Mb = map(int, input().split())
table = []
for i in range(N):
  a,b,c = map(int, input().split())
  table.append([a,b,c])
inf = 10**9
dp = [[[inf]*401 for i in range(401)] for i in range(N+1)]
dp[0][0][0] = 0

for i in range(1,N+1):
  a,b,c = table[i-1]
  for j in range(401):
    for k in range(401):
      dp[i][j][k] = dp[i-1][j][k]
      if 400>=j-a>=0 and 400>=k-b>=0:
        dp[i][j][k] = min(dp[i-1][j][k],dp[i-1][j-a][k-b]+c)
ans = 10**9
i = 1
while max(Ma,Mb)*i <= 400:
  ans = min(ans,dp[N][Ma*i][Mb*i])
  i += 1
if ans == 10**9:
  ans = -1
print(ans)

Buggy python submission (verdict: Runtime Error):
N,Ma,Mb = map(int,input().split())
table = []
sum_a,sum_b,sum_c = 0,0,0

for i in range(N):
  a,b,c=map(int,input().split())
  sum_a += a 
  sum_b += b
  sum_c += c
  table.append((a,b,c))
  
INF = sum_c+1
ans = INF
dp=[[[INF]*(sum_a+1) for i in range(sum_b+1)] for i in range(N+1)]
dp[0][0][0] = 0
for i in range(1,N+1):
  a,b,c = table[i-1]
  for ca in range(sum_a+1):
    for cb in range(sum_b+1):
      dp[i][ca][cb] = min(dp[i-1][ca][cb],dp[i][ca][cb])
      if ca+a < sum_a and cb+b < sum_b:
        dp[i][ca+a][cb+b] = min(dp[i][ca+a][cb+b],dp[i][ca][cb]+c)

for ca in range(sum_a+1):
  for cb in range(sum_b+1):
    if ca*Ma == cb*Mb and ca*cb != 0:
      for i in range(N+1):
        ans = min(ans,dp[i][ca][cb])
print(ans if ans != INF else -1)

Fixed submission:
N, Ma, Mb = map(int, input().split())
table = []
for i in range(N):
  a,b,c = map(int, input().split())
  table.append([a,b,c])
inf = 10**9
dp = [[[inf]*401 for i in range(401)] for i in range(N+1)]
dp[0][0][0] = 0

for i in range(1,N+1):
  a,b,c = table[i-1]
  for j in range(401):
    for k in range(401):
      dp[i][j][k] = dp[i-1][j][k]
      if 400>=j-a>=0 and 400>=k-b>=0:
        dp[i][j][k] = min(dp[i-1][j][k],dp[i-1][j-a][k-b]+c)
ans = 10**9
i = 1
while max(Ma,Mb)*i <= 400:
  ans = min(ans,dp[N][Ma*i][Mb*i])
  i += 1
if ans == 10**9:
  ans = -1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, Ma, Mb = map(int, input().split())

abc = [list(map(int, input().split())) for i in range(N)]
cost = 4001


for i in range(1, 2**N):
  a, b = 0, 0
  tmp = 0
#  flag = 0
  for j in range(N):
    if (i>>j) & 1:
#      flag = 1
      tmp += abc[j][2]
      a += abc[j][0]
      b += abc[j][1]
  if a/Ma == b/Mb:
    cost = min(tmp, cost)
if cost <= 4000:
  print(cost)
else:
  print(-1)

  

Fixed submission:
N, Ma, Mb = map(int, input().split())
table = []
for i in range(N):
  a,b,c = map(int, input().split())
  table.append([a,b,c])
inf = 10**9
dp = [[[inf]*401 for i in range(401)] for i in range(N+1)]
dp[0][0][0] = 0

for i in range(1,N+1):
  a,b,c = table[i-1]
  for j in range(401):
    for k in range(401):
      dp[i][j][k] = dp[i-1][j][k]
      if 400>=j-a>=0 and 400>=k-b>=0:
        dp[i][j][k] = min(dp[i-1][j][k],dp[i-1][j-a][k-b]+c)
ans = 10**9
i = 1
while max(Ma,Mb)*i <= 400:
  ans = min(ans,dp[N][Ma*i][Mb*i])
  i += 1
if ans == 10**9:
  ans = -1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, Ma, Mb = map(int, input().split())

abc = [list(map(int, input().split())) for i in range(N)]
tmp = []

for i in range(2**N):
  a, b = 0, 0
  cost = []
  for j in range(N):
    if (i>>j) & 1:
      cost.append(abc[j][2])
      a += abc[j][0]
      b += abc[j][1]
  if a/Ma == b/Mb and sum(cost) > 0:
    tmp.append(sum(cost))
try:
  print(min(tmp))
except ValueError:
  print(-1)
  

Fixed submission:
N, Ma, Mb = map(int, input().split())
table = []
for i in range(N):
  a,b,c = map(int, input().split())
  table.append([a,b,c])
inf = 10**9
dp = [[[inf]*401 for i in range(401)] for i in range(N+1)]
dp[0][0][0] = 0

for i in range(1,N+1):
  a,b,c = table[i-1]
  for j in range(401):
    for k in range(401):
      dp[i][j][k] = dp[i-1][j][k]
      if 400>=j-a>=0 and 400>=k-b>=0:
        dp[i][j][k] = min(dp[i-1][j][k],dp[i-1][j-a][k-b]+c)
ans = 10**9
i = 1
while max(Ma,Mb)*i <= 400:
  ans = min(ans,dp[N][Ma*i][Mb*i])
  i += 1
if ans == 10**9:
  ans = -1
print(ans)

Buggy python submission (verdict: Runtime Error):
N, Ma, Mb = map(int, input().split())

abc = [list(map(int, input().split())) for i in range(N)]
tmp = []

for i in range(2**N):
  a, b = 0, 0
  cost = []
  for j in range(N):
    if (i>>j) & 1:
      cost.append(abc[j][2])
      a += abc[j][0]
      b += abc[j][1]
  if a/Ma == b/Mb and sum(cost) > 0:
    tmp.append(sum(cost))
print(min(tmp))

Fixed submission:
N, Ma, Mb = map(int, input().split())
table = []
for i in range(N):
  a,b,c = map(int, input().split())
  table.append([a,b,c])
inf = 10**9
dp = [[[inf]*401 for i in range(401)] for i in range(N+1)]
dp[0][0][0] = 0

for i in range(1,N+1):
  a,b,c = table[i-1]
  for j in range(401):
    for k in range(401):
      dp[i][j][k] = dp[i-1][j][k]
      if 400>=j-a>=0 and 400>=k-b>=0:
        dp[i][j][k] = min(dp[i-1][j][k],dp[i-1][j-a][k-b]+c)
ans = 10**9
i = 1
while max(Ma,Mb)*i <= 400:
  ans = min(ans,dp[N][Ma*i][Mb*i])
  i += 1
if ans == 10**9:
  ans = -1
print(ans)