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=map(int,input().split())
dp=[[[4001]*401 for _ in range(401)] for j in range(n+1)]
dp[0][0][0]=0
a=[0]*n; b=[0]*n; c=[0]*n
for i in range(n):
    a[i],b[i],c[i]=map(int,input().split())

for i in range(n):
    for j in range(401):
        for k in range(401):
            if dp[i][j][k]==4001:
                continue
            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=4001
for i in range(1,401):
    for j in range(1,401):
        if i*mb==j*ma:
            ans=min(ans,dp[n][i][j])

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

Solution (Python):
import sys
input = sys.stdin.readline

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**9


dp = [[[INF]*401 for j in range(401)] for i in range(n+1)]

dp[0][0][0] = 0
for i in range(1, n+1) :
    dp[i][A[i-1]][B[i-1]] = C[i-1]

for i in range(1, n+1) :
    for ca in range(1, 401) :
        for cb in range(1, 401) :
            if not (ca-A[i-1] >= 0 and cb-B[i-1] >= 0) :
                dp[i][ca][cb] = min(dp[i][ca][cb], dp[i-1][ca][cb])
            else :
                dp[i][ca][cb] = min(dp[i][ca][cb], dp[i-1][ca][cb], dp[i-1][ca-A[i-1]][cb-B[i-1]]+C[i-1])

ans = INF

for ca in range(1, 401) :
    for cb in range(1, 401) :
        if mb*ca == ma*cb :
            ans = min(ans, dp[n][ca][cb])

if ans == INF :
    ans = -1

print(ans)

Solution (Python):
from itertools import product

MAX = 10**4
N, Ma, Mb = map(int, input().split())
chems = [tuple(map(int, input().split())) for _ in range(N)]
chems = sorted(chems, key=lambda x: x[2])
rng = range(401)
dp = [[[None for _ in rng] for _ in rng] for _ in range(N+1)]
dp[0][0][0] = MAX
for i, (a, b, c) in enumerate(chems):
    for j, k in product(rng, repeat=2):
        if j < a or k < b:
            dp[i+1][j][k] = dp[i][j][k]
        else:
            if dp[i][j-a][k-b] == MAX:
                dp[i+1][j][k] = min(c, dp[i][j][k]) if dp[i][j][k] is not None else c
            elif dp[i][j-a][k-b] is not None:
                dp[i+1][j][k] = min(dp[i][j-a][k-b] + c, dp[i][j][k]) if dp[i][j][k] is not None else dp[i][j-a][k-b] + c
            else:
                dp[i+1][j][k] = dp[i][j][k]
ans = MAX
for j, k in product(rng, repeat=2):
    if j == 0 or k == 0: continue
    if j * Mb == k * Ma and dp[N][j][k] is not None:
        ans = min(ans, dp[N][j][k])
print(-1 if ans == MAX else ans)

Solution (Python):
import copy
n, ma, mb = map(int, input().split())
a, b, c = [0]*n, [0]*n, [0]*n
for i in range(n):
    a[i], b[i], c[i] = map(int, input().split())
   
dp = [[float('inf')] * (sum(b)+1) for _ in range(sum(a)+1)]
dp[0][0] = 0
step = set([(0, 0)])

for i in range(n):
    ai, bi = a[i], b[i]

    s = step.copy()
    tempdp = copy.deepcopy(dp)
    for si in s: 
        if dp[si[0]+ai][si[1]+bi] > dp[si[0]][si[1]] + c[i]:
            tempdp[si[0]+ai][si[1]+bi] = dp[si[0]][si[1]] + c[i]
            step.add((si[0]+ai, si[1]+bi))
        
    dp = tempdp[:]


