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 itertools
N, Ma, Mb = map(int, input().split())
L = [[int(i) for i in input().split()] for _ in range(N)]

inf = N * 101
dp = [[[inf for _ in range(N*10+11)] for _ in range(N*10+11)] for _ in range(N+1)]

dp[0][0][0] = 0
cmb = itertools.product(range(N), range(N*10), range(N*10))
for i, j, k in cmb:
    dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k])
    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 = inf
for i in range(1, N*10//max(Ma, Mb)):
    ans = min(ans, dp[N][i*Ma][i*Mb])

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

Solution (Python):

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

    INF = 10000000

    dp = [[[INF for _ in range(10*N)] for _ in range(10*N)] for _ in range(N)]

    for i, (a, b, c) in enumerate(ABC):
        a -= 1
        b -= 1
        if i == 0:
            dp[0][a][b] = c
            continue
        for ai in range(10*N):
            for bi in range(10*N):
                if a > ai or b > bi:
                    dp[i][ai][bi] = dp[i-1][ai][bi]
                elif ai == a and bi == b:
                    dp[i][ai][bi] = min(dp[i-1][ai][bi], c)
                else:
                    dp[i][ai][bi] = min(dp[i-1][ai-a-1][bi-b-1]+c, dp[i-1][ai][bi])

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

if __name__ == "__main__":
    main()

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):
N,Ma,Mb = map(int,input().split())

sz = 401
table = [[99999999]*(sz+1) for _ in range(sz+1)]
table[0][0] = 0
for n in range(N):
  a,b,c = map(int,input().split())
  for i in range(sz,-1,-1):
    for j in range(sz,-1,-1):
      if table[i][j]!=99999999:
        table[i+a][j+b] = min(table[i+a][j+b],table[i][j]+c)


ans = 99999999
i,j = Ma,Mb
while i<=sz and j<=sz:
  ans = min(ans,table[i][j])
  i += Ma
  j += Mb
print(ans if ans!=99999999 else -1)

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

inf = 10 ** 10
a_sum = sum(a_list)
b_sum = sum(b_list)
dp = [[[inf for _ in range(b_sum + 1)] for _ in range(a_sum + 1)] for _ in range(N + 1)]
dp[0][0][0] = 0
for i in range(N):
    a, b, c = a_list[i], b_list[i], c_list[i]
    for j in range(a_sum + 1):
        for k in range(b_sum + 1):
            if j - a >= 0 and k - b >= 0:
                dp[i + 1][j][k] = min(dp[i][j - a][k - b] + c, dp[i][j][k])
            else:
                dp[i + 1][j][k] = dp[i][j][k]

x = 1
ans = inf
while Ma * x <= a_sum and Mb * x <= b_sum:
    ans = min(ans, dp[-1][Ma * x][Mb * x])
    x += 1

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

Solution (Python):

import sys
INF = float("inf")


def solve(N: int, M_a: int, M_b: int, a: "List[int]", b: "List[int]", c: "List[int]"):
    M = (max(a+b)+1)*N
    DP = [[INF]*(M) for _ in range(M)]
    for aa, bb, cc in zip(a, b, c):
        DP_next = [DP[i][:] for i in range(M)]
        DP_next[aa][bb] = min(cc, DP_next[aa][bb])
        for i in range(1, M):
            if M <= i + aa:
                break
            for j in range(1, M):
                if M <= j+bb:
                    break
                DP_next[i+aa][j+bb] = min(DP[i][j]+cc, DP_next[i+aa][j+bb])
        DP = DP_next

    m = INF
    for i in range(1, M//M_a):
        anum = M_a*i
        bnum = M_b*i
        if anum >= M or bnum >= M:
            break
        m = min(DP[anum][bnum], m)
    if m == INF:
        print(-1)
    else:
        print(m)
    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):
def sol():
    N, Ma, Mb = map(int, input().split())

    A = []
    B = []
    cost = []
    sumA = 0
    sumB = 0

    for _ in range(N):
        a, b, c = map(int, input().split())
        A.append(a)
        B.append(b)
        cost.append(c)
        sumA += a
        sumB += b

    dp = [[[float('inf') for _ in range(sumB * 2 + 1)] for _ in range(sumA * 2 + 1)] for _ in range(N + 1)]
    dp[0][0][0] = 0

    for index in range(N):
        for sA in range(sumA + 1):
            for sB in range(sumB + 1):
                dp[index + 1][sA][sB] = min(
                    dp[index + 1][sA][sB],
                    dp[index][sA][sB]
                )
                dp[index + 1][sA + A[index]][sB + B[index]] = min(
                    dp[index + 1][sA + A[index]][sB + B[index]],
                    dp[index][sA][sB] + cost[index]
                )

    ans = float('inf')
    for sA in range(1, sumA):
        for sB in range(1, sumB):
            if sA * Mb == sB * Ma:
                ans = min(ans, dp[N][sA][sB])

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

