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 = []
for _ in range(N):
  a,b,c = map(int, input().split())
  A.append([a,b,c])
  







gram_max = 410
cost_inf = 10000
dp = [[[ cost_inf for _ in range(gram_max)] for _ in range(gram_max)] for _ in range(N+1)]

dp[0][0][0] = 0
for i in range(1,N+1):
  a,b,c = A[i-1]
  for ca in range(0,gram_max):
    for cb in range(0,gram_max):
      dp[i][ca][cb] = min(dp[i][ca][cb], dp[i-1][ca][cb]) 
      if ca-a >= 0 and cb-b >= 0:
        dp[i][ca][cb] = min(dp[i][ca][cb], dp[i-1][ca-a][cb-b] + c) 
        
ans = 10000
for ca in range(1,gram_max):
  for cb in range(1,gram_max):
    
    if MA*cb == MB*ca:
      ans = min(ans, dp[N][ca][cb])
      
print(ans if ans < 5000 else -1)

Solution (Python):
n,p,q=map(int,input().split())
M2=-10*(-n//2)
M1=10*(n//2)
dp1=[[float("INF")]*(M1+1)for i in range(M1+1)]
dp1[0][0]=0
for _ in range(n//2):
    a,b,c=map(int,input().split())
    for i in range(M1,a-1,-1):
        for j in range(M1,b-1,-1):
            dp1[i][j]=min(dp1[i][j],dp1[i-a][j-b]+c)
dp2=[[float("INF")]*(M2+1)for i in range(M2+1)]
dp2[0][0]=0
for _ in range(-(-n//2)):
    a,b,c=map(int,input().split())
    for i in range(M2,a-1,-1):
        for j in range(M2,b-1,-1):
            dp2[i][j]=min(dp2[i][j],dp2[i-a][j-b]+c)
ans=float("INf")
for i in range(M1+1):
    for j in range(M1+1):
        for c in range(max(1,-min(-i//p,-j//q)),M2//max(p,q)+1):
            ans=min(ans,dp1[i][j]+dp2[c*p-i][c*q-j])
print(ans if ans!= float("INF") else -1)

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

N, Ma, Mb, *ABC = map(int, open(0).read().split())

M = 160010

dp = [0] + [INF] * M
for a, b, c in zip(*[iter(ABC)] * 3):
    T = dp[:]
    p = 401 * a + b
    for i, dpi in enumerate(dp):
        j = p + i
        if j > M:
            break
        if T[j] > dpi + c:
            T[j] = dpi + c
    dp = T

ans = INF
for i in range(1, M):
    a, b = divmod(i, 401)
    if Ma * b == Mb * a and ans > dp[i]:
        ans = dp[i]

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

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, x, y = map(int, readline().split())
    mat = []

    for _ in range(n):
        a, b, c = map(int, readline().split())
        mat.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(1, n + 1):
        a, b, c = mat[i - 1]
        for j in range(401):
            for k in range(401):
                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 = INF
    for i, j in zip(range(x, 401, x), range(y, 401, y)):
        ans = min(ans, dp[n][i][j])

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


if __name__ == '__main__':
    main()

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

n, ma, mb = map(int, input().split())
abc = [list(map(int, input().split())) for _ in range(n)]

dp = [5000 for i in range(170000)]
dp[0] = 0

for a, b, c in abc:
    dpn = dp[:]
    for i in range(170000):
        nex = 401 * b + a + i
        if nex >= 170000:
            break
        dpn[nex] = min(dpn[nex], dp[i] + c)
    dp = dpn

ans = 50000

for i in range(1,170000):
    a = i % 401
    b = i // 401
    if ma * b == mb * a:
        ans = min(ans, dp[i])

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

Solution (Python):
N, Ma, Mb = map(int,input().split())
L = []
for _ in range(N):
    L.append( list(map(int,input().split())) )
dp = [[[10**10 for _ in range(601)] for _ in range(601)] for _ in range(N+10)]
dp[0][0][0] = 0

for l in range(N):
    for i in range(200):
        for j in range(200):
            dp[l+1][i][j] = min(dp[l+1][i][j],dp[l][i][j])
            dp[l+1][i+L[l][0]][j+L[l][1]] = min(dp[l+1][i+L[l][0]][j+L[l][1]],dp[l][i][j] + L[l][2])


ans = 10**10
for i in range(1,10000):
    try: ans = min(ans, dp[N][Ma*i][Mb*i])
    except: pass
if ans == 10**10: print(-1)
else: print(ans)

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

INF = 10 ** 9

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

delta_c = [(Mb * a - Ma * b, c) for a, b, c in abcs]

cost = INF
positives = []
negatives = []
for delta, c in delta_c:
    if delta == 0:
        cost = min(cost, c)
    elif delta > 0:
        positives.append((delta, c))
    else:
        negatives.append((-delta, c))

dp_pos = np.full(401, INF, np.int64)
dp_pos[0] = 0
for delta, c in positives:
    dp_pos[delta:] = np.minimum(dp_pos[:-delta] + c, dp_pos[delta:])

dp_neg = np.full(401, INF, np.int64)
dp_neg[0] = 0
for delta, c in negatives:
    dp_neg[delta:] = np.minimum(dp_neg[:-delta] + c, dp_neg[delta:])

cost = min(cost, np.amin(dp_pos[1:] + dp_neg[1:]))
if cost >= INF:
    print(-1)
else:
    print(cost)

Solution (Python):
n,ma,mb=map(int,input().split())
abc=[list(map(int,input().split())) for _ in range(n)]
import numpy as np
dp=np.full((401,401),4001,int)
dp[0,0]=0
for a,b,c in abc:
  dp[a:,b:]=np.minimum(dp[a:,b:],dp[:-a,:-b]+c)
ma_,mb_=ma,mb
ans=4001
while max(ma_,mb_)<401:
  ans=min(ans,dp[ma_,mb_])
  ma_+=ma
  mb_+=mb
print(ans if ans<4001 else -1)

Solution (Python):


import copy

LI = lambda: list(map(int, input().split()))


N, Ma, Mb = LI()
ABC = [LI() for _ in range(N)]

INF = 10 ** 5


def main():
    w = [[INF] * 401 for _ in range(401)]
    w[0][0] = 0
    for a, b, c in ABC:
        l = copy.deepcopy(w)
        for j in range(a, 401):
            for k in range(b, 401):
                l[j][k] = min(w[j][k], w[j - a][k - b] + c)
        w = l

    ans = INF
    n = 400 // max(Ma, Mb) + 1
    for i in range(1, n):
        ans = min(ans, w[Ma * i][Mb * i])
    ans = -1 if ans == INF else ans
    print(ans)


if __name__ == "__main__":
    main()

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

ans = INF
i = 1
while Ma * i <= 10 * N and Mb * i <= 10 * N:
    ans = min(ans, dp[N][Ma*i][Mb*i])
    i += 1

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

Solution (Python):
def main():
    n, ma, mb = map(int, input().split())
    arr = [list(map(int, input().split())) for _ in range(n)]
    dp = [{} for _ in range(n+1)]
    dp[0][(0, 0)] = 0
    for i in range(n):
        for ky in dp[i].keys():
            if ky in dp[i+1]:
                if dp[i+1][ky] > dp[i][ky]:
                    dp[i+1][ky] = dp[i][ky]
            else:
                dp[i+1][ky] = dp[i][ky]
            t = (ky[0]+arr[i][0], ky[1]+arr[i][1])
            if t in dp[i+1]:
                if dp[i+1][t] > dp[i][ky]+arr[i][2]:
                    dp[i+1][t] = dp[i][ky]+arr[i][2]
            else:
                dp[i+1][t] = dp[i][ky]+arr[i][2]
    ans = float("inf")
    for i in range(1, 41):
        t = (ma*i, mb*i)
        if t in dp[n]:
            if ans > dp[n][t]:
                ans = dp[n][t]
    if ans == float("inf"):
        print("-1")
    else:
        print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
N, Ma, Mb = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]
DP = [0]*(N+1)
for i in range(N+1):
    DP[i] = [[float("inf")]*(10*N+1) for i in range(10*N+1)]
for i in range(N+1):
    DP[i][0][0] = 0
for i in range(N):
    for j in range(1, 10*N+1):
        for k in range(1,10*N+1):
            if DP[i+1][j][k] > DP[i][j-A[i][0]][k-A[i][1]]+A[i][2] and j-A[i][0] >= 0 and k-A[i][1] >= 0:
               DP[i+1][j][k] = min(DP[i][j-A[i][0]][k-A[i][1]]+A[i][2], DP[i][j][k])
            else:
                DP[i+1][j][k] = DP[i][j][k]
kouho = set()
for i in range(1, 10*N+1):
    if Ma*i < 10*N+1 and Mb*i < 10*N+1:
        if DP[N][Ma*i][Mb*i] == float("inf"):
            continue
        else:
            kouho.add(DP[N][Ma*i][Mb*i])
    else:
        break
if len(kouho) == 0:
    print(-1)
else:
    print(min(kouho))

Solution (Python):
def main():
    n, ma, mb = map(int, input().split())
    ABC = []
    for i in range(n):
        a, b, c = map(int, input().split())
        ABC.append((a, b, c))

    INF = 10**18
    dp = [[[INF]*405 for i in range(405)] for j in range(n+1)]
    dp[0][0][0] = 0
    for i in range(n):
        a, b, c = ABC[i]
        for j in range(405):
            for k in range(405):
                dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k])
                nj, nk = j+a, k+b
                if 1 <= nj <= 404 and 1 <= nk <= 404:
                    dp[i+1][nj][nk] = min(dp[i][nj][nk], dp[i][j][k]+c)

    import fractions
    ans = INF
    for j in range(1, 405):
        for k in range(1, 405):
            if ma*k == mb*j:
                ans = min(ans, dp[n][j][k])
    if ans == INF:
        print(-1)
    else:
        print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import math

import queue
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n,ma,mb = map(int,ipt().split())
    dp = [[10**18]*(n*10+1) for _ in [0]*(n*10+1)]
    dp[0][0] = 0
    for _ in range(n):
        a,b,c = map(int,ipt().split())
        for i in range(n*10+1-a):
            dpi = dp[n*10-i-a]
            dpni = dp[n*10-i]
            for j in range(n*10+1-b):
                if dpni[n*10-j] > dpi[n*10-j-b]+c:
                    dpni[n*10-j] = dpi[n*10-j-b]+c
    mi = 10**18
    for i in range(1,n*10//max(ma,mb)+1):
        m = dp[ma*i][mb*i]
        if m < mi:
            mi = m
    if mi == 10**18:
        print(-1)
    else:
        print(mi)
    return

if __name__ == '__main__':
    main()

Solution (Python):
n, ma, mb = map(int, input().split())
r = [0] * n
c = [0] * n
for i in range(n):
  a, b, c[i] = map(int, input().split())
  r[i] = a * mb - b * ma
dp = [[10 ** 9] * 4001 for i in range(n + 1)]
for i in range(n):
  for j in range(4001):
    dp[i+1][j] = min(dp[i+1][j], dp[i][j])
    if j == 2000:
      dp[i+1][j+r[i]] = min(dp[i+1][j+r[i]], c[i])
    if 0 <= j + r[i] <= 4000:
      dp[i+1][j+r[i]] = min(dp[i+1][j+r[i]], dp[i][j] + c[i])
if dp[n][2000] < 10 ** 9:
  print(dp[n][2000])
else:
  print(-1)

Solution (Python):
N, Ma, Mb = map(int, input().split())
abc = [list(map(int, input().split())) for i in range(N)]
INF = 10**5
n = 10*N + 1
dp = [[[INF]*n for i in range(n)] for j in range(N+1)]
dp[0][0][0] = 0

for i, (a, b, c) in enumerate(abc, 1):
  for j in range(n):
    for k in range(n):
      dp[i][j][k] = dp[i-1][j][k]
      if a <= j and b <= k:
        dp[i][j][k] = min(dp[i][j][k], dp[i-1][j-a][k-b]+c)

ans = INF
dp[-1][0][0] = INF
for j in range(n):
  for k in range(n):
    if j * Mb == k*Ma:
      ans = min(ans, dp[-1][j][k])

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

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    INF = 10**5
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    n,ma,mb = LI()
    med = [LI() for _ in range(n)]
    h1 = [INF] * 10000

    for i in range(1,2 ** (n//2)):
        bit = 1
        ta = 0
        tb = 0
        tc = 0
        for j in range(n//2):
            if i & bit:
                a,b,c = med[j]
                ta += a
                tb += b
                tc += c
            bit <<= 1
        dif = ta * mb - tb * ma
        if h1[dif] > tc: h1[dif] = tc

    ans = h1[0]

    for i in range(1,2 ** (n - n//2)):
        bit = 1
        ta = 0
        tb = 0
        tc = 0
        for j in range(n - n//2):
            if i & bit:
                a,b,c = med[j+n//2]
                ta += a
                tb += b
                tc += c
            bit <<= 1
        dif = tb * ma - ta * mb
        if dif == 0:
            ans = min(ans,tc)
        else:
            ans = min(ans,h1[dif]+tc)

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

if __name__ == '__main__':
    main()

Solution (Python):
def resolve():
    import sys
    input = sys.stdin.readline
    
    
    
    n, ma, mb = map(int, input().split())
    
    
    
    
    
    A = [list(map(int, input().split())) for i in range(n)]

    Cost = [[[5000] * 401 for _ in range(401)] for _ in range(n+1)]
    Cost[0][0][0] = 0

    for i in range(n):
        for j in range(400):
            for k in range(400):
                if j+A[i][0] <= 400 and k+A[i][1] <= 400:
                    Cost[i+1][j+A[i][0]][k+A[i][1]] = min(Cost[i+1][j+A[i][0]][k+A[i][1]], Cost[i][j][k]+A[i][2])
                Cost[i+1][j][k] = min(Cost[i+1][j][k], Cost[i][j][k])

    costmin = 5000
    for a in range(1,401):
        for b in range(1,401):
            if a * mb == b * ma:
                costmin = min(costmin, Cost[n][a][b])
    if costmin == 5000:
        print(-1)
    else:
        print(costmin)


resolve()

Solution (Python):

import sys
input = sys.stdin.readline









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


inf = float("inf")
dp = [[inf for _ in range(401)] for _ in range(401)]
dp[0][0] = 0

for a,b,c in ABC:
    for i in range(400,-1,-1):
        for j in range(400,-1,-1):
            if dp[i][j] != inf:
                
                dp[i+a][j+b] = min(dp[i+a][j+b], dp[i][j] + c)

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

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

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

kusuri = []
cusum = [0]*(N+1)
for i in range(N):
  a, b, c = map(int, input().split())
  sa = a*Mb - b*Ma
  kusuri.append((sa, c))
  cusum[i+1] = cusum[i] + c

call = cusum[-1]
dp = [[call+1]*8001 for _ in range(41)]
dp[0][4000] = 0

cmin = call+1
for i, j in enumerate(kusuri):
  d = j[0]
  c = j[1]
  for k in range(4000-100*i, 4000+100*i+1):
    dp[i+1][k] = min(dp[i+1][k], dp[i][k])
    if k + d == 4000:
      cmin = min(cmin, dp[i][k]+c)
    dp[i+1][k+d] = min(dp[i+1][k+d], dp[i][k]+c)
    

if cmin == call+1:
  print(-1)
else:
  print(cmin)

Solution (Python):
import sys
input = sys.stdin.readline
n, ma, mb = map(int, input().split())
inf = 10000
med = [tuple(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 k in range(n):
    for i in range(401):
        for j in range(401):
            a, b, c = med[k]
            if i - a >= 0 and j - b >= 0:
                dp[k+1][i][j] = min(dp[k][i][j], dp[k][i-a][j-b] + c)
            else:
                dp[k+1][i][j] = dp[k][i][j]

ans = inf
for k in range(1, 401):
    if k * ma > 400 or k * mb > 400:
        break
    x, y = k * ma, k * mb
    ans = min(ans, dp[n][x][y])

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

Solution (Python):
import itertools

N,MA,MB = map(int,input().split())
arr = []

for i in range(N):
  l = list(map(int,input().split()))
  arr.append(l)
  
dp = [ [ [50000 for k in range(401)] for j in range(401)] for i in range(N+1) ]
dp[0][0][0] = 0
ans = 50000

for i,a,b in itertools.product(range(1,N+1),range(401),range(401)):
  A = arr[i-1][0]
  B = arr[i-1][1]
  C = arr[i-1][2]
  
  if A <= a and B <= b and dp[i-1][a-A][b-B] != 50000:
    dp[i][a][b] = min(dp[i-1][a][b],dp[i-1][a-A][b-B] + C)
  else:
    dp[i][a][b] = dp[i-1][a][b]

for a,b in itertools.product(range(401),range(401)):
    if a * MB == b * MA and a != 0 and b != 0:
      ans = min(ans,dp[-1][a][b])
    
if ans == 50000:
  print(-1)
else:
  print(int(ans))

Solution (Python):


import sys






























def main():
    mod = 1000000007                
    inf = float('inf')              
    
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    
    
    n, ma, mb = mi()
    a_list = []
    b_list = []
    c_list = []
    for _ in range(n):
        a, b, c = mi()
        a_list.append(a)
        b_list.append(b)
        c_list.append(c)
    
    

    
    dp = [[inf] * (sum(b_list) + 1) for _ in range(sum(a_list) + 1)]
    dp[0][0] = 0
    for i in range(n):
        for x in range(sum(a_list), 0, -1):
            for y in range(sum(b_list), 0, -1):
                i_a, i_b, i_c = a_list[i], b_list[i], c_list[i]
                if x - i_a >= 0 and y - i_b >= 0:
                    dp[x][y] = min(dp[x][y], dp[x-i_a][y-i_b] + i_c)

    
    
    
    ans = inf
    mul = min(sum(a_list) // ma, sum(b_list) // mb)
    for i in range(1, mul+1):
        
        ans = min(ans, dp[i * ma][i * mb])
    
    print(ans) if ans != inf else print(-1)
    


if __name__ == "__main__":
    main()

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

INF = 10**10
dp = [[[INF for _ in range(400+5)] for _ in range(400+5)] for _ in range(n+5)]

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

for i in range(n):
    a, b, c = map(int, input().split())
    for ta in range(400+5-a):
        for tb in range(400+5-b):
            dp[i+1][ta][tb] = min(dp[i+1][ta][tb], dp[i][ta][tb])
            dp[i+1][ta+a][tb+b] = min(dp[i+1][ta+a][tb+b], dp[i][ta][tb]+c)

ans = INF
for i in range(1, n+1):
    t = 1
    while max(ma, mb)*t <= 400:
        ans = min(ans, dp[i][ma*t][mb*t])
        t += 1

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

Solution (Python):
from collections import defaultdict

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

d = defaultdict(lambda: float('inf'))
d[(0, 0)] = 0
for a, b, c in M:
    new_d = d.copy()
    for key_a, key_b in d:
        new_d[(a + key_a, b + key_b)] = min(new_d[(a + key_a, b + key_b)],
                                            d[(key_a, key_b)] + c)
    d = new_d

ans = float('inf')
for i in range(1, 401 // max(Ma, Mb)):
    ans = min(ans, d[(Ma * i, Mb * i)])
print(ans if ans != float('inf') else -1)

Solution (Python):
n,ma,mb = map(int, input().split())
abc = [list(map(int, input().split())) for i in range(n)]
INF = 10 ** 18
dp = [[[INF] * 500 for i in range(500)] for x in range(n+1)]
dp[0][0][0] = 0
for k,(a,b,c) in enumerate(abc):
  for i in range(400):
    for j in range(400):
      dp[k+1][i][j] = min(dp[k+1][i][j],dp[k][i][j])
      dp[k+1][i+a][j+b] = min(dp[k][i+a][j+b],dp[k][i][j]+c)
ans = INF
for i in range(1,500):
  Ma,Mb = i * ma, i * mb
  if Ma > 400 or Mb > 400:
    break
  ans = min(ans,dp[n][Ma][Mb])
if ans == INF:
  print(-1)
else:
  print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    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())
    d={}
    ans=INF
    for i in range(2**(N//2)):
        ma,mb,cost=0,0,0
        for j in range(N//2):
            if (i>>j)&1:
                ma+=a[j]
                mb+=b[j]
                cost+=c[j]
        if ma!=0 and mb!=0 and ma*Mb==mb*Ma:
            ans=min(ans,cost)
        n=min(ma//Ma,mb//Mb)
        ma-=n*Ma
        mb-=n*Mb
        if (ma,mb) in d:
            d[(ma,mb)]=min(d[(ma,mb)],cost)
        else:
            d[(ma,mb)]=cost
    for i in range(1,2**(N-N//2)):
        ma,mb,cost=0,0,0
        for j in range(N-N//2):
            if (i>>j)&1:
                ma+=a[j+N//2]
                mb+=b[j+N//2]
                cost+=c[j+N//2]
        n=max(-(-ma//Ma),-(-mb//Mb))
        if (n*Ma-ma,n*Mb-mb) in d:
            ans=min(ans,cost+d[(n*Ma-ma,n*Mb-mb)])
    print(ans if ans!=INF else -1)

if __name__ == '__main__':
    main()

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

dp = [[[float("INF")]*(n*10+2) for i in range(n*10+2)] for j in range(n+1)]

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

for i in range(n):
    a,b,c = l[i]
    
    for j in range(n*10+1):
        for k in range(n*10+1):
            
            if j >= a and k >= b:
                dp[i+1][j][k] = min(dp[i][j][k],dp[i][j-a][k-b]+c)
            elif dp[i][j][k] != float("INF"):
                dp[i+1][j][k] = dp[i][j][k]
ans = float("INF")

for i in range(1,n*10+1):
    j = i*mb/ma
    if j%1 == 0 and j < n*10:
        ans = min(ans,dp[-1][i][int(j)])
if ans == float("INF"):
    print(-1)
else:
    print(ans)

Solution (Python):
MX = 401

N, Ma, Mb = map(int, input().split())
A = [0]*N
B = [0]*N
C = [0]*N
for i in range(N):
    a, b, c = map(int, input().split())
    A[i] = a
    B[i] = b
    C[i] = c
INF = 10**18
dp = [[[INF]*MX for i in range(MX)] for i in range(N+1)]
dp[0][0][0] = 0
for i in range(1, N+1):
    for j in range(0, MX):
        for k in range(0, MX):
            dp[i][j][k] = dp[i-1][j][k]
            if j < A[i-1] or k < B[i-1]: continue
            dp[i][j][k] = min(dp[i][j][k], dp[i-1][j-A[i-1]][k-B[i-1]]+C[i-1])
ans = INF
for i in range(1, MX):
    if Ma*i > MX-1 or Mb*i > MX-1: break
    ans = min(ans, dp[N][Ma*i][Mb*i])
if ans == INF:
    print(-1)
else:
    print(ans)

Solution (Python):


N,Ma,Mb=map(int,input().split())
D=[tuple(map(int,input().split())) for _ 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):
    a,b,c=D[i]
    for j in range(401):
        for k in range(401):
            if j>=a and k>=b:
                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=float("inf")
for i in range(1,401):
    if 0<Ma*i<=400 and 0<Mb*i<=400:
        ans=min(ans,dp[N][Ma*i][Mb*i])

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

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

abclist=[]
sum_a=sum_b=0
for _ in range(N):
  a,b,c=map(int,input().split())
  sum_a+=a
  sum_b+=b
  abclist.append((a,b,c))


dp=[[[float("inf")]*(sum_b+1) for _ in range(sum_a+1)] for _ in range(N+1)]
dp[0][0][0]=0
for i in range(1,N+1):
  a,b,c=abclist[i-1]
  for j in range(sum_a+1):
    for k in range(sum_b+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)
        

Ma_orig=Ma
Mb_orig=Mb
answer=float("inf")
while Ma<=sum_a and Mb<=sum_b:
  answer=min(answer,dp[N][Ma][Mb])
  Ma+=Ma_orig
  Mb+=Mb_orig
  
if answer==float("inf"):
  print(-1)
else:
  print(answer)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():

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

    INF = 10**10  
    I, J, K = N, sum(A), sum(B)
    dp = [[[INF]*(K+1) for _ in range(J+1)] for _ in range(I+1)]
    dp[0][0][0] = 0
    for i in range(1, I+1):
        for j in range(J+1):
            for k in range(K+1):
                
                if j >= A[i-1] and k >= B[i-1]:
                    dp[i][j][k] = min(dp[i][j][k], dp[i-1]
                                      [j-A[i-1]][k-B[i-1]]+C[i-1])
                dp[i][j][k] = min(dp[i][j][k], dp[i-1][j][k])
    ans = INF
    for j in range(1,J+1):
        for k in range(1,K+1):
            if k*Ma == j*Mb and k*Ma!=0 and j*Mb!=0:
                ans = min(ans, dp[I][j][k])

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


resolve()

Solution (Python):
N, Ma, Mb = map(int, input().split(' '))
med = []
for _ in range(N):
    med.append(tuple(map(int, input().split(' '))))
inf = 10 ** 9 + 7

dp = [[inf for _ in range(401)] for _ in range(401)]
dp[0][0] = 0

for k in range(N):
    (a, b, c) = med[k]
    for i in range(400, -1, -1):
        for j in range(400, -1, -1):
            if dp[i][j] != inf:
                dp[i + a][j + b] = min(dp[i + a][j + b], dp[i][j] + c)

ans = inf
for i in range(401):
    for j in range(401):
        if i == 0 or j == 0:
            continue
        if i * Mb == j * Ma:
            ans = min(ans, dp[i][j])
print(ans if ans != inf else -1)

Solution (Python):


N, Ma, Mb = map(int, input().split())
la, lb, lc = [], [], []
for _ in range(N):
    ai, bi, ci = map(int, input().split())
    la.append(ai)
    lb.append(bi)
    lc.append(ci)

sumA = sum(la)
sumB = sum(lb)

Inf = int(1e10)

dp = [[[Inf for _ in range(sumB + 1)] for _ in range(sumA + 1)] for _ in range(N + 1)]
dp[0][0][0] = 0


for i in range(1, N + 1):
    ai, bi, ci = la[i - 1], lb[i - 1], lc[i - 1]

    for j in range(sumA + 1):
        for k in range(sumB + 1):
            dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j][k])
            if j - ai >= 0 and k - bi >= 0 and dp[i - 1][j - ai][k - bi] != Inf:
                dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j - ai][k - bi] + ci)

ans = Inf
for j in range(1, sumA + 1):
    for k in range(1, sumB + 1):
        if Mb * j == Ma * k:
            ans = min(ans, dp[N][j][k])
if ans == Inf:
    print(-1)
else:
    print(ans)

Solution (Python):


N, Ma, Mb = map(int, input().split())
la, lb, lc = [], [], []
for _ in range(N):
    ai, bi, ci = map(int, input().split())
    la.append(ai)
    lb.append(bi)
    lc.append(ci)
sumA = sum(la)
sumB = sum(lb)

Inf = int(1e10)
dp = [[[Inf for _ in range(sumB + 1)] for _ in range(sumA + 1)] for _ in range(N + 1)]
dp[0][0][0] = 0

for i in range(N):
    ai, bi, ci = la[i], lb[i], lc[i]

    for j in range(sumA + 1):
        for k in range(sumB + 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+ai][k+bi] = min(dp[i+1][j+ai][k+bi], dp[i][j][k]+ci)


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

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

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

INF = 10**4
dp = [[[INF] * (n*10+1) for _ in range(n*10+1)] for _ in range(n+1)]
dp[0][0][0] = 0

for i in range(n):
    a,b,c = map(int,input().split())
    for ai in range(i*10+11):
        for bi in range(i*10+11):
            if ai>=a and bi>=b:
                tmp = dp[i][ai-a][bi-b] + c
            else:
                tmp = INF
            dp[i+1][ai][bi] = min(dp[i][ai][bi], tmp)

ans = min(dp[n][ma*i][mb*i] for i in range(1,n*10//max(ma,mb)+1) )
if ans == INF: ans = -1
    
print(ans)

Solution (Python):
N, A, B = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(N)]

inf = 10 ** 9 + 7
dp = [[[inf] * 401 for _ in range(401)] for _ 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 >= X[i][0] and k >= X[i][1]:
                dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k],
                                      dp[i][j - X[i][0]][k - X[i][1]] + X[i][2])
            else:
                dp[i + 1][j][k] = min(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 * B == k * A:
            ans = min(ans, dp[-1][j][k])

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

Solution (Python):
def half_enumeration(a, *, func):
    """正しい英訳は不明"""
    n = len(a)
    h = n // 2

    def enum(h, *, second_half: bool = False) -> set:
        p = set()
        if second_half:
            b = a[h:]
        else:
            b = a[:h]

        for x in b:
            q = set()
            for e in p:
                q.add(func(e, x))
            p.add(x)
            p.update(q)
        return p

    return enum(h), enum(h, second_half=True)


def main():
    INF = 40 * 100 + 1

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

    def update(e, x):
        return e[0] + x[0], e[1] + x[1]

    xcs = []
    for _ in range(N):
        ai, bi, ci = map(int, input().split())
        x = Ma * bi - Mb * ai  
        xcs.append((x, ci))

    p1, p2 = half_enumeration(xcs, func=update)

    t1 = [INF] * (INF * 2 + 1)
    t2 = [INF] * (INF * 2 + 1)
    for x, c in p1:
        t1[x] = min(t1[x], c)
    for x, c in p2:
        t2[x] = min(t2[x], c)

    ans = min(t1[0], t2[0])
    for x in range(-INF, INF + 1):
        ans = min(ans, t1[x] + t2[-x])

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


if __name__ == '__main__':
    main()

Solution (Python):


def main():
    from collections import defaultdict

    INF = 40 * 100 + 1

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

    memo = defaultdict(lambda: INF)

    for _ in range(N):
        ai, bi, ci = map(int, input().split())
        x = Ma * bi - Mb * ai  

        for key, value in tuple(memo.items()):
            memo[key + x] = min(
                memo[key + x],
                value + ci
            )  
        memo[x] = min(memo[x], ci)  

    print(memo[0] if 0 in memo else -1)


if __name__ == '__main__':
    main()