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

def inpl(): return map(int, input().split())

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

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

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

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

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

Solution (Python):

def inpl(): return map(int, input().split())

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

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

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

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

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

Solution (Python):

def inpl(): return map(int, input().split())

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

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

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

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

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

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

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

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

Solution (Python):
import sys
from collections import deque
import copy


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

def bfs(X, Ma, Mb):
    
    drug_dict = {}
    drug_dict[(0, 0)] = 0
    drug_dict[(X[0][0], X[0][1])] = X[0][2]



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

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

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


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

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from collections import deque
import copy


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

def bfs(X, Ma, Mb):
    
    drug_dict = {}
    drug_dict[(0, 0)] = 0
    drug_dict[(X[0][0], X[0][1])] = X[0][2]



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


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

    cost = bfs(X, Ma, Mb)
    if cost < sys.maxint:
        print cost
    else:
        print -1


if __name__ == '__main__':
    main()

Solution (Python):
N, Ma, Mb = map(int, input().split())
A, B, C = [], [], []
for i in range(N):
    ai, bi, ci = map(int, input().split())
    A.append(ai)
    B.append(bi)
    C.append(ci)

INF = float('inf')
dp = [[[INF]*401 for i in range(401)] for j 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:
                continue
            dp[i+1][a][b] = min(dp[i+1][a][b], dp[i][a][b])
            if a + A[i] <= 400 and b + B[i] <= 400:
                dp[i+1][a+A[i]][b+B[i]] = min(dp[i+1][a+A[i]][b+B[i]], dp[i][a][b]+C[i])

ans = INF
for i in range(1, 401):
    target_a = Ma * i
    target_b = Mb * i
    if target_a > 400 or target_b > 400:
        break
    ans = min(ans, dp[N][target_a][target_b])

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

Solution (Python):
N, Ma, Mb = map(int, input().split())
A, B, C = [], [], []
for i in range(N):
    ai, bi, ci = map(int, input().split())
    A.append(ai)
    B.append(bi)
    C.append(ci)

INF = float('inf')
dp = [[[INF]*401 for i in range(401)] for j 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:
                continue
            dp[i+1][a][b] = min(dp[i+1][a][b], dp[i][a][b])
            if a + A[i] <= 400 and b + B[i] <= 400:
                dp[i+1][a+A[i]][b+B[i]] = min(dp[i+1][a+A[i]][b+B[i]], dp[i][a][b]+C[i])

ans = INF
for i in range(1, 401):
    target_a = Ma * i
    target_b = Mb * i
    if target_a > 400 or target_b > 400:
        break
    ans = min(ans, dp[N][target_a][target_b])

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

Solution (Python):
INF = 1000000
NMAX=40
ABMAX=10
n,ma,mb = map(int,input().split())
a=[]
b=[]
c=[]
for i in range(n):
    ai,bi,ci=map(int,input().split())
    a.append(ai)
    b.append(bi)
    c.append(ci)

dp = [[[INF]*(NMAX*ABMAX+1) for i in range(NMAX*ABMAX+1)] for i in range(n+1)]
dp[0][0][0]=0
for i in range(n):
    for j in range(NMAX*ABMAX+1):
        for k in range(NMAX*ABMAX+1):
            if j+a[i]>NMAX+ABMAX or k+b[i]>NMAX+ABMAX:
                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 = INF
for j in range(1,NMAX*ABMAX+1):
    for k in range(1,NMAX*ABMAX+1):
        if (j*mb==k*ma):
            ans = min(ans,dp[n][j][k])

if ans == INF:
    ans = -1

print(ans)

Solution (Python):
INF = 1000000
NMAX=40
ABMAX=10
n,ma,mb = map(int,input().split())
a=[]
b=[]
c=[]
for i in range(n):
    ai,bi,ci=map(int,input().split())
    a.append(ai)
    b.append(bi)
    c.append(ci)

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

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

if ans == INF:
    ans = -1

print(ans)

Solution (Python):
N,Ma,Mb=map(int,input().split())
Med=[]
cnt_a=0
cnt_b=0
for i in range(N):
  a=[int(x) for x in input().split()]
  Med.append(a)
  cnt_a+=a[0]
  cnt_b+=a[1]
R=max(cnt_a,cnt_b)



dp=[[[float('inf') for j in range(R+1)] for k in range(R+1)] for i in range(N+1)]
dp[0][0][0]=0

for i in range(N):
  for j in range(R+1):
    for k in range(R+1):
      if dp[i][j][k]==float('inf'):
        continue
      dp[i+1][j][k]=min(dp[i][j][k],dp[i+1][j][k])
      dp[i+1][j+Med[i][0]][k+Med[i][1]]=min(dp[i][j+Med[i][0]][k+Med[i][1]],dp[i][j][k]+Med[i][2])

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

Solution (Python):
from collections import defaultdict
N,Ma,Mb=map(int,raw_input().split())

dp=defaultdict(lambda: float("inf"))
dp[(0,0)]=0

for i in range(N):
	a,b,c=map(int,raw_input().split())
	for j in dp.items():
		x=j[0][0]
		y=j[0][1]
		z=j[1]
		na, nb, nc = x+a, y+b, z+c 
		dp[(na, nb)]=min( dp[(na, nb)], nc)


ans=float("inf")
for i in dp.items():
	x=i[0][0]
	y=i[0][1]
	z=i[1]
	if x==0 and y==0:
		continue
	if x*Mb==y*Ma:
		ans=min(ans,z)

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

Solution (Python):
def d_MixingExperiment(N, Ma, Mb, Medicine):
    
    INF = float('inf')
    A = []
    B = []
    C = []
    for a, b, c in Medicine:
        A.append(a)
        B.append(b)
        C.append(c)
    a_max = max(A)
    b_max = max(B)
    dp = [[[INF for k in range(N * b_max + 1)]
           for j in range(N * a_max + 1)] for i in range(N + 1)]
    dp[0][0][0] = 0
    for i in range(N):
        for ca in range(N * a_max + 1):
            for cb in range(N * b_max + 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, N * a_max + 1):
        for cb in range(1, N * b_max + 1):
            if ca * Mb == cb * Ma:
                ans = min(ans, dp[N][ca][cb])
    if ans == INF:
        ans = -1
    return ans

N,Ma,Mb=[int(i) for i in input().split()]
Medicine=[[int(i) for i in input().split()] for j in range(N)]
print(d_MixingExperiment(N, Ma, Mb, Medicine))

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

S = 411
MAX_N = 10
MAX_SIZE = 400
INF = int(1e9)
dp = [[INF] * S for i in range(S)]
dp[0][0] = 0
for i in range(N):
    for j in range(MAX_SIZE, -1, -1):
        for k in range(MAX_SIZE, -1, -1):
            dp[j+a[i]][k+b[i]] = min((dp[j+a[i]][k+b[i]], dp[j][k] + c[i]))

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

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

inf = float('inf')
for i in range(N):
    ai, bi, ci = map(int, input().split())
    ABC.append((ai,bi,ci))

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

for i in range(N):
    for j in range(401):
        for k in range(401):
            if dp[i][j][k] == 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 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 == inf: ans = -1
print(ans)

Solution (Python):
import sys
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache, reduce

INF = float("inf")
sys.setrecursionlimit(10 ** 7)


dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]


def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W


def main():
    N, Ma, Mb = map(int, input().split())
    dp = [[[INF] * 401 for j in range(401)] for i in range(N + 1)]
    dp[0][0][0] = 0
    a_b_c = [map(int, input().split()) for _ in range(N)]

    for i, (a, b, c) in enumerate(a_b_c):
        for j in range(len(dp[0])):
            for k in range(len(dp[0][0])):
                if dp[i][j][k] == INF:
                    continue

                
                dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k])

                
                if j + a < len(dp[0]) and k + b < len(dp[0][0]):
                    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 count(1):
        a, b = Ma * i, Mb * i
        if a >= len(dp[0]) or b >= len(dp[0][0]):
            break
        ans = min(ans, dp[-1][a][b])
    if ans != INF:
        print(ans)
    else:
        print(-1)

