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: Accepted

Solution code:
N, Ma, Mb=map(int,input().strip().split(' '))
inf=10**8
a=[]
b=[]
c=[]
for i in range(N):
    at,bt,ct=map(int,input().strip().split(' '))
    a.append(at)
    b.append(bt)
    c.append(ct)
suma=sum(a)
sumb=sum(b)
dp=[[[inf]*(sumb+1) for i in range(suma+1)]for j in range(N+1)]
dp[0][0][0]=0
for i in range(N):
    for j in range(suma+1):
        for k in range(sumb+1):
            if a[i]<=j and b[i]<=k:
                dp[i+1][j][k]=min(dp[i][j][k],dp[i][j-a[i]][k-b[i]]+c[i])
            else:
                dp[i+1][j][k]=dp[i][j][k]
            
m=min(suma//Ma,sumb//Mb)
ans=-1
for i in range(1,m):
    #if dp[N][i*Ma][i*Mb]!=inf and dp[N][i*Ma][i*Mb]!=0:
    if dp[N][i*Ma][i*Mb]!=inf:
        if ans==-1:
            ans=dp[N][i*Ma][i*Mb]
        else:
            ans=min(ans,dp[N][i*Ma][i*Mb])
        #break
print(ans)

Submission status: Accepted

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

mix=set([(0,0)])
ans=10**9
cost={(0,0):0}
for i in range(N):
    a,b,c=list(map(int,input().split()))
    tmp=mix.copy()
    tmp2=cost.copy()
    for x in tmp:
        d=x[0]+a
        e=x[1]+b
        f=min(tmp2.get((d,e),10**9),tmp2[x]+c)
        cost[(d,e)]=f
        mix.add((d, e))
        if d*Mb==e*Ma and f<ans:
            ans=f
"""
print(cost)
print(mix)
"""
if ans==10**9:
    ans=-1
print(ans)

Submission status: Accepted

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

inf = float('inf')

dp = [[inf for i in range(n*10)] for i in range(n*10)]

for i in range(n):
    for j in range(i*10-1,-1,-1):
        for k in range(i*10-1,-1,-1):
            if dp[j][k]!=inf:
                dp[j + abc[i][0]][k + abc[i][1]] =min(dp[j+abc[i][0]][k+abc[i][1]],dp[j][k]+abc[i][2])
    dp[abc[i][0] - 1][abc[i][1] - 1]= min(dp[abc[i][0] - 1][abc[i][1] - 1], abc[i][2])

ans = inf

for j in range(n*10):
    for k in range(n*10):
        if ma*(k+1)==mb*(j+1):
            ans = min(ans,dp[j][k])

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

Submission status: Accepted

Solution code:
def inpl(): return [int(i) for i in input().split()]

N,Ma,Mb = inpl()
inf = float('inf')
H = {(0,0): 0}
for i in range(N):
    a, b, c = inpl()
    for (ia, ib), ic in H.copy().items():
        H[(ia+a,ib+b)] = min(H.get((ia+a, ib+b), inf),ic+c)

ans = min(H.get((i*Ma, i*Mb), inf) for i in range(1,401))
print(-1 if ans == inf else ans)

Submission status: Accepted

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())
A = [0]*N
for i in range(N):
    A[i] = (Mb*a[i]-Ma*b[i],c[i])

A0 = [A[i][1] for i in range(N) if A[i][0] == 0]
A1 = [A[i] for i in range(N) if A[i][0] > 0]
A2 = [A[i] for i in range(N) if A[i][0] < 0]

def DP(A):
    dp = {0:0}
    for i in range(len(A)):
        update = {}
        for j in dp.keys():
            if A[i][0]+j not in dp:
                update[A[i][0]+j] = A[i][1]+dp[j]
            else:
                update[A[i][0]+j] = min(dp[A[i][0]+j],A[i][1]+dp[j])
        for j in update.keys():
            dp[j] = update[j]
    return dp

