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.



Sample input:
3 1 1
1 2 1
2 1 2
3 3 10


Sample output:
3

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# solution
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
def main():
    # import sys
    # readline = sys.stdin.readline
    # readlines = sys.stdin.readlines
    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()

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
from collections import Counter, deque
import itertools
#from scipy.misc import comb

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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!usr/bin/env python3
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

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
#!usr/bin/env python3
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

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
#!usr/bin/env python3
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

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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=[]
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# DP
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)