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):
N, Ma, Mb = [int(_) for _ in input().split()]
ABC = [[int(_) for _ in input().split()] for _ in range(N)]
dp = {}
dp[0] = {}
dp[0][ABC[0][0] + 1000 * ABC[0][1]] = ABC[0][2]
ans = 5000
for i in range(N - 1):
    for ab in dp[i].keys():
        a = ab % 1000
        b = ab // 1000
        if a * Mb == b * Ma:
            ans = min(ans, dp[i][ab])
        dp[i + 1] = dp.get(i + 1, {})
        dp[i + 1][a + ABC[i + 1][0] + 1000 * (b + ABC[i + 1][1])] = min(
            dp[i][ab] + ABC[i + 1][2], dp[i].get(
                a + ABC[i + 1][0] + 1000 * (b + ABC[i + 1][1]), 0))
for ab in dp[N - 1].keys():
    a = ab % 1000
    b = ab // 1000
    if a * Mb == b * Ma:
        ans = min(ans, dp[N - 1][ab])
if ans == 5000:
    print(-1)
else:
    print(ans)

N, Ma, Mb = [int(_) for _ in input().split()]
ABC = [[int(_) for _ in input().split()] for _ in range(N)]
dp = {}
dp[ABC[0][0] + 1000 * ABC[0][1]] = ABC[0][2]
ans = 5000
for i in range(N - 1):
    dp_old = dp.copy()
    for ab in dp_old.keys():
        a = ab % 1000
        b = ab // 1000
        if a * Mb == b * Ma:
            ans = min(ans, dp_old[ab])
        dp[a + ABC[i + 1][0] + 1000 * (b + ABC[i + 1][1])] = min(
            dp_old[ab] + ABC[i + 1][2],
            dp_old.get(a + ABC[i + 1][0] + 1000 * (b + ABC[i + 1][1]), 5000))
    print(dp)
for ab in dp.keys():
    a = ab % 1000
    b = ab // 1000
    if a * Mb == b * Ma:
        ans = min(ans, dp[ab])
if ans == 5000:
    print(-1)
else:
    print(ans)

N, Ma, Mb = [int(_) for _ in input().split()]
ABC = [[int(_) for _ in input().split()] for _ in range(N)]
dp = {}
dp[0] = 0
dp[ABC[0][0] + 1000 * ABC[0][1]] = ABC[0][2]
ans = 5000
for i in range(N - 1):
    dp_old = dp.copy()
    for ab in dp_old.keys():
        a = ab % 1000
        b = ab // 1000
        if a * Mb == b * Ma > 0:
            ans = min(ans, dp_old[ab])
        dp[a + ABC[i + 1][0] + 1000 * (b + ABC[i + 1][1])] = min(
            dp_old[ab] + ABC[i + 1][2],
            dp_old.get(a + ABC[i + 1][0] + 1000 * (b + ABC[i + 1][1]), 5000))
for ab in dp.keys():
    a = ab % 1000
    b = ab // 1000
    if a * Mb == b * Ma > 0:
        ans = min(ans, dp[ab])
if ans == 5000:
    print(-1)
else:
    print(ans)

Verdicts: Wrong Answer

Wrong Answer

Accepted