sol()

Solution (Python):


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

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





INF = float('inf')
g_max = 10 * N
dp = [[[INF for cb in range(g_max+1)] for ca in range(g_max+1)] for i in range(N+1)]

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

for i in range(N):
    for ca in range(g_max+1):
        for cb in range(g_max+1):
            if dp[i][ca][cb] == INF:
                continue
            a, b, c = d[i]
            
            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)

ans = -1
for ca in range(g_max+1):
    for cb in range(g_max+1):
        c = dp[N][ca][cb]
        if c == INF:
            continue
        if ca*Mb == cb*Ma and c > 0:
            if ans < 0 or c < ans:
                ans = c

print(ans)

Solution (Python):
n,ma,mb=map(int,input().split())
dp=[[[9999]*401for _ in range(401)]for _ in range(n+1)];dp[0][0][0]=0
sa=sb=0
for i in range(1,n+1):
  a,b,c=map(int,input().split())
  sa+=a;sb+=b
  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-a][k-b]+c,dp[i-1][j][k])
      else:dp[i][j][k]=dp[i-1][j][k]
a=9999
for i in range(1,min(sa//ma,sb//mb)+1):
  if dp[n][ma*i][mb*i]!=9999:a=min(a,dp[n][ma*i][mb*i])
print(-1if a==9999else a)

Solution (Python):
N, Ma, Mb = map(int,input().split())
C = [map(int,input().split()) for i in range(N)]
INF = 40000
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 = C[i-1]
  for j in range(401):
      for k in range(401):
        dp[i][j][k] = dp[i-1][j][k]
        if j >= a and k >= b and dp[i-1][j-a][k-b] != INF:
            dp[i][j][k] = min(dp[i][j][k],dp[i-1][j-a][k-b]+c)
ans = INF
Ma2,Mb2 = Ma,Mb
while max(Ma2,Mb2) < 401:
  ans = min(ans,dp[-1][Ma2][Mb2])
  Ma2 += Ma
  Mb2 += Mb    

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

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


def A():
    a, b = LI()
    a -= 2
    b -= 2
    a %= 13
    b %= 13
    if a > b:
        print("Alice")
    elif a < b:
        print("Bob")
    else:
        print("Draw")
    return


def B():
    n, m = LI()
    a = SR(n)
    b = SR(m)
    if a == b:
        print("Yes")
        return
    for y in range(n - m):
        for x in range(n - m):
            if a[y][x] == b[0][0]:
                flg = 0
                for yi in range(m):
                    for xi in range(m):
                        if b[yi][xi] == a[y + yi][x + xi]:
                            continue
                        flg = 1
                        break
                    if flg:
                        break
                if flg:
                    continue
                print("Yes")
                return
    print("No")
    return


def C():
    n, m = LI()
    edg = [[] for i in range(n)]
    for _ in range(m):
        a, b = LI_()
        edg[a].append(b)
        edg[b].append(a)
    def dfs(i, c):
        res = 0
        c[i] = False
        if not (True in c):
            c[i] = True
            return 1
        for e in edg[i]:
            if c[e]:
                res += dfs(e, c)
        c[i] = True
        return res
    print(dfs(0, [True] * n))
    return


def D():
    n, Ma, Mb = LI()
    abc = LIR(n)
    dp = [[inf] * (10 * n + 1) for i in range(10 * n + 1)]
    dp[0][0] = 0
    for a, b, c in abc:
        for i in range(10 * n, a - 1, -1):
            for j in range(10 * n, b - 1, -1):
                dp[i][j] = min(dp[i - a][j - b] + c, dp[i][j])
    ans = inf
    ma = Ma
    mb = Mb
    i = 1
    while 1:
        ans = min(ans, dp[ma * i][mb * i])
        i += 1
        if ma * i > 10 * n or mb * i > 10 * n:
            break
    if ans == inf:
        print(-1)
        return
    
    print(ans)
    return


if __name__ == '__main__':
    D()

Solution (Python):



import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 

n,A,B = [int(i) for i in readline().split()]
abc = [[int(i) for i in readline().split()] for j in range(n)]

ans = 10**9

d = {0:0}
for i in range(n):
    nd = {}
    a,b,c = abc[i]
    for amari,yen in d.items():
        if amari in nd:
            nd[amari] = min(nd[amari],yen)
        else:
            nd[amari] = yen

        yen += c
        amari += a*B-b*A
        if amari == 0 and yen != 0:
            ans = min(ans,yen)
        elif amari in nd:
            nd[amari] = min(nd[amari],yen)
        else:
            nd[amari] = yen
    d = nd


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

Solution (Python):



import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 

n,A,B = [int(i) for i in readline().split()]
abc = [[int(i) for i in readline().split()] for j in range(n)]


res1 = []
res2 = []
eq = 10**9

"""
def dfs(s,g,amari,yen,List):
    global eq
    if s != g:
        a,b,c = abc[s]
        if a*B == A*b:
            eq = min(eq,c)
            dfs(s+1,g,amari,yen,List)
        else: 
            dfs(s+1,g,amari+a*B-b*A,yen+c,List)
            dfs(s+1,g,amari        ,yen,List)
    else:
        if amari != 0:
            List.append((amari,yen))
        else:
            if yen != 0:
                eq = min(eq,yen)
        

dfs(0,n//2,0,0,res1)
dfs(n//2,n,0,0,res2)
"""

def dp(s,g):
    global eq
    d = {0:0}
    for i in range(s,g):
        nd = {}
        a,b,c = abc[i]



        for amari,yen in d.items():
            if amari in nd:
                nd[amari] = min(nd[amari],yen)
            else:
                nd[amari] = yen
            yen += c
            amari += a*B-b*A
            if amari == 0 and yen != 0:
                eq = min(eq,yen)
            elif amari in nd:
                nd[amari] = min(nd[amari],yen)
            else:
                nd[amari] = yen
        d = nd

    
    return list(d.items())


res1 = dp(0,n//2)
res2 = dp(n//2,n)



res2.sort()




from bisect import *

ans = min(10**9,eq)

L = len(res2)
for amari,yen in res1:
    i = bisect(res2,(-amari,-10000000))
    if i < L and res2[i][0] == -amari:
        if 0 < yen+res2[i][1] < ans:
            ans = yen+res2[i][1]


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

Solution (Python):
import math
import copy
from operator import mul
from functools import reduce
from collections import defaultdict
from collections import Counter
from collections import deque

from itertools import product

from itertools import permutations

from itertools import combinations

from itertools import accumulate
from bisect import bisect_left, bisect_right

import re







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


dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
 
def i_inpl(): return int(input())
def l_inpl(): return list(map(int, input().split()))
def line_inpl(x): return [i_inpl() for _ in range(x)]

INF = int(1e30)
MOD = int(1e9)+7 


def create_grid(H, W, value = 0):
    return [[ value for _ in range(W)] for _ in range(H)]



def main():
    N, Ma, Mb = l_inpl()
    abc = []
    for _ in range(N):
        abci = l_inpl()
        abc.append(abci)

    max_ab = 40*10

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

    for i in range(N):
        for j in range(max_ab+1):
            for k in range(max_ab+1):
                
                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+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])
                
    ans = INF
    for ca in range(1, max_ab+1):
        for cb in range(1, max_ab+1):
            if ca*Mb == cb*Ma:
                ans = min(ans, dp[N][ca][cb])
    
    if ans == INF:
        print(-1)
    else:
        print(ans)

    
if __name__ == "__main__":
    main()

Solution (Python):
import math
import copy
from operator import mul
from functools import reduce
from collections import defaultdict
from collections import Counter
from collections import deque

from itertools import product

from itertools import permutations

from itertools import combinations

from itertools import accumulate
from bisect import bisect_left, bisect_right

import re







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


dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
 
def i_inpl(): return int(input())
def l_inpl(): return list(map(int, input().split()))
def line_inpl(x): return [i_inpl() for _ in range(x)]

INF = int(1e30)
MOD = int(1e9)+7 


def create_grid(H, W, value = 0):
    return [[ value for _ in range(W)] for _ in range(H)]



def main():
    N, Ma, Mb = l_inpl()
    abc = []
    for _ in range(N):
        abci = l_inpl()
        abc.append(abci)

    max_ab = 40*10

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

    for i in range(N):
        for j in range(max_ab+1):
            for k in range(max_ab+1):
                
                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+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])
                
    ans = INF
    for ca in range(1, max_ab+1):
        for cb in range(1, max_ab+1):
            if ca*Mb == cb*Ma:
                ans = min(ans, dp[N][ca][cb])
    
    if ans == INF:
        print(-1)
    else:
        print(ans)

    
