Problem:
 Score : 400 points    Problem Statement Dolphin is planning to generate a small amount of a certain chemical substance C. In order to generate the substance C, he must prepare a solution which is a mixture of two substances A and B in the ratio of M_a:M_b . He does not have any stock of chemicals, however, so he will purchase some chemicals at a local pharmacy. The pharmacy sells N kinds of chemicals. For each kind of chemical, there is exactly one package of that chemical in stock. The package of chemical i contains a_i grams of the substance A and b_i grams of the substance B, and is sold for c_i yen (the currency of Japan). Dolphin will purchase some of these packages. For some reason, he must use all contents of the purchased packages to generate the substance C. Find the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C by purchasing any combination of packages at the pharmacy, report that fact.      Constraints  1≦N≦40   1≦a_i,b_i≦10   1≦c_i≦100   1≦M_a,M_b≦10   gcd(M_a,M_b)=1  a_i , b_i , c_i , M_a and M_b are integers.         Input The input is given from Standard Input in the following format:  N  M_a  M_b  a_1  b_1  c_1  a_2  b_2  c_2  :  a_N  b_N  c_N       Output Print the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C, print -1 instead.        Sample Input 1 3 1 1
1 2 1
2 1 2
3 3 10      Sample Output 1 3  The amount of money spent will be minimized by purchasing the packages of chemicals 1 and 2 . In this case, the mixture of the purchased chemicals will contain 3 grams of the substance A and 3 grams of the substance B, which are in the desired ratio: 3:3=1:1 . The total price of these packages is 3 yen.       Sample Input 2 1 1 10
10 10 10      Sample Output 2 -1  The ratio 1:10 of the two substances A and B cannot be satisfied by purchasing any combination of the packages. Thus, the output should be -1 .

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

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 = 5000




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



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+a[i]][k+b[i]] = min(dp[i+1][j+a[i]][k+b[i]], dp[i][j][k] + c[i])


ans = INF










for i in range(1, 401):
        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())

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 = 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):
    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[i]][k+b[i]] = min(dp[i+1][j+a[i]][k+b[i]], dp[i][j][k] + c[i])


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 read_input():
    n, ma, mb = map(int, input().split())

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

    return n, ma, mb, medicines


def submit():
    n, ma, mb, medicines = read_input()

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

    for i in range(1, n + 1):
        
        m = medicines[i - 1]
        for ca in range(401 - m[0]):
            for cb in range(401 - m[1]):
                
                dp[i][ca][cb] = min(dp[i][ca][cb], dp[i - 1][ca][cb])

                
                dp[i][ca + m[0]][cb + m[1]] = min(dp[i][ca + m[0]][cb + m[1]], dp[i - 1][ca][cb] + m[2])


    cost = float('inf')
    for ca in range(1, 401):
        for cb in range(1, 401):
            if ca*mb == cb*ma:
                if cost > dp[n][ca][cb]:
                    cost = dp[n][ca][cb]

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



if __name__ == '__main__':
    submit()

Solution (Python):
from collections import defaultdict

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

dp = [defaultdict(lambda : float('inf')) for _ in range(2)]
zero = float('inf')

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

for _ in range(N) :
    a, b, c = map(int, input().split())
    d = a * Mb - b * Ma
    if d == 0 :
        zero = min(zero, c)
    else :
        i = 0 if d > 0 else 1
        d = abs(d)
        item = list(dp[i].items())[::-1]
        for k, v in item :
            dp[i][k+d] = min(dp[i][k+d], dp[i][k] + c)

ret = zero       
for k, v in dp[0].items() :
    if k in dp[1] and k != 0 :
        ret = min(ret, v + dp[1][k])
        
if ret == float('inf') :
    print(-1)
else :
    print(ret)

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

dp = [ [ [ float("inf") for i in range(401)] for j in range(401)] for k in range(N+1)]
dp[0][0][0] = 0
for i in range(N):
    for j in range(401):
        for k in range(401):
            if j - abc[i][0] >= 0 and k - abc[i][1] >= 0:
                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 = float("inf")
for i in range(1,401):
    for j in range(1,401):
        if (i / j ) == (Ma / Mb):
            ans = min(ans , dp[N][i][j])

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

Solution (Python):

import sys
sys.setrecursionlimit(300000)


