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

Solution (Python):
def main():
    INF = 100 * 40 + 1
    MX = 4000

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

    dp = [[INF] * (MX * 2 + 1) for _ in range(2)]
    i, j = 0, 1
    for _ in range(N):
        ai, bi, ci = map(int, input().split())
        x = Ma * bi - Mb * ai  

        for k in range(-MX, MX + 1):
            dp[j][k] = dp[i][k]
        dp[j][x] = min(dp[j][x], ci)

        for k in range(-MX + x, MX + 1):
            dp[j][k] = min(
                dp[j][k],
                dp[i][k - x] + ci
            )
        i, j = j, i

    res = dp[i][0]
    print(-1 if res == INF else res)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



N,Ma,Mb = LI()
a,b,c = LIR(N,3)

n = N//2

d1 = [[5000]*401 for _ in range(401)]
d2 = [[5000]*401 for _ in range(401)]

for bit in range(1<<n):
    val_a = 0
    val_b = 0
    cost = 0
    for i in range(n):
        if bit>>i&1:
            val_a += a[n-1-i]
            val_b += b[n-1-i]
            cost += c[n-1-i]
    d1[val_a][val_b] = min(d1[val_a][val_b],cost)

for bit in range(1<<(N-n)):
    val_a = 0
    val_b = 0
    cost = 0
    for i in range(N-n):
        if bit>>i&1:
            val_a += a[N-1-i]
            val_b += b[N-1-i]
            cost += c[N-1-i]
    d2[val_a][val_b] = min(d2[val_a][val_b],cost)


vals = []
now = 1
while True:
    if Ma*now <= sum(a) and Mb*now <= sum(b):
        vals.append((Ma*now,Mb*now))
        now += 1
    else:
        break

ans = 5000
for ka in range(401):
    for kb in range(401):
        if d1[ka][kb] != 5000:
            for v in vals[::-1]:
                left_a = v[0]-ka
                left_b = v[1]-kb
                if left_a < 0 or left_b < 0:
                    break
                if d1[ka][kb]+d2[left_a][left_b] < ans:
                    ans = d1[ka][kb]+d2[left_a][left_b]

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

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



N,Ma,Mb = LI()
a,b,c = LIR(N,3)

n = N//2

d1 = [[5000]*401 for _ in range(401)]
d2 = [[5000]*401 for _ in range(401)]

for bit in range(1<<n):
    val_a = 0
    val_b = 0
    cost = 0
    for i in range(n):
        if bit>>i&1:
            val_a += a[n-1-i]
            val_b += b[n-1-i]
            cost += c[n-1-i]
    d1[val_a][val_b] = min(d1[val_a][val_b],cost)

for bit in range(1<<(N-n)):
    val_a = 0
    val_b = 0
    cost = 0
    for i in range(N-n):
        if bit>>i&1:
            val_a += a[N-1-i]
            val_b += b[N-1-i]
            cost += c[N-1-i]
    d2[val_a][val_b] = min(d2[val_a][val_b],cost)

vals = []
now = 1
while True:
    if Ma*now <= sum(a) and Mb*now <= sum(b):
        vals.append((Ma*now,Mb*now))
        now += 1
    else:
        break

ans = 5000
for ka in range(401):
    for kb in range(401):
        for v in vals[::-1]:
            left_a = v[0]-ka
            left_b = v[1]-kb
            if left_a < 0 or left_b < 0:
                break
            if d1[ka][kb]+d2[left_a][left_b] < ans:
                ans = d1[ka][kb]+d2[left_a][left_b]

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

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



N,Ma,Mb = LI()
a,b,c = LIR(N,3)

n = N//2

d1 = defaultdict(lambda:5000)
d2 = defaultdict(lambda:5000)

for bit in range(1<<n):
    val_a = 0
    val_b = 0
    cost = 0
    for i in range(n):
        if bit>>i&1:
            val_a += a[n-1-i]
            val_b += b[n-1-i]
            cost += c[n-1-i]
    if cost < d1[(val_a,val_b)]:
        d1[(val_a,val_b)] = cost

