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 = list(map(int, input().split()))
abc = [list(map(int, input().split())) for _ in range(N)]

an = 0
bn = 0
for a, b, c in abc:
  an += a
  bn += b

ma = max(an, bn) + 1

DP = [[10 ** 10] * ma for _ in range(ma)]

for i in range(N):
  a, b, c = abc[i]
  for j in range(ma - 1, -1, -1):
    for k in range(ma - 1, -1, -1):
      if j == 0 and k == 0:
        DP[a][b] = min(DP[a][b], c)
        continue
      if DP[j][k] == 10 ** 10: continue
      if j + a >= ma or k + b >= ma: continue
      DP[j + a][k + b] = min(DP[j + a][k + b], DP[j][k] + c)

ans = 10 ** 10
a, b = Ma, Mb
while a < ma and b < ma:
  ans = min(ans, DP[a][b])
  a += Ma
  b += Mb

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

Solution (Python):
from itertools import permutations
import sys

sys.setrecursionlimit(10 ** 6)
from bisect import *
from collections import *
from heapq import *

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def SI(): return sys.stdin.readline()[:-1]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
int1 = lambda x: int(x) - 1
def MI1(): return map(int1, sys.stdin.readline().split())
def LI1(): return list(map(int1, sys.stdin.readline().split()))
p2D = lambda x: print(*x, sep="\n")
dij = [(1, 0), (0, 1), (-1, 0), (0, -1)]

def main():
    inf=10**9
    n,ma,mb=MI()
    vv=[]
    cc=[]
    for _ in range(n):
        a,b,c=MI()
        vv.append(a*mb-b*ma)
        cc.append(c)
    dp=[[inf]*8005 for _ in range(n+1)]
    for i in range(n):
        v,c=vv[i],cc[i]
        dp[i + 1][4000+v] = min(dp[i + 1][4000+v], c)
        for j in range(8005):
            pre=dp[i][j]
            if pre==inf:continue
            dp[i + 1][j] = min(dp[i + 1][j], pre)
            dp[i + 1][j + v] = min(dp[i + 1][j + v], pre + c)
    ans=dp[-1][4000]
    if ans==inf:print(-1)
    else:print(ans)

main()

Solution (Python):

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,A,B = LI()
    G = LIR(n)
    ma = sum([a for (a,_,_) in G])
    mb = sum([b for (_,b,_) in G])
    dp = [float("inf")]*((ma+1)*mb+1)
    dp[0] = 0
    for a,b,c in G:
        for sa in range(ma)[::-1]:
            na = sa+a
            if na > ma:
                continue
            for sb in range(mb)[::-1]:
                nb = sb+b
                if nb > mb:
                    continue
                nd = dp[sa*mb+sb]+c
                i = na*mb+nb
                if nd < dp[i]:
                    dp[i] = nd
    ans = float("inf")
    for i in range(1,ma+1):
        s = B*i
        if s%A:
            continue
        j = B*i//A
        if j > mb:
            continue
        k = i*mb+j
        if dp[k] < ans:
            ans = dp[k]
    if ans == float("inf"):
        print(-1)
        return
    print(ans)
    return


if __name__ == "__main__":
    solve()

Solution (Python):

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,A,B = LI()
    G = LIR(n)
    ma = sum([a for (a,_,_) in G])
    mb = sum([b for (_,b,_) in G])
    dp = [[float("inf")]*(mb+1) for i in range(ma+1)]
    dp[0][0] = 0
    for a,b,c in G:
        for sa in range(ma)[::-1]:
            for sb in range(mb)[::-1]:
                na = sa+a
                if na > ma:
                    continue
                nb = sb+b
                if nb > mb:
                    continue
                nd = dp[sa][sb]+c
                if nd < dp[na][nb]:
                    dp[na][nb] = nd
    ans = float("inf")
    for i in range(1,ma+1):
        s = B*i
        if s%A:
            continue
        j = B*i//A
        if j > mb:
            continue
        if dp[i][j] < ans:
            ans = dp[i][j]
    if ans == float("inf"):
        print(-1)
        return
    print(ans)
    return


if __name__ == "__main__":
    solve()

