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

import sys
import numpy as np


def solve(N: int, M_a: int, M_b: int, a: "List[int]", b: "List[int]", c: "List[int]"):
    c_max = 100
    a_max = 10
    total_max = (N+1) * a_max

    t = np.full((N+1, total_max + 1, total_max + 1), c_max * N, dtype=int)
    t[0][0][0] = 0

    for i in range(N):
        t[i+1] = t[i]
        buy = t[i, :-a[i], :-b[i]] + c[i]
        not_buy = t[i, a[i]:, b[i]:]
        cheaper = buy < not_buy
        t[i+1, a[i]:, b[i]:] = np.where(cheaper, buy, not_buy)
    
    t_a, t_b = M_a, M_b
    cheapest = total_max + 1
    while t_a <= total_max and t_b <= total_max:
        total = t[N][t_a][t_b]
        if total < cheapest:
            cheapest = total
        t_a += M_a
        t_b += M_b
    
    if cheapest == total_max + 1:
        print(-1)
    else:
        print(cheapest)

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

Solution (Python):
n, ma, mb = map(int,input().split())
drag_list = []
a_sum = 0
b_sum = 0
for i in range(n):
    a, b, c = map(int,input().split())
    a_sum +=a
    b_sum +=b
    drag = [a,b,c]
    drag_list.append(drag)

INF = 100000
dp = [[[INF for k in range(401)]\
              for j in range(401)]\
              for i in range(n+1)]
dp[0][0][0] = 0
for i in range(n):
    A, B, C = drag_list[i]
    for a in range(a_sum+1):
        for b in range(b_sum+1):
            if dp[i][a][b] == INF:
                continue
            if A + a <= a_sum + 1 and B + b <= b_sum + 1:
                dp[i+1][a][b] = min(dp[i+1][a][b], dp[i][a][b])
                dp[i+1][a+A][b+B] = min(dp[i+1][a+A][b+B], dp[i][a][b] + C)
ans = INF
for a in range(1,401):
    for b in range(1,401):
        if a*mb == b*ma:
            ans = min(ans, dp[n][a][b])
print(-1 if ans == INF else ans)
import numpy as np
dp = np.array(dp)

Solution (Python):
INF = 10**10
n, ma, mb = [int(item) for item in input().split()]
abc = [[int(item) for item in 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())
INF = 10**6
abmax = 10
nmax = 40
medi = []
sumb = 0
suma = 0
for i in range(n):
    a, b, c = map(int,input().split())
    suma += a
    sumb += b
    medi.append([a,b,c])
dp = [[[INF for k in range(nmax*abmax+1)] for j in range(nmax*abmax+1)] for i in range(nmax+1)]
dp[0][0][0] = 0
for i in range(n):
    a, b, c = medi[i]
    for x in range(suma+1):
        for y in range(sumb+1):
            if dp[i][x][y] == INF:
                continue
            if x + a <= suma + 1 and y + b <= sumb + 1:
                dp[i + 1][x][y] = min(dp[i + 1][x][y], dp[i][x][y])
                dp[i + 1][x + a][y + b] = min(dp[i][x][y] + c, dp[i + 1][x + a][y + b])

t = 1
ok = False
ans = INF
for ca in range(1,nmax*abmax+1):
    for cb in range(1,nmax*abmax+1):
        if ma*cb == mb*ca:
            ans = min(ans, dp[n][ca][cb])
    
print(ans if ans != INF else -1)

Solution (Python):
n, ma, mb = map(int,input().split())
INF = 10**6
abmax = 10
nmax = 40
medi = []
sumb = 0
suma = 0
for i in range(n):
    a, b, c = map(int,input().split())
    suma += a
    sumb += b
    medi.append([a,b,c])
dp = [[[INF for k in range(nmax*abmax+1)] for j in range(nmax*abmax+1)] for i in range(nmax+1)]
dp[0][0][0] = 0
for i in range(n):
    a, b, c = medi[i]
    for x in range(suma+1 - a):
        for y in range(sumb+1 - b):
            if dp[i][x][y] == INF:
                continue
            dp[i + 1][x][y] = min(dp[i + 1][x][y], dp[i][x][y])
            dp[i + 1][x + a][y + b] = min(dp[i][x][y] + c, dp[i + 1][x + a][y + b])

t = 1
ok = False
ans = INF
for ca in range(1,nmax*abmax+1):
    for cb in range(1,nmax*abmax+1):
        if ma*cb == mb*ca:
            ans = min(ans, dp[n][ca][cb])
    
print(ans if ans != INF else -1)

Solution (Python):
n, ma, mb = map(int,input().split())
INF = 1000000
abmax = 10
nmax = 40
medi = []
sumb = 0
suma = 0
for i in range(n):
    a, b, c = map(int,input().split())
    suma += a
    sumb += b
    medi.append([a,b,c])
dp = [[[INF for k in range(nmax*abmax+1)] for j in range(nmax*abmax+1)] for i in range(nmax+1)]
dp[0][0][0] = 0
for i in range(n):
    a, b, c = medi[i]
    for x in range(suma+1 - a):
        for y in range(sumb+1 - b):
            if dp[i][x][y] == INF:
                continue
            dp[i + 1][x][y] = min(dp[i + 1][x][y], dp[i][x][y])
            dp[i + 1][x + a][y + b] = min(dp[i][x][y] + c, dp[i + 1][x + a][y + b])

t = 1
ok = False
ans = INF
for ca in range(1,nmax*abmax+1):
    for cb in range(1,nmax*abmax+1):
        if ma*cb == mb*ca:
            ans = min(ans, dp[n][ca][cb])
    
print(ans if ans != INF else -1)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 8)