for bit in range(1<<(N-n)):
    val_a = 0
    val_b = 0
    cost = 0
    for i in range(N-n):
        if bit>>i&1:
            val_a += a[N-1-i]
            val_b += b[N-1-i]
            cost += c[N-1-i]
    if cost < d2[(val_a,val_b)]:
        d2[(val_a,val_b)] = cost

vals = []
now = 1
while True:
    if Ma*now <= sum(a) and Mb*now <= sum(b):
        vals.append((Ma*now,Mb*now))
        now += 1
    else:
        break

ans = 5000
for k in d1.keys():
    for v in vals[::-1]:
        left_a = v[0]-k[0]
        left_b = v[1]-k[1]
        if left_a < 0 or left_b < 0:
            break
        if d1[k]+d2[(left_a,left_b)] < ans:
            ans = d1[k]+d2[(left_a,left_b)]

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

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



N,Ma,Mb = LI()
a,b,c = LIR(N,3)

n = N//2

d1 = defaultdict(lambda:5000)
d2 = defaultdict(lambda:5000)

for bit in range(1<<n):
    L = bit.bit_length()
    val_a = 0
    val_b = 0
    cost = 0
    for i in range(L):
        if bit>>i&1:
            val_a += a[n-1-i]
            val_b += b[n-1-i]
            cost += c[n-1-i]
    if cost < d1[(val_a,val_b)]:
        d1[(val_a,val_b)] = cost

for bit in range(1<<(N-n)):
    L = bit.bit_length()
    val_a = 0
    val_b = 0
    cost = 0
    for i in range(L):
        if bit>>i&1:
            val_a += a[N-1-i]
            val_b += b[N-1-i]
            cost += c[N-1-i]
    if cost < d2[(val_a,val_b)]:
        d2[(val_a,val_b)] = cost

vals = []
now = 1
while True:
    if Ma*now <= sum(a) and Mb*now <= sum(b):
        vals.append((Ma*now,Mb*now))
        now += 1
    else:
        break

ans = 5000
for k in d1.keys():
    for v in vals[::-1]:
        left_a = v[0]-k[0]
        left_b = v[1]-k[1]
        if left_a < 0 or left_b < 0:
            break
        if d1[k]+d2[(left_a,left_b)] < ans:
            ans = d1[k]+d2[(left_a,left_b)]

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

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



N,Ma,Mb = LI()
a,b,c = LIR(N,3)

amax = sum(a)
bmax = sum(b)


dp = [[[5000]*(bmax+1) for _ in range(amax+1)] for _ in range(N)]
dp[0][a[0]][b[0]] = c[0]
dp[0][0][0] = 0
for i in range(1,N):
    for j in range(amax+1):
        for k in range(bmax+1):
            if j == k == 0:
                dp[i][j][k] = 0
            elif j-a[i] < 0 or k-b[i] < 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[i]][k-b[i]]+c[i])

ans = 5000
now = 1
while True:
    if Ma*now <= amax and Mb*now <= bmax:
        if dp[N-1][Ma*now][Mb*now] < ans:
            ans = dp[N-1][Ma*now][Mb*now]
        now += 1
    else:
        break

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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, ma, mb = map(int, readline().split())
inf = 40000
dp = [[[inf] * 401 for _ in range(401)] for _ in range(n + 1)]
ans = inf
dp[0][0][0] = 0
for i in range(n):
    a, b, c = map(int, readline().split())
    for j in range(401):
        for k in range(401):
            if dp[i][j][k] != 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][j][k], dp[i + 1][j][k])
for i in range(1, 401):
    for j in range(1, 401):
        if i * mb == j * ma:
            if ans > dp[n][i][j]:
                ans = dp[n][i][j]
if ans == inf:
    ans = -1
print(ans)