Solution (Python):

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,A,B = LI()
    G = LIR(n)
    n1 = n>>1
    n2 = n-n1
    s1 = defaultdict(lambda : float("inf"))
    s2 = defaultdict(lambda : float("inf"))
    for j in range(1<<n1):
        sa = 0
        sb = 0
        sc = 0
        for i in range(n1):
            if j&(1<<i):
                a,b,c = G[i]
                sa += a
                sb += b
                sc += c
        if s1[(sa,sb)] > sc:
            s1[(sa,sb)] = sc
    m = 1
    for j in range(1<<n2):
        sa = 0
        sb = 0
        sc = 0
        for i in range(n2):
            if j&(1<<i):
                a,b,c = G[i+n1]
                sa += a
                sb += b
                sc += c
        if sa > m:
            m = sa
        if s2[(sa,sb)] > sc:
            s2[(sa,sb)] = sc
    ans = float("inf")
    for (a,b),c in s1.items():
        for i in range(m+1):
            s = B*(i+a)
            if s%A:
                continue
            j = (s//A)-b
            nc = s2[(i,j)]
            cost = c+nc
            if a == b == i == j:
                continue
            if cost < ans:
                ans = cost
    if ans == float("inf"):
        print(-1)
        return
    print(ans)
    return


if __name__ == "__main__":
    solve()

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

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

cmax = N * 10
t = [[INF] * (cmax + 1) for _ in range(cmax + 1)]
for _ in range(N):
    a, b, c = map(int, input().split())
    for aa in range(cmax, 0, -1):
        for bb in range(cmax, 0, -1):
            if t[aa][bb] == INF:
                continue
            t[aa + a][bb + b] = min(t[aa + a][bb + b], t[aa][bb] + c)
    t[a][b] = min(t[a][b], c)

result = INF
for a in range(1, cmax):
    for b in range(1, cmax):
        if a * Mb == b * Ma:
            result = min(result, t[a][b])
if result == INF:
    result = -1
print(result)

Solution (Python):
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]

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

dp=[]
for i in range(n+1):
    tmp=[]
    for j in range(401):
        tmp2=[]
        for k in range(401):
            tmp2.append(float('inf'))
        tmp.append(tmp2)
    dp.append(tmp)
dp[0][0][0]=0
for i in range(n):
    a,b,c=abc[i]
    for j in range(400):
        for k in range(400):
            if dp[i][j][k]!=float('inf'):
                dp[i+1][j+a][k+b]=min(dp[i+1][j+a][k+b],dp[i][j][k]+c)
                dp[i+1][j][k]=min(dp[i+1][j][k],dp[i][j][k])
ans=float('inf')
now=[ma,mb]
while 1:
    a,b=now
    ans=min(ans,dp[n][a][b])
    if a+ma>400 or b+mb>400:
        break
    now=[a+ma,b+mb]
if ans==float('inf'):
    print(-1)
else:
    print(ans)

Solution (Python):
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]