N, Ma, Mb = map(int, input().split())
A = [[int(i) for i in input().split()] for i in range(N)]
memo = {i : {} for i in range(N)}

def dfs(i, a, b):
    if b > 0 and a / b == Ma / Mb:
        return 0
    elif i == N:
        return float('inf')
    elif (a, b) in memo[i]:
        return memo[i][(a, b)]
    else:
        ans = min(dfs(i + 1, a, b), dfs(i + 1, a + A[i][0], b + A[i][1]) + A[i][2])
    memo[i][(a, b)] = ans
    return ans

if dfs(0, 0, 0) == float('inf'):
    print(-1)
else:
    print(dfs(0, 0, 0))

Solution (Python):
import sys
from collections import deque

def main():
    input = sys.stdin.readline
    N, Ma, Mb = map(int, input().split())
    A = []
    a_sum = 1
    b_sum = 1
    for _ in range(N):
        a, b, c = map(int, input().split())
        A.append((a, b, c))
        a_sum += a
        b_sum += b

    DP = [[10**5 for _ in range(b_sum)] for _ in range(a_sum)]
    q = deque()
    for a, b, c in A:
        DP[a][b] = min(DP[a][b], c)

        q_ = deque()
        q_.append((a, b, c))
        while len(q) > 0:
            x, y, z = q.popleft()
            q_.append((x, y, z))

            s, t, u = x+a, y+b, z+c
            if DP[s][t] > u:
                DP[s][t] = u
                q_.append((s, t, u))

        q = q_

    n = min(a_sum // Ma, b_sum // Mb)
    ans = 10**5
    for i in range(1, n):
        x, y = Ma * i, Mb * i
        ans = min(ans, DP[x][y])

    if ans == 10**5:
        return -1
    else:
        return ans


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

Solution (Python):

NM=40
ABM=10
MM=400

N,M1,M2=map(int,input().split())
A=[]
B=[]
C=[]
for i in range(N):
    a,b,c=map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(c)
cmax=max(C)
CM=N*cmax+10
dp=[[[CM for broop in range(401)] for aroop in range(401)] for nroop in range(N+1)]
dp[0][0][0]=0
for i in range(N):
    for cb in range(401):
        for ca in range(401):
            if dp[i][ca][cb]==CM:
                continue
            dp[i+1][ca][cb]=min(dp[i+1][ca][cb],dp[i][ca][cb])
            if ca+A[i]<401 and cb+B[i]<401:
                dp[i+1][ca+A[i]][cb+B[i]]=min(dp[i+1][ca+A[i]][cb+B[i]],dp[i][ca][cb]+C[i])

ans=CM
for cb in range(400):
    for ca in range(400):
        CB=cb+1
        CA=ca+1
        if CA*M2 == CB*M1:
            ans=min(ans,dp[N][CA][CB])

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

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

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


dp=[[100000 for i in range(401)]for j in range(401)]
dp[0][0]=0

for n in range(N):
    for i in range(401):
        for j in range(401):
            if dp[400-i][400-j]<100000:
                if 400-i+L[n][0]<=400 and 400-j+L[n][1]<=400:
                    if dp[400-i+L[n][0]][400-j+L[n][1]]>dp[400-i][400-j]+L[n][2]:
                        dp[400-i+L[n][0]][400-j+L[n][1]]=dp[400-i][400-j]+L[n][2]
ans=100000
for i in range(1,401):
    for j in range(1,401):
        if i*B==j*A and ans>dp[i][j]:
            ans=dp[i][j]
if ans<100000:
    print(ans)
else:
    print(-1)

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

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


dp=[[100000 for i in range(401)]for j in range(401)]
dp[0][0]=0

for n in range(N):
    for i in range(401):
        for j in range(401):
            if dp[400-i][400-j]<100000:
                if 400-i+L[n][0]<=400 and 400-j+L[n][1]<=400:
                    if dp[400-i+L[n][0]][400-j+L[n][1]]>dp[400-i][400-j]+L[n][2]:
                        dp[400-i+L[n][0]][400-j+L[n][1]]=dp[400-i][400-j]+L[n][2]
ans=100000
for i in range(1,401):
    for j in range(1,401):
        if i*B==j*A and ans>dp[i][j]:
            ans=dp[i][j]
if ans<100000:
    print(ans)
else:
    print(-1)

Solution (Python):
def main():
    n,ma,mb = map(int, input().split())
    a,b,c = [],[],[]
    for i in range(n):
        ia,ib,ic = map(int, input().split())
        a.append(ia)
        b.append(ib)
        c.append(ic)
    sa = sum(a)
    sb = sum(b)
    INF = 10**5
    dp = [[ [INF] * (sb+1) for i in range(sa+1)] for i in range(n+1)]
    dp[0][0][0] = 0

    for i in range(n):
        for ca in range(sa):
            for cb in range(sb):
                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])



    res = INF
    for ca in range(1, sa):
        for cb in range(1, sb):
            if ca*mb==cb*ma:
                res = min(res, dp[n][ca][cb])

    if res == INF:
        res = -1
    print(res)