dA1 = DP(A1)
dA2 = DP(A2)
ans = float('inf')

for x in dA1:
    if x != 0 and -x in dA2:
        ans = min(dA1[x]+dA2[-x],ans)
for x in A0:
    ans = min(x,ans)

if ans == float('inf'):
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
n,ma,mb=map(int,input().split())
abc=[list(map(int,input().split())) for _ in range(n)]
a=[t[0] for t in abc]
b=[t[1] for t in abc]
c=[t[2] for t in abc]
mab=max(max(a),max(b))
dp=[[[1<<32 for _ in range(n*mab+1)] for _ in range(n*mab+1)] for _ in range(n+1)]
dp[0][0][0]=0
inf=1<<32
for i in range(n):
    for ca in range(n*mab):
        for cb in range(n*mab):
            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,n*mab):
    for cb in range(1,n*mab):
        if ca*mb==cb*ma:
            ans=min(ans,dp[n][ca][cb])
if ans==inf:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
N, Ma, Mb = map(int, input().split())
r = Ma/Mb
a = 0
b = 0
costlist = [500000]
data = [list(map(int, input().split())) for i in range(N)]
for i in range(N):
    a += data[i][0]
    b += data[i][1]


dp = [[[500000 for i in range(N+1)] for i in range(b+10)] for i in range(a+10)]
dp[0][0][0] = 0

#Aがiグラム、Bがjグラム、k番目までの薬を用いたときの最小コスト
for k in range(N):
    for j in range(b):
        for i in range(a):
            ai = data[k][0]
            bj = data[k][1]
            dp[i+ai][j+bj][k+1] = min(dp[i+ai][j+bj][k+1], dp[i][j][k] + data[k][2])
            dp[i][j][k+1] = min(dp[i][j][k], dp[i][j][k+1])

for i in range(1, a):
    for j in range(1, b):
        if i/j == r:
            costlist.append(dp[i][j][N])
if N == 1:
    if data[0][0]/data[0][1] == r:
        print(data[0][2])
    else:
        print(-1)

elif min(costlist) == 500000:
    print(-1)
else:
    print(min(costlist))

Submission status: Accepted

Solution code:
INF = float('inf')

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

sumA, sumB, sumC = map(sum, zip(*chemicals))

#dp[i][j]: Aをiグラム、Bをjグラム作るのに必要なコストの最小値
dp = [[INF] * (sumB + 1) for _ in range(sumA + 1)]
dp[0][0] = 0
for a, b, c in chemicals:
    for i in reversed(range(sumA + 1)):
        for j in reversed(range(sumB + 1)):
            if dp[i][j] == INF: continue
            if i + a <= sumA and j + b <= sumB:
                dp[i + a][j + b] = min(dp[i + a][j + b], dp[i][j] + c)