if __name__ == '__main__':
    main()

Solution (Python):
N,MA,MB = map(int,raw_input().split())
src = []
sum_a = sum_b = 0
for i in range(N):
  a,b,c = map(int,raw_input().split())
  src.append((a,b,c))
  sum_a += a
  sum_b += b

INF = 10**9
dp = [[[INF for b in range(sum_b+1)] for a in range(sum_a+1)] for i in range(N+1)]
dp[0][0][0] = 0

for i in range(N):
  ai,bi,ci = src[i]
  for a in range(sum_a+1 - ai):
    for b in range(sum_b+1 - bi):
      if dp[i][a][b] == INF: continue
      dp[i+1][a][b] = min(dp[i+1][a][b], dp[i][a][b])
      dp[i+1][a+ai][b+bi] = min(dp[i][a+ai][b+bi], dp[i][a][b] + ci)

ans = INF
n = 1
while True:
  a,b = n*MA,n*MB
  if a > sum_a or b > sum_b: break
  ans = min(ans, dp[N][a][b])
  n += 1

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

Solution (Python):
INF = float('inf')

def solve():
    N, Ma, Mb = map(int, input().split())
    l = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        l.append((a, b, c))
 
    dp = [[[INF] * 401 for _ in range(401)] for _ in range(N + 1)]
    dp[0][0][0] = 0
    
    for i in range(N):
        for ca in range(401):
            for cb in range(401):
                if dp[i][ca][cb] != INF:
                    dp[i + 1][ca][cb] = min(dp[i + 1][ca][cb], dp[i][ca][cb])
                    dp[i + 1][ca + l[i][0]][cb + l[i][1]] = min(dp[i + 1][ca + l[i][0]][cb + l[i][1]], dp[i][ca][cb] + l[i][2])
 
    ans = INF
    for i in range(N+1):
        for ca in range(1, 401):
            for cb in range(1, 401):
                if dp[i][ca][cb] != INF:
                    if Ma * cb == Mb * ca:
                        ans = min(ans, dp[i][ca][cb])
 
    print(-1 if ans == INF else ans)
 
 