def solve(N: int, M_a: int, M_b: int, a: "List[int]", b: "List[int]", c: "List[int]"):
    INF = float('inf')
    dp = [[INF] * 401 for _ in range(401)]
    dp[0][0] = 0
    for i in range(N):
        for x in range(400, -1, -1):
            for y in range(400, -1, -1):
                if dp[x][y] < INF:
                    dp[x + a[i]][y + b[i]] = min(dp[x + a[i]][y + b[i]], dp[x][y] + c[i])
    ret = INF
    for i in range(1, 401):
        for j in range(1, 401):
            if i % M_a == 0 and j % M_b == 0 and i // M_a == j // M_b:
                ret = min(ret, dp[i][j])
    if ret == INF:
        print(-1)
    else:
        print(ret)
    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 = list(map(int, input().split()))
abc = [list(map(int, input().split())) for _ in range(N)]

inf = 1000 * N
M = N * 10 + 1
DP = [[[inf] * M for _ in range(M)] for _ in range(N)]

a, b, c = abc[0]
DP[0][a][b] = c

for i in range(1, N):
  a, b, c = abc[i]
  DP[i][a][b] = min(DP[i - 1][a][b], c)
  for j in range(M - 10):
    for k in range(M - 10):
      DP[i][j][k] = min(DP[i][j][k], DP[i - 1][j][k])
      if DP[i - 1][j][k] < inf:
        jj = j + a
        kk = k + b
        DP[i][jj][kk] = min(DP[i - 1][jj][kk], DP[i - 1][j][k] + c)
ans = inf
i = MA
j = MB
while i < M and j < M:
  ans = min(ans, DP[-1][i][j])
  i += MA
  j += MB

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

Solution (Python):
from collections import defaultdict
from bisect import bisect_right
INF = float('inf')
N, Ma, Mb = map(int, input().split())
a, b, c = [None] * N, [None] * N, [None] * N
for i in range(N):
    a[i], b[i], c[i] = map(int, input().split())
hN = N // 2
dic1 = defaultdict(lambda : INF)
for i in range(1 << hN):
    _a, _b, _c = 0, 0, 0
    for j in range(hN):
        if (i >> j) & 1:
            _a += a[j]
            _b += b[j]
            _c += c[j]
    if _c < dic1[(_a, _b)]:
        dic1[(_a, _b)] = _c
dic2 = defaultdict(lambda : INF)
for i in range(1 << (N - hN)):
    _a, _b, _c = 0, 0, 0
    for j in range(N - hN):
        if (i >> j) & 1:
            _a += a[hN+j]
            _b += b[hN+j]
            _c += c[hN+j]
    if _c < dic2[(_a, _b)]:
        dic2[(_a, _b)] = _c
ans = INF
for k in dic1:
    _a, _b = k
    v = dic1[k]
    for i in range(1, N+1):
        ans = min(ans, v + dic2[(Ma*i-_a,Mb*i-_b)])
ans = ans if ans < INF else -1
print(ans)

Solution (Python):
from collections import defaultdict
N, Ma, Mb = map(int, input().split())
INF, MAX = 1000000000, 10 * N + 1
dp = defaultdict(lambda : INF)
dp[(0, 0)] = 0
for i in range(N):
    a, b, c = map(int, input().split())
    predp = dp.copy()
    for ab, v in predp.items():
        _a, _b = ab
        dp[(_a+a, _b+b)] = min(dp[(_a+a, _b+b)], v+c)
ans = INF
for i in range(1, N+1):
    ans = min(ans, dp[(Ma*i, Mb*i)])
ans = -1 if ans == INF else ans
print(ans)

Solution (Python):
INF = float('inf')
N,Ma,Mb = map(int,input().split())
abc = [list(map(int,input().split())) for _ in range(N)]
dp = [[[INF]*(10*N+11) for _ in range(10*N+11)] for _ in range(N+1)]
dp[0][0][0] = 0

for i in range(N):
    for a1 in range(10*N+1):
        for b1 in range(10*N+1):
            a,b,c = abc[i]
            dp[i+1][a1][b1] = min(dp[i][a1][b1],dp[i][a1-a][b1-b]+c)

ans = INF
for a1 in range(1,10*N+1):
    for b1 in range(1,10*N+1):
        if a1*Mb == b1*Ma:
            ans = min(ans,dp[N][a1][b1])

if ans == INF:
    ans = -1

print(ans)

Solution (Python):
"""
13:56
"""
import os
import sys
from collections import defaultdict
from fractions import Fraction

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18