Solution (Python):
def main():
    N, Ma, Mb = map(int, input().split())
    yakuhin = []
    for i in range(N):
        yakuhin.append(list(map(int, input().split())))
    
    INF = 10**15
    DP = [[[INF for _ in range(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(DP[i][j][k] != INF):
                    DP[i+1][j][k] = min(DP[i+1][j][k], DP[i][j][k])
                    DP[i+1][j+yakuhin[i][0]][k+yakuhin[i][1]] = min(DP[i+1][j+yakuhin[i][0]][k+yakuhin[i][1]], DP[i][j][k]+yakuhin[i][2])
    
    ans = INF
    for i in range(1, N+1):
        for j in range(1, 401):
            for k in range(1, 401):
                if(j*Mb == k*Ma):
                    ans = min(ans, DP[i][j][k])    
        
    print(ans if ans != INF else -1)
    
main()

Solution (Python):
def main():
    N, Ma, Mb = map(int, input().split())
    yakuhin = []
    sa = 0
    sb = 0
    for i in range(N):
        yakuhin.append(list(map(int, input().split())))
        sa += yakuhin[i][0]
        sb += yakuhin[i][1]
    
    INF = 10**15
    DP = [[[INF for _ in range(sb+1)] for _ in range(sa+1)] for _ in range(N+1)]
    
    for i in range(N):
        DP[i][0][0] = 0
    
    for i in range(N):
        for j in range(sa+1):
            for k in range(sb+1):
                if(j >= yakuhin[i][0] and k >= yakuhin[i][1]):
                    DP[i+1][j][k] = min(DP[i][j][k], DP[i][j-yakuhin[i][0]][k-yakuhin[i][1]]+yakuhin[i][2])
                else:
                    DP[i+1][j][k] = min(DP[i+1][j][k], DP[i][j][k])
        
    ans = INF
    for i in range(1, N+1):
        for j in range(1, sa+1):
            for k in range(1, sb+1):
                if(j*Mb == k*Ma):
                    ans = min(ans, DP[i][j][k])    
        
    print(ans if ans != INF else -1)
    
main()

Solution (Python):
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

N, Ma, Mb = map(int, input().split())
A = [0] * N
B = [0] * N
C = [0] * N

for i in range(N):
    A[i], B[i], C[i] = map(int, input().split())

INF = 1 << 50
LEN = 500
dp = [[INF] * (LEN + 1) for _ in range(LEN + 1)]
dp[0][0] = 0

for i in range(N):
    for j in range(LEN, A[i] - 1, -1):
        for k in range(LEN, B[i] - 1, -1):
            dp[j][k] = min(dp[j][k], dp[j - A[i]][k - B[i]] + C[i])

cnt = 1
ans = INF
while cnt * Ma < LEN and cnt * Mb < LEN:
    ans = min(ans, dp[cnt * Ma][cnt * Mb])
    cnt += 1

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

Solution (Python):
MOD = 10 ** 9 + 7
INF = 10  ** 7
def main():
    n,a,b = map(int,input().split())
    m = n//2
    Chemical = [tuple(map(int,input().split())) for _ in range(n)]
    Chemical1 = Chemical[:m]
    Chemical2 = Chemical[m:]
    Sum1 = [[INF] * 405 for _ in range(405)]
    Sum2 = [[INF] * 405 for _ in range(405)]
    for bit in range(1<<m):
        cost,v1,v2 = 0,0,0
        for i in range(m):
            if (bit>>i)&1:
                d,e,f = Chemical1[i]
                v1 += d
                v2 += e
                cost += f
        Sum1[v1][v2] = min(Sum1[v1][v2],cost)
    
    for bit in range(1<<(n - m)):
        cost,v1,v2 = 0,0,0
        for i in range(n - m):
            if (bit>>i)&1:
                d,e,f = Chemical2[i]
                v1 += d
                v2 += e
                cost += f
        Sum2[v1][v2] = min(Sum2[v1][v2],cost)
    
    ans = INF
    for i in range(405):
        for j in range(405):
            if Sum1[i][j] != INF:
                C = Sum1[i][j]
                k = max((i + a - 1)//a,(j + b - 1)//b,1)
                while k * a - i < 405 and k*b - j < 405:
                    if Sum2[k*a - i][k*b - j] != INF:
                        ans = min(ans,Sum2[k*a - i][k*b - j] + C)
                    k += 1
    print(ans if ans != INF else -1)
if __name__ == '__main__':
    main()

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

INF = 10000

dp = [[INF for b in range(401)] for a in range(401)]

dp[0][0] = 0

sum_a = 0
sum_b = 0

for i in range(1,N+1):
    ai,bi,ci = map(int,input().split())
    sum_a += ai
    sum_b += bi
    for a in range(sum_a+1,-1,-1):
        for b in range(sum_b+1,-1,-1):
            if a-ai >= 0 and b-bi >= 0:
                dp[a][b] = min(dp[a][b],dp[a-ai][b-bi]+ci)


ans = INF
for j in range(1,401):
    if Ma*j > 400 or Mb*j > 400:
        break
    ans = min(ans,dp[Ma*j][Mb*j])
print(ans if ans != INF else -1)

Solution (Python):
N,Ma,Mb = map(int,input().split())
 
INF = float('inf')
 
dp = [[INF for b in range(401)] for a in range(401)]
 
dp[0][0] = 0
 
for i in range(1,N+1):
    ai,bi,ci = map(int,input().split())
    for a in range(400,-1,-1):
        for b in range(400,-1,-1):
            if a-ai >= 0 and b-bi >= 0:
                dp[a][b] = min(dp[a][b],dp[a-ai][b-bi]+ci)
 
 
ans = INF
for j in range(1,401):
    if Ma*j > 400 or Mb*j > 400:
        break
    ans = min(ans,dp[Ma*j][Mb*j])
print(ans if ans != INF else -1)

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

INF = float('inf')

dp = [[INF for b in range(401)] for a in range(401)]

dp[0][0] = 0

sa = 0
sb = 0

for i in range(1,N+1):
    ai,bi,ci = map(int,input().split())
    sa += ai
    sb += bi
    for a in range(sa+1,-1,-1):
        for b in range(sb+1,-1,-1):
            if a-ai >= 0 and b-bi >= 0:
                dp[a][b] = min(dp[a][b],dp[a-ai][b-bi]+ci)


ans = INF
for j in range(1,401):
    if Ma*j > 400 or Mb*j > 400:
        break
    ans = min(ans,dp[Ma*j][Mb*j])
print(ans if ans != INF else -1)

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

INF = 10000

dp = [[INF for b in range(401)] for a in range(401)]

dp[0][0] = 0

for i in range(1,N+1):
    ai,bi,ci = map(int,input().split())
    for a in range(400,-1,-1):
        for b in range(400,-1,-1):
            if a-ai >= 0 and b-bi >= 0:
                dp[a][b] = min(dp[a][b],dp[a-ai][b-bi]+ci)


ans = INF
for j in range(1,401):
    if Ma*j > 400 or Mb*j > 400:
        break
    ans = min(ans,dp[Ma*j][Mb*j])
print(ans if ans != INF else -1)

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

INF = 10000

dp = [[[INF for b in range(401)] for a in range(401)] for i in range(N+1)]

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

for i in range(1,N+1):
    ai,bi,ci = map(int,input().split())
    for a in range(401):
        for b in range(401):
            if a-ai >= 0 and b-bi >= 0:
                dp[i][a][b] = min(dp[i-1][a][b],dp[i-1][a-ai][b-bi]+ci)
            else:
                dp[i][a][b] = dp[i-1][a][b]

ans = INF
for j in range(1,401):
    if Ma*j >= 400 or Mb*j >= 400:
        break
    ans = min(ans,dp[N][Ma*j][Mb*j])
print(ans if ans != INF else -1)

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

INF = 10000

dp = [[[INF for b in range(401)] for a in range(401)] for i in range(N+1)]

for i in range(N+1):
    dp[i][0][0] = 0


for i in range(1,N+1):
    ai,bi,ci = map(int,input().split())
    for a in range(401):
        for b in range(401):
            if a-ai >= 0 and b-bi >= 0:
                dp[i][a][b] = min(dp[i-1][a][b],dp[i-1][a-ai][b-bi]+ci)
            else:
                dp[i][a][b] = dp[i-1][a][b]

ans = INF
for j in range(1,401):
    if Ma*j >= 400 or Mb*j >= 400:
        break
    ans = min(ans,dp[N][Ma*j][Mb*j])
print(ans if ans != INF else -1)

Solution (Python):

import io
from scipy.misc import comb

def inpl(): return [int(i) for i in input().split()]

nim,mike,kite = inpl()
inf = float('inf')
H = {(0,0): 0}
for i in range(nim):
    a, b, c = inpl()
    for (ia, ib), ic in H.copy().items():
        H[(ia+a,ib+b)] = min(H.get((ia+a, ib+b), inf),ic+c)

ans = min(H.get((i*mike, i*kite), inf) for i in range(1,401))
print(-1 if ans == inf else ans)

Solution (Python):
n,ma,mb = map(int,input().split())
L = []
sa = 0
sb = 0
for i in range(n):
    a,b,c = map(int,input().split())
    sa += a
    sb += b
    L.append([a,b,c])
dp = [[[10000 for i in range(sb+1)] for i in range(sa+1)] for i in range(n+1)]
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(sa+1):
        for k in range(sb+1):
            if j-a >= 0 and k-b >= 0:
                dp[i+1][j][k] = min(dp[i][j-a][k-b]+c,dp[i+1][j][k],dp[i][j][k])
            else:
                dp[i+1][j][k] = min(dp[i+1][j][k],dp[i][j][k])
k = max(ma,mb)
t = min(sa,sb)
cur = 1
ans = 10000
while k*cur <= t:
    ans = min(ans,dp[n][ma*cur][mb*cur])
    cur += 1
if ans == 10000:
    print(-1)
else:
    print(ans)

Solution (Python):
def main():
    
    
    
    N, Ma, Mb = map(int, input().split())
    med = [tuple(map(int, input().split())) for _ in range(N)]
    INF = float('inf')
    M = 110
    dp = [[INF] * (M + 1) for _ in range(M + 1)]
    dp[0][0] = 0

    for a, b, c in med:
        for i in range(M, a - 1, -1):
            for j in range(M, b - 1, -1):
                here = dp[i - a][j - b]
                dp[i][j] = min(dp[i][j], here + c)

    ans = INF
    ma = Ma; mb = Mb
    while ma < M and mb < M:
        ans = min(ans, dp[ma][mb])
        ma += Ma
        mb += Mb
    
    if ans < INF:
        print(ans)
    else:
        print(-1)


if __name__ == "__main__":
    main()

Solution (Python):
N,Ma,Mb=map(int,input().split())
a=[0 for k in range(N)]
b=[0 for k in range(N)]
c=[0 for k in range(N)]
import copy
for k in range(N):
    a[k],b[k],c[k]=map(int,input().split())
Sa=sum(a)
Sb=sum(b)
dp =[[[5000 for k in range(Sb+1)]for k in range(Sa+1)]for k in range(N+1)]
dp[0][0][0]=0
for k in range(N):
    listj=[]
    listl=[]
    for j in range(Sa-a[k]+1):
        for l in range(Sb-b[k]+1):
            if dp[k][j][l]!=5000:
                listj.append(j)
                listl.append(l)
    for m in range(len(listl)):
        dp[k+1][listj[m]+a[k]][listl[m]+b[k]]=min(dp[k][listj[m]][listl[m]]+c[k], dp[k][listj[m]+a[k]][listl[m]+b[k]])
        dp[k+1][listj[m]][listl[m]]=min(dp[k][listj[m]][listl[m]], dp[k+1][listj[m]][listl[m]])
ans=[5000]
for k in range(1, min(Sa//Ma,Sb//Mb)+1):
    ans.append(dp[-1][Ma*k][Mb*k])
if min(ans)==5000:
    print(-1)
else:
    print(min(ans))

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

N, Ma, Mb = map(int, input().split())
MAX = 400
INF = float('inf')
dp = [[INF] * (MAX+1) for _ in range(MAX+1)]
dp[0][0] = 0
for _ in range(N):
    a, b, c = map(int, input().split())
    for i in range(a, MAX+1)[::-1]:
        for j in range(b, MAX+1)[::-1]:
            dp[i][j] = min(dp[i][j], dp[i-a][j-b] + c)
ans = INF
x = 1
while Ma*x <= MAX and Mb*x <= MAX:
    ans = min(ans, dp[Ma*x][Mb*x])
    x += 1
print(ans if ans < INF else -1)

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

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

Solution (Python):




import sys
input = sys.stdin.readline



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

    INF = N * 100 + 1

    dp = [[[INF for cb in range(411)] for ca in range(411)] for i in range(N+1)]
    dp[0][0][0] = 0

    for i in range(N):
        for ca in range(N*10):
            for cb in range(N*10):
                if dp[i][ca][cb] == INF:
                    continue
                dp[i+1][ca][cb] = min(dp[i+1][ca][cb],dp[i][ca][cb])
                d = abc[i]
                dp[i+1][ca+d[0]][cb+d[1]] = min(dp[i+1][ca+d[0]][cb+d[1]],dp[i][ca][cb]+d[2])

    res = INF

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

    if res == INF:
        res = -1

    print(res)



main()

Solution (Python):
from fractions import gcd

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

dp=[[[float("inf") for i in range(0,401)] for j in range(0,401)] for k in range(0,N)]

for i in range(0,401):
    for j in range(0,401):
        if not (i==0 and j==0):
            g=gcd(i,j)
            I=i//g
            J=j//g
            if I==ma and J==mb:
                dp[0][i][j]=0
            else:
                g=gcd(i+med[0][0],j+med[0][1])
                I=(i+med[0][0])//g
                J=(j+med[0][1])//g
                if I==ma and J==mb:
                    dp[0][i][j]=med[0][2]

for i in range(1,N):
    a,b,c=med[i]
    for j in range(0,401):
        for k in range(0,401):
            if 400>=j+a and 400>=k+b:
                dp[i][j][k]=min(dp[i-1][j][k],c+dp[i-1][j+a][k+b])
            else:
                dp[i][j][k]=dp[i-1][j][k]

if dp[N-1][0][0]==float("inf"):
    print(-1)
else:
    print(dp[N-1][0][0])

Solution (Python):
from fractions import gcd

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

dp=[[[float("inf") for i in range(0,401)] for j in range(0,401)] for k in range(0,N)]

for i in range(0,401):
    for j in range(0,401):
        if not (i==0 and j==0):
            g=gcd(i,j)
            I=i//g
            J=j//g
            if I==ma and J==mb:
                dp[0][i][j]=0
            else:
                g=gcd(i+med[0][0],j+med[0][1])
                I=(i+med[0][0])//g
                J=(j+med[0][1])//g
                if I==ma and J==mb:
                    dp[0][i][j]=med[0][2]

for i in range(1,N):
    a,b,c=med[i]
    for j in range(0,401-a):
        for k in range(0,401-b):
            dp[i][j][k]=min(dp[i-1][j][k],c+dp[i-1][j+a][k+b])

if dp[N-1][0][0]==float("inf"):
    print(-1)
else:
    print(dp[N-1][0][0])

Solution (Python):
from fractions import gcd

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

dp=[[[float("inf") for i in range(0,401)] for j in range(0,401)] for k in range(0,N)]
dp=[[float("inf") for i in range(0,401)] for j in range(0,401)]

for i in range(0,401):
    for j in range(0,401):
        if not (i==0 and j==0):
            g=gcd(i,j)
            I=i//g
            J=j//g
            if I==ma and J==mb:
                dp[i][j]=0
            else:
                g=gcd(i+med[0][0],j+med[0][1])
                I=(i+med[0][0])//g
                J=(j+med[0][1])//g
                if I==ma and J==mb:
                    dp[i][j]=med[0][2]

for i in range(1,N):
    a,b,c=med[i]
    for j in range(0,401-a):
        for k in range(0,401-b):
            dp[j][k]=min(dp[j][k],c+dp[j+a][k+b])

if dp[0][0]==float("inf"):
    print(-1)
else:
    print(dp[0][0])

Solution (Python):
NMAX=40
ABMAX=10
INF=1000000
N,MA,MB=map(int,input().split())
A=[0]*NMAX
B=[0]*NMAX
C=[0]*NMAX
dp=[[[INF for _ in range(NMAX*ABMAX+1)] for _ in range(NMAX*ABMAX+1)] for _ in range(NMAX+1)]
for i in range(N):
  A[i],B[i],C[i]=map(int,input().split())

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])
if ans==INF: ans=-1
print(ans)

Solution (Python):

import math
import heapq
import bisect
import numpy as np
from collections import Counter, deque
import itertools


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

DP = [[10**9 for _ in range(401)] for _ in range(401)]
DP[0][0] = 0
for _ in range(N):
    a,b,c = map(int, input().split())
    for i in range(401)[::-1]:
        for j in range(401)[::-1]:
            if DP[i][j] != 10**9:
                DP[i+a][j+b] = min(DP[i+a][j+b], DP[i][j]+c)

ans = 10**9
A,B = Ma, Mb
while max(A,B) <= 400:
    ans = min(ans, DP[A][B])
    A += Ma
    B += Mb

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

Solution (Python):
import sys

input = sys.stdin.readline
sys.setrecursionlimit(100000)
mod = 10 ** 9 + 7


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


def read_index():
    return map(lambda x: int(x) - 1, input().split())


def read_list():
    return list(read_values())


def read_lists(N):
    return [read_list() for n in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v

    def __str__(self):
        return str(self.v)

    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n)

    def get(self, init=None):
        if self.v is None:
            return init
        else:
            return self.v


def main():
    N, MA, MB = read_values()
    dp = [
        [[10 ** 20 for b in range(N * 10 + 1)] for a in range(N * 10 + 1)]
        for n in range(N + 1)
    ]

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

    for n in range(1, N + 1):
        a, b, c = read_values()
        for aa in range(N * 10):
            for bb in range(N * 10 - b):
                if aa - a < 0 or bb - b < 0:
                    dp[n][aa][bb] = dp[n - 1][aa][bb]
                else:
                    dp[n][aa][bb] = min(
                        dp[n - 1][aa - a][bb - b] + c, dp[n - 1][aa][bb]
                    )

    res = V(min)
    max_k = (10 * N) // max(MA, MB)
    for k in range(1, max_k + 1):
        res.ud(dp[N][MA * k][MB * k])

    print(res if res.get() < 10 ** 20 else -1)


if __name__ == "__main__":
    main()

Solution (Python):
n,ma,mb=map(int,input().split())
abc=[list(map(int,input().split())) for i in range(n)]
dp=[[[10**9]*401 for i in range(401)] for j in range(n+1)]
dp[0][0][0]=0
for i in range(1,n+1):
  for j in range(401):
    for k in range(401):
      if j-abc[i-1][0]>=0 and k-abc[i-1][1]>=0:
        dp[i][j][k]=min(dp[i-1][j][k],dp[i-1][j-abc[i-1][0]][k-abc[i-1][1]]+abc[i-1][2])
      else:
        dp[i][j][k]=dp[i-1][j][k]
ans=10**18
for j in range(1,401):
  for k in range(1,401):
    if dp[n][j][k]<10**9 and k*ma==j*mb:
      ans=min(ans,dp[n][j][k])
if ans==10**18:
  print(-1)
else: 
  print(ans)

Solution (Python):
from copy import deepcopy
N, A, B = map(int, input().split())
ABC = [tuple(map(int, input().split())) for _ in range(N)]
R = 500

dp = [[10**18] * R for _ in range(R)]
dp[0][0] = 0

for a, b, c in ABC:
    newDp = deepcopy(dp)
    for x in range(R):
        for y in range(R):
            if x + a < R and y + b < R:
                newDp[x + a][y + b] = min(newDp[x + a][y + b], dp[x][y] + c)
    dp = newDp

ans = 10**18
for x in range(1, R):
    for y in range(1, R):
        if B * x == A * y:
            ans = min(ans, dp[x][y])
print(ans if ans < 10**18 else -1)

Solution (Python):


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

dp=[[10**9 for i in range(401)] for j in range(401)]
dp[0][0]=0

for _ in range(N):
    a,b,c=map(int,input().split())
    for i in range(401)[::-1]:
        for j in range(401)[::-1]:
            if dp[i][j]!=10**9:
                dp[i+a][j+b]=min(dp[i+a][j+b],dp[i][j]+c)

ans=10**9
for i in range(Ma,401)[::Ma]:
    j=i//Ma*Mb
    if j>=401:
        break
    ans=min(ans,dp[i][j])

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