ans = INF
for i in range(1, min(sumA // Ma, sumB // Mb) + 1):
    ans = min(ans, dp[Ma * i][Mb * i])

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

Submission status: Accepted

Solution code:
N,ma,mb = map(int,raw_input().split())

a=[]
b=[]
c=[]
for i in range(N):
	at,bt,ct=map(int,raw_input().split())

	a.append(at)
	b.append(bt)
	c.append(ct)
	

dp = [[[10**10 for i in range(400+1)] for j in range(400+1)] for k 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]<10**10):
				dp[i+1][j][k]=min(dp[i+1][j][k],dp[i][j][k])
				dp[i+1][j+a[i]][k+b[i]]=min(dp[i+1][j+a[i]][k+b[i]],dp[i][j][k]+c[i])

ans=10**10

for i in range(1,401):
	for j in range(1,401):
		if i*mb==j*ma:
			ans=min(ans,dp[N][i][j])

if ans>=10**10:
	print -1
else:
	print ans

Submission status: Accepted

Solution code:
# ABC 054 D
N, Ma, Mb = map(int, input().split())
P = [list(map(int, input().split())) for k in range(N)]
INF = 10000
dp = [[[INF for m in range(401)] for k in range(401)] for l in range(N+1)]
dp[0][0][0] = 0

for k in range(N):
    a = P[k][0]
    b = P[k][1]
    c = P[k][2]
    for l in range(401):
        for m in range(401):
            if dp[k][l][m] != INF:
                dp[k+1][l][m] = min(dp[k+1][l][m],dp[k][l][m])
                dp[k+1][l+a][m+b] = min(dp[k+1][l+a][m+b], dp[k][l][m]+c) 

ans = INF
for l in range(1,401):
    for m in range(1,401):
        if Ma*m == Mb*l:
            ans = min(ans,dp[N][l][m])

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

Submission status: Accepted

Solution code:
N, Ma, Mb = map(int, input().split())
INF = 10 ** 9
# dp[a][b] = min_cost
dp = {(0, 0): 0}

for _ in range(N):
    a, b, c = map(int, input().split())
    for (pa, pb), pc in dp.copy().items():
        dp[pa + a, pb + b] = min(dp.get((pa + a, pb + b), INF), pc + c)

ans = min(dp.get((i * Ma, i * Mb), INF) for i in range(1, 401))
print(ans if ans != INF else -1)

Submission status: Accepted

Solution code:
N, Ma, Mb = map(int, input().split())
D = []
atotal, btotal = 0, 0
for i in range(N):
    a, b, c = map(int, input().split())
    D.append((a, b, c))
    atotal += a
    btotal += b
Inf = 10**4

def DP(i, a, b, memo = {}):
    if i == 0 and a == b == 0:
        return 0
    elif i == 0:
        return Inf
    try:
        return memo[(i, a, b)]
    except KeyError:
        if a - D[i-1][0] >= 0 and b - D[i-1][1] >= 0:
            ret = min(DP(i-1, a, b, memo), DP(i-1, a-D[i-1][0], b-D[i-1][1], memo) + D[i-1][2])
        else:
            ret = DP(i-1, a, b, memo)
        memo[(i, a, b)] = ret
        return ret

memo = {}
ans = Inf
for a in range(1, atotal+1):
    for b in range(1, btotal+1):
        if a*Mb == b*Ma:
            ans = min(ans, DP(N, a, b, memo))

print(ans if ans < Inf else -1)

Submission status: Accepted

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

INF = float("inf")
ma = 401
dp = [[[INF] * ma for j in range(ma)] for i in range(N + 1)]

dp[0][0][0] = 0
for i in range(N):
    for j in range(ma):
        for k in range(ma):
            if dp[i][j][k] == INF:
                continue
            dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k])
            dp[i + 1][j + rec[i][0]][k + rec[i][1]] = min(dp[i + 1][j + rec[i][0]][k + rec[i][1]], dp[i][j][k] + rec[i][2])

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

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

Submission status: Accepted

Solution code:
N,Ma,Mb = map(int,input().split())
first_half = []
last_half = []
for i in range(N):
	a,b,c = map(int,input().split())
	if i < N // 2:
		first_half.append((a,b,c))
	else:
		last_half.append((a,b,c))

first_half_combination = {}
last_half_combination = {}
def make_all_combination(i,a,b,c,d_list,flag):
	if i == len(d_list):
		if c == 0:
			return

		w = Ma * b - Mb * a
		if flag:
			if w not in first_half_combination or c < first_half_combination[w]:
				first_half_combination[w] = c
		else:
			if -w not in last_half_combination or c < last_half_combination[-w]:
				last_half_combination[-w] = c
		return
	make_all_combination(i+1,a,b,c,d_list,flag)
	make_all_combination(i+1,a+d_list[i][0],b+d_list[i][1],c+d_list[i][2],d_list,flag)

make_all_combination(0,0,0,0,first_half,True)
make_all_combination(0,0,0,0,last_half,False)