if __name__ == '__main__':
    solve()

Solution (Python):
INF = float('inf')

def solve():
    N, Ma, Mb = map(int, input().split())
    l = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        l.append((a, b, c))
 
    dp = [[[INF] * 401 for _ in range(401)] for _ in range(N + 1)]
    dp[0][0][0] = 0
    
    for i in range(N):
        for ca in range(401):
            for cb in range(401):
                if dp[i][ca][cb] != INF:
                    dp[i + 1][ca][cb] = min(dp[i + 1][ca][cb], dp[i][ca][cb])
                    dp[i + 1][ca + l[i][0]][cb + l[i][1]] = min(dp[i + 1][ca + l[i][0]][cb + l[i][1]], dp[i][ca][cb] + l[i][2])
 
    ans = INF
    for i in range(len(dp)):
        for ca in range(1, 401):
            for cb in range(1, 401):
                if dp[i][ca][cb] != INF:
                    if Ma * cb == Mb * ca:
                        ans = min(ans, dp[i][ca][cb])
 
    print(-1 if ans == INF else ans)
 
 
if __name__ == '__main__':
    solve()

Solution (Python):
INF = float('inf')

def solve():
    N, Ma, Mb = map(int, input().split())
    l = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        l.append((a, b, c))
 
    dp = [[[INF] * 401 for _ in range(401)] for _ in range(N + 1)]
    dp[0][0][0] = 0
    for i in range(len(l)):
        for a in range(401):
            for b in range(401):
                if dp[i][a][b] != INF:
                    dp[i + 1][a][b] = min(dp[i + 1][a][b], dp[i][a][b])
                    dp[i + 1][a + l[i][0]][b + l[i][1]] = min(dp[i + 1][a + l[i][0]][b + l[i][1]], dp[i][a][b] + l[i][2])
 
    ans = INF
    for i in range(len(dp)):
        for a in range(1, 401):
            for b in range(1, 401):
                if dp[i][a][b] != INF:
                    if Ma * b == Mb * a:
                        ans = min(ans, dp[i][a][b])
 
    print(-1 if ans == INF else ans)
 
 