if __name__ == '__main__':
    main()

Solution (Python):
def mixing_experiment(N: int, Ma: int, Mb: int, drags: list)->int:
    INF = float('inf')

    max_A = min(100, N * max(a for a, _, _ in drags))
    max_B = min(100, N * max(b for _, b, _ in drags))

    dp = [[INF] * (max_B+1) for _ in range(max_A+1)]
    dp[0][0] = 0

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

    ans = INF
    for ca in range(1, max_A+1):
        for cb in range(1, max_B+1):
            if ca * Mb != cb * Ma:
                continue
            ans = min(ans, dp[ca][cb])

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


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

Solution (Python):



def mixing_experiment(N: int, Ma: int, Mb: int, drags: list)->int:
    INF = float('inf')

    max_A = min(100, N * max(a for a, _, _ in drags))
    max_B = min(100, N * max(b for _, b, _ in drags))

    dp = [[INF] * (max_B+1) for _ in range(max_A+1)]
    dp[0][0] = 0

    
    

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

    
    

    ans = INF
    for ca in range(1, max_A+1):
        for cb in range(1, max_B+1):
            if ca * Mb != cb * Ma:
                continue
            ans = min(ans, dp[ca][cb])

    
    

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


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

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

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

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

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

Solution (Python):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n,ma,mb = li()
drugs = [tuple(li()) for _ in range(n)]

