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 .

Solution (Python):
n, ma, mb = list(map(int, input().split()))

a, b, c = [], [], []

for i in range(n):
    aa,bb,cc = list(map(int,input().split()))
    a.append(aa)
    b.append(bb)
    c.append(cc)

dp = [[0]*(sum(b)+1) for i in range(sum(a)+1)]

max_a = 0
max_b = 0
for i in range(n):
    max_a += a[i]
    max_b += b[i]
    for ai in range(a[i], max_a+1)[::-1]:
        for bi in range(b[i], max_b+1)[::-1]:
            if dp[ai - a[i]][bi - b[i]]:
                if dp[ai][bi]:
                    dp[ai][bi] = min(dp[ai-a[i]][bi-b[i]]+c[i],dp[ai][bi])
                else:
                    dp[ai][bi] = dp[ai-a[i]][bi-b[i]]+c[i]
            elif ai == a[i] and bi == b[i]:
                if dp[ai][bi]:
                    dp[ai][bi] = min(c[i],dp[ai][bi])
                else:
                    dp[ai][bi] = c[i]

ans = float("inf")
for i in range(1, min(max_a//ma, max_b//mb)):
    if dp[i*ma][i*mb]:
        ans = min(ans, dp[i*ma][i*mb])
if ans == float("inf"):
    print(-1)
else:
    print(ans)

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

dp= [[[float("inf")] * (10 * N + 11) for a in range(10 * N + 11) ] for i in range(N + 1)]
dp[0][0][0] = 0

for i in range(N):
    a, b, c = L[i]
    for ca in range(10 * N + 1):
        for cb in range(10 * N + 1):
            dp[i+1][ca][cb] = min(dp[i+1][ca][cb], dp[i][ca][cb])
            dp[i+1][ca + a][cb + b] = min(dp[i+1][ca + a][cb + b], dp[i][ca][cb] + c)

reslis = []
ra, rb = (Ma, Mb)
while ra <= 10 * N and rb <= 10 * N:
    reslis.append(dp[N][ra][rb])
    ra += Ma
    rb += Mb

reslis.sort()
res = reslis[0]
if res == float("inf"):
    print(-1)
else:
    print(res)

Solution (Python):
N,Ma,Mb=map(int,input().split())
dp=[[[10**9]*401 for i in range(401)] for i in range(N)]
for i in range(N):
  a,b,c=map(int,input().split())
  if i==0:
    dp[0][a][b]=c
    dp[0][0][0]=0
  else:
    for p in range(401):
      for q in range(401):
        if p>=a and q>=b:
          dp[i][p][q]=min(dp[i-1][p][q],dp[i-1][p-a][q-b]+c)
        else:
          dp[i][p][q]=dp[i-1][p][q]
ans=10**9
for a in range(1,401):
  if Mb*a/Ma<=400 and Mb*a%Ma==0:
    ans=min(ans,dp[N-1][a][Mb*a//Ma])
if ans==10**9:
  print(-1)
else:
  print(ans)

Solution (Python):
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:
        if ans==-1:
            ans=dp[N][i*Ma][i*Mb]
        else:
            ans=min(ans,dp[N][i*Ma][i*Mb])
        
print(ans)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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


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))

Solution (Python):
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 = [[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)

Solution (Python):
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

Solution (Python):

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)

Solution (Python):
N, Ma, Mb = map(int, input().split())
INF = 10 ** 9

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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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()

Solution (Python):

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]
    
    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) 
        
        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)

Solution (Python):
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)

Solution (Python):

import sys
stdin = sys.stdin

def li(): return [int(x) for x in stdin.readline().split()]
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return [float(x) for x in stdin.readline().split()]
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(ns())
def nf(): return float(ns())

n, ma, mb = li()
a,b,c = list(), list(), list()
for _ in range(n):
    ai,bi,ci = li()
    a.append(ai)
    b.append(bi)
    c.append(ci)
    
INF = 10**5
dp = [[[INF for _ in range(10*n+1)] for _ in range(10*n+1)] for _ in range(n+1)]
dp[0][0][0] = 0

for dr in range(n):
    for wa in range(10*n+1):
        for wb in range(10*n+1):
            dp[dr+1][wa][wb] = min(dp[dr][wa][wb], dp[dr][wa-a[dr]][wb-b[dr]] + c[dr])
            
ans = INF
k = 1
while k*ma <= 10*n and k*mb <= 10*n:
    ans = min(ans, dp[n][k*ma][k*mb])
    k += 1
    
if ans == INF:
    print(-1)
else:
    print(ans)

Solution (Python):
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)

Solution (Python):
n, ma, mb = map(int, input().split())
dp = [[[float('inf') for _ in range(401)] for _ in range(401)] for _ in range(n)]
dp[0][0][0] = 0
a0, b0, c0 = map(int, input().split())
dp[0][a0][b0] = c0

for i in range(1, n):
	dp[i][0][0] = 0
	a, b, c = map(int, input().split())
	for j in range(401):
		for k in range(401):
			if j >= a and k >= b:
				dp[i][j][k] = min(dp[i-1][j][k], dp[i-1][j-a][k-b] + c)
			else:
				dp[i][j][k] = dp[i-1][j][k]

ans = float("inf")
cnt = 400 // max(ma, mb)
for i in range(1, cnt + 1):
	ans = min(ans, dp[-1][ma * i][mb * i])

if ans <= 4000:
	print(ans)
else:
	print(-1)

Solution (Python):
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 ca+a[i]<M and cb+b[i]<M:
                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]])
            dp[i+1][ca][cb]=min([dp[i][ca][cb],dp[i+1][ca][cb]])
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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[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)

Solution (Python):
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)

Solution (Python):

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()

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):

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)

Solution (Python):
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)

Solution (Python):
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

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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):

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)