Buggy python submission (verdict: Time Limit Exceeded):
nmax, abmax, inf = 40, 10, 1000000

a, b, c = [0] * nmax, [0] * nmax, [0] * nmax
dp = [[[0] * (nmax * abmax + 1) for _ in range(nmax * abmax + 1)] for _ in range(nmax + 1)]

n, ma, mb = map(int, input().split())
for i in range(n):
  x, y, z = map(int, input().split())
  a[i], b[i], c[i] = x, y, z

for i in range(n + 1):
  for ca in range(nmax * abmax + 1):
    for cb in range(nmax * abmax + 1):
      dp[i][ca][cb] = inf

dp[0][0][0] = 0

for i in range(n):
  for ca in range(0, 401):
    for cb in range(0, 401):
      if dp[i][ca][cb] == inf:
        continue
      dp[i + 1][ca][cb] = min(dp[i + 1][ca][cb], dp[i][ca][cb])
      dp[i + 1][ca + a[i]][cb + b[i]] = min(dp[i + 1][ca + a[i]][cb + b[i]], dp[i][ca][cb] + c[i])

ans = inf
for ca in range(1, 401):
  for cb in range(1, 401):
    if ca * mb == cb * ma:
      ans = min(ans, dp[n][ca][cb])

if ans == inf:
  ans = -1

print(ans)


Fixed submission:
import sys
inf = float('inf')
n, ma, mb = map(int, input().split())
t = [[inf] * 401 for _ in range(401)]
for _ in range(n):
  a, b, c = map(int, input().split())
  for aa in range(400, 0, -1):
    for bb in range(400, 0, -1):
      if t[aa][bb] == inf:
        continue
      if t[a + aa][b + bb] > c + t[aa][bb]:
        t[a + aa][b + bb] = c + t[aa][bb]
  if t[a][b] > c:
    t[a][b] = c
result = inf
for a in range(400, 0, -1):
  for b in range(400, 0, -1):
    if a * mb == b * ma and t[a][b] < result:
      result = t[a][b]
if result == inf:
  print(-1)
else:
  print(result)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
n, ma, mb = map(int, input().split())
tc = [[] for _ in range(100 * n + 1)]
tn = [[] for _ in range(100 * n + 1)]
high = 100 * n + 1
for i in range(n):
  a, b, c = map(int, input().split())
  if a * mb == b * ma:
    high = c + 1
  tn[c].append((a, b))
  for j in range(1, high):
    for aa, bb in tc[j]:
      tn[j].append((aa, bb))
      tn[c + j].append((a + aa, b + bb))
      if (a + aa) * mb == (b + bb) * ma:
        high = c + j + 1
  tc = tn
  tn = [[] for _ in range(100 * n + 1)]
for i in range(1, 100 * n + 1):
  for a, b in tc[i]:
    if a * mb == b * ma:
      print(i)
      sys.exit()
print(-1)


Fixed submission:
import sys
inf = float('inf')
n, ma, mb = map(int, input().split())
t = [[inf] * 401 for _ in range(401)]
for _ in range(n):
  a, b, c = map(int, input().split())
  for aa in range(400, 0, -1):
    for bb in range(400, 0, -1):
      if t[aa][bb] == inf:
        continue
      if t[a + aa][b + bb] > c + t[aa][bb]:
        t[a + aa][b + bb] = c + t[aa][bb]
  if t[a][b] > c:
    t[a][b] = c
result = inf
for a in range(400, 0, -1):
  for b in range(400, 0, -1):
    if a * mb == b * ma and t[a][b] < result:
      result = t[a][b]
if result == inf:
  print(-1)
else:
  print(result)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
n, ma, mb = map(int, input().split())
tc = [[] for _ in range(100 * n + 1)]
tn = [[] for _ in range(100 * n + 1)]
high = 100 * n + 1
for i in range(n):
  a, b, c = map(int, input().split())
  if a % ma == 0 and b % mb == 0 and a // ma == b // mb:
    high = c + 1
  tn[c].append((a, b))
  for j in range(1, high):
    for aa, bb in tc[j]:
      tn[j].append((aa, bb))
      tn[c + j].append((a + aa, b + bb))
      if (a + aa) % ma == 0 and (b + bb) % mb == 0 and (a + aa) // ma == (b + bb) // mb:
        high = c + j + 1
  tc = tn
  tn = [[] for _ in range(100 * n + 1)]
for i in range(1, 100 * n + 1):
  for a, b in tc[i]:
    if a % ma == 0 and b % mb == 0 and a // ma == b // mb:
      print(i)
      sys.exit()
print(-1)


Fixed submission:
import sys
inf = float('inf')
n, ma, mb = map(int, input().split())
t = [[inf] * 401 for _ in range(401)]
for _ in range(n):
  a, b, c = map(int, input().split())
  for aa in range(400, 0, -1):
    for bb in range(400, 0, -1):
      if t[aa][bb] == inf:
        continue
      if t[a + aa][b + bb] > c + t[aa][bb]:
        t[a + aa][b + bb] = c + t[aa][bb]
  if t[a][b] > c:
    t[a][b] = c