INF = 10**18

dp = [[[INF]*401 for _ in range(401)] for _ in range(41)]
dp[0][0][0] = 0

for i in range(n):
    ai, bi, ci = drugs[i]
    for a in range(401):
        for b in range(401):
            if a+ai <= 400 and b+bi <= 400:
                dp[i+1][a+ai][b+bi] = min(dp[i][a][b] + ci,
                                          dp[i+1][a+ai][b+bi])

            dp[i+1][a][b] = min(dp[i][a][b],
                                dp[i+1][a][b])
            
ans = INF
for ai in range(1,401):
    for bi in range(1,401):
        if ai/bi == ma/mb and dp[n][ai][bi] < INF:
            ans = min(ans, dp[n][ai][bi])
            
print(ans if ans < INF else -1)

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

max_a = sum(list(zip(*info))[0]) + 1
max_b = sum(list(zip(*info))[1]) + 1

dp = [[[float("inf")]*(max_b) for i in range(max_a)] for i in range(n+1)]

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

for ni in range(1,len(info)+1):
  for ai in range(0,max_a):
    for bi in range(0,max_b):
      if ai-info[ni-1][0] < 0 or bi-info[ni-1][1] < 0:
        dp[ni][ai][bi] = dp[ni-1][ai][bi]
      else:
        dp[ni][ai][bi] = min(dp[ni-1][ai][bi],dp[ni-1][ai-info[ni-1][0]][bi-info[ni-1][1]]+info[ni-1][2])
    
ans = float("inf")
for ai in range(1,max_a):
  for bi in range(1,max_b):
    if ai*mb == bi*ma:
      ans = min(ans,dp[n][ai][bi])

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

Solution (Python):
n,a,b=map(int,input().split())
d=[[0]*401 for i in range(401)]
h=[]
for i in range(n):
    s=[]
    c=d[:]
    p,q,r=map(int,input().split())
    if d[p][q]!=0:
        d[p][q]=min(r,c[p][q])
    else:
        s.append([p,q])
        d[p][q]=r
    h=sorted(h,key=lambda x:x[1]+x[0])
    for j in range(len(h)-1,-1,-1):
        if h[j][0]+p<401 and h[j][1]+q<401:
            if c[h[j][0]+p][h[j][1]+q]!=0:
                d[h[j][0]+p][h[j][1]+q]=min(r+c[h[j][0]][h[j][1]],d[h[j][0]+p][h[j][1]+q])
            else:
                s.append([h[j][0]+p,h[j][1]+q])
                d[h[j][0]+p][h[j][1]+q]=r+c[h[j][0]][h[j][1]]
    else:
        h+=s
else:
    c=float("inf")
    k=1
    while a*k<401 and b*k<401:
        if d[a*k][b*k]!=0:
            c=min(c,d[a*k][b*k])
        k+=1
    else:
        if c==float("inf"):
            print(-1)
        else:
            print(c)

Solution (Python):
N,Ma,Mb=list(map(int, input().strip().split()))
abc=[list(map(int, input().strip().split())) for i in range(N)]
maxW=N*10
dp=[{} for __ in range(N+1)]
dp[0][(0,0)]=0
for i in range(N):
    a,b,c=abc[i]
    for k,v in dp[i].items():
        dp[i+1][k]=min(dp[i][k], dp[i+1][k]if k in dp[i+1] else float("inf"))
        dp[i+1][k[0]+a,k[1]+b]=min(dp[i][k]+c, dp[i+1][k[0]+a,k[1]+b] if (k[0]+a,k[1]+b)in dp[i+1] else float("inf"))

cost=float("inf")
Mad=Ma
Mbd=Mb
while Ma<=maxW and Mb <=maxW:
    if (Ma,Mb) in dp[-1]:
        cost=min(dp[-1][Ma,Mb],cost)
    Ma+=Mad
    Mb+=Mbd
print(-1 if cost==float("inf") else cost)

Solution (Python):

import itertools
import sys
import math
from functools import lru_cache





from queue import Queue