n,ma,mb=map(int,input().split())
abc=[list(map(int,input().split())) for i in range(n)]
dp=[[[float('inf')]*(400+1) for i in range(400+1)] 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(400):
        for k in range(400):
            if dp[i][j][k]!=float('inf'):
                dp[i+1][j+a][k+b]=min(dp[i+1][j+a][k+b],dp[i][j][k]+c)
                dp[i+1][j][k]=min(dp[i+1][j][k],dp[i][j][k])
ans=float('inf')
now=[ma,mb]
while 1:
    a,b=now
    ans=min(ans,dp[n][a][b])
    if a+ma>400 or b+mb>400:
        break
    now=[a+ma,b+mb]
if ans==float('inf'):
    print(-1)
else:
    print(ans)

Solution (Python):
n,MA,MB=map(int,input().split())
INF=10**10
A=[]
B=[]
C=[]
for i in range(n):
  a,b,c=map(int,input().split())
  if MA*b-MB*a>0:
    A.append([MA*b-MB*a,c])
  elif MA*b-MB*a<0:
    B.append([MB*a-MA*b,c])
  else:
    C.append(c)
dp1=[[INF]*10010 for i in range(len(A)+1)]
for i in range(1,len(A)+1):
  for j in range(1,10010):
    if j<A[i-1][0]:
      dp1[i][j]=dp1[i-1][j]
    elif j==A[i-1][0]:
      dp1[i][j]=min(A[i-1][1],dp1[i-1][j])
    else:
      dp1[i][j]=min(dp1[i-1][j],dp1[i-1][j-A[i-1][0]]+A[i-1][1])
dp2=[[INF]*10010 for i in range(len(B)+1)]
for i in range(1,len(B)+1):
  for j in range(1,10010):
    if j<B[i-1][0]:
      dp2[i][j]=dp2[i-1][j]
    elif j==B[i-1][0]:
      dp2[i][j]=min(B[i-1][1],dp2[i-1][j])
    else:
      dp2[i][j]=min(dp2[i-1][j],dp2[i-1][j-B[i-1][0]]+B[i-1][1])
ans=INF
for i in range(10010):
  a=dp1[-1][i]+dp2[-1][i]
  if a<ans:
    ans=a
for i in range(len(C)):
  if C[i]<ans:
    ans=C[i]
if ans<INF:
  print(ans)
else:
  print(-1)

Solution (Python):

INF = float('inf')

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

cmax = N * 10
t = [[INF] * (cmax + 1) for _ in range(cmax + 1)]
for _ in range(N):
    a, b, c = map(int, input().split())
    for aa in range(cmax, 0, -1):
        for bb in range(cmax, 0, -1):
            if t[aa][bb] == INF:
                continue
            t[aa + a][bb + b] = min(t[aa + a][bb + b], t[aa][bb] + c)
    t[a][b] = min(t[a][b], c)

result = INF
for a in range(1, cmax):
    for b in range(1, cmax):
        if a * Mb == b * Ma:
            result = min(result, t[a][b])
if result == INF:
    result = -1
print(result)

Solution (Python):
n, A, B = map(int, input().split())
z = [tuple(map(int, input().split())) for _ in range(n)]

inf = 10 ** 18

d = {(0, 0):0}
for a, b, cost in z:
    newd = []
    for (x, y), c in d.items():
        new = (a+x, b+y)
        if new not in d or d[new] > c + cost:
            newd.append((new, c+cost))
    for new, newc in newd:
        d[new] = newc

ans = inf
for i in range(1, 10 * n):
    t = (A * i, B * i)
    if t in d:
        ans = min(ans, d[t])
if ans == inf:
    print(-1)
else:
    print(ans)

Solution (Python):
INF = 10**18

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

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

for i in range(N):
    a, b, c = map(int, input().split())
    for j in range(400):
        for k in range(400):
            dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k])
            if j + a <= 400 and k + b <= 400:
                dp[i + 1][j + a][k + b] = min(dp[i + 1][j + a][k + b], dp[i][j][k] + c)

res = INF

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

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

Solution (Python):
INF = 10**18

def solve(N, Ma, Mb, sol):
    if N % 2 == 1:
        sol.append((0, 0, 0))
        N += 1

    M = N // 2

    sol1 = sol[:M]
    sol2 = sol[M:]

    S1 = []
    D = dict()
    res = INF

    for i in range(2**M):
        A1, B1, C1 = 0, 0, 0
        A2, B2, C2 = 0, 0, 0
        for j in range(M):
            if (i >> j) & 1:
                a1, b1, c1 = sol1[j]
                a2, b2, c2 = sol2[j]
                A1 += a1
                B1 += b1
                C1 += c1
                A2 += a2
                B2 += b2
                C2 += c2
        S1.append((A1, B1, C1))
        x = Mb * A2 - Ma * B2
        if x == 0 and C2 > 0:
            res = min(res, C2)
        if not x in D or 0 < C2 < D[x]:
            D[x] = C2

    for i in range(2**M):
        a, b, c = S1[i]
        x = Ma * b - Mb * a
        if x == 0 and c > 0:
            res = min(res, c)
        if x in D and D[x] + c > 0:
            res = min(res, D[x] + c)

    return res

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

res = solve(N, Ma, Mb, sol)

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