if __name__ == '__main__':
    solve()

Solution (Python):
INF = float('inf')

def solve():
    N, Ma, Mb = map(int, input().split())
    l = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        l.append((a, b, c))
 
    dp = [[[INF] * 401 for _ in range(401)] for _ in range(N + 1)]
    dp[0][0][0] = 0
    for i in range(len(l)):
        for a in range(len(dp[0])):
            for b in range(len(dp[0][0])):
                if dp[i][a][b] != INF:
                    dp[i + 1][a][b] = min(dp[i + 1][a][b], dp[i][a][b])
                    dp[i + 1][a + l[i][0]][b + l[i][1]] = min(dp[i + 1][a + l[i][0]][b + l[i][1]], dp[i][a][b] + l[i][2])
 
    ans = INF
    for i in range(len(dp)):
        for a in range(1, len(dp[0])):
            for b in range(1, len(dp[0][0])):
                if dp[i][a][b] != INF:
                    if Ma * b == Mb * a:
                        ans = min(ans, dp[i][a][b])
 
    print(-1 if ans == INF else ans)
 
 
if __name__ == '__main__':
    solve()

Solution (Python):
N, Ma, Mb = [int(i) for i in input().split()]
a, b, c = [], [], []
for i in range(N):
    a_, b_, c_ = [int(i) for i in input().split()]
    a.append(a_)
    b.append(b_)
    c.append(c_)
cost = [[float('inf')] * 401 for _ in range(401)]


def solve():
    max_a, max_b = 1, 1
    for num in range(N):
        for ca in range(max_a, -1, -1):
            for cb in range(max_b, -1, -1):
                cost[ca + a[num]][cb + b[num]] = min(cost[ca][cb] + c[num], cost[ca + a[num]][cb + b[num]])
        cost[a[num]][b[num]] = min(cost[a[num]][b[num]], c[num])
        max_a += a[num]
        max_b += b[num]


solve()
ans = float('inf')
most = ans
for i in range(1, 401):
    for n in range(1, 401):
        if i * Mb is n * Ma:
            ans = min(ans, cost[i][n])
if ans is most:
    print(-1)
else:
    print(ans)

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


dp = [[[10 ** 15 for i in range(n + 1)] for j in range(401)] \
    for k in range(401)]

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

for i in range(1, n + 1):
    for j in range(401):
        for k in range(401):
            if j < a[i] or k < b[i]:
                dp[k][j][i] = dp[k][j][i - 1]
            else:
                dp[k][j][i] = min(dp[k][j][i - 1], \
                    dp[k - b[i]][j - a[i]][i - 1] + c[i])

i = 1
ans = 10 ** 15
while True:
    if i * ma > 400 or i * mb > 400:
        break
    ans = min(ans, dp[i * mb][i * ma][n])
    i += 1

if ans == 10 ** 15:
    ans = -1

print(ans)

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


dp = [[[10 ** 15 for i in range(n + 1)] for j in range(401)] \
    for k in range(401)]

dp[0][0][0] = 0
sa = [0] * (n + 1)
sb = [0] * (n + 1)

for i in range(1, n + 1):
    sa[i] = sa[i - 1] + a[i]
    sb[i] = sb[i - 1] + b[i]