ans = float('inf')
for i in range(1, min(sum(a)//ma, sum(b)//mb)+1):
    ans = min(ans, dp[ma*i][mb*i])
    
if ans == float('inf'): print(-1)
else : print(ans)

Solution (Python):
n,ma,mb=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
suma=0;sumb=0
for i in range(n):
  suma+=l[i][0]
  sumb+=l[i][1]
dp=[[[float('inf') for i in range(sumb+1)] for i in range(suma+1)] for i in range(n+1)]
dp[0][0][0]=0
for i in range(n):
  for j in range(suma):
    for k in range(sumb):
      dp[i+1][j][k]=min(dp[i+1][j][k],dp[i][j][k])
      if j+l[i][0]<=suma and k+l[i][1]<=sumb:
        dp[i+1][j+l[i][0]][k+l[i][1]]=min(dp[i+1][j+l[i][0]][k+l[i][1]],dp[i][j][k]+l[i][2])
ans=[]
for j in range(suma+1):
  for k in range(sumb+1):
    if j*mb==k*ma and j*j!=0:
      ans.append(dp[n][j][k])
if min(ans)==float('inf'):
  print(-1)
else:
  print(min(ans))

Solution (Python):
n,ma,mb=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
suma=0;sumb=0
for i in range(n):
  suma+=l[i][0]
  sumb+=l[i][1]
dp=[[[float('inf') for i in range(sumb+1)] for j in range(suma+1)] for i in range(n+1)]
dp[0][0][0]=0
for i in range(n):
  for j in range(suma):
    for k in range(sumb):
      dp[i+1][j][k]=min(dp[i+1][j][k],dp[i][j][k])
      if j+l[i][0]<=suma and k+l[i][1]<=sumb:
        dp[i+1][j+l[i][0]][k+l[i][1]]=min(dp[i+1][j+l[i][0]][k+l[i][1]],dp[i][j][k]+l[i][2])
ans=[]
for j in range(suma+1):
  for k in range(sumb+1):
    if j*mb==k*ma and j*j!=0:
      ans.append(dp[n][j][k])
if min(ans)==float('inf'):
  print(-1)
else:
  print(min(ans))

Solution (Python):
import numpy as np

n,ma,mb=map(int,input().split())
L=10*n

dp=np.zeros((L+1,L+1),dtype="int64")
dq=np.zeros((L+1,L+1),dtype="int64")

inf=10**18
dp+=inf
dp[0][0]=0

for i in range(n):
    a,b,c=map(int,input().split())
    dq=dp.copy()
    dq[a:,b:]=np.minimum(dq[a:,b:],dp[:-a,:-b]+c)
    dp=dq
    
ans=inf
for i in range(1,401):
    if ma*i > L or mb*i > L:
        break
    ans=min(ans,dp[ma*i][mb*i])
if ans==inf:
    print(-1)
else:
    print(ans)

Solution (Python):
import copy
n,ma,mb=map(int,input().split())
abc=[list(map(int,input().split())) for i in range(n)]
l=10*n
inf=float("inf")
data=[[inf]*(l+1) for i in range(l+1)]
data[0][0]=0
for u in abc:
    a,b,c=u
    h=copy.deepcopy(data)
    for i in range(l-a+1):
        for j in range(l-b+1):
            if data[i][j]!=inf:
                h[i+a][j+b]=min(h[i+a][j+b],data[i][j]+c)
    data=h
k=1
ans=inf
while ma*k<=l and mb*k<=l:
    if data[ma*k][mb*k]!=inf:
        ans=min(ans,data[ma*k][mb*k])
    k+=1
if ans!=inf:
    print(ans)
else:
    print(-1)

Solution (Python):

def examD(inf):
    N, Ma, Mb = LI()
    abc = [0,0,0]*N
    maxA = 0; maxB = 0
    for i in range(N):
        abc[i] = LI()
        if maxA<abc[i][0]:
            maxA=abc[i][0]
        if maxB<abc[i][1]:
            maxB=abc[i][1]
    dp = [[[inf]*(maxB*(N+1)+1) for _ in range(maxA*(N+1)+1)]for _ in range(N+1)]
    dp[0][0][0] = 0
    for i in range(N):
        for j in range(maxA*N+1):
            for k in range(maxB*N+1):
                dp[i+1][j][k] = min(dp[i][j][k],dp[i+1][j][k])
                dp[i+1][j+abc[i][0]][k+abc[i][1]] = min(dp[i][j+abc[i][0]][k+abc[i][1]],dp[i][j][k]+abc[i][2])

    ans = inf
    for j in range(1,maxA * N+1):
        for k in range(1,maxB * N+1):
            if j%Ma==0 and k%Mb==0:
                if j//Ma==k//Mb:
                    ans = min(ans,dp[N][j][k])
    if ans==inf:
        ans = -1
    print(ans)


import sys
import copy
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD(inf)

Solution (Python):


import numpy as np

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

dp = np.zeros((U + 1, U + 1), dtype = np.int64)
temp = np.zeros_like(dp)

INF = 10 ** 18
dp += INF
dp[0, 0] = 0

for _ in range(N):
    a, b, c = map(int, input().split())
    temp[:] = dp.copy()
    temp[a:, b:] = np.minimum(temp[a:, b:], dp[:-a, :-b] + c)
    dp = temp 

answer = INF
for t in range(1, 401):
    a = Ma * t
    b = Mb * t
    if max(a, b) >= U: 
        break
    answer = min(answer, dp[a, b])

if answer == INF:
    answer = -1

print (answer)

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

INF = 10 ** 8

lst = [[INF] * 401 for i in range(401)]
lst[0][0] = 0

for _ in range(N):
    a, b, c = map(int, input().split())
    for i in range(400, a - 1, -1):
        for j in range(400, b - 1, -1):
            if lst[i- a][j - b] == INF:
                pass
            else:
                lst[i][j] = min(lst[i][j], lst[i - a][j - b] + c)

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

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

Solution (Python):
n,ma,mb = map(int,input().split())
med = [tuple(map(int,input().split())) for _ in range(n)]
maa = sum(x[0] for x in med)
mab = sum(x[1] for x in med)
dp = [[10**6]*(mab+1) for _ in range(maa+1)]
dp[0][0] = 0
for a,b,c in med:
  for i in range(maa,a-1,-1):
    for j in range(mab,b-1,-1):
      if dp[i][j] > dp[i-a][j-b] + c:
        dp[i][j] = dp[i-a][j-b] + c
mi = min(dp[ma*i][mb*i] for i in range(1,min(maa//ma,mab//mb)+1))
if mi > 10**5:
  mi = -1
print(mi)

Solution (Python):
n,ma,mb=map(int,input().split())
dp=[[[10**30 for j in range(401)]for i in range(401)]for k in range(n+1)]
dp[0][0][0]=0 
l=[]
for i in range(n):
    a,b,c=map(int,input().split())
    l.append([a,b,c])
for i in range(1,n+1):
    a,b,c=l[i-1][0],l[i-1][1],l[i-1][2]
    for x in range(401):
        for y in range(401):
            if x<a or y<b:
                dp[i][x][y]=dp[i-1][x][y]
                continue 
            dp[i][x][y]=min(dp[i-1][x][y],dp[i-1][x-a][y-b]+c)
            
mini=10**30 
for i in range(1,40):
    mini=min(mini,dp[n][ma*i][mb*i])
if mini>=10**30 :
    print(-1)
else:
    print(mini)

Solution (Python):
def main():
    n,ma,mb = map(int,input().split())
    dp = [[[float("inf") for _ in range(401)] for _ in range(401)] for _ in range(n+1)]
    dp[0][0][0]=0

    for i in range(1,n+1):
        a,b,c = map(int,input().split())
        for j in range(401):
            for k in range(401):
                if a<=j and b<=k:
                    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")
    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==float("inf"):
        print(-1)
    else:
        print(ans)
if __name__ == "__main__":
    main()

Solution (Python):
def d():
    nmax, mmax = 40, 10
    n, ma, mb = 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_)

    dp = [[[float('inf') for _ in range(n*mmax+1)] for _ in range(n*mmax+1) ] for _ in range(n+1)]
    dp[0][0][0] = 0

    for i in range(n):
        for ca in range(n*mmax+1):
            for cb in range(n*mmax+1):
                if dp[i][ca][cb] == float('inf'):
                    continue
                dp[i+1][ca][cb] = min(dp[i+1][ca][cb], dp[i][ca][cb])
                if ca + a[i] < n*mmax+1 and cb + b[i] < n*mmax+1:
                    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 = float('inf')
    for ca in range(1, n*mmax+1):
        for cb in range(1, n*mmax+1):
            if ma * cb == mb * ca:
                ans = min(ans, dp[n][ca][cb])

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


if __name__ == '__main__':
    d()

Solution (Python):
inf=10**10
n,ma,mb=map(int,input().split())
abc=[list(map(int,input().split())) for _ in range(n)]
memo=[[inf]*401 for _ in range(401)]
memo[0][0]=0
for a,b,c in abc:
  for i in range(400,-1,-1):
    for j in range(400,-1,-1):
      if memo[i][j]!= inf :
        memo[i+a][j+b]= min(memo[i+a][j+b], memo[i][j] + c)


ans=inf
for i in range(1,min(400//ma,400//mb)):
  ans=min(ans, memo[ma*i][mb*i])

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

Solution (Python):
n, ma, mb = map(int, input().split(" "))
info_med = []
for _ in range(n):
  info_med.append(list(map(int, input().split(" "))))
info_med.sort(key=lambda x: x[2])
dp = []
dp.append([(0, 0, [-1])])

for min_c in range(1, n * 100 + 1):
  temp_list = []
  for i, (a, b, c) in enumerate(info_med):
    if min_c - c >= 0:
      for pre_a, pre_b, pre_i in dp[min_c - c]:
        if pre_i[-1] < i:
          if (a + pre_a) * mb == (b + pre_b) * ma:
            print(min_c)
            exit()
          temp_list.append((a + pre_a, b + pre_b, pre_i + [i]))
  dp.append(temp_list)
print("-1")

Solution (Python):

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

lis = [[float("inf")] * 401 for i in range(401)]
lis[0][0] = 0

for x in range(N):

    a,b,c = map(int,input().split())

    olis = []
    for i in lis:
        new = []
        for j in i:
            new.append(j)
        olis.append(new)

    for i in range(401):

        for j in range(401):

            if olis[i][j] != float("inf"):

                if i + a < 400 and j + b < 400:

                    lis[i+a][j+b] = min( olis[i][j] + c , lis[i+a][j+b] )

ans = float("inf")

for i in range(400):

    i += 1

    for j in range(400):

        j += 1

        if i * Mb == j * Ma:

            ans = min(ans , lis[i][j])

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

Solution (Python):
def getlist():
	return list(map(int, input().split()))

INF = float("inf")
N, Ma, Mb = getlist()
L = [[INF] * 401 for i in range(401)]
L[0][0] = 0


for i in range(N):
	a, b, c = getlist()
	for j in range(401):
		for k in range(401):
			if L[400 - j][400 - k] != INF:
				L[400 - j + b][400 - k + a] = min(L[400 - j][400 - k] + c, L[400 - j + b][400 - k + a])



ans = INF
x = Ma
y = Mb
while y <= 400 and x <= 400:
	ans = min(ans, L[y][x])
	x += Ma
	y += Mb

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

Solution (Python):
import sys
import numpy as np
input = sys.stdin.readline

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

    INF = int(1e6)
    MAX = (N+1) * 10 + 1
    dp = np.full((MAX, MAX), INF, dtype=np.int64)
    tmp = np.zeros_like(dp)

    dp[0][0] = 0
    for i in range(N):
        a, b, c = map(int, input().split())
        tmp = dp.copy()
        tmp[a:, b:] = np.minimum(tmp[a:, b:], dp[:-a, :-b] + c)
        dp = tmp
    
    ans = INF
    a, b = Ma, Mb
    while a < MAX and b < MAX:
        ans = min(ans, dp[a][b])
        a += Ma
        b += Mb
    
    if ans == INF:
        ans = -1
    
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
N, Ma, Mb = map(int, input().split())
l = []
for i in range(N):
    l.append(list(map(int, input().split())))
ans = float('inf')

Mab = 10 * N + 1
dp = [[[float('inf')] * Mab for _ in range(Mab)] for _ in range(N + 1)]
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] == float('inf'):
                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])


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 == float('inf'):
    print(-1)
else:
    print(int(ans))

Solution (Python):
n, ma, mb = map(int, input().split())
ls = [list(map(int, input().split())) for _ in range(n)]
MAX_N = 10 * n + 1
dp = [[[float('inf')] * MAX_N for _ in range(MAX_N)] for _ in range(n + 1)]
dp[0][0][0] = 0

for k, (a, b, c) in enumerate(ls):
    for i in range(MAX_N):
        for j in range(MAX_N):
            dp[k + 1][i][j] = min(dp[k + 1][i][j], dp[k][i][j])
            if i + a < MAX_N and j + b < MAX_N:
                dp[k + 1][i + a][j + b] = min(dp[k][i][j] + c,
                                              dp[k][i + a][j + b])
p = min([
    dp[n][ma * i][mb * i] for i in range(1, MAX_N)
    if ma * i < MAX_N and mb * i < MAX_N
])
if p < float('inf'):
    print(p)
else:
    print(-1)

Solution (Python):
N,Ma,Mb = map(int,input().split())
ABC = [list(map(int,input().split())) for i in range(N)]

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

for i in range(1,N+1):
    for A in range(10*N+1):
        for B in range(10*N+1):
            a,b,c = ABC[i-1]
            dp[i][A][B] = min(dp[i][A][B], dp[i-1][A][B], c+dp[i-1][A-a][B-b])
            
i = 1
ans = float("inf")
while i*Ma <= 10*N and i*Mb <= 10*N:
    ans = min(ans, dp[N][i*Ma][i*Mb])
    i += 1
if ans == float("inf"):
    print(-1)
else:
    print(ans)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, a_ratio, b_ratio = LI()
L = LIR(n)
A, B, cost = [[L[i][j] for i in range(n)] for j in range(3)]
dp = [[INF] * 401 for _ in range(401)]
dp[0][0] = 0
for i in range(n):
    for j in range(401, -1, -1):
        for k in range(401, -1, -1):
            if 0 <= j + A[i] <= 400 and 0 <= k + B[i] <= 400:
                dp[j + A[i]][k + B[i]] = min(dp[j + A[i]][k + B[i]], dp[j][k] + cost[i])



ans = INF
for l in range(1, min(400 // b_ratio, 400 // a_ratio)):
    ans = min(ans, dp[a_ratio * l][b_ratio * l])


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

Solution (Python):
import copy

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

INF, W = 10 ** 10, N * 10 * max(Ma, Mb)
dp = [[INF] * (W * 2 + 1)]

for a, b, c in ABC:
    dp.append(copy.deepcopy(dp[-1]))
    df = a * Mb - b * Ma
    dp[-1][df + W] = min(dp[-1][df + W], c)
    for d in range(-W, W + 1):
        if not dp[-2][d] == INF:
            dp[-1][df + d] = min(c + dp[-2][d], dp[-2][df + d], dp[-1][df + d])

print("-1" if dp[N][W] == INF else dp[N][W])

Solution (Python):
N, Ma, Mb = map(int, input().split())
ABC = [tuple(map(int, input().split())) for _ in range(N)]
ABC.sort(key=lambda x: x[2])

dp = [[(0, 0, [-1])]]

for p in range(1, N * 100 + 1):
    tmp = []
    for i, (a, b, c) in enumerate(ABC):
        if p - c >= 0:
            for ta, tb, ti in dp[p - c]:
                if ti[-1] < i:
                    if (a + ta) * Mb == (b + tb) * Ma:
                        print(p)
                        exit()
                    tmp.append((a + ta, b + tb, ti + [i]))
    dp.append(tmp)

print("-1")

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

s = 400

DP = [[0] * (s+1) for i in range(s+1)]
C = [[float("inf")] * (s+1) for i in range(s+1)]
DP[0][0] = 1
C[0][0] = 0

for k in range(n):
    for i in range(s, -1, -1):
        for j in range(s, -1, -1):
            x = i-A[k][0]
            y = j-A[k][1]
            
            if x >= 0 and y >= 0 and DP[x][y] == 1 and C[x][y] + A[k][2] < C[i][j]:
                C[i][j] = C[x][y] + A[k][2]
                DP[i][j] = 1
            

ans = float("inf")
ma0 = ma
mb0 = mb
while ma <= s and mb <= s:
    if DP[ma][mb] == 1:
        ans = min(ans, C[ma][mb])
    ma += ma0
    mb += mb0

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

Solution (Python):
inf = float("inf")
N, Ma, Mb = map(int, input().split())
ABC = []
for _ in range(N):
    a, b, c = map(int, input().split())
    ABC.append((a, b, c))
dp = [[inf] * 420 for _ in range(420)]
dp[0][0] = 0
for a, b, c in ABC:
    for i in range(410, -1, -1):
        for j in range(410, -1, -1):
            if dp[i][j] < inf:
                dp[i+a][j+b] = min(dp[i+a][j+b], dp[i][j] + c)
ans = inf
n = 1
while max(Ma * n, Mb * n) < 420:
    ans = min(ans, dp[Ma * n][Mb * n])
    n += 1
print(ans if ans < inf else -1)

Solution (Python):
n,ma,mb=map(int,input().split())
ab=[[[10**6]*401 for _ in range(401)] for _ in range(n+1)]
ab[0][0][0]=0

for p in range(1,n+1):
    a,b,c=map(int,input().split())
    for i in range(400):
        for j in range(400):
            ab[p][i][j]=min(ab[p][i][j],ab[p-1][i][j])
            if i+a<=400 and j+b<=400:
                ab[p][i+a][j+b]=min(ab[p][i+a][j+b],ab[p-1][i][j]+c)

ans=10**6-1
for i in range(401):
    for j in range(401):
        if i!=0 and j!=0:
            if i*mb==j*ma:
                ans=min(ans,ab[n][i][j])

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

Solution (Python):
N, Ma, Mb = map(int, input().split())
dp = [{} for i in range(N+1)]
dp[0][(0, 0)] = 0
for i in range(N):
  a, b, c = map(int, input().split())
  for p, q in dp[i]:
    if (p, q) not in dp[i+1]: dp[i+1][(p, q)] = 100000
    dp[i+1][(p, q)] = min(dp[i+1][(p, q)], dp[i][(p, q)])
    if (p+a, q+b) not in dp[i+1]: dp[i+1][(p+a, q+b)] = 100000
    dp[i+1][(p+a, q+b)] = min(dp[i+1][(p+a, q+b)], dp[i][(p, q)]+c)

rs = []
for k in range(1, 10000):
  if (k*Ma, k*Mb) in dp[N]:
    r = dp[N][(k*Ma, k*Mb)]
    rs.append(r)
if len(rs) == 0:
  print(-1)
else:
  r = min(rs)
  print(r)

Solution (Python):
def main():
    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())
    
    dp = [[[float('inf')] * 401 for _ in range(401)] for _ in range(41)]
    dp[0][0][0] = 0
    for i in range(N):
        for j in range(N*10+1):
            for k in range(N*10+1):
                if dp[i][j][k] == float('inf'):
                    continue
                
                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 = float('inf')
    for i in range(1,N*10+1):
        for j in range(1,N*10+1):
            if Ma * j == Mb * i:
                ans = min(ans,dp[N][i][j])

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

    print(ans)

main()

Solution (Python):
def main():

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

    cur = dict()
    cur[(0, 0)] = 0
    for i in range(N):
        temp = cur.copy()
        for p in cur:
            a, b = p[0]+drug[i][0], p[1]+drug[i][1]

            if (a, b) not in temp:
                temp[(a, b)] = cur[p] + drug[i][2]
            else:
                temp[(a, b)] = min(temp[(a, b)], cur[p]+drug[i][2])
        cur = temp

    
    min_c = float('inf')
    for p in cur:
        if p != (0, 0) and p[0]*Mb == p[1]*Ma:
            min_c = min(min_c, cur[p])
    if min_c == float('inf'):
        return -1
    else:
        return min_c

if __name__ == '__main__':
    print(main())

Solution (Python):
import sys
input=sys.stdin.readline
n,ma,mb=map(int,input().split())
med=[]
for i in range(n):
    a,b,c=map(int,input().split())
    med.append((a,b,c))
INF=10000
dp=[[[INF]*401 for j in range(401)] for i in range(n+1)]
dp[0][0][0]=0
for i in range(n):
    for a in range(401):
        for b in range(401):
            if dp[i][a][b]!=INF:
                dp[i+1][a+med[i][0]][b+med[i][1]]=min(dp[i+1][a+med[i][0]][b+med[i][1]],dp[i][a][b]+med[i][2])
                dp[i+1][a][b]=min(dp[i+1][a][b],dp[i][a][b])
minc=INF
for a in range(1,401):
    for b in range(1,401):
        if a*mb==b*ma:
            minc=min(dp[n][a][b],minc)
if minc==INF:
    print(-1)
else:
    print(minc)

Solution (Python):
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**18 for i in range(401)]for j in range(401)]
dp[0][0]=0

p,q=0,0
for a,b,c in abc:
    for j in reversed(range(q+1)):
        for i in range(p+1):
            if dp[i][j]==10**18:
                continue
            else:
                dp[i+a][j+b]=min(dp[i+a][j+b],dp[i][j]+c)
    p+=a
    q+=b

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

Solution (Python):
INF = float('inf')
n, ma, mb = map(int, input().split())
cmax = n * 10
t = [[INF] * (cmax + 1) for _ in range(cmax + 1)]
for _ in range(n):
  a, b, c = map(int, input().split())
  for aa in range(cmax, 0, -1):
    for bb in range(cmax, 0, -1):
      if t[aa][bb] == INF:
        continue
      t[aa + a][bb + b] = min(t[aa + a][bb + b], t[aa][bb] + c)
  t[a][b] = min(t[a][b], c)
result = INF
for a in range(1, cmax):
  for b in range(1, cmax):
    if a * mb == b * ma:
      result = min(result, t[a][b])
if result == INF:
  result = -1
print(result)

Solution (Python):
INF = float('inf')
n, ma, mb = map(int, input().split())
ccmax = n * 10
t = [[INF] * (ccmax + 1) for _ in range(ccmax + 1)]
for _ in range(n):
  a, b, c = map(int, input().split())
  for aa in range(ccmax, 0, -1):
    taa = t[aa]
    for bb in range(ccmax, 0, -1):
      if taa[bb] == INF:
        continue
      if t[a + aa][b + bb] > c + taa[bb]:
        t[a + aa][b + bb] = c + taa[bb]
  if t[a][b] > c:
    t[a][b] = c
result = INF
for a in range(1, ccmax):
  ta = t[a]
  for b in range(1, ccmax):
    if a * mb == b * ma and ta[b] < result:
      result = ta[b]
if result == INF:
  result = -1
print(result)

Solution (Python):
INF = float('inf')
MAXAB = 10
n, ma, mb = map(int, input().split())
t = [[INF] * (n * MAXAB + 1) for _ in range(n * MAXAB + 1)]
t[0][0] = 0
for _ in range(n):
  a, b, c = map(int, input().split())
  for aa in range(n * MAXAB, -1, -1):
    for bb in range(n * MAXAB, -1, -1):
      if t[aa][bb] == INF:
        continue
      if t[a + aa][b + bb] > t[aa][bb] + c:
        t[a + aa][b + bb] = t[aa][bb] + c
result = INF
for a in range(1, n * MAXAB + 1):
  for b in range(1, n * MAXAB + 1):
    if a * mb == b * ma and result > t[a][b]:
      result = t[a][b]
if result == INF:
  result = -1
print(result)

Solution (Python):
INF = float('inf')
n, ma, mb = map(int, input().split())
t = [[INF] * 401 for _ in range(401)]
t[0][0] = 0
for _ in range(n):
  a, b, c = map(int, input().split())
  for aa in range(400, -1, -1):
    for bb in range(400, -1, -1):
      if t[aa][bb] == INF:
        continue
      if t[a + aa][b + bb] > t[aa][bb] + c:
        t[a + aa][b + bb] = t[aa][bb] + c
result = INF
for a in range(400, 0, -1):
  for b in range(400, 0, -1):
    if a * mb == b * ma and t[a][b] < result:
      result = t[a][b]
if result == INF:
  result = -1
print(result)

Solution (Python):
nmax, abmax, inf = 40, 10, 1000000

dp = [[[inf] * (nmax * abmax + 1) for _ in range(nmax * abmax + 1)] for _ in range(nmax + 1)]

n, ma, mb = map(int, input().split())

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

for i in range(n):
  a, b, c = map(int, input().split())
  dpi = dp[i]
  dpi1 = dp[i + 1]
  for ca in range(0, 401):
    for cb in range(0, 401):
      if dpi[ca][cb] == inf:
        continue
      dpi1[ca][cb] = min(dpi1[ca][cb], dpi[ca][cb])
      dpi1[ca + a][cb + b] = min(dpi1[ca + a][cb + b], dpi[ca][cb] + c)

ans = inf
dpn = dp[n]
for ca in range(1, 401):
  for cb in range(1, 401):
    if ca * mb == cb * ma:
      ans = min(ans, dpn[ca][cb])

if ans == inf:
  ans = -1

print(ans)

Solution (Python):
import sys
inf = float('inf')
n, ma, mb = map(int, input().split())
t = [[inf] * 401 for _ in range(401)]
t[0][0] = 0
for _ in range(n):
  a, b, c = map(int, input().split())
  for aa in range(400, -1, -1):
    for bb in range(400, -1, -1):
      if t[aa][bb] == inf:
        continue
      if t[a + aa][b + bb] > c + t[aa][bb]:
        t[a + aa][b + bb] = c + t[aa][bb]
result = inf
for a in range(400, 0, -1):
  for b in range(400, 0, -1):
    if a * mb == b * ma and t[a][b] < result:
      result = t[a][b]
if result == inf:
  print(-1)
else:
  print(result)

Solution (Python):
import sys
inf = float('inf')
n, ma, mb = map(int, input().split())
t = [[inf] * 401 for _ in range(401)]
for _ in range(n):
  a, b, c = map(int, input().split())
  for aa in range(400, 0, -1):
    for bb in range(400, 0, -1):
      if t[aa][bb] == inf:
        continue
      if t[a + aa][b + bb] > c + t[aa][bb]:
        t[a + aa][b + bb] = c + t[aa][bb]
  if t[a][b] > c:
    t[a][b] = c
result = inf
for a in range(400, 0, -1):
  for b in range(400, 0, -1):
    if a * mb == b * ma and t[a][b] < result:
      result = t[a][b]
if result == inf:
  print(-1)
else:
  print(result)

Solution (Python):

import sys


def solve(N: int, M_a: int, M_b: int, a: "List[int]", b: "List[int]", c: "List[int]"):
    INF=1000000000
    NMAX=40
    ABMAX=10
    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 i in range(1, NMAX*ABMAX+1):
        for j in range(1, NMAX*ABMAX+1):
            if (i*M_b == j * M_a):
                ans = min(ans, dp[N][i][j])
    
    if ans == INF:
        ans = -1
    
    print(ans)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    M_a = int(next(tokens))  
    M_b = int(next(tokens))  
    a = [int()] * (N)  
    b = [int()] * (N)  
    c = [int()] * (N)  
    for i in range(N):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
        c[i] = int(next(tokens))
    solve(N, M_a, M_b, a, b, c)

if __name__ == '__main__':
    main()