INF = 10**8
f_0 = first_half_combination[0] if 0 in first_half_combination else INF
l_0 = last_half_combination[0] if 0 in last_half_combination else INF
ans = min(f_0,l_0) if min(f_0,l_0) < INF else -1
for w in first_half_combination.keys():
	if w == 0:
		continue
	if w in last_half_combination:
		cost = first_half_combination[w] + last_half_combination[w]
		if w == 0:
			cost = min(first_half_combination[w], last_half_combination[w])
		if ans == -1 or cost < ans:
			ans = cost
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def main():
    N, Ma, Mb = map(int, input().split())
    items = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        items.append((a, b, c))

    dp = [[INF] * 401 for _ in range(401)]
    dp[0][0] = 0
    for i in range(N):
        a, b, c = items[i]
        for y in range(len(dp) - 1, -1, -1):
            for x in range(len(dp[0]) - 1, -1, -1):
                if dp[y][x] != INF:
                    dp[y + a][x + b] = min(dp[y + a][x + b], dp[y][x] + c)

    ans = INF
    for y in range(1, len(dp)):
        for x in range(1, len(dp[0])):
            if y * Mb == x * Ma:
                ans = min(ans, dp[y][x])

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
inf = 10**10
n,ma,mb = map(int, input().split())

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

memo = {}
def rec(i, maj, mbj):
    k = str(i)+"-"+str(maj)+"-"+str(mbj)
    if k in memo:
        return memo[k]
    # print(i, ":", maj, mbj, maj/ma, mbj/mb)
    ret = 0
    if i==n:
        if maj>0 and mbj>0 and maj/ma == mbj/mb:
            ret = 0
        else:
            ret = inf
    elif maj>0 and mbj>0 and maj/ma == mbj/mb:
        ret = 0
    else:
        r1 = rec(i+1, maj, mbj) # i番目を使わない場合
        # i番目を使う場合
        r2 = rec(i+1, maj+a[i], mbj+b[i]) + c[i]
        if r1>0 and r2==0:
            ret = r1
        elif r1==0 and r2>0:
            ret = r2
        else:
            ret = min(r1,r2)
    memo[k] = ret
    return ret

ret = rec(0, 0, 0)
if ret == inf:
    print(-1)
else:
    print(ret)

Submission status: Accepted

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

INF = 10**9
MAX = 401
dp = [[INF] * MAX for i in range(MAX)]
dp[0][0] = 0
amax = bmax = 0
for a,b,n in src:
    for i in reversed(range(bmax+1)):
        for j in range(amax+1):
            if dp[i][j] == INF: continue
            dp[i+b][j+a] = min(dp[i+b][j+a], dp[i][j] + n)
    amax += a
    bmax += b

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

Submission status: Accepted

Solution code:
nmax,abmax=40,10
n,ma,mb=map(int,input().split())
a,b,c=[None]*n,[None]*n,[None]*n
inf=float("inf")
for i in range(n):
  a[i],b[i],c[i]=map(int, input().split())
dp = [[[inf for _ in range(nmax*abmax+1)] for _ in range(nmax*abmax+1)] for _ in range(nmax+1)]
dp[0][0][0]=0
for i in range(n):
  for ca in range(nmax*abmax+1):
    for cb in range(nmax*abmax+1):
      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,nmax*abmax+1):
  for cb in range(1,nmax*abmax+1):
    if ca*mb==cb*ma:ans=min(ans,dp[n][ca][cb])
if ans==inf:
  print(-1)
else:
  print(ans)

Submission status: Accepted

Solution code:
N,Ma,Mb=map(int,input().split())
a=[0 for i in range(N)]
b=[0 for i in range(N)]
c=[0 for i in range(N)]
M=401
dp=[[[10**4 for cb in range(M)]for ca in range(M)]for i in range(N+1)]
for i in range(N):
    a[i],b[i],c[i]=map(int,input().split())
dp[0][0][0]=0
for i in range(N):
    for ca in range(M):
        for cb in range(M):
            if dp[i][ca][cb]==10**4:
                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=10**4