n, a, b = list(map(int, input().split()))

ll = [[-1 for i in range(401)] for j in range(401)]


ll[0][0] = 0
for i in range(n):
    ap, bp, price = list(map(int, input().split()))
    
    
    
    
    for aa in reversed(range(390)):
        for bb in reversed(range(390)):
            if ll[aa][bb] >= 0:
                if ll[aa + ap][bb + bp] < 0:
                    ll[aa + ap][bb + bp] = 99999
                ll[aa + ap][bb + bp] = min(ll[aa + ap][bb + bp], ll[aa][bb] + price)
              
ret = 9999999
for aa in range(401):
    for bb in range(1, 401):
        if abs(float(aa) / bb - float(a) / b) < 0.001 and ll[aa][bb] > 0:
            ret = min(ret, ll[aa][bb])
if ret > 900000:
    print('-1')
else:
    print(ret)

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

inf = float("inf")

dp = [ [ [inf] * 401 for _ in range(401)] for _ in range(N+1)]
dp[0][0][0] = 0

for i in range(N):
  a,b,c = ABC[i]
  for ma in range(400):
    for mb in range(400):
      dp[i+1][ma][mb] = min(dp[i+1][ma][mb],
                            dp[i][ma][mb])
      if ma + a > 400: continue
      if mb + b > 400: continue
        
      dp[i+1][ma+a][mb+b] = \
      min(dp[i+1][ma+a][mb+b],
          dp[i][ma][mb]+c)
      
ans = inf
for i in range(1,40000):
  if Ma*i > 400 or Mb*i > 400: break
  ans = min(ans, dp[N][Ma*i][Mb*i])

if ans == inf:
  print(-1); exit()
print(ans)

Solution (Python):
def main():
    N, MA, MB = map(int, input().split())
    dp = [[[float("inf")] * (401) for _ in range(401)] for _ in range(2)]
    dp[0][0][0] = 0
    dp[1][0][0] = 0
    for i in range(N):
        a, b, c = map(int, input().split())
        prev = (i-1) % 2
        current = i % 2
        for j in range(401):
            for k in range(401):
                if j-a >= 0 and k-b >= 0:
                    dp[current][j][k] = min(dp[prev][j][k],
                                            dp[prev][j-a][k-b] + c)
                else:
                    dp[current][j][k] = dp[prev][j][k]
    ans = float("inf")
    for i in range(1, 401):
        a = MA * i
        b = MB * i
        if a <= 400 and b <= 400:
            ans = min(ans, dp[(N-1) % 2][a][b])
        else:
            continue
    if ans == float("inf"):
        print(-1)
    else:
        print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
n, ma, mb = map(int, input().split())
abc = [tuple(int(x) for x in input().split()) for _ in range(n)]

c = n * 100 + 1

m = 401

dp = [[[99999 for _ in range(m)] for _ in range(m)] for _ in range(n + 1)]

ans = 99999
dp[0][0][0] = 0
for i in range(n):
    for j in range(m):
        for k in range(m):
            if dp[i][j][k] < 9999:
                dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k])
                dp[i+1][j+abc[i][0]][k+abc[i][1]] = min(dp[i+1][j+abc[i][0]][k+abc[i][1]], dp[i][j][k] + abc[i][2])
                if (j+abc[i][0]) * mb == (k+abc[i][1]) * ma:
                    ans = min(ans, dp[i+1][j+abc[i][0]][k+abc[i][1]])

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

Solution (Python):
N,Ma,Mb=map(int,input().split())
medic=[tuple(map(int,input().split())) for _ in range(N)]
from collections import defaultdict
inf = float('inf')
dp=defaultdict(lambda :inf)
dp[(0,0)]=0
for n in range(N):
    dpc=dp.copy()
    a,b,c=medic[n]
    for d,cost in dpc.items():
        da,db=d
        dp[(da+a,db+b)]=min(dp[(da+a,db+b)],c+cost)
ans=inf
nowa,nowb=Ma,Mb
while nowa<=400 and nowb<=400:
    ans=min(ans,dp[nowa,nowb])
    nowa+=Ma
    nowb+=Mb