Solution (Python):
import sys
input = sys.stdin.buffer.readline
n,ma,mb=map(int,input().split())
ABC=[list(map(int,input().split())) for _ in range(n)]
infi=10**15
dp=[[[infi]*(401) for _ in range(401)]for _ in range(41)]
dp[0][0][0]=0
for i in range(n):
    a,b,c=ABC[i]
    for j in range(401-a):
        for k in range(401-b):
            ni=i+1
            nj=j+a
            nk=k+b
            dp[ni][j][k]=min(dp[ni][j][k], dp[i][j][k])
            dp[ni][nj][nk]=min(dp[i][j][k]+c,dp[ni][nj][nk])

ans=infi
for i in range(1,401):
    if ma*i>400:
        break
    if mb*i>400:
        break    
    ans=min(ans, dp[n][ma*i][mb*i])
print(ans if ans!=infi else -1)

Solution (Python):




from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys





N,Ma,Mb=map(int,input().split())
ABC=[list(map(int,input().split())) for i in range(N)]
idle_max=10**9
dp=[[[idle_max for b in range(400)] for a in range(400)] for i in range(N)]
used=set()
a,b,c=ABC[0]
dp[0][a][b]=c

for n in range(1,N):
    a,b,c=ABC[n]

    for x in range(400):
        for y in range(400):
            if x-a>=0 and y-b>=0 and dp[n-1][x-a][y-b]!=idle_max:
                dp[n][x][y]=min(dp[n-1][x-a][y-b]+c,dp[n-1][x][y])
            else:
                dp[n][x][y]=dp[n-1][x][y]
    dp[n][a][b]=min(dp[n-1][a][b],c)
value=idle_max
for x in range(1,400):
    for y in range(1,400):
        z=gcd(x,y)
        a=x//z;b=y//z
        if Ma==a and Mb==b:
            value=min(dp[-1][x][y],value)

if value!=idle_max:
    print(value)
else:
    print(-1)

Solution (Python):




from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys





N,Ma,Mb=map(int,input().split())
ABC=[list(map(int,input().split())) for i in range(N)]
idle_max=10**9
dp=[[idle_max for m in range(403*400)] for i in range(N)]
used=set()
a,b,c=ABC[0]
dp[0][a*401+b]=c
for n in range(1,N):
    a,b,c=ABC[n]
    for i in range(len(dp[0])):
        if i-(a*401+b)>=0 and dp[n-1][i-(a*401+b)]!=idle_max:
            dp[n][i]=min(dp[n-1][i-(a*401+b)]+c,dp[n-1][i])
        else:
            dp[n][i]=dp[n-1][i]
    dp[n][a*401+b]=min(dp[n-1][a*401+b],c)

value=idle_max
for i in range(len(dp[0])):
    if dp[-1][i]:
        A=i//401;B=i%401
        if A and B:
            Z=gcd(A,B)
            A//=Z;B//=Z
            if Ma==A and Mb==B:
                value=min(value,dp[-1][i])
if value!=idle_max:
    print(value)
else:
    print(-1)

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,Ma,Mb=MI()
    a=[0]*N
    b=[0]*N
    c=[0]*N
    cab=[[0,0,0] for _ in range(N)]
    for i in range(N):
        cab[i][1],cab[i][2],cab[i][0]=MI()
    cab.sort()
    for i in range(N):
        a[i]=cab[i][1]
        b[i]=cab[i][2]
        c[i]=cab[i][0]
        
    
    inf=10**5
    
    dp=[[[inf]*(N*10+1) for _ in range(N*10+1)] for i in range(N+1)]
    dp[0][0][0]=0
    
    ans=inf
    for i in range(N):
        for j in range(N*10+1-a[i]):
            for k in range(N*10+1-b[i]):
                if dp[i][j][k]!=inf:
                    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])
                    if (j+a[i])*Mb==(k+b[i])*Ma:
                        ans=min(ans,dp[i+1][j+a[i]][k+b[i]])
                    
    if ans==inf:
        print(-1)
    else:
        print(ans)
    
    
    

main()

Solution (Python):
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True 
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n,ma,mb = inm()
a = []
b = []
c = []
for i in range(n):
    aa,bb,cc = inm()
    a.append(aa)
    b.append(bb)
    c.append(cc)
