Problem:


Score : 400 points

Problem Statement

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.  

Constraints

1≦N≦40 

1≦a_i,b_i≦10 

1≦c_i≦100 

1≦M_a,M_b≦10 

gcd(M_a,M_b)=1

a_i, b_i, c_i, M_a and M_b are integers.

Input

The input is given from Standard Input in the following format:

N M_a M_b  
a_1 b_1 c_1  
a_2 b_2 c_2
:  
a_N b_N c_N  

Output

Print the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C, print -1 instead.

Sample Input 1

3 1 1
1 2 1
2 1 2
3 3 10

Sample Output 1

3

The amount of money spent will be minimized by purchasing the packages of chemicals 1 and 2.

In this case, the mixture of the purchased chemicals will contain 3 grams of the substance A and 3 grams of the substance B, which are in the desired ratio: 3:3=1:1.

The total price of these packages is 3 yen.  

Sample Input 2

1 1 10
10 10 10

Sample Output 2

-1

The ratio 1:10 of the two substances A and B cannot be satisfied by purchasing any combination of the packages. Thus, the output should be -1.



Sample input:
3 1 1
1 2 1
2 1 2
3 3 10


Sample output:
3

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

N, Ma, Mb = map(int, input().split())
M = [tuple(map(int, input().split())) for _ in range(N)]

d = defaultdict(lambda: float('inf'))
d[(0, 0)] = 0
for a, b, c in M:
    new_d = d.copy()
    for key_a, key_b in d:
        new_d[(a + key_a, b + key_b)] = min(new_d[(a + key_a, b + key_b)],
                                            new_d[(key_a, key_b)] + c)
    d = new_d

ans = float('inf')
for i in range(1, 401 // max(Ma, Mb)):
    ans = min(ans, d[(Ma * i, Mb * i)])
print(ans if ans != float('inf') else -1)

Submission status: Wrong Answer

Solution code:
n,ma,mb = map(int, input().split())
abc = [list(map(int, input().split())) for i in range(n)]
INF = 10 ** 18
dp = [[INF] * 500 for i in range(500)]
dp[0][0] = 0
for a,b,c in abc:
  for i in range(400):
    for j in range(400):
      dp[i+a][j+b] = min(dp[i+a][j+b],dp[i][j]+c)
ans = INF
for i in range(1,500):
  Ma,Mb = i * ma, i * mb
  if Ma > 400 or Mb > 400:
    break
  ans = min(ans,dp[Ma][Mb])
if ans == INF:
  print(-1)
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
n,ma,mb = map(int, input().split())
abc = [list(map(int, input().split())) for i in range(n)]
INF = 10 ** 18
dp = [[INF] * 500 for i in range(500)]
dp[0][0] = 0
for a,b,c in abc:
  for i in range(400):
    for j in range(400):
      dp[i+a][j+b] = min(dp[i+a][j+b],dp[i][j]+c)
ans = INF
for i in range(1,50):
  Ma,Mb = i * ma, i * mb
  if Ma > 400 or Mb > 400:
    break
  ans = min(ans,dp[Ma][Mb])
if ans == INF:
  print(-1)
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
n,ma,mb = map(int,input().split())
l = [list(map(int,input().split())) for i in range(n)]

dp = [[[float("INF")]*(n*10+2) for i in range(n*10+2)] for j in range(n+1)]

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

for i in range(n):
    a,b,c = l[i]
    print(a,b,c)
    for j in range(n*10+1):
        for k in range(n*10+1):
            
            if j >= a and k >= b:
                dp[i+1][j][k] = min(dp[i][j][k],dp[i][j-a][k-b]+c)
            elif dp[i][j][k] != float("INF"):
                dp[i+1][j][k] = dp[i][j][k]
ans = float("INF")

for i in range(1,n*10+1):
    j = i*mb/ma
    if j%1 == 0 and j < n*10:
        ans = min(ans,dp[-1][i][int(j)])
if ans == float("INF"):
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
MX = 401

N, Ma, Mb = map(int, input().split())
A = [0]*N
B = [0]*N
C = [0]*N
for i in range(N):
    a, b, c = map(int, input().split())
    A[i] = a
    B[i] = b
    C[i] = c
INF = 100
dp = [[[INF]*MX for i in range(MX)] for i in range(N+1)]
dp[0][0][0] = 0
for i in range(1, N+1):
    for j in range(0, MX):
        for k in range(0, MX):
            dp[i][j][k] = dp[i-1][j][k]
            if j < A[i-1] or k < B[i-1]: continue
            dp[i][j][k] = min(dp[i][j][k], dp[i-1][j-A[i-1]][k-B[i-1]]+C[i-1])
ans = INF
for i in range(1, MX):
    if Ma*i > MX-1 or Mb*i > MX-1: break
    ans = min(ans, dp[N][Ma*i][Mb*i])
if ans == INF:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    
    N,Ma,Mb=map(int,input().split())
    A, B, C = [0] * N, [0] * N, [0] * N
    for i in range(N):
        A[i], B[i], C[i] = map(int, input().split())

    # https://qiita.com/drken/items/dc53c683d6de8aeacf5a#%E9%A1%9E%E9%A1%8C-3
    
    INF = 10**10  # INF+INFを計算してもオーバーフローしない範囲で大きく
    
    P = list(map(int, input().split()))  # P[i]:=i番目の問題の点数
    I, J, K = N,sum(B),sum(C)
    # dpテーブル dp[i][j]:=i番目以下でj点となるなら1．ならないなら0
    dp = [[[INF]*(K+1) for _ in range(J+1)] for _ in range(I+1)]
    # 初期条件
    # for i in range(I+1):
    dp[0][0][0]=0
    # ループ
    for i in range(1, I+1):
        for j in range(J+1):
            for k in range(K+1):
                # i-1番目を選ぶ場合
                if j>=B[i-1] and k>=C[i-1]:
                    dp[i][j][k] = min(dp[i][j][k], dp[i-1][j-B[i-1]][k-C[i-1]]+A[i-1])
                else:
                    dp[i][j][k]=min(dp[i][j][k],dp[i-1][j][k])
    ans=INF
    for j in range(1,J+1):
        for k in range(1,K+1):
            if k*Ma==j*Mb:
                ans=min(ans,dp[I][j][k])

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

    
    
resolve()

Submission status: Wrong Answer

Solution code:
# https://abc054.contest.atcoder.jp/tasks/abc054_d

N, Ma, Mb = map(int, input().split())
la, lb, lc = [], [], []
for _ in range(N):
    ai, bi, ci = map(int, input().split())
    la.append(ai)
    lb.append(bi)
    lc.append(ci)

sumA = sum(la)
sumB = sum(lb)

Inf = int(1e10)

dp = [[[Inf for _ in range(sumB + 1)] for _ in range(sumA + 1)] for _ in range(N + 1)]
dp[0][0][0] = 0


for i in range(1, N + 1):
    ai, bi, ci = la[i - 1], lb[i - 1], lc[i - 1]

    for j in range(sumA + 1):
        for k in range(sumB + 1):
            dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j][k])
            if i - ai >= 0 and k - bi >= 0 and dp[i - 1][j - ai][k - bi] != Inf:
                dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j - ai][k - bi] + ci)

ans = Inf
for j in range(1, sumA + 1):
    for k in range(1, sumB + 1):
        if Mb * j == Ma * k:
            ans = min(ans, dp[N][j][k])
if ans == Inf:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
# https://abc054.contest.atcoder.jp/tasks/abc054_d

N, Ma, Mb = map(int, input().split())
la, lb, lc = [], [], []
for _ in range(N):
    ai, bi, ci = map(int, input().split())
    la.append(ai)
    lb.append(bi)
    lc.append(ci)

sumA = sum(la)
sumB = sum(lb)

Inf = 50000

dp = [[[Inf for _ in range(sumB + 1)] for _ in range(sumA + 1)] for _ in range(N + 1)]
# for i in range(n):
#     for j in range(n):
#         dp[i][0][j] = Inf
#         dp[i][j][0] = Inf
dp[0][0][0] = 0

# print(N, Ma, Mb)
# print(*la)
# print(*lb)
# print(*lc)

for i in range(1, N + 1):
    ai, bi, ci = la[i - 1], lb[i - 1], lc[i - 1]

    for j in range(sumA + 1):
        for k in range(sumB + 1):
            if j - ai >= 0 and k - bi >= 0:
                dp[i][j][k] = min(dp[i - 1][j - ai][k - bi] + ci, dp[i][j][k], dp[i - 1][j][k])


# for n in range(N + 1):
#     for row in dp[n]:
#         print(*row)
#     print("----")

ans = Inf
for j in range(1, sumA + 1):
    for k in range(1, sumB + 1):
        if j / k == Ma / Mb:
            # print(j, k, dp[N][j][k])
            ans = min(ans, dp[N][j][k])

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

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = map(int, input().split())
Ag = [0]
Bg = [0]
Cost = [0]
for _ in range(N):
    a, b, c = map(int, input().split())
    Ag.append(a)
    Bg.append(b)
    Cost.append(c)

ans = sum(Cost) + 1
DP = [[[float('inf')] * (sum(Bg)+1) for _ in range(sum(Ag) + 1)] for __ in range(N + 1)]

DP[0][0][0] = 0

for i in range(1, N+1):
    for j in range(1,sum(Ag)+1):
        for k in range(1,sum(Bg)+1):
            DP[i][j][k] = min(DP[i-1][j][k], DP[i-1][j-Ag[i]][k - Bg[i]] + Cost[i])
            if Ma * k == Mb * j:
                ans = min(DP[i][j][k], ans)
if ans == sum(Cost) + 1:
    print(-1)
    exit()
print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

def mix(abc,Ma,Mb):
    n = len(abc)
    OP = []
    for i in range(2**n):
        i_bin = bin(i)[2:]
        if len(i_bin) < n:
            pad = "0" * (n-len(i_bin))
            i_bin = pad + i_bin
        a,b,c = 0,0,0
        for j in range(len(i_bin)):
            if i_bin[j] == "1":
                a += abc[j][0]
                b += abc[j][1]
                c += abc[j][2]
        d = Mb*a-Ma*b
        OP.append((d,c))
    OP.sort(key = lambda x:x[0])
    return OP

def main():
    N,Ma,Mb = map(int,input().split())
    abc = [list(map(int,input().split())) for _ in range(N)]
    INF = 4001

    abc1,abc2 = abc[:len(abc)//2],abc[len(abc)//2:]
    DC1,DC2 = mix(abc1,Ma,Mb),mix(abc2,Ma,Mb)
    D2 = [DC2[i][0] for i in range(len(DC2))]
    D2_set = set(D2)

    ans = INF
    for d1,c1 in DC1:
        if -d1 in D2_set:
            ind = bisect_left(D2,-d1)
            c2 = DC2[ind][1]
            if c1 + c2 > 0:
                ans = min(ans,c1+c2)
    if len(abc1) == 0:
        if abc2[0][0]*Mb-abc2[0][1]*Ma == 0:
            ans = abc2[0][2]

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

def mix(abc,Ma,Mb):
    n = len(abc)
    OP = []
    for i in range(2**n):
        i_bin = bin(i)[2:]
        if len(i_bin) < n:
            pad = "0" * (n-len(i_bin))
            i_bin = pad + i_bin
        a,b,c = 0,0,0
        for j in range(len(i_bin)):
            if i_bin[j] == "1":
                a += abc[j][0]
                b += abc[j][1]
                c += abc[j][2]
        d = Mb*a-Ma*b
        OP.append((d,c))
    OP.sort(key = lambda x:x[0])
    return OP

def main():
    N,Ma,Mb = map(int,input().split())
    abc = [list(map(int,input().split())) for _ in range(N)]
    INF = 4001

    abc1,abc2 = abc[:len(abc)//2],abc[len(abc)//2:]
    DC1,DC2 = mix(abc1,Ma,Mb),mix(abc2,Ma,Mb)
    D2 = [DC2[i][0] for i in range(len(DC2))]
    D2_set = set(D2)

    ans = INF
    for i in range(len(DC1)):
        d1,c1 = DC1[i][0],DC1[i][1]
        if -d1 in D2_set:
            ind = bisect_left(D2,-d1)
            c2 = DC2[ind][1]
            if c1 + c2 > 0:
                ans = min(ans,c1+c2)
    if len(DC1) == 0:
        if DC2[0][0] == 0:
            ans = DC2[0][1]

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def half_enumeration(a, *, func):
    """正しい英訳は不明"""
    n = len(a)
    h = n // 2

    def enum(h, *, second_half: bool = False) -> set:
        p = set()
        if second_half:
            b = a[h:]
        else:
            b = a[:h]

        for x in b:
            q = set()
            for e in p:
                q.add(func(e, x))
            p.add(x)
            p.update(q)
        return p

    return enum(h), enum(h, second_half=True)


def main():
    INF = 40 * 100 + 1

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

    def update(e, x):
        return e[0] + x[0], e[1] + x[1]

    xcs = []
    for _ in range(N):
        ai, bi, ci = map(int, input().split())
        x = Ma * bi - Mb * ai  # Σai:Σbi=Ma:Mb<->Ma*Σbi-Mb*Σai=0
        xcs.append((x, ci))

    p1, p2 = half_enumeration(xcs, func=update)

    table = [INF] * (INF * 2 + 1)

    ans = INF
    for x, c in p1:
        table[x] = min(table[x], c)
        if x == 0: ans = min(ans, c)

    for x, c in p2:
        if table[-x] != INF: ans = min(ans, c + table[-x])
    print(-1 if ans == INF else ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

from itertools import product

N,Ma,Mb = LI()
a,b,c = LIR(N,3)

n = N//2
Z1 = list(product([0,1],repeat=n))
Z2 = list(product([0,1],repeat=N-n))

d1 = defaultdict(lambda:5000)
d2 = defaultdict(lambda:5000)

for bit in range(1<<n):
    L = bit.bit_length()
    val_a = 0
    val_b = 0
    cost = 0
    for i in range(L):
        if bit>>i&1:
            val_a += a[n-1-i]
            val_b += b[n-1-i]
            cost += c[n-1-i]
    if cost < d1[(val_a,val_b)]:
        d1[(val_a,val_b)] = cost

for bit in range(1<<(N-n)):
    L = bit.bit_length()
    val_a = 0
    val_b = 0
    cost = 0
    for i in range(L):
        if bit>>i&1:
            val_a += a[N-1-i]
            val_b += b[N-1-i]
            cost += c[N-1-i]
    if cost < d2[(val_a,val_b)]:
        d2[(val_a,val_b)] = cost

vals = []
now = 1
while True:
    if Ma*now <= sum(a) and Mb*now <= sum(b):
        vals.append((Ma*now,Mb*now))
        now += 1
    else:
        break

ans = 5000
for v in vals:
    for k in d1.keys():
        left_a = v[0]-k[0]
        left_b = v[1]-k[1]
        if d2[(left_a,left_b)]:
            if d1[k]+d2[(left_a,left_b)] < ans:
                ans = d1[k]+d2[(left_a,left_b)]

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

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

N,Ma,Mb = LI()
a,b,c = LIR(N,3)

amax = sum(a)
bmax = sum(b)

# dp[i][j][k]: 薬品kまで見て，Aがiグラム，Bがjグラムの最小予算
dp = [[[5000]*N for _ in range(bmax+1)] for _ in range(amax+1)]
dp[a[0]][b[0]][0] = c[0]
for k in range(1,N):
    for i in range(amax+1):
        for j in range(bmax+1):
            if i == j == 0:
                dp[i][j][k] = 0
            elif i-a[k] < 0 or j-b[k] < 0:
                dp[i][j][k] = dp[i][j][k-1]
            else:
                dp[i][j][k] = min(dp[i][j][k-1], dp[i-a[k]][j-b[k]][k-1]+c[k])

ans = 5000
now = 1
while True:
    if Ma*now <= amax and Mb*now <= bmax:
        if dp[Ma*now][Mb*now][N-1] < ans:
            ans = dp[Ma*now][Mb*now][N-1]
        now += 1
    else:
        break

if ans == 5000:
    print('-1')
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N, Ma, Mb = map(int, input().split())
    yakuhin = []
    for i in range(N):
        yakuhin.append(list(map(int, input().split())))
    
    INF = 10**15
    DP = [[[INF for _ in range(401)] for _ in range(401)] for _ in range(N+1)]
    DP[0][0][0] = 0
    
    for i in range(N):
        for j in range(401):
            for k in range(401):
                if(DP[i][j][k] != INF):
                    DP[i+1][j][k] = min(DP[i+1][j][k], DP[i][j][k])
                    DP[i+1][j+yakuhin[i][0]][k+yakuhin[i][1]] = min(DP[i+1][j+yakuhin[i][0]][k+yakuhin[i][1]], DP[i][j][k]+yakuhin[i][2])
    
    ans = INF
    for i in range(1, N):
        for j in range(1, 401):
            for k in range(1, 401):
                if(j*Mb == k*Ma):
                    ans = min(ans, DP[i][j][k])    
        
    print(ans if ans != INF else -1)
    
main()

Submission status: Wrong Answer

Solution code:
def main():
    N, Ma, Mb = map(int, input().split())
    yakuhin = []
    sa = 0
    sb = 0
    for i in range(N):
        yakuhin.append(list(map(int, input().split())))
        sa += yakuhin[i][0]
        sb += yakuhin[i][1]
    
    INF = 10**15
    DP = [[[INF for _ in range(sb+1)] for _ in range(sa+1)] for _ in range(N+1)]
    DP[0][0][0] = 0
    
    for i in range(N):
        for j in range(sa+1):
            for k in range(sb+1):
                if(j >= yakuhin[i][0] and k >= yakuhin[i][1]):
                    DP[i+1][j][k] = min(DP[i][j-yakuhin[i][0]][k-yakuhin[i][1]], DP[i][j-yakuhin[i][0]][k-yakuhin[i][1]]+yakuhin[i][2])
                else:
                    DP[i+1][j][k] = min(DP[i+1][j][k], DP[i][j][k])
        
    ans = INF
    for i in range(1, N):
        for j in range(1, sa+1):
            for k in range(1, sb+1):
                if(j*Mb == k*Ma):
                    ans = min(ans, DP[i][j][k])    
        
    print(ans if ans != INF else -1)
    
main()

Submission status: Wrong Answer

Solution code:
def main():
    N, Ma, Mb = map(int, input().split())
    yakuhin = []
    sa = 0
    sb = 0
    for i in range(N):
        yakuhin.append(list(map(int, input().split())))
        sa += yakuhin[i][0]
        sb += yakuhin[i][1]
    
    INF = 10**15
    DP = [[[INF for _ in range(sb+1)] for _ in range(sa+1)] for _ in range(N+1)]
    DP[0][0][0] = 0
    
    for i in range(N):
        for j in range(sa+1):
            for k in range(sb+1):
                if(j >= yakuhin[i][0] and k >= yakuhin[i][1]):
                    DP[i+1][j][k] = min(DP[i+1][j][k], DP[i][j-yakuhin[i][0]][k-yakuhin[i][1]]+yakuhin[i][2])
                else:
                    DP[i+1][j][k] = min(DP[i+1][j][k], DP[i][j][k])
        
    ans = INF
    for i in range(1, N):
        for j in range(1, sa+1):
            for k in range(1, sb+1):
                if(j*Mb == k*Ma):
                    ans = min(ans, DP[i][j][k])    
        
    print(ans if ans != INF else -1)
    
main()

Submission status: Wrong Answer

Solution code:
def main():
    N, Ma, Mb = map(int, input().split())
    yakuhin = []
    sa = 0
    sb = 0
    for i in range(N):
        yakuhin.append(list(map(int, input().split())))
        sa += yakuhin[i][0]
        sb += yakuhin[i][1]
    
    INF = 10**15
    DP = [[[INF for _ in range(sb+1)] for _ in range(sa+1)] for _ in range(N+1)]
    
    for i in range(N):
        DP[i][0][0] = 0
    
    for i in range(N):
        for j in range(sa+1):
            for k in range(sb+1):
                if(j >= yakuhin[i][0] and k >= yakuhin[i][1]):
                    DP[i+1][j][k] = min(DP[i][j][k], DP[i][j-yakuhin[i][0]][k-yakuhin[i][1]]+yakuhin[i][2])
                else:
                    DP[i+1][j][k] = min(DP[i+1][j][k], DP[i][j][k])
        
    ans = INF
    for i in range(1, N):
        for j in range(1, sa+1):
            for k in range(1, sb+1):
                if(j*Mb == k*Ma):
                    ans = min(ans, DP[i][j][k])    
        
    print(ans if ans != INF else -1)
    
main()

Submission status: Wrong Answer

Solution code:
def main():
    N, Ma, Mb = map(int, input().split())
    yakuhin = []
    sa = 0
    sb = 0
    for i in range(N):
        yakuhin.append(list(map(int, input().split())))
        sa += yakuhin[i][0]
        sb += yakuhin[i][1]
    
    INF = 10**15
    DP = [[[INF for _ in range(sb+1)] for _ in range(sa+1)] for _ in range(N+1)]
    
    for i in range(N):
        DP[i][0][0] = 0
    
    for i in range(N):
        for j in range(1, sa+1):
            for k in range(1, sb+1):
                if(j >= yakuhin[i][0] and k >= yakuhin[i][1]):
                    DP[i+1][j][k] = min(DP[i][j][k], DP[i][j-yakuhin[i][0]][k-yakuhin[i][1]]+yakuhin[i][2])
                else:
                    DP[i+1][j][k] = DP[i][j][k]
        
    ans = INF
    for i in range(1, N):
        for j in range(1, sa+1):
            for k in range(1, sb+1):
                if(j*Mb == k*Ma):
                    ans = min(ans, DP[i][j][k])    
        
    print(ans if ans != INF else -1)
    
main()

Submission status: Wrong Answer

Solution code:
def main():
    N, Ma, Mb = map(int, input().split())
    yakuhin = []
    sa = 0
    sb = 0
    for i in range(N):
        yakuhin.append(list(map(int, input().split())))
        sa += yakuhin[i][0]
        sb += yakuhin[i][1]
    
    INF = 10**15
    DP = [[[INF for _ in range(sb+1)] for _ in range(sa+1)] for _ in range(N+1)]
    
    for i in range(N):
        DP[i][0][0] = 0
    
    for i in range(N):
        for j in range(sa+1):
            for k in range(sb+1):
                if(j >= yakuhin[i][0] and k >= yakuhin[i][1]):
                    DP[i+1][j][k] = min(DP[i][j][k], DP[i][j-yakuhin[i][0]][k-yakuhin[i][1]]+yakuhin[i][2])
        
    ans = INF
    for i in range(1, N):
        for j in range(1, sa+1):
            for k in range(1, sb+1):
                if(j*Mb == k*Ma):
                    ans = min(ans, DP[i][j][k])    
        
    print(ans if ans != INF else -1)
    
main()

Submission status: Wrong Answer

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

INF = 10000

dp = [[INF for b in range(401)] for a in range(401)]

dp[0][0] = 0

for i in range(1,N+1):
    ai,bi,ci = map(int,input().split())
    for a in range(401):
        for b in range(401):
            if a-ai >= 0 and b-bi >= 0:
                dp[a][b] = min(dp[a][b],dp[a-ai][b-bi]+ci)
            else:
                dp[a][b] = dp[a][b]

ans = INF
for j in range(1,401):
    if Ma*j >= 400 or Mb*j >= 400:
        break
    ans = min(ans,dp[Ma*j][Mb*j])
print(ans if ans != INF else -1)

Submission status: Wrong Answer

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

INF = 10000

dp = [[INF for b in range(401)] for a in range(401)]

dp[0][0] = 0

for i in range(1,N+1):
    ai,bi,ci = map(int,input().split())
    for a in range(401):
        for b in range(401):
            if a-ai >= 0 and b-bi >= 0:
                dp[a][b] = min(dp[a][b],dp[a-ai][b-bi]+ci)
            else:
                dp[a][b] = dp[a][b]

ans = INF
for j in range(1,401):
    if Ma*j > 400 or Mb*j > 400:
        break
    ans = min(ans,dp[Ma*j][Mb*j])
print(ans if ans != INF else -1)

Submission status: Wrong Answer

Solution code:
import itertools

N, M_a, M_b = map(int, input().split())

a_l = [None] * N
b_l = [None] * N
c_l = [None] * N

result = -1

for i in range(N):
    a_l[i], b_l[i], c_l[i] = map(int, input().split())
    # print(a_l[i], b_l[i], c_l[i])

for i in range(N+1):
    for V in itertools.combinations_with_replacement([n for n in range(N)], i):
        if len(V) == 0:
            continue
        # print(V)
        # continue
        a_total = sum(a_l[v] for v in V)
        b_total = sum(b_l[v] for v in V)
        c_total = sum(c_l[v] for v in V)
        if result < c_total and result != -1:
            continue
        # print('a_total:', a_total)
        # print('b_total:', b_total)
        if a_total / b_total == M_a / M_b:
            # print(f'調合可能 {c_total}')
            result = c_total
print(result)

Submission status: Wrong Answer

Solution code:
import itertools

N, M_a, M_b = map(int, input().split())

a_l = [None] * N
b_l = [None] * N
c_l = [None] * N

c_total_l = []

for i in range(N):
    a_l[i], b_l[i], c_l[i] = map(int, input().split())
    # print(a_l[i], b_l[i], c_l[i])

for i in range(N):
    for V in itertools.combinations_with_replacement([n for n in range(N)], i):
        if len(V) == 0:
            continue
        a_total = sum(a_l[v] for v in V)
        b_total = sum(b_l[v] for v in V)
        c_total = sum(c_l[v] for v in V)
        # print('a_total:', a_total)
        # print('b_total:', b_total)
        if a_total / b_total == M_a / M_b:
            # print(f'調合可能 {c_total}')
            c_total_l.append(c_total)

print(min(c_total_l) if len(c_total_l) > 0 else -1)

Submission status: Wrong Answer

Solution code:
def main():
    # import sys
    # readline = sys.stdin.readline
    # readlines = sys.stdin.readlines
    N, Ma, Mb = map(int, input().split())
    med = [tuple(map(int, input().split())) for _ in range(N)]
    INF = float('inf')
    M = 110
    dp = [[INF] * (M + 1) for _ in range(M + 1)]
    dp[0][0] = 0

    for a, b, c in med:
        for i in range(M + 1 - a):
            for j in range(M + 1 - b):
                here = dp[i][j]
                dp[i + a][j + b] = min(dp[i + a][j + b], here + c)

    ans = INF
    ma = Ma; mb = Mb
    while ma < M and mb < M:
        ans = min(ans, dp[ma][mb])
        ma += Ma
        mb += Mb
    
    if ans < INF:
        print(ans)
    else:
        print(-1)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    # import sys
    # readline = sys.stdin.readline
    # readlines = sys.stdin.readlines
    N, Ma, Mb = map(int, input().split())
    med = [tuple(map(int, input().split())) for _ in range(N)]
    INF = 10 ** 6
    M = 500
    dp = [[INF] * (M + 1) for _ in range(M + 1)]
    dp[0][0] = 0

    for a, b, c in med:
        for i in range(M + 1 - a):
            for j in range(M + 1 - b):
                here = dp[i][j]
                dp[i + a][j + b] = min(dp[i + a][j + b], here + c)

    ans = INF
    ma = Ma; mb = Mb
    while ma < M and mb < M:
        ans = min(ans, dp[ma][mb])
        ma += Ma
        mb += Mb
    
    if ans < INF:
        print(ans)
    else:
        print(-1)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    # import sys
    # readline = sys.stdin.readline
    # readlines = sys.stdin.readlines
    N, Ma, Mb = map(int, input().split())
    med = [tuple(map(int, input().split())) for _ in range(N)]
    INF = 10 ** 6
    M = 100
    dp = [[INF] * (M + 1) for _ in range(M + 1)]
    dp[0][0] = 0

    for a, b, c in med:
        for i in range(M + 1 - a):
            for j in range(M + 1 - b):
                here = dp[i][j]
                dp[i + a][j + b] = min(dp[i + a][j + b], here + c)

    ans = INF
    ma = Ma; mb = Mb
    while ma < 100 and mb < 100:
        ans = min(ans, dp[ma][mb])
        ma += Ma
        mb += Mb
    
    if ans < INF:
        print(ans)
    else:
        print(-1)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N,Ma,Mb=map(int,input().split())
a=[0 for k in range(N)]
b=[0 for k in range(N)]
c=[0 for k in range(N)]

for k in range(N):
    a[k],b[k],c[k]=map(int,input().split())
Sa=sum(a)
Sb=sum(b)
dp =[[5000 for k in range(Sb+1)]for k in range(Sa+1)]
dp[0][0]=0
for k in range(N):
    listj=[]
    listl=[]
    for j in range(Sa-a[k]+1):
        for l in range(Sb-b[k]+1):
            if dp[j][l]!=5000:
                listj.append(j)
                listl.append(l)
    for m in range(len(listl)):
        dp[listj[m]+a[k]][listl[m]+b[k]]=min(dp[listj[m]][listl[m]]+c[k], dp[listj[m]+a[k]][listl[m]+b[k]])

ans=[5000]
for k in range(1, min(Sa//Ma,Sb//Mb)+1):
    ans.append(dp[Ma*k][Mb*k])
if min(ans)==5000:
    print(-1)
else:
    print(min(ans))

Submission status: Wrong Answer

Solution code:
N,Ma,Mb=map(int,input().split())
a=[0 for k in range(N)]
b=[0 for k in range(N)]
c=[0 for k in range(N)]

for k in range(N):
    a[k],b[k],c[k]=map(int,input().split())
Sa=sum(a)
Sb=sum(b)
dp =[[5000 for k in range(Sb+10)]for k in range(Sa+10)]
dp[0][0]=0
for k in range(N):
    for j in range(Sa-a[k]+10):
        for l in range(Sb-b[k]+10):
            dp[j+a[k]][l+b[k]]=min(dp[j][l]+c[k], dp[j+a[k]][l+b[k]])

ans=[5000]
for k in range(1, min(Sa//Ma,Sb//Mb)+1):
    ans.append(dp[Ma*k][Mb*k])
if min(ans)==5000:
    print(-1)
else:
    print(min(ans))

Submission status: Wrong Answer

Solution code:
N,Ma,Mb=map(int,input().split())
a=[0 for k in range(N)]
b=[0 for k in range(N)]
c=[0 for k in range(N)]

for k in range(N):
    a[k],b[k],c[k]=map(int,input().split())
Sa=sum(a)
Sb=sum(b)
dp =[[5000 for k in range(Sb+1)]for k in range(Sa+1)]
dp[0][0]=0
for k in range(N):
    for j in range(Sa-a[k]+1):
        for l in range(Sb-b[k]+1):
            dp[j+a[k]][l+b[k]]=min(dp[j][l]+c[k], dp[j+a[k]][l+b[k]])

ans=[5000]
for k in range(1, min(Sa//Ma,Sb//Mb)+1):
    ans.append(dp[Ma*k][Mb*k])
if min(ans)==5000 or min(ans)==0:
    print(-1)
else:
    print(min(ans))

Submission status: Wrong Answer

Solution code:
N,Ma,Mb=map(int,input().split())
a=[0 for k in range(N)]
b=[0 for k in range(N)]
c=[0 for k in range(N)]

for k in range(N):
    a[k],b[k],c[k]=map(int,input().split())
Sa=sum(a)
Sb=sum(b)
dp =[[5000 for k in range(Sb+1)]for k in range(Sa+1)]
dp[0][0]=0
for k in range(N):
    for j in range(Sa-a[k]+1):
        for l in range(Sb-b[k]+1):
            dp[j+a[k]][l+b[k]]=min(dp[j][l]+c[k], dp[j+a[k]][l+b[k]])

ans=[5000]
for k in range(1, min(Sa//Ma,Sb//Mb)+1):
    ans.append(dp[Ma*k][Mb*k])
if min(ans)==5000:
    print(-1)
else:
    print(min(ans))

Submission status: Wrong Answer

Solution code:
N,Ma,Mb=map(int,input().split())
a=[0 for k in range(N)]
b=[0 for k in range(N)]
c=[0 for k in range(N)]

for k in range(N):
    a[k],b[k],c[k]=map(int,input().split())
Sa=sum(a)
Sb=sum(b)
dp =[[5000 for k in range(Sb+10)]for k in range(Sa+10)]
dp[0][0]=0
for k in range(N):
    for j in range(Sa-a[k]+2):
        for l in range(Sb-b[k]+2):
            dp[j+a[k]][l+b[k]]=min(dp[j][l]+c[k], dp[j+a[k]][l+b[k]])
ans=[5000]

for k in range(1, min(sum(a)//Mb,sum(b)//Ma)+1):
    ans.append(dp[Mb*k][Ma*k])
if min(ans)==5000:
    print(-1)
else:
    print(min(ans))

Submission status: Wrong Answer

Solution code:
N,Ma,Mb=map(int,input().split())
a=[0 for k in range(N)]
b=[0 for k in range(N)]
c=[0 for k in range(N)]

for k in range(N):
    a[k],b[k],c[k]=map(int,input().split())
Sa=sum(a)
Sb=sum(b)
dp =[[5000 for k in range(Sb+1)]for k in range(Sa+1)]
dp[0][0]=0
for k in range(N):
    for j in range(Sa-a[k]+1):
        for l in range(Sb-b[k]+1):
            dp[j+a[k]][l+b[k]]=min(dp[j][l]+c[k], dp[j+a[k]][l+b[k]])
ans=[5000]

for k in range(1, min(sum(a),sum(b))//max(Ma,Mb)):
    ans.append(dp[Mb*k][Ma*k])
if min(ans)==5000:
    print(-1)
else:
    print(min(ans))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N, Ma, Mb = map(int, input().split())
MAX = 400
dp = [[float('inf')] * (MAX+1) for _ in range(MAX+1)]
dp[0][0] = 0
for _ in range(N):
    a, b, c = map(int, input().split())
    for i in range(a, MAX+1):
        for j in range(b, MAX+1):
            dp[i][j] = min(dp[i][j], dp[i-a][j-b] + c)
ans = float('inf')
x = 1
while Ma*x <= MAX and Mb*x <= MAX:
    ans = min(ans, dp[Ma*x][Mb*x])
    x += 1
print(ans if ans < float('inf') else -1)