for i in range(1, n + 1):
    for j in range(sa[i] + 1):
        for k in range(sb[i] + 1):
            if j < a[i] or k < b[i]:
                dp[k][j][i] = dp[k][j][i - 1]
            else:
                dp[k][j][i] = min(dp[k][j][i - 1], \
                    dp[k - b[i]][j - a[i]][i - 1] + c[i])

i = 1
ans = 10 ** 15
while True:
    if i * ma > 400 or i * mb > 400:
        break
    ans = min(ans, dp[i * mb][i * ma][n])
    i += 1

if ans == 10 ** 15:
    ans = -1

print(ans)

Solution (Python):
import fractions

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


dp = [[[10 ** 15 for i in range(n + 1)] for j in range(401)] \
    for k in range(401)]

dp[0][0][0] = 0
sa = [0] * (n + 1)
sb = [0] * (n + 1)

for i in range(1, n + 1):
    for j in range(401):
        for k in range(401):
            if j < a[i] or k < b[i]:
                dp[k][j][i] = dp[k][j][i - 1]
            else:
                dp[k][j][i] = min(dp[k][j][i - 1], \
                    dp[k - b[i]][j - a[i]][i - 1] + c[i])

i = 1
ans = 10 ** 15
while True:
    if i * ma > 400 or i * mb > 400:
        break
    ans = min(ans, dp[i * mb][i * ma][n])
    i += 1

if ans == 10 ** 15:
    ans = -1

print(ans)

"""
import fractions

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


dp = [[[10 ** 15 for i in range(n + 1)] for j in range(401)] \
    for k in range(401)]

dp[0][0][0] = 0
sa = [0] * (n + 1)
sb = [0] * (n + 1)

for i in range(1, n + 1):
    sa[i] = sa[i - 1] + a[i]
    sb[i] = sb[i - 1] + b[i]

for i in range(1, n + 1):
    for j in range(sa[i] + 1):
        for k in range(sb[i] + 1):
            if j < a[i] or k < b[i]:
                dp[k][j][i] = dp[k][j][i - 1]
            else:
                dp[k][j][i] = min(dp[k][j][i - 1], \
                    dp[k - b[i]][j - a[i]][i - 1] + c[i])

i = 1
ans = 10 ** 15
while True:
    if i * ma > 400 or i * mb > 400:
        break
    ans = min(ans, dp[i * mb][i * ma][n])
    i += 1

if ans == 10 ** 15:
    ans = -1

print(ans)
"""

Solution (Python):
import fractions

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


dp = [[[10**15 for i in range(n + 1)] for j in range(401)] \
    for k in range(401)]

dp[0][0][0] = 0
sa = [0] * (n + 1)
sb = [0] * (n + 1)

for i in range(1, n + 1):
    sa[i] = sa[i - 1] + a[i]
    sb[i] = sb[i - 1] + b[i]

for i in range(1, n + 1):
    for j in range(sa[i] + 1):
        for k in range(sb[i] + 1):
            
            if j < a[i] or k < b[i]:
                dp[k][j][i] = dp[k][j][i - 1]
            else:
                dp[k][j][i] = min(dp[k][j][i - 1], \
                    dp[k - b[i]][j - a[i]][i - 1] + c[i])

i = 1
ans = 10 ** 15
while True:
    if i * ma > 400 or i * mb > 400:
        break
    ans = min(ans, dp[i * mb][i * ma][n])
    i += 1

if ans == 10 ** 15:
    ans = -1

print(ans)

Solution (Python):
import fractions

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


dp = [[[10**15 for i in range(n + 1)] for j in range(401)] \
    for k in range(401)]

dp[0][0][0] = 0
sa = [0] * (n + 1)
sb = [0] * (n + 1)

for i in range(1, n + 1):
    sa[i] = sa[i - 1] + a[i]
    sb[i] = sb[i - 1] + b[i]