N, MA, MB = list(map(int, sys.stdin.readline().split()))
A, B, C = list(zip(*[map(int, sys.stdin.readline().split()) for _ in range(N)]))

A = np.array(A)
B = np.array(B)
C = np.array(C)


SUMMAX = 400
dp = np.full((SUMMAX + 1, SUMMAX + 1), IINF)
dp[0, 0] = 0
for a, b, c in zip(A, B, C):
    dp[a:, b:] = np.minimum(dp[a:, b:], dp[:-a, :-b] + c)


ans = IINF
ma, mb = MA, MB
while ma <= SUMMAX and mb <= SUMMAX:
    ans = min(dp[ma, mb], ans)
    ma += MA
    mb += MB
if ans < IINF:
    print(int(ans))
else:
    print(-1)

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

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())
dp=[[0]*401 for _ in range(401)]
for _ in range(n):
  a,b,c=map(int,input().split())
  for i in range(400,-1,-1):
    for j in range(400,-1,-1):
      if i-a<0 or j-b<0:
        continue
      else:
        if dp[i-a][j-b]==0:
          continue
        else:
          if dp[i][j]==0:
            dp[i][j]=dp[i-a][j-b]+c
          else:
            dp[i][j]=min(dp[i][j],dp[i-a][j-b]+c)
  if dp[a][b]==0:
    dp[a][b]=c
  else:
    dp[a][b]=min(dp[a][b],c)
ans=10**10
for i in range(401):
  if i*ma>400 or i*mb>400:
    break
  if dp[i*ma][i*mb]==0:
    continue
  else:
    ans=min(ans,dp[i*ma][i*mb])
if ans==10**10:
  print(-1)
else:
  print(ans)

Solution (Python):
N,Ma,Mb=map(int,input().split())
a,b,c=map(list,zip(*[list(map(int,input().split())) for i in range(N)]))
ma,mb=sum(a),sum(b)
MAX=5000
dp=[[[MAX]*(mb+1) for i in range(ma+1)] for j in range(N+1)]
dp[0][0][0]=0
for i in range(N):
    for j in range(ma):
        for k in range(mb):
            dp[i+1][j][k]=min(dp[i][j][k],dp[i+1][j][k])
            if j+a[i]<=ma and k+b[i]<=mb:
                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=MAX
x=1
while x*Ma<=ma and x*Mb<=mb:
    ans=min(ans,dp[N][x*Ma][x*Mb])
    x+=1
print(ans if ans<MAX else -1)

Solution (Python):
from subprocess import*
call(('pypy3','-c',"""
I=lambda:list(map(int,input().split()))
n,Ma,Mb=I()
m=[[0,0,0]]+[I()for _ in range(n)]
dp=[[[10**18]*(10*n+1)for _ in range(10*n+1)]for _ in range(n+1)]
dp[0][0][0]=0
for i in range(1,n+1):
    for j in range(10*n+1):
        if j-m[i][0]<0:
            dp[i][j]=dp[i-1][j]
            continue
        for k in range(10*n+1):
            if k-m[i][1]<0:
                dp[i][j][k]=dp[i-1][j][k]
            else:
                dp[i][j][k]=min(dp[i-1][j-m[i][0]][k-m[i][1]]+m[i][2],dp[i-1][j][k])
ans=10**18
for i in range(1,10*n+1):
    j=Mb*i/Ma
    if j==int(j) and j<10*n+2:
        ans=min(ans,dp[-1][i][int(j)])
print(-(ans==10**18)or ans)
"""))

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

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


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 j in range(401):
        for k in range(401):
            if j - a >= 0 and k - b >= 0:
                dp[i+1][j][k] = min(dp[i][j][k], dp[i][j-a][k-b] + c)
            else:
                dp[i+1][j][k] = dp[i][j][k]

ans = inf

for j in range(1,401):
    for k in range(1,401):
        if j * Mb == Ma * k:
            ans = min(ans, dp[N][j][k])

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

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

inf = float('inf')

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


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

ans = inf

for j in range(1,401):
    for k in range(1,401):
        if j * Mb == Ma * k and ans > dp[N][j][k]:
            ans = dp[N][j][k]

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

Solution (Python):
n,ma,mb=map(int,input().split())
inf=10**4
A=[]
append=A.append
for i in range(n):
  a,b,c=map(int,input().split())
  append([a,b,c])
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=A[i][0],A[i][1],A[i][2]
  for j in range(10*(i+1)+1):
    for k in range(10*(i+1)+1):
      if j>=a and k>=b:
        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]