if __name__ == "__main__":
    main()

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

Solution (Python):
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)
M = 401
dp = [[[float('INF') for _ in range(M)]
       for _ in range(M)] for _ in range(N + 1)]
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] == 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 j in range(1, M):
    for k in range(1, M):
        if j * Mb == k * Ma:
            ans = min(ans, dp[N][j][k])
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 [0]*n]

memo=[[10**4 for _ in [0]*(n*10+1)] for _ in [0]*(n*10+1)]
memo[0][0]=0

for N in range(n):
    for i in range(N*10,-1,-1):
        for j in range(N*10,-1,-1):
            a,b,c=abc[N]
            memo[i+a][j+b]=min(memo[i+a][j+b],memo[i][j]+c)

m=10**4
for i in range(1,n*10):
    for j in range(1,n*10):
        if i*mb==ma*j:
            m=min(m,memo[i][j])
if m==10**4:
    print(-1)
else:
    print(m)

Solution (Python):



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

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

Solution (Python):


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

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

Solution (Python):
N,Ma,Mb=map(int,input().split())
abc = list(list(map(int,input().split()))for i in range(N))
DP = [[[10**9]*(10*N+1) for i in range(10*N+1)]for i in range(N+1)]
DP[0][0][0] = 0
for i in range(N):
    for j in range(10*N+1):
        for k in range(10*N+1):
            if j >= abc[i][0] and k >= abc[i][1]:
                DP[i+1][j][k] = min(DP[i][j][k],DP[i][j-abc[i][0]][k-abc[i][1]]+abc[i][2])
            else:
                DP[i+1][j][k] = DP[i][j][k]