dp = [[BIG]*(10+1) for i in range(10+1)]
dp[0][0] = 0
for t in range(n):
    dp2 = [[BIG]*(10*(t+1)+1) for i in range(10*(t+1)+1)]
    for i in range(10*t+1):
        for j in range(10*t+1):
            if dp[i][j]<BIG:
                dp2[i][j] = min(dp2[i][j],dp[i][j])
                dp2[i+a[t]][j+b[t]] = min( \
                     dp2[i+a[t]][j+b[t]], dp[i][j]+c[t])
    dp = dp2
mn = BIG
for i in range(1,5000):
    if i*max(ma,mb)>n*10:
        break
    
    mn = min(mn, dp[i*ma][i*mb])
print(-1 if mn==BIG else mn)

Solution (Python):

N, Ma, Mb = map(int,input().split())
rate = Ma / Mb
a = [0] * N
b = [0] * N
c = [0] * N
MA = 0
MB = 0
MC = 0
for i in range(N):
    a[i], b[i], c[i] = map(int, input().split())
    MA += a[i]
    MB += b[i]
    MC += c[i]
    
dp = [[[MC for i in range(MB+1)] for j in range(MA+1)] for k in range(N+1)]
dp[0][0][0] = 0

for it_N in range(N):
    for it_MA in range(MA+1):
        for it_MB in range(MB+1):
            if it_MA >= a[it_N] and it_MB >= b[it_N]:
                if dp[it_N][it_MA-a[it_N]][it_MB-b[it_N]] != MC:
                    dp[it_N+1][it_MA][it_MB] = min(dp[it_N][it_MA-a[it_N]][it_MB-b[it_N]] + c[it_N], dp[it_N][it_MA][it_MB])
                else:
                    dp[it_N+1][it_MA][it_MB]=dp[it_N][it_MA][it_MB]
            else:
                dp[it_N+1][it_MA][it_MB]=dp[it_N][it_MA][it_MB]

ans = -1
for a in range(1, MA+1):
    for b in range(1, MB+1):
        if a/b == rate and dp[N][a][b] != MC:
            if ans == -1:
                ans = dp[N][a][b]
            else:
                ans = min(dp[N][a][b], ans)
print(ans)

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

dp = [[[10**10 for _ in range(430)] for _ in range(430)] for _ in range(45)]

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

for i in range(n):
    a=l[i][0]
    b=l[i][1]
    c=l[i][2]
    for j in range(410):
        for k in range(410):
            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=10**10
for j in range(1,410):
    for k in range(1,410):
        if ma*k == mb*j:
            ans=min(ans,dp[n][j][k])
if ans == 10**10:
    print(-1)
else:
    print(ans)

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

INF = 40 * 100 + 1
ab_max = 10 * n

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

for i, (a, b, c) in enumerate(abc, 1):
    for j in range(ab_max + 1):
        for k in range(ab_max + 1):
            dp[i][j][k] = dp[i-1][j][k]
            if j - a >= 0 and k - b >= 0:
                if dp[i-1][j-a][k-b] != INF:
                    dp[i][j][k] = min(dp[i][j][k], dp[i-1][j-a][k-b] + c)


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

if ans == INF:
    ans = -1

print(ans)

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

def main():
    N,A,B = map(int,input().split())
    INF = 10**10
    dp = np.full((N+1,10*N+1,10*N+1),INF)
    dp[0][0][0] = 0
    
    for i in range(N):
        a,b,c = map(int,input().split())
        dp[i+1][a:,b:] = np.minimum(dp[i][a:,b:],dp[i][:10*N+1-a,:10*N+1-b]+c)
        dp[i+1] = np.minimum(dp[i],dp[i+1])
    
    ans = INF
    x,y = A,B
    while x < 10*N+1 and y < 10*N+1:
        ans = min(ans,dp[N][x][y])
        x += A
        y += B
        
    print(int(ans) if ans != INF else -1)

if __name__ == "__main__":
    main()

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

def main():
    N,A,B = map(int,input().split())
    INF = 10**10
    dp = np.full((N+1,10*N+2,10*N+2),INF)
    dp[0][0][0] = 0
    
    for i in range(N):
        a,b,c = map(int,input().split())
        dp[i+1][a:,b:] = np.minimum(dp[i][a:,b:],dp[i][:10*N+2-a,:10*N+2-b]+c)
        dp[i+1] = np.minimum(dp[i],dp[i+1])
    
    ans = INF
    x,y = A,B
    while x <= 10*N+1 and y <= 10*N+1:
        ans = min(ans,dp[N][x][y])
        x += A
        y += B
        
    print(int(ans) if ans != INF else -1)