ans=inf
for i in range(1,10*n+1):
  if i*mb%ma==0 and i*mb//ma<10*n+1:
    j=i*mb//ma
    ans=min(ans,DP[n][i][j])
print(ans if ans!=inf else -1)

Solution (Python):
n,ma,mb=map(int,input().split())
inf=10**4
A=[]
append=A.append
for i in range(n):
  a,b,c=map(int,input().split())
  append([a,b,c])
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=A[i][0],A[i][1],A[i][2]
  for j in range(10*(i+1)+1):
    for k in range(10*(i+1)+1):
      if j>=a and k>=b:
        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]
ans=inf
for i in range(10*n+1):
  for j in range(10*n+1):
    if i*mb==j*ma and i*j!=0:
      ans=min(ans,DP[n][i][j])
print(ans if ans!=inf else -1)

Solution (Python):
n,ma,mb=map(int,input().split())
A=[]
for i in range(n):
  a,b,c=map(int,input().split())
  A.append([a,b,c])
DP=[[[float("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=A[i][0],A[i][1],A[i][2]
  for j in range(10*(i+1)+1):
    for k in range(10*(i+1)+1):
      if j>=a and k>=b:
        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]
ans=float("inf")
for i in range(10*n+1):
  for j in range(10*n+1):
    if i*mb==j*ma and i*j!=0:
      ans=min(ans,DP[n][i][j])
print(ans if ans!=float("inf") else -1)

Solution (Python):
N,Ma,Mb=map(int,input().split())
medic=[tuple(map(int,input().split())) for i in range(N)]
from collections import defaultdict
dp=defaultdict(lambda : float('inf'))
dp[(0,0)]=0
for a,b,c in medic:
    dpc=dp.copy()
    for k,v in dpc.items():
        x,y=k
        dp[(x+a,y+b)]=min(dp[(x+a,y+b)],v+c)
ans=10**10
for i in range(1,1+N):
    ans=min(ans,dp[(Ma*i,Mb*i)])
if ans>10**9:
    print(-1)
else:
    print(ans)

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

N, Ma, Mb = map(int, input().split())
a = []
b = []
c = []
suma = 0
sumb = 0
for i in range(0, N):
	inpt = list(map(int, input().split()))
	a.append(inpt[0])
	b.append(inpt[1])
	c.append(inpt[2])
	suma += inpt[0]
	sumb += inpt[1]
dp = []
for i in range(0, N+1):
	lst1 = []
	for j in range(0, suma+1):
		lst2 = []
		for k in range(0, sumb+1):
			lst2.append(-1)
		lst1.append(lst2)
	dp.append(lst1)
dp[0][0][0] = 0
for i in range(0, N):
	for j in range(0, suma+1):
		for k in range(0, sumb+1):
			if dp[i][j][k] != -1:
				if dp[i+1][j][k] == -1:
					dp[i+1][j][k] = dp[i][j][k]
				else:
					dp[i+1][j][k] = smaller(dp[i][j][k], dp[i+1][j][k])
				if dp[i+1][j+a[i]][k+b[i]] == -1:
					dp[i+1][j+a[i]][k+b[i]] = dp[i][j][k] + c[i]
				else:
					dp[i+1][j+a[i]][k+b[i]] = smaller(dp[i][j][k]+c[i], dp[i+1][j+a[i]][k+b[i]])
ans = -1
index = 1
while index * Ma <= suma and index * Mb <= sumb:
	if dp[N][index*Ma][index*Mb] != -1:
		if ans == -1: ans = dp[N][index*Ma][index*Mb]
		else: ans = smaller(ans, dp[N][index*Ma][index*Mb])
	index += 1
print(ans)

Solution (Python):

N,A,B = list(map(int,input().split()))

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

D = [[[10**27 for i in range(401)] for j in range(401)] for k in range(N+1)]
D[0][0][0] = 0
for i in range(N):
    for j in range(401):
        for k in range(401):
            if D[i][j][k]<10**26:
                D[i+1][j][k]=min(D[i+1][j][k],D[i][j][k])
                D[i+1][j+item[i][0]][k+item[i][1]] = min(D[i+1][j+item[i][0]][k+item[i][1]],D[i][j][k]+item[i][2])

i=1
min_cost=10**27
while A*i<=400 and B*i<=400:
    min_cost = min(D[N][A*i][B*i],min_cost)
    i+=1
if min_cost==10**27:
	print(-1)
else:
	print(min_cost)

Solution (Python):

N,A,B = list(map(int,input().split()))

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

D = [[[10**27 for i in range(401)] for j in range(401)] for k in range(N+1)]
D[0][0][0] = 0
for i in range(N):
    for j in range(401):
        for k in range(401):
            if D[i][j][k]<10**26:
                D[i+1][j][k]=min(D[i+1][j][k],D[i][j][k])
                D[i+1][j+item[i][0]][k+item[i][1]] = min(D[i+1][j+item[i][0]][k+item[i][1]],D[i][j][k]+item[i][2])

i=1
min_cost=10**27
while A*i<=400 and B*i<=400:
    min_cost = min(D[N][A*i][B*i],min_cost)
    i+=1
if min_cost==10**27:
	print(-1)
else:
	print(min_cost)

Solution (Python):
N,Ma,Mb=map(int,input().split())
abc=[]
INF=float("inf")
for i in range(N):
    a,b,c=map(int,input().split())
    abc.append((a,b,c))
stack=set([(0,0)])
dp=[[[INF]*(401) for j in range(401)] for i in range(N+1)]
dp[0][0][0]=0
for i in range(N):
    dp[i+1][0][0]=0
    a,b,c=abc[i]
    stack_=set()
    for x,y in stack:
        dp[i+1][x][y]=min(dp[i][x][y],dp[i+1][x][y])
        dp[i+1][x+a][y+b]=min(dp[i+1][x+a][y+b],dp[i][x+a][y+b],dp[i][x][y]+c)
        stack_.add((x+a,y+b))
        stack_.add((x,y))
    stack=stack_
k=1
ans=INF
while(Ma*k<=400 and Mb*k<=400):
    ans=min(dp[N][Ma*k][Mb*k],ans)
    k+=1
if ans==INF:
    print(-1)
else:
    print(ans)

Solution (Python):

n,a,b = map(int,input().split())
l = [list(map(int,input().split())) for i in range(n)]
INF = 500
dp = [[[INF for i in range(411)] for j in range(411)] for k in range(n+1)]
dp[0][0][0] = 0
for i in range(n):
    for j in range(a*n+1):
        for k in range(b*n+1):
            if j - l[i][0]>=0 and k - l[i][1]>=0:
                dp[i+1][j][k] = min(dp[i][j][k],dp[i][j - l[i][0]][k - l[i][1]]+l[i][2])
            else:
                dp[i+1][j][k] = dp[i][j][k]
i =1
ans = INF
while i*max(a,b)<=400:
    ans = min(ans, dp[n][i*a][i*b])
    i+=1
if ans==INF:
    print(-1)
else:
    print(ans)

Solution (Python):
n,Ma, Mb = map(int, input().split())
dp=[[[1000000]*510 for i in range(510)] for j 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(410):
        for k in range(410):
            dp[i][j][k]=min(dp[i-1][j][k], dp[i][j][k])
            p=j+a
            q=k+b
            dp[i][p][q]=min(dp[i][p][q], dp[i-1][j][k]+c)
ans=1000000
for i in range(1, 410//max(Ma, Mb)+1):
    ans=min(dp[n][i*Ma][i*Mb], ans)
if ans==1000000:
    print(-1)
else:
    print(ans)

Solution (Python):

"""
D - Mixing Experiment
https://atcoder.jp/contests/abc054/tasks/abc054_d

"""
import sys


def solve(N, Ma, Mb, items):
    d = dict()
    d[(0, 0)] = 0
    for a, b, c in items:
        nd = dict()
        for k, v in d.items():
            t = d.get((k[0], k[1]), float('inf')) + c
            if d.get((k[0]+a, k[1]+b), float('inf')) > t:
                nd[(k[0]+a, k[1]+b)] = t
        d.update(nd)
    res = []
    for k, v in d.items():
        if k == (0, 0):
            continue
        a, b = k
        if a*Mb == b*Ma:
            res.append(v)
    return min(res) if res else -1


def main(args):
    N, Ma, Mb = map(int, input().split())
    items = [[int(i) for i in input().split()] for _ in range(N)]
    ans = solve(N, Ma, Mb, items)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):



import numpy as np

N,MA,MB = map(int,input().split())
U = N*10

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

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

for _ in range(N):
  a,b,c = map(int,input().split())
  temp[:] = dp.copy()
  temp[a:,b:] = np.minimum(temp[a:,b:], dp[:-a,:-b]+c)
  dp = temp
  
answer = INF
for t in range(1,401):
  a = MA*t
  b = MB*t
  if max(a,b) >= U:
    break
  answer = min(answer,dp[a,b])

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

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

N, Ma, Mb = map(int, input().split())
dp = [[inf] * (400 + 1) for _ in range(400 + 1)]
dp[0][0] = 0

max_a = 0
max_b = 0
for i in range(N):
    a, b, c = map(int, input().split())
    max_a += a
    max_b += b
    for j in reversed(range(max_a + 1 - a)):
        for k in range(max_b + 1 - b):
            if dp[j][k] == inf: continue
            dp[j + a][k + b] = min(dp[j][k] + c, dp[j + a][k + b])

j = Ma
k = Mb
ans = inf
while j < 400 + 1 and k < 400 + 1:
    ans = min(ans, dp[j][k])
    j += Ma
    k += Mb

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

Solution (Python):
def solve():
    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 = sum(c)
    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):
        for j in range(10*N+1-a[i]):
            for k in range(10*N+1-b[i]):
                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 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:
        ans = -1

    print(ans)


solve()

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

INF = 10 ** 18
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 = map(int, input().split())
    for j in range(10*N+1-a):
        for k in range(10*N+1-b):
            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 j in range(1, 10*N+1):
    if j * Ma > 10 * N  or j * Mb > 10 * N:
        break

    ans = min(ans, dp[N][j*Ma][j*Mb])

if ans == INF:
    ans = -1

print(ans)

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

N_max = 40
a_max = 10
b_max = 10
c_max = 100

inf = N_max * c_max + 1
dp = [[inf] * (N_max * b_max + 1) for _ in range(N_max * a_max + 1)]
dp[0][0] = 0

for i, (aa, bb, cc) in enumerate(e):
    for a in reversed(range(a_max * i + 1)):
        for b in reversed(range(b_max * i + 1)):
            if dp[a][b] == inf: continue
            if a + aa < N_max * a_max + 1 and b + bb < N_max * b_max + 1:
                dp[a + aa][b + bb] = min(dp[a + aa][b + bb], dp[a][b] + cc)

ans = inf
for i in range(1, N_max * a_max + 1):
    if i * Ma >= N_max * a_max + 1 or i * Mb >= N_max * b_max + 1: break
    
    
    ans = min(ans, dp[i * Ma][i * Mb])

print(ans if ans < inf else -1)

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

N_max = 40
a_max = 10
b_max = 10
c_max = 100

inf = N_max * c_max + 1
dp = [[[inf for _ in range(N_max * b_max + 1)] for _ in range(N_max * a_max + 1)] for _ in range(N_max + 1)]
dp[0][0][0] = 0



for i, (aa, bb, cc) in enumerate(e, 1):
    for a in range(a_max * i + 1):
        for b in range(b_max * i + 1):
            dp[i][a][b] = min(dp[i][a][b], dp[i - 1][a][b])
            if a + aa < N_max * a_max + 1 and b + bb < N_max * b_max + 1:
                dp[i][a + aa][b + bb] = min(dp[i][a + aa][b + bb], dp[i - 1][a][b] + cc)



ans = inf
for a in range(N_max * a_max + 1):
    for b in range(N_max * b_max + 1):
        
        
        if b * Ma == a * Mb and a * b > 0:
            ans = min(ans, dp[N][a][b])

print(ans if ans < inf else -1)

Solution (Python):
N,Ma,Mb = map(int,input().split())
abc = [list(map(int,input().split())) for _ in range(N)]
inf = 10**9
dp = [[inf for i in range(N*10+1)] for i in range(N*10+1)]
dp[0][0] = 0
max_a = 0
max_b = 0
for i in range(N):
    max_a += abc[i][0]
    max_b += abc[i][1]
    for ai in range(abc[i][0],max_a+1)[::-1]:
        for bi in range(abc[i][1],max_b+1)[::-1]:
            dp[ai][bi] = min(dp[ai][bi],dp[ai-abc[i][0]][bi-abc[i][1]]+abc[i][2])
ans = inf
for i in range(1,min(max_a//Ma,max_b//Mb)):
    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 = []
for _ in range(N):
    a, b, c = map(int, input().split())
    ABC.append((a, b, c))

import sys
INT_MAX = sys.maxsize


drugstores = ABC[:]
drugstores = [(c, a, b) for a,b,c in drugstores]
drugstores = sorted(drugstores)


mix_list = [(0, 0, 0)]

ans = INT_MAX
import heapq
for d_c, d_a, d_b in drugstores:
    for m_c, m_a, m_b in mix_list[:]:
        a = d_a + m_a
        b = d_b + m_b
        c = d_c + m_c
        
        if a * Mb == b * Ma:
            
            ans = min(ans, c)
        elif ans > c:
            heapq.heappush(mix_list, (c, a, b))

if ans == INT_MAX:
    ans = -1
print(ans)

Solution (Python):
N,*M = map(int,input().split())
abc = [list(map(int,input().split())) for _ in [0]*N]
INF = 100*40*100
dp = [[[INF]*500 for _ in [0]*500] 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(410):
        for k in range(410):
            dp[i][j][k] = min(dp[i-1][j][k],dp[i-1][j-a][k-b]+c)
ans = min([dp[-1][M[0]*i][M[1]*i] for i in range(1,1+400//(max(M)))])
if ans == INF:ans=-1
print(ans)

Solution (Python):
n, ma, mb = map(int,input().split())
INF = 10**9
dp = {(0,0):0}
for _ in range(n):
  a, b, c = map(int, input().split())
  
  for (ea, eb), ec in dp.copy().items():
    dp[ea+a,eb+b] = min(dp.get((ea+a,eb+b),INF), ec+c)
    
ans = min(dp.get((i*ma,i*mb),INF) for i in range(1,401))
print(ans if ans != INF else -1)

Solution (Python):
import sys
from functools import lru_cache

sys.setrecursionlimit(2*10**5)


N, Ma, Mb = map(int, input().split())
a, b, c = (
    zip(*(map(int, input().split()) for _ in range(N))) if N else
    ((), ())
)



@lru_cache(maxsize=None)
def f(i, x, y):
    return (
        10**18 if (i < 0 and not (x == y == 0)) or x < 0 or y < 0 else
        0 if x == 0 and y == 0 else
        min(
            f(i - 1, x - a[i - 1], y - b[i - 1]) + c[i - 1],
            f(i - 1, x, y)
        )
    )



A = sum(a)
B = sum(b)
opt = min(
    f(N, x, Mb * (x // Ma))
    for x in range(Ma, A + 1)
    if x % Ma == 0 and Mb * (x // Ma) <= B
)
ans = (
    opt if opt < 10**18 else
    -1
)


print(ans)

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


dp = [[[float('inf')] * 401 for j in range(401)] for i in range(N + 1)]
dp[0][0][0] = 0
for i, (a, b, c) in enumerate(Drug):
    for ma in range(401):
        for mb in range(401):
            if (ma + a < 401) and (mb + b < 401):
                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, 401):
    if (Ma * i) < 401 and (Mb * i) < 401:
        ans = min(ans, dp[-1][Ma * i][Mb * i])
    else:
        break

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

Solution (Python):
n, ma, mb = map(int, input().split())
x = [list(map(int, input().split())) for _ in range(n)]
a_limit = [0]
b_limit = [0]
for i in range(n):
    a_limit.append(a_limit[-1] + x[i][0])
    b_limit.append(b_limit[-1] + x[i][1])

INF = 10 ** 9
DP = [None for _ in range(n + 1)]
for i in range(n + 1):
    DP[i] = [[INF for k in range(b_limit[-1] + 1)] for j in range(a_limit[-1] + 1)]

DP[0][0][0] = 0
for i in range(n):
    for j in range(a_limit[i] + 1):
        for k in range(b_limit[i] + 1):
            DP[i + 1][j][k] = min(DP[i + 1][j][k], DP[i][j][k])
            DP[i + 1][j + x[i][0]][k + x[i][1]] = min(DP[i + 1][j + x[i][0]][k + x[i][1]], DP[i][j][k] + x[i][2])

ans = INF
for j in range(1, a_limit[-1] + 1):
    for k in range(1, b_limit[-1] + 1):
        if k * ma == j * mb:
            ans = min(ans, DP[-1][j][k])
if ans == INF:
    print(-1)
else:
    print(ans)