for i in range(1,M):
    for j in range(1,M):
        if i*Mb==j*Ma:
            if dp[N][i][j]<ans:
                ans=dp[N][i][j]
if ans!=10**4:
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_s(): return list(input().split())

N,Ma,Mb = inpl()
dp = [[INF for mb in range(410)] for ma in range(410)]

#dp[ma][mb] = i番目までの薬品でma,mbを作る最小コスト

dp[0][0] = 0

for i in range(N):
	a,b,c = inpl()
	for mb in reversed(range(410)):
		for ma in reversed(range(410)):
			if dp[ma][mb] != INF:
				dp[ma+a][mb+b] = min(dp[ma][mb]+c,dp[ma+a][mb+b])

ma = Ma
mb = Mb
k = 1
ans = INF
while ma < 410 and mb < 410:
	ans = min(dp[ma][mb],ans)
	k += 1
	ma += Ma
	mb += Mb

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

Submission status: Accepted

Solution code:
n,ma,mb = map(int,input().split())
x = []
amax = 0
bmax = 0
for i in range(n):
    x1,y1,z1=[int(i) for i in input().split()]
    x.append([x1,y1,z1])
    amax += x1
    bmax += y1
inf = float("inf")
dp = [[inf] * (amax+1) for i in range(bmax+1)]
dp[0][0] = 0
aru = {(0,0)}
for i in x:
  a = i[0]
  b = i[1]
  c = i[2]
  aru2 = list(aru)
  aru2 = sorted(aru2,key=lambda x:-x[0])
  for j in aru2:
    dp[j[1]+b][j[0]+a] = min(dp[j[1]+b][j[0]+a],dp[j[1]][j[0]]+c)
    aru.add((j[0]+a,j[1]+b))
ans = inf
mam = ma+0
mbm = mb + 0
while (amax >= ma and bmax >= mb):
  suu = dp[mb][ma]
  ans = min(ans,suu)
  mb += mbm
  ma += mam
if ans == inf:
  print(-1)
else:
  print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, MA, MB, ABC):
    dp = defaultdict(lambda: defaultdict(
        lambda: defaultdict(lambda: sys.maxsize)))
    AB_MAX = N*10
    dp[0][0][0] = 0
    for i, (a, b, c) in enumerate(ABC):
        for j in range(AB_MAX):
            for k in range(AB_MAX):
                if not (i in dp and j in dp[i] and k in dp[i][j]):
                    continue
                dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k])
                dp[i+1][j+a][k+b] = min(dp[i+1][j+a][k+b], dp[i][j][k] + c)

    ans = sys.maxsize
    for a in dp[N]:
        if a == 0:
            continue
        for b in dp[N][a]:
            if b == 0:
                continue
            if a*MB == b*MA:
                ans = min(ans, dp[N][a][b])

    return ans if ans != sys.maxsize else -1


def main():
    N, MA, MB = read_int_n()
    ABC = [read_int_n() for _ in range(N)]

    print(slv(N, MA, MB, ABC))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

N,Ma,Mb = map(int, sdin().split())
ABC = []
for i in range(N):
    ABC.append(tuple(map(int, sdin().split())))


sum_a = [0]
sum_b = [0]
for a,b,c in ABC:
    sum_a.append(sum_a[-1]+a)
    sum_b.append(sum_b[-1]+b)
    
INF = float("inf")
MAX_W = 400
dp = [[[INF for _ in range(sum_b[-1]+1)] for _ in range(sum_a[-1]+1)] for _ in range(N+1)]
for i in range(N+1):
    dp[i][0][0] = 0

for i, (ai, bi, ci) in enumerate(ABC):
    for a in range(sum_a[i+1]+1):
        for b in range(sum_b[i+1]+1):
            if a-ai>=0 and b-bi>=0:
                dp[i+1][a][b] = min(dp[i][a][b], dp[i][a-ai][b-bi]+ci)
            else:
                dp[i+1][a][b] = dp[i][a][b]
        