if __name__ == "__main__":
    main()

Solution (Python):
import numpy as np
n, ma, mb = map(int, input().split())
r = 10 * n + 6
inf = 10**5
dp = np.full((n+1, r, r), inf)
dp[0][0][0] = 0
for i in range(n):
    a, b, c = map(int, input().split())
    dp[i+1][a:, b:] = np.minimum(
        dp[i][:r-a, :r-b] + c, dp[i][a:, b:]
    )
    dp[i+1] = np.minimum(dp[i+1], dp[i])

ans = inf
x, y = 0, 0
while True:
    x += ma
    y += mb
    if x < r and y < r:
        ans = min(ans, dp[n][x][y])
    else:
        break
if ans == inf:
    print(-1)
else:
    print(int(ans))

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

inf = 10**9
dp = [[[inf for i in range(n*11)] for j in range(n*11)] for k in range(n+1)]
for i in range(n):
    
    dp[i][0][0] = 0
for i in range(1,n+1):
    typeA,typeB,cost = data[i-1]
    for j in range(n*11-typeA):
        for k in range(n*11-typeB):
            if dp[i][j+typeA][k+typeB] > dp[i-1][j][k] + cost:
                dp[i][j+typeA][k+typeB] = dp[i-1][j][k] + cost
            dp[i][j][k] = min(dp[i][j][k], dp[i-1][j][k])
ans = inf
for i in range(1,n*11):
    for j in range(1,n*11):
        if i / j == ma / mb:
            
            ans = min(ans, dp[n][i][j])
if ans == inf:
    print(-1)
else:
    print(ans)

Solution (Python):
n,ma,mb = map(int,input().split())
abc = [list(map(int,input().split())) for i in range(n)]
dp = [[[10000 for i in range(n*10+1)] for j in range(n*10+1)] for k in range(n+1)]
for i in range(n+1):
  dp[i][0][0] = 0
for i in range(1,n+1):
  x,y,c = abc[i-1]
  for j in range(1,n*10+1):
    for k in range(1,n*10+1):
      if j>=x and k>=y:
        dp[i][j][k] = min(dp[i][j][k],dp[i-1][j][k],dp[i-1][j-x][k-y]+c)
      else:
        dp[i][j][k] = min(dp[i][j][k],dp[i-1][j][k])
ans = 10000000
for i in range(1,n*10+1):
  for j in range(1,n*10+1):
    if i*mb == j*ma:
      ans = min(ans,dp[n][i][j])
if ans == 10000:
  print(-1)
else:
  print(ans)

Solution (Python):

def solve():
    N, M_a, M_b = [int(x) for x in input().split()]
    inf = 100000
    t = [[inf] * (10 * N + 1) for _ in range(10 * N + 1)]
    t[0][0] = 0
    for _ in range(N):
        ai, bi, ci = [int(x) for x in input().split()]
        for ta in reversed(range(ai, len(t))):
            for tb in reversed(range(bi, len(t[0]))):
                t[ta][tb] = min(t[ta][tb], t[ta - ai][tb - bi] + ci)
    ans = min(
        t[k * M_a][k * M_b]
        for k in range(1, min((len(t) - 1) // M_a, (len(t[0]) - 1) // M_b) + 1)
    )
    print(-1 if ans >= inf else ans)


solve()

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

W = max([Ma, Mb])*(N+1)
C = 100*(N+1)

dp = [[[10000]*W for _ in range(W)] for _ in range(N+1)]
dp[0][0][0] = 0

for i in range(1, N+1):
    a, b, c = abc[i-1]
    for j in range(W):
        for k in range(W):
            if (j-a) < 0 or (k-b) < 0:
                dp[i][j][k] = dp[i-1][j][k]
            else:
                dp[i][j][k] = min([
                    dp[i-1][j][k],
                    dp[i-1][j-a][k-b]+c
                ])

ans = C
ma, mb = Ma, Mb
while ma < W and mb < W:
    t = dp[N][ma][mb]
    if t < ans:
        ans = t
    ma += Ma
    mb += Mb

print(ans if ans < C else -1)

Solution (Python):
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))