if ans>4000:ans=-1
print(ans)

Solution (Python):
from collections import defaultdict

MAX = 40001


def main():
    N, Ma, Mb = [int(a) for a in input().split()]

    abc = [
        [int(a) for a in input().split()]
        for _ in range(N)
    ]

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

    A, B, C = abc[0]
    
    

    mem = {(0, A, B): C, (0, 0, 0): 0}

    def solve(i, a, b):
        if (i, a, b) in mem:
            return mem[(i, a, b)]
        if i == 0:
            return MAX
        ai, bi, ci = abc[i]
        tukawanai = solve(i - 1, a, b)
        if a - ai >= 0 and b - bi >= 0:
            pre = solve(i - 1, a - ai, b - bi)
            if pre != MAX:
                mem[(i, a, b)] = min(tukawanai, pre + ci)
                return mem[(i, a, b)]
        mem[(i, a, b)] = tukawanai
        return mem[(i, a, b)]

    res = MAX
    for a in range(1, 10 * N + 1):
        aa = a * Mb
        if aa % Ma != 0:
            continue
        b = aa // Ma
        if b >= 10 * N + 1:
            continue
        c = solve(N - 1, a, b)
        res = min(res, c)

    if res == MAX:
        res = -1
    print(res)


if __name__ == '__main__':
    main()

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

dp = [[10**18 for _ in range(401)] for _ in range(401)]

dp[0][0]=0
for i in range(N):
    a,b,c=map(int,input().split())
    for ca in range(i * 10, -1, -1):
        for cb in range(i * 10, -1, -1):
            dp[ca+a][cb+b] = min(dp[ca+a][cb+b],dp[ca][cb]+c)

ans = 10**18
for ca in range(401):
    for cb in range(401):
        if not ca*Mb == cb*Ma or ca == 0:
            continue
        if ans > dp[ca][cb]:
            ans = dp[ca][cb]
        
if ans == 10**18:
    print(-1)
else:
    print(ans)

Solution (Python):

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






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


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

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

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

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

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

Solution (Python):
inf = 9999999999
N, Ma, Mb = map(int, input().split())
d = [{} for i in range(N + 1)]
for i in range(N):
    a, b, c = map(int, input().split())
    for (j, k) in d[i]:
        d[i + 1][j, k] = d[i][j, k]
    d[i + 1][a, b] = min(c, d[i].get((a, b), inf))
    for (j, k) in d[i]:
        d[i + 1][j + a, k + b] = min(d[i][j, k] + c, d[i].get((j + a, k + b), inf))
ans = inf
for (j, k) in d[N]:
    if j * Mb == k * Ma:
        ans = min(ans, d[N][j, k])
if ans == inf:
    ans = -1
print(ans)

Solution (Python):
inf = 9999999999
N, Ma, Mb = map(int, input().split())
d = [{} for i in range(N + 1)]
for i in range(N):
    a, b, c = map(int, input().split())
    for (j, k) in d[i]:
        d[i + 1][j, k] = d[i][j, k]
    d[i + 1][a, b] = min(c, d[i].get((a, b), inf))
    for (j, k) in d[i]:
        d[i + 1][j + a, k + b] = min(d[i][j, k] + c, d[i].get((j + a, k + b), inf))
ans = inf
for (j, k) in d[N]:
    if j * Mb == k * Ma:
        ans = min(ans, d[N][j, k])
if ans == inf:
    ans = -1
print(ans)

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

dp = [ [ [float("inf")] * (25*Mb+1) for _ in range(25*Ma+1)] for _ in range(N+1)]
dp[0][0][0] = 0


for i in range(0, N):
    a,b,c = ABC[i]

    for ma in range(0,10*Ma+1):
        for mb in range(0,10*Mb+1):
            
            dp[i+1][ma+a][mb+b] = min(dp[i+1][ma+a][mb+b],
                    dp[i][ma][mb] + c) 
            dp[i+1][ma][mb] = min(dp[i+1][ma][mb],
                                  dp[i][ma][mb])