for i in range(1, n + 1):
    for j in range(401):
        if j > sa[i]:
            break
        for k in range(401):
            if k > sb[i]:
                break
            
            if j < a[i] or k < b[i]:
                dp[k][j][i] = dp[k][j][i - 1]
            else:
                dp[k][j][i] = min(dp[k][j][i - 1], \
                    dp[k - b[i]][j - a[i]][i - 1] + c[i])

i = 1
ans = 10 ** 15
while True:
    if i * ma > 400 or i * mb > 400:
        break
    ans = min(ans, dp[i * mb][i * ma][n])
    i += 1

if ans == 10 ** 15:
    ans = -1

print(ans)

Solution (Python):
from collections import defaultdict


INF = 10 ** 10 // 3
def main():
    N, Ma, Mb = map(int, input().split())
    l = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        l.append((a, b, c))

    dp = [[[INF] * 401 for _ in range(401)] for _ in range(N + 1)]
    dp[0][0][0] = 0
    for i in range(len(l)):
        for a in range(len(dp[0])):
            for b in range(len(dp[0][0])):
                if dp[i][a][b] != INF:
                    dp[i + 1][a][b] = min(dp[i + 1][a][b], dp[i][a][b])
                    dp[i + 1][a + l[i][0]][b + l[i][1]] = min(dp[i + 1][a + l[i][0]][b + l[i][1]], dp[i][a][b] + l[i][2])

    ans = INF
    for i in range(len(dp)):
        for a in range(1, len(dp[0])):
            for b in range(1, len(dp[0][0])):
                if dp[i][a][b] != INF:
                    if Ma * b == Mb * a:
                        ans = min(ans, dp[i][a][b])

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


if __name__ == '__main__':
    main()

Solution (Python):
maxm = 10000
def makelist(l, n, m):
	return [[[maxm for i in range(m)] for j in range(n)] for k in range(l)]

N, Ma, Mb = map(int, input().split())
drugs = [0]*N
for i in range(N):
	a, b, c = map(int, input().split())
	drugs[i] = (a, b, c)

dp = makelist(N, 401, 401)
a0, b0, c0 = drugs[0]
dp[0][a0][b0] = c0
dp[0][0][0] = 0
for i in range(1, N):
	a, b, c = drugs[i]
	for ia in range(401):
		for ib in range(401):
			if ia - a < 0 or ib - b < 0:
				dp[i][ia][ib] = dp[i-1][ia][ib]
			else:
				dp[i][ia][ib] = \
					min(dp[i-1][ia][ib], dp[i-1][ia-a][ib-b] + c)

i = 1
ans = maxm
while Ma * i <= 400 and Mb * i <= 400:
	now = dp[N-1][Ma*i][Mb*i]
	ans = min(ans, now)
	i += 1

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

Solution (Python):
import sys
from collections import defaultdict




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


def read_str_list():
    return input().split()


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


def read_str():
    return input()


M = 10
inf = 10 ** 20


def solve():
    n, ma, mb = read_int_list()
    p = [read_int_list() for i in range(n)]
    min_c = defaultdict(lambda: inf)
    min_c[(0, 0)] = 0

    for i, (a, b, c) in enumerate(p):
        for u in range(M * i, -1, -1):
            for v in range(M * i, -1, -1):
                if (u, v) in min_c:
                    cost = min_c[(u, v)]
                    if cost + c < min_c[((u + a), (v + b))]:
                        min_c[((u + a), (v + b))] = cost + c

    res = inf
    for u in range(0, M * n + 1):
        for v in range(0, M * n + 1):
            if u == 0 and v == 0:
                continue
            if u * mb == v * ma:
                if (u, v) in min_c:
                    cost = min_c[(u, v)]
                    if cost < res:
                        res = cost
    if res == inf:
        res = -1
    return res


def main():
    res = solve()
    print(res)


main()