ans = 10**9
for i in range(1,10*N//max(Ma,Mb)+1):
    ans = min(DP[-1][i*Ma][i*Mb],ans)
if ans >= 10**8:
    print(-1)
else:
    print(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())

INF = 10 ** 5
nums = 401

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

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

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


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

if res == INF:
    res = -1

print(res)

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

INF = 10 ** 5
nums = 401

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

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

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


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

if res == INF:
    res = -1

print(res)

Solution (Python):
N,Ma,Mb = map(int,input().split())
ABC = [list(map(int,input().split())) for _ in [0]*N]
INFTY = 10**8
dp = [[[INFTY]*10*(N+1) for _ in [0]*10*(N+1)] for _ in [0]*(N+1)]
dp[0][0][0] = 0
for i in range(1,N+1):
    a,b,c = ABC[i-1]
    for j in range(10*(N+1)):
        for k in range(10*(N+1)):
            dp[i][j][k] = dp[i-1][j][k]
            if j-a>=0 and k-b>=0:
                dp[i][j][k] = min(dp[i][j][k],dp[i-1][j-a][k-b]+c)
ans = min(dp[-1][Ma*k][Mb*k] for k in range(1,(10*(N+1)-1)//max(Ma,Mb)+1))
if ans == INFTY : ans = -1
print(ans)

Solution (Python):

def main():
    num, wariai_a, wariai_b = map(int, input().split())
    data = [list(map(int, input().split())) for i in range(num)]

    inf = 100 * num + 1
    dp = [[[inf for i in range(400)] for j in range(400)] for k in range(num + 1)]
    dp[0][0][0] = 0
    for i in range(1, num + 1):
        a, b, c = data[i - 1]
        for j in range(400):
            for k in range(400):
                dp[i][j][k] = min(dp[i - 1][j][k], dp[i][j][k])
                if j + a < 400 and k + b < 400:
                    dp[i][j + a][k + b] = min(dp[i][j + a][k + b], dp[i - 1][j][k] + c)

    ans = inf
    for j in range(1, 400):
        for k in range(1, 400):
            if j * wariai_b == k * wariai_a:
                ans = min(ans, dp[num][j][k])

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


if __name__ == '__main__':
    main()

Solution (Python):
n,ma,mb=map(int,input().split())
dp=[[[5000]*401for _ in range(401)] for _ in range(n+1)]
dp[0][0][0]=0
maa=mab=0
for i in range(1,n+1):
  a,b,c=map(int,input().split())
  maa+=a
  mab+=b
  for j in range(401):
    for k in range(401):dp[i][j][k]=min(dp[i-1][j][k],dp[i-1][j-a][k-b]+c)
ans=5000
for i in range(1,min(maa//ma,mab//mb)+1):
  if dp[n][ma*i][mb*i]!=5000:ans=min(ans,dp[n][ma*i][mb*i])
print(-1if ans==5000else ans)

Solution (Python):
def smaller(x, y):
	if x < y:
		return x
	return y

N, Ma, Mb = map(int, input().split())
dp = []
for i in range(0, 401):
	lst = []
	for j in range(0, 401):
		lst.append(-1)
	dp.append(lst)
dp[0][0] = 0
for i in range(0, N):
	a, b, c = map(int, input().split())
	next = []
	for j in range(0, 401):
		for k in range(0, 401):
			if dp[j][k] != -1:
				if dp[j+a][k+b] == -1:
					next.append([j+a, k+b, dp[j][k]+c])
				else:
					if dp[j][k]+c == smaller(dp[j+a][k+b], dp[j][k]+c):
						next.append([j+a, k+b, dp[j][k]+c])
	for j in range(0, len(next)):
		if next[j][0] <= 400 and next[j][1] <= 400:
			dp[next[j][0]][next[j][1]] = next[j][2]
min = 5000000000000000
index = 1
while Ma*index <= 400 and Mb*index <= 400:
	if dp[Ma*index][Mb*index] != -1:
		min = smaller(min, dp[Ma*index][Mb*index])
	index += 1
if min == 5000000000000000:
	print(-1)
else:
	print(min)

Solution (Python):
def calc():
    N,Ma,Mb = map(int, input().split())
    arr=[list(map(int,input().split())) for _ in range(N)]
    dp=[[[5000]*401 for _ in range(401)] for _ in range(41)]
    dp[0][0][0] = 0
    for i in range(N):
        a,b,c=arr[i]
        for j in range(10*N+1):
            for k in range(10*N+1):
                if dp[i][j][k] == 5000:
                    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=5000
    for i in range(1,401):
            if i*Ma>400 or i*Mb>400:
                break
            ans = min(ans, dp[N][i*Ma][i*Mb])
    if ans == 5000:
        print(-1)
    else:
        print(ans)

calc()

Solution (Python):
N,Ma,Mb = map(int, input().split())
arr=[list(map(int,input().split())) for _ in range(N)]
dp=[[[5000]*401 for _ in range(401)] for _ in range(41)]
dp[0][0][0] = 0
for i in range(N):
    a,b,c=arr[i]
    for j in range(10*N+1):
        for k in range(10*N+1):
            if dp[i][j][k] == 5000:
                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=5000
for i in range(1,401):
        if i*Ma>400 or i*Mb>400:
            break
        ans = min(ans, dp[N][i*Ma][i*Mb])
if ans == 5000:
    print(-1)
else:
    print(ans)

Solution (Python):
N,Ma,Mb = map(int, input().split())
arr=[list(map(int,input().split())) for _ in range(N)]
INF = 5000
dp=[[[INF]*(10*N+1) for _ in range(10*N+1)] for _ in range(N+1)]
dp[0][0][0] = 0
for i in range(N):
    a,b,c=arr[i]
    for j in range(10*N+1):
        for k in range(10*N+1):
            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+a][k+b]=min(dp[i+1][j+a][k+b],dp[i][j][k]+c)

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

Solution (Python):
def calc():
  n,ma,mb=map(int,input().split())
  dp=[[[5000]*401 for _ in range(401)] for _ in range(41)]
  arr=[list(map(int,input().split())) for _ in range(n)]
  dp[0][0][0]=0
  for t in range(n):
    a,b,c=arr[t]
    for i in range(401):
      for j in range(401):
        if dp[t][i][j]==5000:
          continue
        dp[t+1][i][j]=min(dp[t+1][i][j],dp[t][i][j])
        dp[t+1][i+a][j+b]=min(dp[t+1][i+a][j+b],dp[t][i][j]+c)
  ans=5000
  for i in range(1,401):
    if i*ma>400 or i*mb>400:
      break
    ans=min(ans,dp[n][i*ma][i*mb])
  if ans==5000:
    print(-1)
  else:
    print(ans)
    
calc()

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



arr=[list(map(int,input().split())) for _ in range(N)]











INF = 5000




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



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


ans = INF










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

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

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



arr=[list(map(int,input().split())) for _ in range(N)]











INF = 5000




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


ans = INF










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

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