ans = float("inf")

for i in range(1,25  + 1):
    if Ma*i <= 100*Ma and Mb*i <= 100*Mb:
        ans = min(ans, dp[N][Ma*i][Mb*i])
    else:
        break
print(ans if ans != float("inf") else "-1")

Solution (Python):
n, ma, mb = list(map(int, input().split()))
negs, poss, ans = [], [], 100 * n + 1
for _ in range(n):
    a, b, c = list(map(int, input().split()))
    sub = a * mb - b * ma
    if sub > 0:
        poss.append((sub, c))
    elif sub < 0:
        negs.append((-sub, c))
    else:
        ans = min(c, ans)

def sums(tups):
    ret = {0: 0}
    for x, c in tups:
        tmp = ret.copy()
        for x0, c0 in tmp.items():
            if x + x0 in ret:
                ret[x + x0] = min(c + c0, ret[x + x0])
            else:
                ret[x + x0] = c + c0
    del ret[0]
    return ret

nsum = sums(negs)
psum = sums(poss)

for v in nsum:
    if v in psum:
        ans = min(ans, nsum[v] + psum[v])
if ans == 100 * n + 1:
    ans = -1
print(ans)

Solution (Python):
























def push(k,v):
    if k in dic:
        dic[k] = min(dic[k],v)
    else:
        dic[k] = v
n,x,y = map(int,str(input()).split(' '))
dic = {}
for i in range(0,n):
    a = list(map(int,str(input()).split(' ')))
    k,v =a[0]*y-a[1]*x,a[2]
    l = []
    for j in dic:
        l.append([j,dic[j]])
    for jj in l:
        push(jj[0]+k,jj[1]+v)
    push(k,v)
if 0 in dic:
    print(dic[0])
else:
    print('-1')

Solution (Python):
n, ma, mb = map(int, input().split())
abc = [tuple(map(int, input().split())) for _ in range(n)]
csup = sum(_abc[2] for _abc in abc) + 1
dp = [[[csup for _ in range(400)] for _ in range(400)] for _ in range(n+1)]
dp[0][0][0] = 0

for i in range(n):
  for _a in range(400):
    for _b in range(400):
      if dp[i][_a][_b] != csup:
        dp[i+1][_a][_b] = min(dp[i+1][_a][_b], dp[i][_a][_b])
        dp[i+1][_a + abc[i][0]][_b + abc[i][1]] = min(dp[i+1][_a + abc[i][0]][_b + abc[i][1]], dp[i][_a][_b] + abc[i][2])

ans = csup
for ai in range(1, 400):
  for bi in range(1, 400):
    if ai * mb == bi * ma:
      ans = min(ans, dp[n][ai][bi])
if ans == csup:
  print(-1)
else:
  print(ans)

Solution (Python):
M_ab=400
M_n=40
inf=float('inf')
n,Ma,Mb=map(int,input().split())
A,B,C=[],[],[]
for _ in range(n):
    a,b,c=map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(c)
DP=[[[inf]*(M_ab+1) for _ in range(M_ab+1)] for _ in range(M_n+1)]
DP[0][0][0]=0
for i in range(n):
    for ca in range(M_ab+1):
        for cb in range(M_ab+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,M_ab+1):
    for cb in range(1,M_ab+1):
        if ca*Mb==cb*Ma:
            ans=min(ans,DP[n][ca][cb])
if ans==inf:
    ans=-1
print(ans)

Solution (Python):
def push(k,v):
    if k in dic:
        dic[k] = min(dic[k],v)
    else:
        dic[k] = v
n,x,y = map(int,str(input()).split(' '))
dic = {}
for i in range(0,n):
    a = list(map(int,str(input()).split(' ')))
    k,v =a[0]*y-a[1]*x,a[2]
    l = []
    for j in dic:
        l.append([j,dic[j]])
    for jj in l:
        push(jj[0]+k,jj[1]+v)
    push(k,v)
if 0 in dic:
    print(dic[0])
else:
    print('-1')