N, Ma, Mb = lr()
INF = 5000
limit = N * 10 + 1
dp = [[INF] * limit for _ in range(limit)]
dp[0][0] = 0
for _ in range(N):
    a, b, c = lr()
    used = set()
    for i in range(limit):
        if i < a:
            continue
        for j in range(limit):
            if j < b:
                continue
            
            if (i-a, j-b) not in used:
                if dp[i][j] > dp[i-a][j-b] + c:
                    used.add((i, j))
                    dp[i][j] = dp[i-a][j-b] + c

answer = INF
for i in range(1, limit):
    if i % Ma != 0:
        continue
    for j in range(1, limit):
        if j % Mb != 0:
            continue
        if i // Ma == j // Mb:
            if dp[i][j] < answer:
                answer = dp[i][j]

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

Solution (Python):

import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def allptn(abc,ma,mb):
    inf=10**9
    
    allabc=[(0,0,0)]
    for a,b,c in abc:
        n=len(allabc)
        for i in range(n):
            aa,bb,cc=allabc[i]
            allabc.append((aa+a,bb+b,cc+c))
    
    
    
    vc={}
    ans=inf
    for a,b,c in allabc[1:]:
        val=a*mb-b*ma
        if val:
            pc=vc.setdefault(val,inf)
            if c<pc:vc[val]=c
        else:
            if c<ans:ans=c
    res=[(val,c) for val,c in sorted(vc.items())]
    return ans,res

def main():
    n,ma,mb=MI()
    abc=[LI() for _ in range(n)]
    inf=10**9
    
    
    
    
    
    ans,pre=allptn(abc[:n//2],ma,mb)
    
    ret,pos=allptn(abc[n//2:],ma,mb)
    ans=min(ans,ret)
    
    
    j=len(pos)-1
    for val,c in pre:
        while val+pos[j][0]>0 and j:j-=1
        if val+pos[j][0]==0:ans=min(ans,c+pos[j][1])
    if ans==inf:print(-1)
    else:print(ans)

main()

Solution (Python):
import numpy as np
N, ma, mb = map(int, input().split())
abc = tuple(map(int, input().split()) for _ in range(N))

inf = 10000
dp = np.full((401, 401), inf, dtype=int)
dp[0][0] = 0

for a, b, c in abc:
    for i in range(400, a - 1, -1):
        dp[i][b:] = np.minimum(dp[i][b:], dp[i - a][: - b] + c)

ans = inf
x = 1

while True:
    Ma = x * ma
    Mb = x * mb
    if Ma > 400 or Mb > 400:
        break
    ans = min(ans, dp[Ma][Mb])
    x += 1

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

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n,ma,mb=MI()
    inf=10**9
    abc=[LI() for _ in range(n)]
    dp=[[[inf]*(n*10+1) for _ in range(10*n+1)] for _ in range(n+1)]
    dp[0][0][0]=0
    for i,(a,b,c) in enumerate(abc):
        for j in range(n*10+1):
            for k in range(n*10+1):
                pre=dp[i][j][k]
                if pre==inf:continue
                dp[i+1][j][k]=min(dp[i+1][j][k],pre)
                dp[i+1][j+a][k+b]=min(dp[i+1][j+a][k+b],pre+c)
    ans=inf
    for i in range(1,105):
        a=i*ma
        b=i*mb
        if a>n*10 or b>n*10:break
        ans=min(ans,dp[n][a][b])
    if ans==inf:print(-1)
    else:print(ans)

main()

Solution (Python):
import numpy as np

n, ma, mb = map(int, input().split())
U = 400
dp = np.full((U+1, U+1), float("inf"))
dp[0, 0] = 0
for i in range(n):
  a, b, c = map(int, input().split())
  np.minimum(dp[a:, b:], dp[:-a, :-b]+c, out=dp[a:, b:])
ans = float("inf")
for i in range(1, 1+U//max(ma, mb)):
  na, nb = i*ma, i*mb
  ans = min(ans, dp[na, nb])
if ans == float("inf"):
  print(-1)
else:
  print(int(ans))