result = inf
for a in range(400, 0, -1):
  for b in range(400, 0, -1):
    if a * mb == b * ma and t[a][b] < result:
      result = t[a][b]
if result == inf:
  print(-1)
else:
  print(result)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
n, ma, mb = map(int, input().split())
tc = [[] for _ in range(100 * n + 1)]
tn = [[] for _ in range(100 * n + 1)]
high = 100 * n + 1
for i in range(n):
  a, b, c = map(int, input().split())
  if a % ma == 0 and b % mb == 0 and a // ma == b // mb:
    high = c + 1
  tn[c].append((a, b))
  for j in range(1, high):
    for aa, bb in tc[j]:
      tn[j].append((aa, bb))
      tn[c + j].append((a + aa, b + bb))
      if (a + aa) % ma == 0 and (b + bb) % mb == 0 and (a + aa) // ma == (b + bb) // mb:
        high = c + j + 1
  tc = tn
  tn = [[] for _ in range(100 * n + 1)]
for i in range(1, 100 * n + 1):
  for a, b in tc[i]:
    if a % ma == 0 and b % mb == 0 and a // ma == b // mb:
      print(i)
      sys.exit()
print(-1)


Fixed submission:
import sys
inf = float('inf')
n, ma, mb = map(int, input().split())
t = [[inf] * 401 for _ in range(401)]
for _ in range(n):
  a, b, c = map(int, input().split())
  for aa in range(400, 0, -1):
    for bb in range(400, 0, -1):
      if t[aa][bb] == inf:
        continue
      if t[a + aa][b + bb] > c + t[aa][bb]:
        t[a + aa][b + bb] = c + t[aa][bb]
  if t[a][b] > c:
    t[a][b] = c
result = inf
for a in range(400, 0, -1):
  for b in range(400, 0, -1):
    if a * mb == b * ma and t[a][b] < result:
      result = t[a][b]
if result == inf:
  print(-1)
else:
  print(result)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
n, ma, mb = map(int, input().split())
tc = [[] for _ in range(100 * n + 1)]
tn = [[] for _ in range(100 * n + 1)]
for i in range(n):
  a, b, c = map(int, input().split())
  tn[c].append((a, b))
  for j in range(1, 100 * n + 1):
    for aa, bb in tc[j]:
      tn[j].append((aa, bb))
      tn[c + j].append((a + aa, b + bb))
  tc = tn
  tn = [[] for _ in range(100 * n + 1)]
for i in range(1, 100 * n + 1):
  for a, b in tc[i]:
    if a % ma == 0 and b % mb == 0 and a // ma == b // mb:
      print(i)
      sys.exit()
print(-1)


Fixed submission:
import sys
inf = float('inf')
n, ma, mb = map(int, input().split())
t = [[inf] * 401 for _ in range(401)]
for _ in range(n):
  a, b, c = map(int, input().split())
  for aa in range(400, 0, -1):
    for bb in range(400, 0, -1):
      if t[aa][bb] == inf:
        continue
      if t[a + aa][b + bb] > c + t[aa][bb]:
        t[a + aa][b + bb] = c + t[aa][bb]
  if t[a][b] > c:
    t[a][b] = c
result = inf
for a in range(400, 0, -1):
  for b in range(400, 0, -1):
    if a * mb == b * ma and t[a][b] < result:
      result = t[a][b]
if result == inf:
  print(-1)
else:
  print(result)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
n, ma, mb = map(int, input().split())
t = [[[] for _ in range(100 * n + 1)] for _ in range(n + 1)]
for i in range(n):
  a, b, c = map(int, input().split())
  t[i + 1][c].append((a, b))
  for j in range(1, 100 * n + 1):
    for aa, bb in t[i][j]:
      t[i + 1][j].append((aa, bb))
      t[i + 1][c + j].append((a + aa, b + bb))
for i in range(1, 100 * n + 1):
  for a, b in t[n][i]:
    if a % ma == 0 and b % mb == 0 and a // ma == b // mb:
      print(i)
      sys.exit()
print(-1)


Fixed submission:
import sys
inf = float('inf')
n, ma, mb = map(int, input().split())
t = [[inf] * 401 for _ in range(401)]
for _ in range(n):
  a, b, c = map(int, input().split())
  for aa in range(400, 0, -1):
    for bb in range(400, 0, -1):
      if t[aa][bb] == inf:
        continue
      if t[a + aa][b + bb] > c + t[aa][bb]:
        t[a + aa][b + bb] = c + t[aa][bb]
  if t[a][b] > c:
    t[a][b] = c
result = inf
for a in range(400, 0, -1):
  for b in range(400, 0, -1):
    if a * mb == b * ma and t[a][b] < result:
      result = t[a][b]
if result == inf:
  print(-1)
else:
  print(result)