Problem: Dolphin is planning to generate a small amount of a certain chemical substance C.   In order to generate the substance C, he must prepare a solution which is a mixture of two substances A and B in the ratio of M_a:M_b.   He does not have any stock of chemicals, however, so he will purchase some chemicals at a local pharmacy.   The pharmacy sells N kinds of chemicals. For each kind of chemical, there is exactly one package of that chemical in stock.   The package of chemical i contains a_i grams of the substance A and b_i grams of the substance B, and is sold for c_i yen (the currency of Japan).   Dolphin will purchase some of these packages. For some reason, he must use all contents of the purchased packages to generate the substance C.   Find the minimum amount of money required to generate the substance C.   If it is not possible to generate the substance C by purchasing any combination of packages at the pharmacy, report that fact.

Submission attempts (in order):
INF = float('inf')
n, ma, mb = map(int, input().split())
cmax = n * 10
t = [[INF] * (cmax + 1) for _ in range(cmax + 1)]
for _ in range(n):
  a, b, c = map(int, input().split())
  for aa in range(cmax, 0, -1):
    for bb in range(cmax, 0, -1):
      if t[aa][bb] == INF:
        continue
      t[a + aa][b + bb] = min(t[a + aa][b + bb], t[aa][bb] + c)
  t[a][b] = min(t[a][b], c)
result = INF
for a in range(1, cmax):
  for b in range(1, cmax):
    if a * mb == b * ma:
      t[a][b] = min(t[a][b], result)
if result == INF:
  result = -1
print(result)


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


import sys
inf = float('inf')
n, ma, mb = map(int, input().split())
t = [[inf] * 401 for _ in range(401)]
t[0][0] = 0
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]
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 == 0:
  print(-1)
else:
  print(result)


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:
      print(i)
      sys.exit()
print(-1)


import sys
inf = float('inf')
n, ma, mb = map(int, input().split())
t = [[inf] * 401 for _ in range(401)]
t[0][0] = 0
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]
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)


# DP
INF = float('inf')

N, Ma, Mb = map(int, input().split())

cmax = N * 10
t = [[INF] * (cmax + 1) for _ in range(cmax + 1)]
for _ in range(N):
    a, b, c = map(int, input().split())
    for aa in range(cmax, 0, -1):
        for bb in range(cmax, 0, -1):
            if t[aa][bb] == INF:
                continue
            t[aa + a][bb + b] = min(t[aa + a][bb + b], t[aa][bb] + c)
    t[a][b] = min(t[a][b], c)

result = INF
for a in range(1, cmax):
    for b in range(1, cmax):
        if a * Mb == b * Ma:
            result = min(result, t[a][b])
if result == INF:
    result = -1
print(result)


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted