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: Runtime Error

Solution code:
import numpy as np

N, Ma, Mb = map(int, input().split())
l = []
for i in range(N):
    l.append(list(map(int, input().split())))
ans = 4001

dp = np.full((N + 1, 100 * N, 100 * N), 4001)
dp[0][0][0] = 0
for n in range(N):
    for a in range(10 * N):
        for b in range(10 * N):
            if dp[n][a][b] == 4001:
                continue
            dp[n + 1][a][b] = min(dp[n + 1][a][b], dp[n][a][b])
            dp[n + 1][a + l[n][0]][b + l[n][1]] = min(dp[n + 1][a + l[n][0]][b + l[n][1]], dp[n][a][b] + l[n][2])
# np.set_printoptions(threshold=np.inf)
# print(dp)
for a in range(1, 10 * N):
    for b in range(1, 10 * N):
        if a * Mb == b * Ma:
            ans = min(ans, dp[n + 1][a][b])
if ans == 4001:
    print(-1)
else:
    print(ans)

Submission status: Runtime Error

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

from operator import itemgetter
abc.sort(key=itemgetter(2))

dp=[[10**100 for i in range(401)]for j in range(401)]
dp[0][0]=0
from collections import deque
q=deque([(0,0)])
p=0
for a,b,c in abc:
    l=[]
    while q:
        i,j=q.popleft()
        l.append([i,j])
        l.append([i+a,j+b])
        dp[a+i][b+j]=min(dp[a+i][b+j],dp[i][j]+c)
    q=deque(l)

ans=1145141919
i=1
while ma*i<500 and mb*i<500:
    ans=min(ans,dp[ma*i][mb*i])
    i+=1
if ans==1145141919:
    print(-1)
else:
    print(ans)

Submission status: Runtime Error

Solution code:
n,a,b=[int(x) for x in str(input()).split()]
A=[]
for i in range(n):
  A.append([int(x) for x in str(input()).split()])
 
ans=[]
for i in range(10):
  y=[]
  for j in range(10):
    x=[]
    for k in range(n):
      x.append(-1)
    y.append(x)
  ans.append(y)

#print(ans)
#print(A)
def f(ma,mb,cost,index):

  if ans[ma][mb][index]==-1:
    newa=ma+A[index][0]
    newb=mb+A[index][1]
    newc=cost+A[index][2]
    
    #print(index,newa,newb,newc)
    if newa*b==a*newb:
      ans[ma][mb][index]=newc
    elif index==n-1:
      ans[ma][mb][index]=100000000000000000000
    else:
      ith=f(newa,newb,newc,index+1)
      ans[ma][mb][index]= min(ith,f(ma,mb,cost,index+1))
  return ans[ma][mb][index]
  
 
if f(0,0,0,0)<100000000000000000000:
  print(f(0,0,0,0))
else:
  print(-1)

Submission status: Runtime Error

Solution code:
N,Ma,Mb=map(int,input().split())
abc=[list(map(int,input().split())) for _ in range(N)]
INF=5000
dp=[[[INF]*(10*N+1) for _ in range(10*N+1)] for _ in range(N+1)]
dp[0][0][0]=0
for n in range(N):
  a,b,c=abc[n][0],abc[n][1],abc[n][2]
  for i in range(10*N+1):
    for j in range(10*N+1):
      if dp[n][i][j]==INF:
        continue
      dp[n+1][i][j]=min(dp[n+1][i][j],dp[n][i][j])
      dp[n+1][i+a][j+b]=min(dp[n+1][i+a][j+b],dp[n][i][j]+c)

ans=INF
for i in range(1,10*N+1):
  if (i*Ma>INF or i*Mb>INF):
    break
  ans=min(ans,dp[N][i*Ma][i*Mb])
  
if ans==INF:
  print(-1)
else:
  print(ans)

Submission status: Runtime Error

Solution code:
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)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
#ABC54 D

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7

n,ma,mb = map(int,input().split())
x = [list(map(int,input().split())) for _ in range(n)]
sa = 0
sb = 0
for a,b,c in x:
    sa += a
    sb += b
dp = [[float('inf')]*(sb+1) for _ in range(sa+1)]
dp[0][0] = 0
for a,b,c in x:
    y = [[0]*(sb+1) for _ in range(sb+1)]
    for i in range(sa+1):
        for j in range(sb+1):
            if i+a < sa+1 and j+b < sb+1:
                if y[i][j] == 0:
                    dp[i+a][j+b] = min(dp[i+a][j+b],dp[i][j]+c)
                    y[i+a][j+b] = 1
ans = float('inf')
for i in range(1,sa+1):
    for j in range(1,sb+1):
        if i*mb == j*ma:
            ans = min(ans,dp[i][j])
if ans == float('inf'):
    print(-1)
else:
    print(ans)

Submission status: Runtime Error

Solution code:


import math
N,Ma,Mb = map(int,input().split(" "))
drags = [list(map(int,input().split(" "))) for  _ in range(N)]
INF = 10 ** 9
lim = N * 10 + 1
dp = [[[ INF for i in range(lim)] for j in range(lim)] for _ in range(N+1)]
dp[0][0][0] = 0
for num in range( N ):
    a,b,c = drags[num]
    for i in range(a,401): #薬品厨のa の量
        for j in range(b,401 ):#薬品厨のb の量
             dp[num + 1][i][j] = min(dp[num][i ][j] ,dp[num][i - a][j - b]  + c)

ans = INF
for i in range(1,41):
    MA = Ma * i
    MB = Mb * i
    if MA >= lim or MB >= lim:
        break
    ans = min(ans, dp[-1][MA][MB])
if ans == INF:
    ans = -1
print(ans)

Submission status: Runtime Error

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

INF = 10 ** 5
nums = 7

dp = [[[INF for _ in range(nums)] for _ in range(nums)] for _ in range(N+1)]

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

for i in range(N):
    for sum_a in range(nums):
        for sum_b in range(nums):
            if dp[i][sum_a][sum_b] == INF:
                continue
            dp[i+1][sum_a+a[i]][sum_b+b[i]] = min(dp[i+1][sum_a+a[i]][sum_b+b[i]], dp[i][sum_a][sum_b] + c[i])
            dp[i+1][sum_a][sum_b] = min(dp[i+1][sum_a][sum_b], dp[i][sum_a][sum_b])
            #dp[i+1][sum_a+a[i]][sum_b+b[i]] = min(dp[i+1][sum_a+a[i]][sum_b+b[i]], dp[i][sum_a][sum_b] + c[i])
            #print(i, dp[i][sum_a][sum_b], sum_a, sum_b)


res = INF
for i in range(1, nums):
    for j in range(1, nums):
        if i * Mb == j * Ma:
            res = min(res, dp[N][i][j])

if res == INF:
    res = -1

print(res)

Submission status: Runtime Error

Solution code:
N, Ma, Mb = map(int, input().split())
A = [0]*N
B = [0]*N
C = [0]*N

for i in range(N):
    A[i], B[i], C[i] = map(int, input().split())

# print(N, Ma, Mb, A, B, C)

A = [Mb * a for a in A]
B = [Ma * b for b in B]
D = []
for i in range(N):
    D.append((A[i] - B[i], C[i]))
# print(N, Ma, Mb, A, B, C, D)

Dp = []
Dn = []
Dz = []
for d in D:
    if d[0] == 0:
        Dz.append(d)
    elif d[0] > 0:
        Dp.append(d)
    else:
        Dn.append(d)

Dp.sort(key=lambda x: x[0])
Dn.sort(key=lambda x: x[0])
Dz.sort(key=lambda x: x[1])
min_c = 100*41
if len(Dz) > 0:
    min_c = Dz[0][1]
# print(Dp, Dn, Dz, min_c)

Ds = Dn
Dl = Dp
# if len(Dp) < len(Dn):
#     Ds, Dl = Dp, Dn
# else:
#     Ds, Dl = Dn, Dp

if len(Ds) or len(Dl) == 0:
    if min_c == 100*41:
        print(-1)
    else:
        print(min_c)
    exit()

def calc(D, l):
    s = 0
    c = 0
    for i in range(len(D)):
        if (l >> i) & 1 != 0:
            s += D[i][0]
            c += D[i][1]
    return (s, c)

Ls = len(Ds)
Ll = len(Dl)
Ds_memo = []
Dl_memo = []

for ls in range(1 << (Ls - 1)):
    Ds_memo.append(calc(Ds, ls + 1))
for ll in range(1 << (Ll - 1)):
    Dl_memo.append(calc(Dl, ll + 1))

Ds_memo.sort(key=lambda x: x[1])
Dl_memo.sort(key=lambda x: x[0])

# print(Ds_memo, Dl_memo)

for ls in Ds_memo:
    s = ls[0]
    c = ls[1]
    # print('ls', ls, s, c)
    if c >= min_c:
        break
    for ll in Dl_memo:
        sl = ll[0]
        cl = ll[1]
        # print('ll', ll, sl, cl)
        if s + sl > 0:
            break
        if c + cl >= min_c:
            continue
        if s + sl == 0:
            min_c = c + cl

if min_c == 100*41:
    print(-1)
else:
    print(min_c)

Submission status: Runtime Error

Solution code:
N, Ma, Mb = map(int, input().split())
A = [0]*N
B = [0]*N
C = [0]*N

for i in range(N):
    A[i], B[i], C[i] = map(int, input().split())

# print(N, Ma, Mb, A, B, C)

A = [Mb * a for a in A]
B = [Ma * b for b in B]
D = []
for i in range(N):
    D.append((A[i] - B[i], C[i]))
# print(N, Ma, Mb, A, B, C, D)

Dp = []
Dn = []
Dz = []
for d in D:
    if d[0] == 0:
        Dz.append(d)
    elif d[0] > 0:
        Dp.append(d)
    else:
        Dn.append(d)

Dp.sort(key=lambda x: x[0])
Dn.sort(key=lambda x: x[0])
Dz.sort(key=lambda x: x[1])
min_c = 100*41
if len(Dz) > 0:
    min_c = Dz[0][1]
# print(Dp, Dn, Dz, min_c)

Ds = Dn
Dl = Dp
# if len(Dp) < len(Dn):
#     Ds, Dl = Dp, Dn
# else:
#     Ds, Dl = Dn, Dp

if len(Ds) == 0:
    if min_c == 100*41:
        print(-1)
    else:
        print(min_c)
    exit()

def calc(D, l):
    s = 0
    c = 0
    for i in range(len(D)):
        if (l >> i) & 1 != 0:
            s += D[i][0]
            c += D[i][1]
    return (s, c)

Ls = len(Ds)
Ll = len(Dl)
Ds_memo = []
Dl_memo = []

for ls in range(1 << (Ls - 1)):
    Ds_memo.append(calc(Ds, ls + 1))
for ll in range(1 << (Ll - 1)):
    Dl_memo.append(calc(Dl, ll + 1))

Ds_memo.sort(key=lambda x: x[1])
Dl_memo.sort(key=lambda x: x[0])

# print(Ds_memo, Dl_memo)

for ls in Ds_memo:
    s = ls[0]
    c = ls[1]
    # print('ls', ls, s, c)
    if c >= min_c:
        break
    for ll in Dl_memo:
        sl = ll[0]
        cl = ll[1]
        # print('ll', ll, sl, cl)
        if s + sl > 0:
            break
        if c + cl >= min_c:
            continue
        if s + sl == 0:
            min_c = c + cl

if min_c == 100*41:
    print(-1)
else:
    print(min_c)

Submission status: Runtime Error

Solution code:
N, Ma, Mb = map(int, input().split())
A = [0]*N
B = [0]*N
C = [0]*N

for i in range(N):
    A[i], B[i], C[i] = map(int, input().split())

# print(N, Ma, Mb, A, B, C)

A = [Mb * a for a in A]
B = [Ma * b for b in B]
D = []
for i in range(N):
    D.append((A[i] - B[i], C[i]))
# print(N, Ma, Mb, A, B, C, D)

Dp = []
Dn = []
Dz = []
for d in D:
    if d[0] == 0:
        Dz.append(d)
    elif d[0] > 0:
        Dp.append(d)
    else:
        Dn.append(d)

Dp.sort(key=lambda x: x[0])
Dn.sort(key=lambda x: x[0])
Dz.sort(key=lambda x: x[1])
min_c = 100*41
if len(Dz) > 0:
    min_c = Dz[0][1]
# print(Dp, Dn, Dz, min_c)

Ds = []
Dl = []
if len(Dp) < len(Dn):
    Ds, Dl = Dp, Dn
else:
    Ds, Dl = Dn, Dp

def calc(D, l):
    s = 0
    c = 0
    for i in range(len(D)):
        if (l >> i) & 1 != 0:
            s += D[i][0]
            c += D[i][1]
    return s, c

Ls = len(Ds)
Ll = len(Dl)
for ls in range(1 << (Ls - 1)):
    s, c = calc(Ds, ls + 1)
    # print('ls', ls, s, c)
    if c >= min_c:
        continue
    for ll in range(1 << (Ll - 1)):
        sl, cl = calc(Dl, ll + 1)
        # print('ll', ll, sl, cl)
        if c >= min_c:
            continue
        if s + sl == 0:
            min_c = c + cl

if min_c == 100*41:
    print(-1)
else:
    print(min_c)

Submission status: Runtime Error

Solution code:
from itertools import combinations, permutations
def inpl(): return list(map(int, input().split()))

N, M = inpl()
G = [[0]*(N+1) for _ in range(N+1)]

for _ in range(M):
    a, b = inpl()
    G[a][b] = 1
    G[b][a] = 1

ans = 0
for X in permutations(range(2, N+1)):
    a = 1
    for i in range(N-1):
        b = X[i]
        if G[a][b] == 0:
            break
        a = b
    else:
        ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
n,ma,mb=map(int,input().split())
inf=10**4
A=[]
append=A.append
for i in range(n):
  a,b,c=map(int,input().split())
  append([a,b,c])
DP=[[[inf]*(10*n+1) for _ in range(10*n+1)] for _ in range(n+1)]
DP[0][0][0]=0
for i in range(n):
  a,b,c=A[i][0],A[i][1],A[i][2]
  for j in range(10*(i+1)+1):
    for k in range(10*(i+1)+1):
      if j>=a and k>=b:
        DP[i+1][j][k]=min(DP[i][j-a][k-b]+c,DP[i][j][k])
      else:
        DP[i+1][j][k]=DP[i][j][k]
ans=inf
for i in range(1,10*n+1):
  if i*mb%ma==0:
    j=i*mb//ma
    ans=min(ans,DP[n][i][j])
print(ans if ans!=inf else -1)

Submission status: Runtime Error

Solution code:
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))

Submission status: Runtime Error

Solution code:
N,M=list(map(int,input().split()))

e_list = []
for i in range(M):
    a,b,c = list(map(int,input().split()))
    e_list.append([a-1,b-1,-c])

vi=0
min_dis_list = [10**27 for i in range(N)]
min_dis_list[vi] = 0
prev_list = [-1 for i in range(N)]

for i in range(N-1):
    for e in e_list:
        u,v,d = e
        if min_dis_list[v]>min_dis_list[u]+d:
            min_dis_list[v]=min_dis_list[u]+d
            prev_list[v]=u

neg_loop_flag=False
for e in e_list:
    u,v,d = e
    if min_dis_list[u] + d < min_dis_list[v]:
        neg_loop_flag=True
        break
if neg_loop_flag:
    print("inf")
else:
    print(-min_dis_list[N-1])

Submission status: Runtime Error

Solution code:

N,A,B = list(map(int,input().split()))

item=[]
for i in range(N):
    a,b,c = list(map(int,input().split()))
    item.append([a,b,c])

D = [[[10**27 for i in range(401)] for j in range(401)] for k in range(N+1)]
D[0][0][0] = 0
for i in range(N):
    for j in range(401):
        for k in range(401):
            if D[i][j][k]<10**26:
                D[i+1][j][k]=min(D[i+1][j][k],D[i][j][k])
                D[i+1][j+item[i][0]][k+item[i][1]] = min(D[i+1][j+item[i][0]][k+item[i][1]],D[i][j][k]+item[i][2])

i=1
min_cost=10**27
while A*i<=400 and B*i<=400:
    min_cost = min(D[A*i][B*i],min_cost)
    i+=1
if min_cost==10**27:
	print(-1)
else:
	print(min_cost)

Submission status: Runtime Error

Solution code:

N,A,B = list(map(int,input().split()))

item=[]
for i in range(N):
    a,b,c = list(map(int,input().split()))
    item.append([a,b,c])

D = [[[10**27 for i in range(401)] for j in range(401)] for k in range(N+1)]
D[0][0] = 0
for i in range(N):
    for j in range(401):
        for k in range(401):
            if D[j][k]<10**26:
                D[i+1][j][k]=min(D[i+1][j][k],D[i][j][k])
                D[i+1][j+item[i][0]][k+item[i][1]] = min(D[i+1][j+item[i][0]][k+item[i][1]],D[i][j][k]+item[i][2])

i=1
min_cost=10**27
while A*i<=400 and B*i<=400:
    min_cost = min(D[A*i][B*i],min_cost)
    i+=1
if min_cost==10**27:
	print(-1)
else:
	print(min_cost)

Submission status: Runtime Error

Solution code:
N,A,B = list(map(int,input().split()))

item=[]
for i in range(N):
    a,b,c = list(map(int,input().split()))
    item.append([a,b,c])

D = [[10**27 for i in range(401)] for j in range(401)]
D[0][0] = 0
for i in range(N):
    for j in range(401):
        for k in range(401):
            if D[j][k]<10**27:
                D[j+item[i][0]][k+item[i][1]] = min(D[j+item[i][0]][k+item[i][1]],D[j][k]+item[i][2])

i=1
min_cost=10**27
while A*i<=400 and B*i<=400:
    min_cost = min(D[A*i][B*i],min_cost)
    i+=1
if min_cost==10**27:
	print(-1)
else:
	print(min_cost)

Submission status: Runtime Error

Solution code:
N, Ma, Mb = map(int, input().split())
e = [tuple(map(int, input().split())) for _ in range(N)]

N_max = 40
a_max = 10
b_max = 10
c_max = 100

inf = N_max * c_max + 1
dp = [[[inf for _ in range(N_max * b_max + 1)] for _ in range(N_max * a_max + 1)] for _ in range(N_max + 1)]
dp[0][0][0] = 0

# print(dp[N])

for i, (aa, bb, cc) in enumerate(e, 1):
    for a in range(a_max * i + 1):
        for b in range(b_max * i + 1):
            dp[i][a][b] = min(dp[i][a][b], dp[i - 1][a][b])
            dp[i][a + aa][b + bb] = min(dp[i][a + aa][b + bb], dp[i - 1][a][b] + cc)

# print(dp[N])

ans = inf
for a in range(N_max * a_max + 1):
    for b in range(N_max * b_max + 1):
        # a:b=Ma:Mb
        # b*Ma=a*Mb
        if b * Ma == a * Mb and a * b > 0:
            ans = min(ans, dp[N][a][b])

print(ans if ans < inf else -1)

Submission status: Runtime Error

Solution code:
inf = 10**18

N,Ma,Mb = map(int,input().split())
abc = [list(map(int,input().split())) for _ in range(N)]
inf = 10**9
dp = [[inf for i in range(N*10)] for i in range(N*10)]
dp[0][0] = 0
max_a = 0
max_b = 0
for i in range(N):
    max_a += abc[i][0]
    max_b += abc[i][1]
    for ai in range(abc[i][0],max_a+1)[::-1]:
        for bi in range(abc[i][1],max_b+1)[::-1]:
            dp[ai][bi] = min(dp[ai][bi],dp[ai-abc[i][0]][bi-abc[i][1]]+abc[i][2])
ans = inf
for i in range(1,min(max_a//Ma,max_b//Mb)):
    ans = min(ans,dp[i*Ma][i*Mb])
if ans == inf:
    print(-1)
else:
    print(ans)

Submission status: Runtime Error

Solution code:
N,Ma,Mb = map(int,input().split())
abc = [list(map(int,input().split())) for _ in range(N)]
inf = 10**9
dp = [[inf for i in range(N*10)] for i in range(N*10)]
dp[0][0] = 0
max_a = 0
max_b = 0
for i in range(N):
    max_a += abc[i][0]
    max_b += abc[i][1]
    for ai in range(abc[i][0],max_a+1)[::-1]:
        for bi in range(abc[i][1],max_b+1)[::-1]:
            dp[ai][bi] = min(dp[ai][bi],dp[ai-abc[i][0]][bi-abc[i][1]]+abc[i][2])
ans = inf
for i in range(1,min(max_a//Ma,max_b//Mb)):
    ans = min(ans,dp[i*Ma][i*Mb])
if ans == inf:
    print(-1)
else:
    print(ans)

Submission status: Runtime Error

Solution code:
#D問題
NM=40
ABM=10
MM=400

N,M1,M2=map(int,input().split())
A=[]
B=[]
C=[]
for i in range(N):
    a,b,c=map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(c)
cmax=max(C)
CM=N*max(C)+1
dp=[[[CM for broop in range(401)] for aroop in range(401)] for nroop in range(NM)]
dp[0][0][0]=0
for i in range(N-1):
    for cb in range(401):
        for ca in range(401):
            if dp[i][ca][cb]==CM:
                continue
            dp[i+1][ca][cb]=min(dp[i+1][ca][cb],dp[i][ca][cb])
            if ca+A[i]<401 and cb+B[i]<401: 
                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=CM
for cb in range(400):
    CB=cb+1
    for ca in range(400):
        CA=ca+1
        if ca*M2 == cb*M1:
            ans=min(ans,dp[N][CA][CB])

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

Submission status: Runtime Error

Solution code:
#D問題
NM=40
ABM=10
MM=400

N,M1,M2=map(int,input().split())
A=[]
B=[]
C=[]
for i in range(N):
    a,b,c=map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(c)
cmax=max(C)
CM=N*cmax
dp=[[[CM for broop in range(401)] for aroop in range(401)] for nroop in range(NM)]
dp[0][0][0]=0
for i in range(N):
    for cb in range(401):
        for ca in range(401):
            if dp[i][ca][cb]==CM:
                continue
            dp[i+1][ca][cb]=min(dp[i+1][ca][cb],dp[i][ca][cb])
            if ca+A[i]<400 and cb+B[i]<400: 
                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=CM
for cb in range(400):
    CB=cb+1
    for ca in range(400):
        CA=ca+1
        if ca*M2 == cb*M1:
            ans=min(ans,dp[N][CA][CB])

if ans == N*cmax:
    print(-1)
else:
    print(ans)

Submission status: Runtime Error

Solution code:
#D問題
NM=40
ABM=10
MM=400

N,M1,M2=map(int,input().split())
A=[]
B=[]
C=[]
for i in range(N):
    a,b,c=map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(c)
cmax=max(C)
CM=N*cmax
dp=[[[CM for broop in range(401)] for aroop in range(401)] for nroop in range(NM)]
dp[0][0][0]=0
for i in range(N):
    for cb in range(401):
        for ca in range(401):
            if dp[i][ca][cb]==CM:
                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=CM
for cb in range(400):
    CB=cb+1
    for ca in range(400):
        CA=ca+1
        if ca*M2 == cb*M1:
            ans=min(ans,dp[N][CA][CB])

if ans == N*cmax:
    print(-1)
else:
    print(ans)

Submission status: Runtime Error

Solution code:
def mixing_experiment(N: int, Ma: int, Mb: int, drags: list)->int:
    INF = float('inf')
    max_A = N * max(a for a, _, _ in drags)
    max_B = N * max(b for _, b, _ in drags)
    dp = [[INF] * (max_A+1) for _ in range(max_B+1)]
    dp[0][0] = 0

    for (a, b, c) in drags:
        for ca in range(max_A-a, -1, -1):
            for cb in range(max_B-b, -1, -1):
                dp[ca+a][cb+b] = \
                    min(dp[ca+a][cb+b], dp[ca][cb] + c)
        dp[a][b] = min(dp[a][b], c)

    ans = min(
        dp[ca][cb]
        for ca in range(1, max_A+1)
        for cb in range(1, max_B+1)
        if ca * Mb == cb * Ma)
    # print(dp)

    return ans if 0 < ans and ans < INF else -1


if __name__ == "__main__":
    N = 0
    N, Ma, Mb = map(int, input().split())
    drags = [tuple(int(s) for s in input().split()) for _ in range(N)]
    ans = mixing_experiment(N, Ma, Mb, drags)
    print(ans)

Submission status: Runtime Error

Solution code:
def mixing_experiment(N: int, Ma: int, Mb: int, drags: list)->int:
    INF = float('inf')
    max_A = N * max(a for a, _, _ in drags)
    max_B = N * max(b for _, b, _ in drags)
    dp = [
        [[INF] * (max_A+1) for _ in range(max_B+1)]
        for _ in range(N+1)
    ]
    dp[0][0][0] = 0

    for n, (a, b, c) in enumerate(drags):
        for ca in range(max_A+1):
            for cb in range(max_B+1):
                dp[n+1][ca][cb] = min(dp[n+1][ca][cb], dp[n][ca][cb])

                if max_A < ca + a or max_B < cb + b:
                    continue

                dp[n+1][ca+a][cb+b] = \
                    min(dp[n+1][ca+a][cb+b], dp[n][ca][cb] + c)

    ans = min(
        dp[N][ca][cb]
        for ca in range(max_A+1)
        for cb in range(max_B+1)
        if ca > 0 and cb > 0 and ca * Mb == cb * Ma)
    # print(dp)

    return ans if 0 < ans and ans < INF else -1


if __name__ == "__main__":
    N = 0
    N, Ma, Mb = map(int, input().split())
    drags = [tuple(int(s) for s in input().split()) for _ in range(N)]
    ans = mixing_experiment(N, Ma, Mb, drags)
    print(ans)

Submission status: Runtime Error

Solution code:
def mixing_experiment(N: int, Ma: int, Mb: int, drags: list) -> int:
    INF = float('inf')
    max_A = max(a for a, _, _ in drags) * N
    max_B = max(b for _, b, _ in drags) * N
    dp = [
        [[INF]*(max_A + 1) for _ in range(max_B + 1)]
        for _ in range(N + 1)
    ]

    dp[0][0][0] = 0
    for n in range(N):
        a, b, c = drags[n]
        for ca in range(max_A + 1):
            for cb in range(max_B + 1):
                # 薬品 n を加えない
                dp[n + 1][ca][cb] = min(dp[n + 1][ca][cb], dp[n][ca][cb])

                if max_A < ca + a or max_B < cb + b:
                    continue

                # 薬品 n を加える
                dp[n + 1][ca + a][cb + b] = \
                    min(dp[n + 1][ca + a][cb + b], dp[n][ca][cb] + c)

    ans = min(
        dp[N][ca][cb]
        for ca in range(max_A + 1)
        for cb in range(max_B + 1)
        if ca * Mb == cb * Ma and ca != 0)
    return ans if 0 < ans and ans < INF else -1


if __name__ == "__main__":
    N = 0
    N, Ma, Mb = map(int, input().split())
    drags = [tuple(int(s) for s in input().split()) for _ in range(N)]
    ans = mixing_experiment(N, Ma, Mb, drags)
    print(ans)

Submission status: Runtime Error

Solution code:
mod=1777777777
n,ma,mb=map(int,input().split())
d=[[mod for j in range(100)] for i in range(100)]
for i in range(n):
    a,b,c=map(int,input().split())
    x=a
    y=b
    for j in range(100-a,0,-1):
        for k in range(100-b,0,-1):
            if d[j][k]!=mod:
                d[j+a][k+b]=min(d[j+a][k+b],d[j][k]+c)
    d[x][y]=min(d[x][y],c)
ans=mod
for i in range(100):
    if i%ma==0 and mb*i//ma<100:
        ans=min(ans,d[i][mb*i//ma])

print([ans,-1][ans==mod])

Submission status: Runtime Error

Solution code:
mod=1777777777
n,ma,mb=map(int,input().split())
d=[[mod for j in range(100)] for i in range(100)]
for i in range(n):
    a,b,c=map(int,input().split())
    x=a
    y=b
    for j in range(100-a,0,-1):
        for k in range(100-b-1,0,-1):
            if d[j][k]!=mod:
                d[j+a][k+b]=min(d[j+a][k+b],d[j][k]+c)
    d[x][y]=min(d[x][y],c)
ans=mod
for i in range(100):
    if i%ma==0 and mb*i//ma<100:
        ans=min(ans,d[i][mb*i//ma])

print([ans,-1][ans==mod])

Submission status: Runtime Error

Solution code:
mod=1777777777
n,ma,mb=map(int,input().split())
d=[[mod for j in range(100)] for i in range(100)]
for i in range(n):
    a,b,c=map(int,input().split())
    x=a
    y=b
    d[x][y]=min(d[x][y],c)
    for j in range(100-a):
        for k in range(100-b):
            if d[j][k]!=mod:
                d[j+a][k+b]=min(d[j+a][k+b],d[j][k]+c)
ans=mod
for i in range(100):
    if i%a==0:
        ans=min(ans,d[i][b*i//a])

print([ans,-1][ans==mod])

Submission status: Runtime Error

Solution code:
import math


N,Ma,Mb=map(int,input().split())
cost=list()
for i in range(N):
    a,b,c=map(int,input().split())
    cost.append([a,b,c])

dp=[[float("inf") for _ in range(11)]for _ in range(11)]
dp[0][0]=0

from collections import deque

for i in range(N):
    c=cost[i]
    for j in range(0,11):
        for z in range(0,11):
            a=j+c[0]
            b=z+c[1]
            k=math.gcd(a,b)
            a=a//k
            b=b//k
            if(a>10 or b>10):
                continue
            dp[a][b]=min(dp[a][b],dp[j][z]+c[2])


ans=10**10
for i in range(1,100):
    a=i*Ma
    b=i*Mb
    if(a>10 or b>10):
        break
    ans=min(ans,dp[a][b])
if(ans==10**10):
    print(-1)

else:
    print(ans)

Submission status: Runtime Error

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

#A

#B

#C
"""
n,m = LI()
v = LIR(m)
p2 = [1]
for i in range(n):
    p2.append(p2[-1]*2)
dp = [[0 for i in range(p2[n])] for j in range(n)]
dp[0][1] = 1
for i in range(1,p2[n]):
    for k in range(n):
        for a,b in v:
            a -= 1
            b -= 1
            if k==a or k == b:
                c = p2[a]
                d = p2[b]
                if i&c and not i&d:
                    dp[b][i|d] += dp[k][i]
                if (not i&c) and i&d:
                    dp[a][i|c] += dp[k][i]
ans = 0
for i in range(1,n):
    ans += dp[i][-1]
print(ans)
"""

#D
n,p,q = LI()
v = [None for j in range(n)]
sa = 0
sb = 0
for i in range(n):
    a,b,c = LI()
    v[i] = [a,b,c]
    sa += a
    sb += b
dp = [[[[float("inf") for i in range(sa+1)] for j in range(sb+1)] for k in range(n+1)] for l in range(n+1)]
dp[0][0][0][0] = 0
for i in range(n):
    for j in range(n):
        for a in range(sa):
            for b in range(sb):
                ai,bi,ci = v[i]
                if a + ai <= sa and b+bi <= sb:
                    dp[i+1][j+1][a+ai][b+bi] = min(dp[i+1][j+1][a+ai][b+bi],dp[i][j][a][b]+ci,dp[i][j+1][a+ai][b+bi])
li = [[p,q]]
while li[-1][0] <= sa and li[-1][1] <= sb:
    li.append([li[-1][0]+p,li[-1][1]+q])
li.pop(-1)
ans = float("inf")
for i in range(1,n+1):
    for a,b in li:
        ans = min(ans,dp[n][i][a][b])
if ans == float("inf"):
    print(-1)
else:
    print(ans)
#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T

Submission status: Runtime Error

Solution code:
N,MA,MB=map(int,input().split())
S=[list(map(int,input().split())) for i in range(N)]

DPLIST=[[10**6 for i in range(401)] for j in range(401)]
DPLIST[0][0]=0

for a,b,c in S:
    for i in range(400,a-1,-1):
        for j in range(400,b-1,-1):
            if DPLIST[i][j]>DPLIST[i-a][j-b]+c:
                DPLIST[i][j]=DPLIST[i-a][j-b]+c
                
ANS=10**6
for k in range(1,400//max(i,j)):
    if DPLIST[MA*k][MB*k]<ANS:
        ANS=DPLIST[MA*k][MB*k]

if ANS==10**6:
    print(-1)
else:
    print(ANS)

Submission status: Runtime Error

Solution code:
n,Ma,Mb=map(int,input().split())
inf=float("inf")
dp=[[[inf]*401 for _ in range(401)] for _ in range(n+1)]
dp[0][0][0]=0

l=[tuple(map(int,input().split())) for _ in range(n)]

for i in range(1,n+1):
    for j in range(401):
        for k in range(401):
            a,b,c=l[i-1]
            dp[i][j][k]=min(dp[i][j][k],dp[i-1][j][k])
            if j-a>=0 and k-b>=0:
                dp[i][j][k]=min(dp[i][j][k],dp[i-1][j-a][k-b]+c)

ans=inf
for i in range(1,1+max(400//Ma,400//Mb)):
    ans=min(ans,dp[n][Ma*i][Mb*i])

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