k = 1           
ans = INF
while (k*Ma<=sum_a[-1] and k*Mb<=sum_b[-1]):
    ans = min(ans, dp[N][k*Ma][k*Mb])
    k += 1
    
if ans == INF:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

n, ma, mb = map(int, input().split())
dp = defaultdict(lambda: float('inf'))
dp[0, 0] = 0
sa = 0
for _ in range(n):
    a, b, c = map(int, input().split())
    for (i, j), d in dp.copy().items():
        k = (i + a, j + b)
        dp[k] = min(dp[k], d + c)
    sa += a

ans = float('inf')
i = 1
while ma * i <= sa:
    ta = ma * i
    tb = mb * i
    i += 1
    ans = min(ans, dp[ta, tb])

print(-1 if ans == float('inf') else ans)

Submission status: Accepted

Solution code:
ABMAX = 10
L = 10**6

n, ma, mb = list(map(int, input().split()))
a, b, c = [0]*n, [0]*n, [0]*n
for i in range(n):
    a[i], b[i], c[i] = list(map(int, input().split()))

dp = [[[L for cb in range(n*ABMAX+1)] for ca in range(n*ABMAX+1)] for i in range(n+1)]
dp[0][0][0] = 0

for i in range(n):
    for ca in range(n*ABMAX+1):
        for cb in range(n*ABMAX+1):
            if dp[i][ca][cb] == L:
                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 = L
for ca in range(1, n*ABMAX):
    for cb in range(1, n*ABMAX):
        if ca*mb == cb*ma:
            ans = min(ans, dp[n][ca][cb])

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import copy
 
N, Ma, Mb = map(int, input().split())
a = []
b = []
c = []
for i in range(N):
    ai, bi, ci = map(int, input().split())
    a.append(ai)
    b.append(bi)
    c.append(ci)
 
def update(mincosti, a_b, c):
    if a_b not in mincosti:
        mincosti[a_b] = c
        return
    if mincosti[a_b] > c:
        mincosti[a_b] = c
        return
 
mincost = [{(a[0],b[0]): c[0]}]
 
for i in range(1,N):
    mincost.append(copy.deepcopy(mincost[i-1]))
    update(mincost[i], (a[i],b[i]), c[i])
    for a_, b_ in mincost[i-1].keys():
        a_b = (a_+a[i], b_+b[i])
        update(mincost[i], a_b, mincost[i-1][(a_,b_)]+c[i])
 
Na = Ma
Nb = Mb
 
ans = None
 
while (Na <= 400) and (Nb <= 400):
    if (Na, Nb) in mincost[N-1].keys():
        if ans == None:
            ans = mincost[N-1][(Na, Nb)]
        else:
            ans = min(ans, mincost[N-1][(Na, Nb)])
    Na += Ma
    Nb += Mb
 
if ans == None:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
N, Ma, Mb = map(int, input().split())
L = [[float("inf")]*410 for i in range(410)]
P = [[0, 0]]
L[0][0] = 0

for i in range(N):
    a, b, c = map(int, input().split())
    OldP = P.copy()
    Lsub = [L[p[0]][p[1]] for p in OldP]
    for xp, p in enumerate(OldP):
        if L[p[0]+a][p[1]+b] == float("inf"):
            P.append([p[0]+a, p[1]+b])
        L[p[0]+a][p[1]+b] = min(L[p[0]+a][p[1]+b], Lsub[xp]+c)
g = [Ma, Mb]
ans = float("inf")
while g[0]<410 and g[1]<410:
    ans = min(ans, L[g[0]][g[1]])
    g[0] += Ma
    g[1] += Mb
print(ans if ans != float("inf") else -1)

Submission status: Accepted

Solution code:
N,M,K=map(int,input().split())
table=[]
for i in range(N):
    a,b,c=map(int,input().split())
    table.append([a,b,c])

