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

dp = [ [ [10**9]*401  for _ in range(401) ] for _ in range(n+1) ]
dp[0][0][0] = 0
for i in range(n):
    ai,bi,ci = abcl[i]
    for a in range(401):
        for b in range(401):
            dp[i+1][a][b] = min(dp[i][a][b], dp[i+1][a][b])
            if a+ai < 401 and b+bi < 401:
                dp[i+1][a+ai][b+bi] = min(dp[i][a][b] + ci, dp[i+1][a+ai][b+bi])


ans = 10**9
dp_row = dp[n]
for a in range(1,401):
    for b in range(1,401):
        if ma*b == mb*a:
            v = dp[n][a][b]
            ans = min(ans,v)

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

Solution (Python):
from itertools import groupby, accumulate, product, permutations, combinations
def solve():
  ans = float('inf')
  N, A, B = map(int, input().split())
  mae = [list(map(int, input().split())) for _ in range((N+1)//2)]
  usiro = [list(map(int, input().split())) for _ in range(N-(N+1)//2)]
  if N==1:
    a,b,c = mae[0]
    if a*B==b*A:
      return c
    return -1
  mae_lis = [[float('inf')]*201 for _ in range(201)]
  mae_lis[0][0] = 0
  for p in product([0,1],repeat=(N+1)//2):
    a,b,c = 0,0,0
    for i in range((N+1)//2):
      if p[i]==1:
        a += mae[i][0];b += mae[i][1];c += mae[i][2]
    mae_lis[a][b] = min(mae_lis[a][b],c)
  usiro_lis = [[float('inf')]*201 for _ in range(201)]
  usiro_lis[0][0] = 0
  for p in product([0,1],repeat=N-(N+1)//2):
    a,b,c = 0,0,0
    for i in range(N-(N+1)//2):
      if p[i]==1:
        a += usiro[i][0];b += usiro[i][1];c += usiro[i][2]
    usiro_lis[a][b] = min(usiro_lis[a][b],c)
  for i in range(1,201):
    for j in range(1,201):
      c = mae_lis[i][j]
      if c==float('inf'):
        continue
      for k in range(201):
        if (i+k)*B%A!=0:
          continue
        l = (i+k)*B//A - j
        if l<0 or l>200 or usiro_lis[k][l]==float('inf'):
          continue
        ans = min(ans,c+usiro_lis[k][l])
  for k in range(1,201):
    if k*B%A!=0:
      continue
    l = k*B//A
    if l>200 or usiro_lis[k][l]==float('inf'):
      continue
    ans = min(ans,usiro_lis[k][l])
  return ans if ans<float('inf') else -1
print(solve())

Solution (Python):

import sys
INF = 10**10
def main(N, Ma, Mb, ABC):
    MX = 10 * N
    dp = [[[INF] * (MX + 1) for _ in range(MX + 1)] for __ in range(N + 1)]
    dp[0][0][0] = 0
    for i in range(N):
        ni = i + 1
        ma, mb, mc = ABC[i]
        for na in range(MX + 1):
            a = na - ma
            for nb in range(MX + 1):
                b = nb - mb
                dp[ni][na][nb] = dp[i][na][nb]
                if a >= 0 and b >= 0:
                    dp[ni][na][nb] = min(dp[ni][na][nb], dp[i][a][b] + mc)

    ans = INF
    for a in range(MX + 1):
        for b in range(MX + 1):
            if a == 0 and b == 0: continue
            if a * Mb == b * Ma:
                ans = min(ans, dp[N][a][b])
    if ans == INF:
        print(-1)
    else:
        print(ans)

if __name__ == '__main__':
    input = sys.stdin.readline
    N, Ma, Mb = map(int, input().split())
    ABC = [tuple(map(int, input().split())) for _ in range(N)]
    main(N, Ma, Mb, ABC)

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

item = [tuple(map(int, input().split())) for _ in range(n)]

INF = 5000
dp = [[INF] * 444 for _ in range(444)]


dp[0][0] = 0

for i in range(n):
    next = [[INF] * 444 for _ in range(444)]
    for j in range(401):
        for k in range(401):
            a, b, c = item[i]
            next[j + a][k + b] = min(next
                                     [j + a][k + b], dp[j][k] + c)
            next[j][k] = min(next[j][k], dp[j][k])
    dp = next

ans = INF
for i in range(1, 401):
    for j in range(1, 401):  
        if ma * j == i * mb:
            ans = min(ans, dp[i][j])
print(ans if ans != INF else -1)

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

dp = [ [ [10**9]*401  for _ in range(401) ] for _ in range(n+1) ]
dp[0][0][0] = 0
for i in range(n):
    ai,bi,ci = abcl[i]
    for a in range(401):
        for b in range(401):
            if a+ai < 401 and b+bi < 401:
                dp[i+1][a][b] = min(dp[i][a][b], dp[i+1][a][b])
                dp[i+1][a+ai][b+bi] = min(dp[i][a][b] + ci, dp[i+1][a+ai][b+bi])


ans = 10**9
dp_row = dp[n]
for a in range(1,401):
    for b in range(1,401):
        if ma*b == mb*a:
            v = dp[n][a][b]
            ans = min(ans,v)

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

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

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

    import math
    ans = INF
    for j in range(1, M):
        for k in range(1, M):
            g = math.gcd(j, k)
            if j//g == ma and k//g == mb:
                ans = min(ans, dp[n][j][k])
    if ans == INF:
        print(-1)
    else:
        print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
N,MA,MB = map(int,input().split())
ABC = [tuple(map(int,input().split())) for i in range(N)]

from collections import defaultdict
from copy import copy
d = defaultdict(lambda: float('inf'))
d[(0,0)] = 0
for a,b,c in ABC:
    d2 = copy(d)
    for (pa,pb),pc in d.items():
        d2[(pa+a, pb+b)] = min(d2[(pa+a, pb+b)], pc+c)
    d = d2

ans = float('inf')
for (a,b),c in d.items():
    if a==b==0: continue
    if a*MB == b*MA:
        ans = min(ans, c)
print(-1 if ans==float('inf') else ans)

Solution (Python):
def main():
    N, MA, MB = list(map(int, input().split()))
    chemicals = [list(map(int, input().split())) for _ in range(N)]
    INF_COST = 1 + sum([chemical[2] for chemical in chemicals])
    MAX_A, MAX_B = 400, 400
    
    
    
    
    dp = [[INF_COST for _ in range(1 + MAX_B)] for _ in range(1 + MAX_A)]
    dp[0][0] = 0
    for chemical in chemicals:
        da, db, dc = chemical
        for a in range(MAX_A, -1, -1):
            if a + da > MAX_A:
                continue
            for b in range(MAX_B, -1, -1):
                if b + db > MAX_B:
                    continue
                dp[a + da][b + db] = min(dp[a + da][b + db], dc + dp[a][b])
    ans = INF_COST
    scale = 1
    while scale * MA <= MAX_A and scale * MB <= MAX_B:
        ans = min(ans, dp[scale * MA][scale * MB])
        scale += 1
    if ans == INF_COST:
        print(-1)
    else:
        print(ans)


if __name__ == '__main__':
    main()

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

def main():
    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 == 0 and j == 0:
                    if dp[a][b]:
                        dp[a][b] = min(dp[a][b], c)
                    else:
                        dp[a][b] = c
                if dp[i][j] and i+a <= 400 and j+b <= 400:
                    if dp[i+a][j+b]:
                        dp[i+a][j+b] = min(dp[i+a][j+b], dp[i][j]+c)
                    else:
                        dp[i+a][j+b] = dp[i][j] + c
    
    ans = 1000000
    x, y = ma, mb
    while x <= 400 and y <= 400:
        if dp[x][y]:
            ans = min(dp[x][y], ans)
        x += ma
        y += mb
    if ans == 1000000:
        print(-1)
    else:
        print(ans)
    
if __name__ == "__main__":
    main()

Solution (Python):
N, Ma, Mb = map(int, input().split())
items = []
for i in range(N):
    a, b, c = map(int, input().split())
    items.append((a, b, c))

dp = [[[10**8 for cb in range(420)] for ca in range(420)]
      for i in range(N + 1)]
dp[0][0][0] = 0

for i in range(N):
    for ca in range(400):
        for cb in range(400):
            dp[i+1][ca][cb] = min(dp[i+1][ca][cb], dp[i][ca][cb])
            dp[i+1][ca+items[i][0]][cb+items[i][1]] = \
                min(dp[i+1][ca+items[i][0]][cb+items[i][1]],
                    dp[i][ca][cb]+items[i][2])


ans = 10**8
for ca in range(1, 401):
    if ca*Mb % Ma == 0 and ca*Mb//Ma <= 400:
        ans = min(ans, dp[N][ca][ca*Mb//Ma])
if ans == 10**8:
    print(-1)
else:
    print(ans)

Solution (Python):

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

    INF = 10**12
    dp = [[INF] * 401 for _ in range(401)]
    dp[0][0] = 0

    for i in range(N):
        for j in range(400,-1,-1):
            for k in range(400,-1,-1):
                if j - a[i] >= 0 and k - b[i] >= 0:
                    dp[j][k] = min(dp[j][k], dp[j-a[i]][k-b[i]]+c[i])
    
    ans = INF
    for j in range(400):
        for k in range(400):
            if j*Mb == k*Ma != 0:
                ans = min(ans,dp[j][k])
    print(ans) if ans < INF else print(-1)

if __name__ == "__main__":
    main()

Solution (Python):


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

    INF = 10**12
    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(400,-1,-1):
            for k in range(400,-1,-1):
                dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k])
                if j - a[i] >= 0 and k - b[i] >= 0:
                    dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j-a[i]][k-b[i]]+c[i])
    
    ans = INF
    for j in range(400):
        for k in range(400):
            if j*Mb == k*Ma != 0:
                ans = min(ans,dp[N][j][k])
    print(ans) if ans < INF else print(-1)

if __name__ == "__main__":
    main()

Solution (Python):
N,MA,MB = map(int,input().split())
ABC = [tuple(map(int,input().split())) for i in range(N)]

INF = float('inf')
from collections import defaultdict
dp = defaultdict(lambda: INF)
dp[(0,0)] = 0
for a,b,c in ABC:
    dp2 = defaultdict(lambda: INF)
    for (pa,pb),pc in dp.items():
        dp2[(pa,pb)] = min(dp2[(pa,pb)], pc)
        dp2[(pa+a, pb+b)] = min(dp2[(pa+a, pb+b)], pc+c)
    dp = dp2

ans = INF
for i in range(1,401):
    ans = min(ans, dp[(MA*i, MB*i)])
print(-1 if ans==INF else ans)

Solution (Python):
N, Ma, Mb = map(int, input().split())
Items = [list(map(int, input().split())) for _ 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(Items):
    for x in range(401):
        for y in range(401):
            dp[i + 1][x][y] = min(dp[i + 1][x][y], dp[i][x][y])
            if x + a > 400 or y + b > 400:
                continue
            dp[i + 1][x + a][y + b] = min(dp[i + 1][x + a][y + b], dp[i][x][y] + c)

ans = float('inf')
i = 1
while Ma * i <= 400 and Mb * i <= 400:
    ans = min(ans, dp[-1][Ma * i][Mb * i])
    i += 1
print(ans if ans != float('inf') else -1)

Solution (Python):
N, Ma, Mb = map(int, input().split())
dp = [[[10**18]*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] = 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)

ans = 10**18

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

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

Solution (Python):
n,ma,mb=map(int,input().split())
INF=10**18
dp=[[[INF for _ in range(401)] for _ in range(401)] for _ in range(n+1)]
ce=[[] for _ in range(n)]
for i in range(n):
  ce[i]=tuple(map(int,input().split()))
dp[0][0][0]=0
for k in range(n):
  for i in range(401):
    for j in range(401):
      a,b,c=ce[k]
      dp[k+1][i][j]=min(dp[k+1][i][j],dp[k][i][j])
      if i+a<401 and j+b<401:
        dp[k+1][i+a][j+b]=min(dp[k+1][i+a][j+b],dp[k][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[n][i][j])
if ans==INF:ans=-1
print(ans)

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

inf = 10 ** 9
lst = [[inf for _ in range(401)] for _ in range(401)]
lst[0][0] = 0
for a, b, c in abc:
    for i in range(400, -1, -1):
        if i - a < 0:
            break
        for j in range(400, -1, -1):
            if j - b < 0:
                break
            lst[i][j] = min(lst[i][j], lst[i - a][j - b] + c)

ans = inf
a = ma
b = mb
while 1:
    try:
        ans = min(ans, lst[a][b])
        a += ma
        b += mb
    except IndexError:
        break
if ans == inf:
    print(-1)
else:
    print(ans)

Solution (Python):
dp_n = 410
n, ma, mb = map(int, input().split())
dp = [[float('inf')]*dp_n for _ in range(dp_n)]
dp[0][0] = 0
for i in range(n):
    a, b, c = map(int, input().split())
    for j in reversed(range(dp_n)):
        for i in reversed(range(dp_n)):
            if dp[i][j] != float('inf'):
                try:
                    dp[i+a][j+b] = min(dp[i+a][j+b],dp[i][j]+c)
                except:
                    pass
res = float('inf')
for j in range(1,dp_n):
    for i in range(1,dp_n):
        if i*mb == j*ma:
            res = min(res,dp[i][j])
print(res if res != float('inf') else -1)

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

d={(0,0):0}
for i in range(n):
  nd={}
  for j in d:
    if j in nd:
      nd[j]=min(nd[j],d[j])
    else:
      nd[j]=d[j]
    s=(j[0]+item[i][0],j[1]+item[i][1])
    if s in nd:
      nd[s]=min(nd[s],d[j]+item[i][2])
    else:
      nd[s]=d[j]+item[i][2]
  d=nd
m=10**9
for i in d:
  if i==(0,0):
    continue
  if i[0]*mb==i[1]*ma:
    m=min(m,d[i])
if m==10**9:
  m=-1

print(m)

Solution (Python):
import math


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


def read_ints():
    return list(map(int, input().strip().split(' ')))


def read_into(*A):
    for arr, a in zip(A, read_ints()):
        arr.append(a)


def solve():
    """
    dp[400][400] = 160.000*10 = 1.600.000

    dp[x+a[i]][y+b[i]] = min(dp[x+a[i]][y+b[i]], c[i]+dp[x][y])
    dp[0][0] = 0
    """
    n, ma, mb = read_ints()
    A, B, C = [], [], []
    for _ in range(n):
        read_into(A, B, C)
    dp_upper_A, dp_upper_B = max(A)*n+1, max(B)*n+1
    dp = [
        [[math.inf]*dp_upper_B for _ in range(dp_upper_A)] for _ in range(n+1)
    ]
    dp[0][0][0] = 0
    for i in range(n):
        for x in range(dp_upper_A):
            for y in range(dp_upper_B):
                if dp[i][x][y] == math.inf:
                    continue
                dp[i+1][x][y] = min(dp[i+1][x][y], dp[i][x][y])
                dp[i+1][x+A[i]][y+B[i]] = min(dp[i+1][x+A[i]][y+B[i]], C[i]+dp[i][x][y])
    i = 1
    min_cost = math.inf
    while i*ma < dp_upper_A and i*mb < dp_upper_B:
        min_cost = min(min_cost, dp[n][i*ma][i*mb])
        i += 1
    return -1 if min_cost == math.inf else min_cost


if __name__ == '__main__':
    print(solve())

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

inf = 10**18
dp = [[[inf]*(411) for j in range(411)] for i in range(n+1)]
for i in range(n+1):
    dp[i][0][0] = 0
for i in range(n):
    for j in range(400):
        for k in range(400):
            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 = 10**18
for j in range(1,401):
    for k in range(1,401):
        if ma*k == mb*j:
            ans = min(ans,dp[n][j][k]) 
if ans == 10**18:
    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))
dp = {(0, 0): 0}
for a, b, c in abc:
    newdp = dp.copy()
    for (ka, kb), p in dp.items():
        k = (a+ka, b+kb)
        if k in newdp:
            newdp[k] = min(newdp[k], p+c)
        else:
            newdp[k] = p+c
    dp = newdp
INF = 10**10
ans = INF
for (a, b), c in dp.items():
    if ma*b!=mb*a or a==b==0:
        continue
    ans = min(ans, c)
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
NMAX = 40
ABMAX = 10
INF = 1000000
for i in range(N):
    a[i], b[i], c[i] = map(int, input().split())
dp = [[[INF]*(NMAX*ABMAX+1)for _ in range(NMAX*ABMAX+1)]for _ in range(N+1)]
dp[0][0][0] = 0
for i in range(N):
    for ca in range(NMAX*ABMAX+1):
        for cb in range(NMAX*ABMAX+1):
            if dp[i][ca][cb] == INF:
                continue
            dp[i+1][ca][cb] = min(dp[i+1][ca][cb], dp[i][ca][cb])
            dp[i+1][ca+a[i]][cb+b[i]
                             ] = min(dp[i+1][ca+a[i]][cb+b[i]], dp[i][ca][cb]+c[i])
ans = INF
for ca in range(1, NMAX*ABMAX+1):
    for cb in range(1, NMAX*ABMAX+1):
        if ca * mb == cb * ma:
            ans = min(ans, dp[N][ca][cb])
print(ans if ans != INF else -1)

Solution (Python):
N,Ma,Mb=map(int,(input().split()))
a,b,c=[0]*N,[0]*N,[0]*N

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

MA=sum(a)
MB=sum(b)
dp=[[[float("inf") for i in range(MB+1)] for j in range(MA+1)] for k in range(N+1)]
dp[0][0][0]=0
for i in range(N):
  for j in range(MA):
    for k in range(MB):
      if dp[i][j][k]==float("inf"):
        continue
      dp[i+1][j][k]=min(dp[i+1][j][k],dp[i][j][k])
      dp[i+1][j+a[i]][k+b[i]]=min(dp[i+1][j+a[i]][k+b[i]],dp[i][j][k]+c[i])

ans=float("inf")
for i in range(1,MA+1):
  for j in range(1,MB+1):
    if i*Mb==j*Ma:
      ans=min(ans,dp[N][i][j])

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

Solution (Python):
import sys
import numpy as np
from collections import deque,defaultdict

inp=lambda v,w:v[0]*w[0]+v[1]*w[1]
isprop = lambda a,b: a[0]*b[1]==a[1]*b[0]
cwsum = lambda l:[sum([x[0] for x in l]), sum([x[1] for x in l])]
chmin = lambda a,b:(a,b)[b<a]

            
def main():
    N,Ma,Mb=map(int,input().split())
    infty=10**9
    ch=[list(map(int,input().split())) for _ in range(N)]
    p=[(-1)*Mb,Ma]
    
    
        
    
        
    
    pch=[[inp(v[:2],p),v[2]] for v in ch]
    X=N*max([abs(w[0]) for w in pch])
    dp=[[infty]*(2*X+1) for _ in range(N)]
    for i in range(N):
        for j in range(2*X+1):
            if i==0 and j==X+pch[0][0]:
                dp[i][j]=pch[0][1]
            elif i>0 and j==X+pch[i][0]:
                dp[i][j]=chmin(dp[i-1][j],pch[i][1])
            elif i>0 and j>=pch[i][0] and j<=2*X+pch[i][0]:
                dp[i][j]=chmin(dp[i-1][j],dp[i-1][j-pch[i][0]]+pch[i][1])
            elif i>0:
                dp[i][j]=dp[i-1][j]
    
    
    
    if dp[N-1][X]==infty:
        print(-1)
    else:
        print(dp[N-1][X])

if __name__=='__main__':
    main()

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n, ma, mb = list(map(int, input().split()))
abc = [None]*n
for i in range(n):
    a,b,c = tuple(map(int, input().split()))
    abc[i] = (a*mb - b*ma, c)
abc.sort()
def halfcomb(l):
    n = len(l)
    n1 = n//2
    n2 = (n+1)//2
    l1 = []
    l2 = []
    def sub(v1, v2, i, bound, ll, ind=0):
        if i==bound:
            ll.append((v1,v2))
            return
        sub(v1, v2, i+1, bound, ll, ind)
        sub(v1+l[ind+i][0], v2+l[ind+i][1], i+1, bound, ll, ind)
    sub(0, 0, 0, n1, l1)
    sub(0, 0, 0, n2, l2, ind=n1)
    return l1, l2
l1, l2 = halfcomb(abc)
d2 = {}
for v,c in l2:
    if v in d2:
        d2[v] = min(d2[v], c)
    else:
        d2[v] = c
ans = float("inf")
for v,c in l1:
    if v==0 and c>0:
        ans = min(ans, c)
for v,c in l2:
    if v==0 and c>0:
        ans = min(ans, c)
        
for v,c in l1:
    if -v in d2 and c+d2[-v]>0:
        ans = min(ans, c+d2[-v])
if ans == float("inf"):
    ans = -1
print(ans)

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n, ma, mb = list(map(int, input().split()))
abc = [None]*n
for i in range(n):
    a,b,c = tuple(map(int, input().split()))
    abc[i] = (a*mb - b*ma, c)
abc.sort()
def halfcomb(l):
    n = len(l)
    n1 = n//2
    n2 = (n+1)//2
    l1 = []
    l2 = []
    
    for b in range(1, 1<<n2):
        v11, v12 = 0, 0
        v21, v22 = 0, 0
        for i in range(n2):
            if b&1<<i:
                item = l[n1+i]
                v21+=item[0]
                v22+=item[1]
                if b<(1<<n1) and i<n1:
                    item = l[i]
                    v11+=item[0]
                    v12+=item[1]
        if b<(1<<n1):
            l1.append((v11,v12))
        l2.append((v21,v22))
    return l1, l2
l1, l2 = halfcomb(abc)
d2 = {}
for v,c in l2:
    if v in d2:
        d2[v] = min(d2[v], c)
    else:
        d2[v] = c
ans = float("inf")
for v,c in l1:
    if v==0:
        ans = min(ans, c)
for v,c in l2:
    if v==0:
        ans = min(ans, c)
        
for v,c in l1:
    if -v in d2:
        ans = min(ans, c+d2[-v])
if ans == float("inf"):
    ans = -1
print(ans)

Solution (Python):
n,ma,mb=map(int,input().split())
subs=[list(map(int,input().split())) for _ in range(n)]
infi=10**18
dp=[[[infi]*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=subs[i-1]
    for j in range(401):
        for k in range(401):
            if j+a<=400 and k+b<=400:
                dp[i][j+a][k+b]=min(dp[i][j+a][k+b],dp[i-1][j][k]+c)
            dp[i][j][k]=min(dp[i][j][k],dp[i-1][j][k])
ans=infi
for i in range(1,401):
    for j in range(1,401):
        if i%ma==0 and j%mb==0:
            if i//ma==j//mb:
                ans=min(ans,dp[n][i][j])
if ans==infi:
    ans=-1
print(ans)

Solution (Python):
N,M,L=map(int,input().split())
X=[list(map(int,input().split())) for i in range(N)]
INF=10**9
DP=[[INF]*411 for i in range(411)]
DP[0][0]=0
for i in range(N):
  for j in range(400,-1,-1):
    for k in range(401):
      DP[j+X[i][0]][k+X[i][1]]=min(DP[j+X[i][0]][k+X[i][1]],DP[j][k]+X[i][2])
P=INF
for i in range(1,401):
  if i*max(M,L)>400:
    break
  P=min(P,DP[i*M][i*L])
if P==INF:
  print(-1)
else:
  print(P)

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


dp = [[[float('inf')]*401 for j in range(401)] for i in range(41)]
dp[0][0][0] = 0

for i in range(n):
    a, b, c = items[i]
    for anow in range(400):
        for bnow in range(400):
            
            dp[i+1][anow][bnow] = min(dp[i+1][anow][bnow], dp[i][anow][bnow])
            
            if 0<=anow+a<401 and 0<=bnow+b<401:
                dp[i+1][anow+a][bnow+b] = min(dp[i+1][anow+a][bnow+b], dp[i][anow][bnow]+c)
                

ans = float('inf')
for a in range(1, 401):
    for b in range(1, 401):
        if ma*b==mb*a:
            ans = min(ans, dp[n][a][b])
if ans == float('inf'):
    print(-1)
else:
    print(ans)

Solution (Python):
N,A,B=map(int,input().split())
inf=float("inf")
dp=[[inf]*401 for _ in range(401)]
dp[0][0]=0
for i in range(N):
    a,b,c=map(int,input().split())
    for j in range(-1,-402,-1):
        for k in range(-1,-402,-1):
            if dp[j][k]!=inf:
                dp[j+a][k+b]=min(dp[j+a][k+b],dp[j][k]+c)
ans=inf
for i in range(1,401):
    for j in range(1,401):
        if i/j==A/B:
            ans=min(ans,dp[i][j])
if ans==inf:
    ans=-1
print(ans)

Solution (Python):
def getN():
    return int(input())
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7





N, Ma, Mb = getNM()
query = [getList() for i in range(N)]


dp = [[[float('inf')] * 501 for i in range(501)] for i in range(N + 1)]
dp[0][0][0] = 0


for i in range(N):
    
    for j in range(501):
        
        for l in range(501):
            a, b, c = query[i]
            if j - a >= 0 and l - b >= 0:
                dp[i + 1][j][l] = min(dp[i][j][l], dp[i][j - a][l - b] + c)
            else:
                dp[i + 1][j][l] = dp[i][j][l]

ans = float('inf')
for j in range(1, 500):
    for l in range(1, 500):
        if Ma * l == Mb * j and dp[N][j][l] < 10 ** 9 + 7:
            ans = min(ans, dp[N][j][l])
if ans < 10 ** 9 + 7:
    print(ans)
else:
    print(-1)

Solution (Python):
n,ma,mb= map(int,input().split())
al=[]
bl=[]
cl=[]
for _ in range(n):
    a,b,c= map(int,input().split())
    al.append(a)
    bl.append(b)
    cl.append(c)
suma=sum(al)
MAX=999999999999
gmincost = [MAX]
def dfsMa(id,count,sel):
    if id==n:return
    dfsMa(id+1,count,sel)
    tempsum = al[id]+count
    bsum=costsum=0
    for j in sel+[id]:
        bsum+=bl[j]
        costsum+=cl[j]
    if costsum > gmincost[0]:
        return
    elif tempsum%ma == 0:
        k=tempsum//ma
        if bsum==k*mb:
            gmincost[0] = min(gmincost[0],costsum)
            return
    dfsMa(id+1,count+al[id],sel+[id])
    return
dfsMa(0,0,[])
if gmincost[0]==MAX:print(-1)
else:print(gmincost[0])

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

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

for i in range(10*N+1):
    for j in range(10*N+1):
        if i!=0 and j!=0 and i*Mb==j*Ma:
            ans = min(ans, dp[N][i][j])

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

Solution (Python):
N, A, B = map(int, input().split())
chem = []
for i in range(N):
    a, b, c = map(int, input().split())
    chem.append((a, b, c))

MAXX = N*10 + 1
MAXMONEY = 100*MAXX
money = [[MAXMONEY]*MAXX for _ in range(MAXX)]
ans = MAXMONEY

money[0][0] = 0
for i in range(N):
    a, b, cc = chem[i]
    for c1 in range(MAXX-1, -1, -1):
        for c2 in range(MAXX-1, -1, -1):
            if money[c1][c2] < MAXMONEY:
                newm = money[c1][c2] + cc
                if (c1 + a < MAXX) and (c2+b < MAXX) and (money[c1+a][c2+b] > newm):
                    money[c1 + a][c2 + b] = newm


rr = int(min((MAXX+0.9*A)//A, (MAXX+0.9*B)//B))
for bai in range(1, rr):
    if money[A*bai][B*bai] < ans:
        ans = money[A*bai][B*bai]

if ans >= MAXMONEY: ans = -1
print(ans)

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

MA=sum(a)
MB=sum(b)
dp=[[[float("inf") for i in range(MB+1)] for j in range(MA+1)] for k in range(N+1)]
dp[0][0][0]=0
for i in range(N):
    for j in range(MA):
        for k in range(MB):
            if dp[i][j][k]==float("inf"):
                continue
            dp[i+1][j][k]=min(dp[i+1][j][k],dp[i][j][k])
            dp[i+1][j+a[i]][k+b[i]]=min(dp[i+1][j+a[i]][k+b[i]],dp[i][j][k]+c[i])

ans=float("inf")
for i in range(1,MA+1):
    for j in range(1,MB+1):
        if i*mb==j*ma:
            ans=min(ans,dp[N][i][j])

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