inf=10**9
#[N+1][400a][400b]
dp=[[[inf]*401 for i in range(401)]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 j in range(401):
        for k in range(401):
            dp[i][j][k]=dp[i-1][j][k]
            if 400>=j-a>=0 and 400>=k-b>=0:
                dp[i][j][k] =min(dp[i-1][j][k],dp[i-1][j-a][k-b]+c)

ans =10**9
i=1
while max(M,K)*i <401:
    ans = min(ans,dp[N][M*i][K*i])
    i=i+1
if ans ==10**9:
    print(-1)
else:
    print(ans)

Submission status: Accepted

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

INF = 10**9
dp = [[INF for j in range(401)] for i in range(401)]
dp[0][0] = 0
maxa = maxb = 0
for a,b,c in src:
    for ai in range(maxa,-1,-1):
        for bi in range(maxb,-1,-1):
            dp[ai+a][bi+b] = min(dp[ai+a][bi+b], dp[ai][bi] + c)
    maxa += a
    maxb += b

ans = INF
for i in range(1,401):
    if MA*i > 400 or MB*i > 400: break
    ans = min(ans, dp[MA*i][MB*i])
print(-1 if ans == INF else ans)

Submission status: Accepted

Solution code:
n, ma, mb = map(int, input().split())
INF = float("inf")
dp = [[[INF for i in range(10*n+1)] for i in range(10*n+1)] for i in range(n+1)]
dp[0][0][0],ans = 0,INF
for i in range(1,n+1):
    a,b,c = (int(l) for l in input().split())
    for j in range(10*(i-1)+1):
        for k in range(10*(i-1)+1):
            if dp[i-1][j][k] != INF:
              dp[i][j][k] = min(dp[i][j][k], dp[i-1][j][k])
              dp[i][j+a][k+b] = min(dp[i][j+a][k+b], dp[i-1][j][k]+c)
for i in range(ma,10*n+1,ma):
	if i*mb//ma<=10*n: ans=min(ans,dp[n][i][i*mb//ma])
if ans==INF: print(-1)
else: print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import copy

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

def update(mincosti, a_b, c):
    if a_b not in mincosti:
        mincosti[a_b] = c
        return
    if mincosti[a_b] > c:
        mincosti[a_b] = c
        return

mincost = [{(a[0],b[0]): c[0]}]

for i in range(1,N):
    mincost.append(copy.deepcopy(mincost[i-1]))
    update(mincost[i], (a[i],b[i]), c[i])
    for a_, b_ in mincost[i-1].keys():
        a_b = (a_+a[i], b_+b[i])
        update(mincost[i], a_b, mincost[i-1][(a_,b_)]+c[i])

Na = Ma
Nb = Mb

ans = None

while (Na <= 400) and (Nb <= 400):
    if (Na, Nb) in mincost[N-1].keys():
        if ans == None:
            ans = mincost[N-1][(Na, Nb)]
        else:
            ans = min(ans, mincost[N-1][(Na, Nb)])
    Na += Ma
    Nb += Mb

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return map(int, input().split())

N, Ma, Mb = inpl()
INF = 4001

DP = [[INF]*(411) for _ in range(411)]
DP[0][0] = 0

for n in range(N):
    DP2 = [[INF]*(411) for _ in range(411)]
    a, b, c = inpl()
    for i in range(10*(n+1) + 1):
        for j in range(10*(n+1) + 1):
            if DP[i][j] == INF:
                continue
            DP2[i][j] = min(DP[i][j], DP2[i][j])
            DP2[i+a][j+b] = min(DP[i+a][j+b], DP[i][j] + c)
    DP = DP2

x = 1
ans = INF
while Ma*x <= 400 and Mb*x <= 400:
    ans = min(ans, DP[Ma*x][Mb*x])
    
    x += 1

if ans < INF:
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return map(int, input().split())

N, Ma, Mb = inpl()
INF = 4001

DP = [[INF]*(411) for _ in range(411)]
DP[0][0] = 0

for _ in range(N):
    DP2 = [[INF]*(411) for _ in range(411)]
    a, b, c = inpl()
    for i in range(400):
        for j in range(400):
            DP2[i][j] = min(DP[i][j], DP2[i][j])
            DP2[i+a][j+b] = min(DP[i+a][j+b], DP[i][j] + c)
    DP = DP2

x = 1
ans = INF
while Ma*x <= 400 and Mb*x <= 400:
    ans = min(ans, DP[Ma*x][Mb*x])
    
    x += 1

if ans < INF:
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
N, Ma, Mb = map(int, input().split())
INF = 10**9+7
dp = [[[INF]*(401) for _ in range(401)] for __ in range(N+1)]
dp[0][0][0] = 0

for n in range(N):
    x, y, c = map(int, input().split())
    for a in range(401):
        for b in range(401):
            if dp[n][a][b] == INF:
                continue
            dp[n+1][a][b] = min(dp[n+1][a][b], dp[n][a][b])
            if a+x <= 400 and b+y <= 400:
                dp[n+1][a+x][b+y] = min(dp[n+1][a+x][b+y], dp[n][a][b]+c)

ans = INF
a, b = 0, 0
while True:
    a += Ma
    b += Mb
    if a > 400 or b > 400:
        break
    ans = min(ans, dp[N][a][b])
if ans == INF:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque
import copy


def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def bfs(X, Ma, Mb):
    #node_list = deque([0, 1])
    drug_dict = {}
    drug_dict[(0, 0)] = 0
    drug_dict[(X[0][0], X[0][1])] = X[0][2]
##    drug_dict[0] = ((0, 0) ,0)
##    drug_dict[1] = ((X[0][0], X[0][1]), X[0][2])
##    depth = 1
    min_cost = sys.maxint
    new_drug_dict = copy.deepcopy(drug_dict)
    for i in range(1, len(X)):
        for drug in drug_dict:
            cost = drug_dict[drug]
            new_drug = (drug[0] + X[i][0], drug[1] + X[i][1])
            if new_drug not in drug_dict:
                new_drug_dict[new_drug] = cost + X[i][2]
            else:
                new_drug_dict[new_drug] = min(drug_dict[new_drug], cost + X[i][2])
            if new_drug[1] * Ma == new_drug[0] * Mb:
                if new_drug_dict[new_drug] < min_cost:
                    min_cost = new_drug_dict[new_drug]
        drug_dict = copy.deepcopy(new_drug_dict)
    return min_cost

def dp_search(X, Ma, Mb, AMax, BMax):
    dp = [[sys.maxint for i in range(AMax + 1)] for j in range(BMax + 1)]
    dp[0][0] = 0

    for i in range(len(X)):
        for a in range(AMax, -1, -1):
            for b in range(BMax, -1, -1):
                if dp[a][b] >= sys.maxint:
                    continue;
                if a + X[i][0] <= AMax and b + X[i][1] <= BMax:
                    dp[a + X[i][0]][b + X[i][1]] = min(dp[a + X[i][0]][b + X[i][1]], dp[a][b] + X[i][2])
    min_cost = sys.maxint
    for a in range(0 , AMax):
        for b in range(0, BMax):
            if a * Mb == b * Ma and a != 0 and b != 0:
                if dp[a][b] < min_cost:
                    min_cost = dp[a][b]
    return min_cost


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, Ma, Mb] = [int(input_raw[0]), int(input_raw[1]), int(input_raw[2])]
    X = []
    for i in range(N):
        input_raw = read_func().strip().split()
        [a, b, c] = [int(input_raw[0]), int(input_raw[1]), int(input_raw[2])]
        X.append((a, b, c))

    #cost = bfs(X, Ma, Mb)
    cost = dp_search(X, Ma, Mb, N * 10, N * 10)
    if cost < sys.maxint:
        print cost
    else:
        print -1


if __name__ == '__main__':
    main()