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:
import sys

INF = float('inf')

n, ma, mb = map(int, sys.stdin.readline().split())
abc = map(int, sys.stdin.read().split())
abc = list(zip(*[abc] * 3))

def main():
    sa = 0
    sb = 0
    for i in range(n):
        sa += abc[i][0]
        sb += abc[i][1]

    res = [[INF] * (sb + 1) for _ in range(sa + 1)]
    res[0][0] = 0

    for a, b, c in abc:

        for j in range(sa, -1, -1):
            for k in range(sb, -1, -1):
                if j >= a and k >= b:
                    res[j][k] = min(res[j][k], res[j-a][k-b] + c)

    ans = INF
    for i in range(1, min(sa//ma, sb//mb) + 1):
        ans = min(ans, res[ma*i][mb*i])

    if ans == INF:
        return -1
    else:
        return ans

if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np

I = np.array(sys.stdin.read().split(), dtype=np.int64)
n, ma, mb = I[:3]
abc = I[3:].reshape(n, 3)

def main():
    sa, sb = np.sum(abc, axis=0)[:2]
    res = np.full((sa+1, sb+1), np.inf)
    res[0, 0] = 0

    for a, b, c in abc:
        res[a:, b:] = np.minimum(res[a:, b:], res[:-a, :-b] + c)
    
    a = np.arange(ma, sa+1, ma)
    b = np.arange(mb, sb+1, mb)
    lim = min(sa//ma, sb//mb)
    ans = np.amin(res[a[:lim], b[:lim]])
    
    if ans == np.inf:
        return -1
    return int(ans)

if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np

I = np.array(sys.stdin.read().split(), dtype=np.int64)
n, ma, mb = I[:3]
abc = I[3:].reshape(n, 3)

def main():
    sa, sb = np.sum(abc, axis=0)[:2]
    res = np.full((sa+1, sb+1), np.inf)
    res[0, 0] = 0

    for a, b, c in abc:
        res[a:, b:] = np.minimum(res[a:, b:], res[:-a, :-b] + c)
    
    ans = np.inf
    for i in range(1, min(sa//ma, sb//mb) + 1):
        ans = np.minimum(ans, res[ma * i, mb * i])
    
    if ans == np.inf:
        return -1
    return int(ans)
    
if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

Solution code:
import sys

INF = float('inf')

n, ma, mb = map(int, sys.stdin.readline().split())
a, b, c = [], [], []
for _ in range(n):
    ai, bi, ci = map(int, sys.stdin.readline().split())
    a.append(ai); b.append(bi); c.append(ci)

def main():
    sa = sum(a)
    sb = sum(b)
    res = [[[INF] * (sb + 1) for _ in range(sa + 1)] for _ in range(n + 1)]
    res[0][0][0] = 0
    for i in range(n):
        for j in range(sa + 1):
            for k in range(sb + 1):
                if j >= a[i] and k >= b[i]:
                    res[i+1][j][k] = min(res[i][j-a[i]][k-b[i]] + c[i], res[i][j][k])
                else:
                    res[i+1][j][k] = res[i][j][k]
    ans = INF
    for i in range(1, min(sa//ma, sb//mb)+1):
        ans = min(ans, res[n][ma * i][mb * i])
    
    if ans == INF:
        return -1
    else:
        return ans

if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

Solution code:
N,Ma,Mb=map(int,input().split())
INF=40*100+1
dp=[[INF for i in range(10*N+1)] for j in range(10*N+1)]
dp[0][0]=0
ans=INF
for p in range(N):
  a,b,c=map(int,input().split())
  for i in range(len(dp)-1,-1,-1):
    for j in range(len(dp[i])-1,-1,-1):
      if dp[i][j]!=INF:
        dp[i+a][j+b]=min(dp[i+a][j+b],dp[i][j]+c)
        if (i+a)/(j+b)==Ma/Mb:
          ans=min(ans,dp[i+a][j+b])
          
if ans==INF:
  print(-1)
else:
  print(ans)

Submission status: Accepted

Solution code:
N, A, B = map(int, input().split())
X = [1<<20] * 4040
for _ in range(N):
    a, b, c = map(int, input().split())
    t = A*b-B*a
    X = [min(X[i], (0 if (i-t)%4040 == 0 else X[(i-t)%4040]) + c) for i in range(4040)]
print(-1 if X[0] > 1<<18 else X[0])

Submission status: Accepted

Solution code:
N, A, B = map(int, input().split())
X = [1<<20] * 8080
for _ in range(N):
    a, b, c = map(int, input().split())
    t = A*b-B*a
    X = [min(X[i], (0 if (i-t)%8080 == 0 else X[(i-t)%8080]) + c) for i in range(8080)]
print(-1 if X[0] > 1<<18 else X[0])

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N,M_a,M_b=list(map(int,input().split()))
    A,B,C=[],[],[]
    for i in range(N):
        a,b,c=list(map(int,input().split()))
        A.append(a)
        B.append(b)
        C.append(c)

    dp=[[[1e10 for i in range(sum(B)+max(B)+1)] for j in range(sum(A)+max(A)+1)] for k in range(N+1)]
    dp[0][0][0]=0

    for i in range(N):
        for c_a in range(sum(A)+1):
            for c_b in range(sum(B)+1):
                if dp[i][c_a][c_b]==1e10:
                    continue
                dp[i+1][c_a][c_b]=min(dp[i+1][c_a][c_b],dp[i][c_a][c_b])
                dp[i+1][c_a+A[i]][c_b+B[i]]=min(dp[i+1][c_a+A[i]][c_b+B[i]],dp[i][c_a][c_b]+C[i])
    ans_list=[1e10]
    for c_a in range(1,sum(A)+1):
        for c_b in range(1,sum(B)+1):
            if c_a*M_b==c_b*M_a:
                ans_list.append(dp[N][c_a][c_b])
    ans=min(ans_list)
    if  ans==1e10:
        print(-1)
    else:
        print(ans)

Submission status: Accepted

Solution code:


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 = float("inf")
sA = sum(a)
sB = sum(b)
# 薬A,Bの合計がi,jになるのに必要な最小金額
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 a[i] <= j and  b[i] <= k:
                dp[i+1][j][k] = min(dp[i][j][k], dp[i][j - a[i]][k - b[i]] + c[i])
            else:
                dp[i+1][j][k] = dp[i][j][k]


ans = INF

x = Ma
y = Mb
while x <= sA and y <= sB :

    ans = min(ans, dp[N][x][y])
    x += Ma
    y += Mb


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

Submission status: Accepted

Solution code:
import copy

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

INF, W = 10000, 10 * max(Ma, Mb) * N
dp = [[INF] * (2 * W + 1)]

for a, b, c in ABC:
    dp.append(copy.deepcopy(dp[-1]))
    df = a * Mb - b * Ma
    dp[-1][df] = min(dp[-1][df], c)
    for d in range(-W, W + 1):
        dp[-1][df + d] = min(dp[-1][df + d], c + dp[-2][d])

print("-1" if dp[N][0] == INF else dp[N][0])

Submission status: Accepted

Solution code:
import copy

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

INF, W = 10 ** 10, 10 * 10 * N
dp = [[INF] * (2 * W + 1)]

for a, b, c in ABC:
    dp.append(copy.deepcopy(dp[-1]))
    df = a * Mb - b * Ma
    dp[-1][df] = min(c, dp[-1][df])
    for d in range(-W, W + 1):
        dp[-1][df + d] = min(c + dp[-2][d], dp[-1][df + d])

print("-1" if dp[N][0] == INF else dp[N][0])

Submission status: Accepted

Solution code:
n,ma,mb=map(int,input().split())
abc = [list(map(int,input().split())) for _ in range(n)]
suma = sum([abc[i][0] for i in range(n)])+1
sumb = sum([abc[i][1] for i in range(n)])+1
dp = [[[5000 for j in range(sumb)]for i in range(suma)]for k in range(n+1)]
for i in range(n+1):
    dp[i][0][0] = 0
for k in range(n):
    for i in range(suma):
        for j in range(sumb):
            if i>=abc[k][0] and j>=abc[k][1]:
                dp[k+1][i][j] = min(dp[k][i][j],dp[k][i-abc[k][0]][j-abc[k][1]]+abc[k][2])
            else:
                dp[k+1][i][j]=dp[k][i][j]

flag = 0
res = 5000
for i in range(1,500):
    if ma*i >=suma or mb*i >= sumb:
        break
    res = min(res,dp[n][ma*i][mb*i])
    flag = 1
if res != 5000:
    print(res)
else:
    print("-1")

Submission status: Accepted

Solution code:
n, ma, mb = [int(i) for i in input().split()]
lst = [[int(i) for i in input().split()] for _ in range(n)]
lst.sort(key=lambda x: x[2])
dic = {}
def push(k,v):
    if k in dic:
        dic[k] = min(dic[k],v)
    else:
        dic[k] = v


for i in lst:
    k, v = i[0] * mb - i[1] * ma, i[2]
    l = [[j, dic[j]] for j in dic]
    for item in l:
        push(item[0] + k, item[1] + v)
    push(k, v)

print(dic[0] if 0 in dic else '-1')

Submission status: Accepted

Solution code:
inf=10**10
n,ma,mb=map(int,input().split())
abc=[list(map(int,input().split())) for _ in range(n)]
memo=[[inf]*401 for _ in range(401)]
memo[0][0]=0
for a,b,c in abc:
  for i in range(400,-1,-1):
    for j in range(400,-1,-1):
      if memo[i][j]!= inf :
        memo[i+a][j+b]= min(memo[i+a][j+b], memo[i][j] + c)


ans=inf
for i in range(1,min(400//ma,400//mb)):
  ans=min(ans, memo[ma*i][mb*i])

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
from itertools import product
from copy import deepcopy
def resolve():
    n,ma,mb=map(int,input().split())
    ABC=[tuple(map(int,input().split())) for _ in range(n)]
    M=400

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

    for a,b,c in ABC:
        for x,y in product(range(M,-1,-1),repeat=2):
            if(x+a<=M and y+b<=M):
                dp[x+a][y+b]=min(dp[x+a][y+b],dp[x][y]+c)

    ans=INF
    for x,y in product(range(1,M+1),repeat=2):
        if(x*mb==y*ma): ans=min(ans,dp[x][y])
    if(ans==INF): ans=-1
    print(ans)
resolve()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
from itertools import product
from copy import deepcopy
def resolve():
    n,ma,mb=map(int,input().split())
    ABC=[tuple(map(int,input().split())) for _ in range(n)]
    M=400

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

    for a,b,c in ABC:
        ndp=deepcopy(dp)
        for x,y in product(range(M+1),repeat=2):
            if(x+a<=M and y+b<=M):
                ndp[x+a][y+b]=min(ndp[x+a][y+b],dp[x][y]+c)
        dp=ndp

    ans=INF
    for x,y in product(range(1,M+1),repeat=2):
        if(x*mb==y*ma): ans=min(ans,dp[x][y])
    if(ans==INF): ans=-1
    print(ans)
resolve()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
from itertools import product
def resolve():
    n,ma,mb=map(int,input().split())
    ABC=[tuple(map(int,input().split())) for _ in range(n)]
    M=400

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

    for a,b,c in ABC:
        ndp=[a[:] for a in dp]
        for x,y in product(range(M+1),repeat=2):
            if(x+a<=M and y+b<=M):
                ndp[x+a][y+b]=min(ndp[x+a][y+b],dp[x][y]+c)
        dp=ndp

    ans=INF
    for x,y in product(range(1,M+1),repeat=2):
        if(x*mb==y*ma): ans=min(ans,dp[x][y])
    if(ans==INF): ans=-1
    print(ans)
resolve()

Submission status: Accepted

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

    dp = [[[float('inf')] * (405) for _ in range(405)] for _ in range(N + 5)]    
    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] == 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,401):
        for j in range(1,401):
            if Ma * j == Mb * i:
                ans = min(ans, dp[N][i][j])

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n,ma,mb=map(int,input().split())
dp=[[[4001]*401 for _ in range(401)] for j in range(n+1)]
dp[0][0][0]=0
a=[0]*n; b=[0]*n; c=[0]*n
for i in range(n):
    a[i],b[i],c[i]=map(int,input().split())

for i in range(n):
    for j in range(401):
        for k in range(401):
            if dp[i][j][k]==4001:
                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=4001
for i in range(1,401):
    for j in range(1,401):
        if i*mb==j*ma:
            ans=min(ans,dp[n][i][j])

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

Submission status: Accepted

Solution code:
from itertools import product

MAX = 10**4
N, Ma, Mb = map(int, input().split())
chems = [tuple(map(int, input().split())) for _ in range(N)]
chems = sorted(chems, key=lambda x: x[2])
rng = range(401)
dp = [[[None for _ in rng] for _ in rng] for _ in range(N+1)]
dp[0][0][0] = MAX
for i, (a, b, c) in enumerate(chems):
    for j, k in product(rng, repeat=2):
        if j < a or k < b:
            dp[i+1][j][k] = dp[i][j][k]
        else:
            if dp[i][j-a][k-b] == MAX:
                dp[i+1][j][k] = min(c, dp[i][j][k]) if dp[i][j][k] is not None else c
            elif dp[i][j-a][k-b] is not None:
                dp[i+1][j][k] = min(dp[i][j-a][k-b] + c, dp[i][j][k]) if dp[i][j][k] is not None else dp[i][j-a][k-b] + c
            else:
                dp[i+1][j][k] = dp[i][j][k]
ans = MAX
for j, k in product(rng, repeat=2):
    if j == 0 or k == 0: continue
    if j * Mb == k * Ma and dp[N][j][k] is not None:
        ans = min(ans, dp[N][j][k])
print(-1 if ans == MAX else ans)

Submission status: Accepted

Solution code:
import copy
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())
   
dp = [[float('inf')] * (sum(b)+1) for _ in range(sum(a)+1)]
dp[0][0] = 0
step = set([(0, 0)])

for i in range(n):
    ai, bi = a[i], b[i]

    s = step.copy()
    tempdp = copy.deepcopy(dp)
    for si in s: 
        if dp[si[0]+ai][si[1]+bi] > dp[si[0]][si[1]] + c[i]:
            tempdp[si[0]+ai][si[1]+bi] = dp[si[0]][si[1]] + c[i]
            step.add((si[0]+ai, si[1]+bi))
        
    dp = tempdp[:]
#28 20

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

Submission status: Accepted

Solution code:
n,ma,mb=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
suma=0;sumb=0
for i in range(n):
  suma+=l[i][0]
  sumb+=l[i][1]
dp=[[[float('inf') for i in range(sumb+1)] for i in range(suma+1)] for i in range(n+1)]
dp[0][0][0]=0
for i in range(n):
  for j in range(suma):
    for k in range(sumb):
      dp[i+1][j][k]=min(dp[i+1][j][k],dp[i][j][k])
      if j+l[i][0]<=suma and k+l[i][1]<=sumb:
        dp[i+1][j+l[i][0]][k+l[i][1]]=min(dp[i+1][j+l[i][0]][k+l[i][1]],dp[i][j][k]+l[i][2])
ans=[]
for j in range(suma+1):
  for k in range(sumb+1):
    if j*mb==k*ma and j*j!=0:
      ans.append(dp[n][j][k])
if min(ans)==float('inf'):
  print(-1)
else:
  print(min(ans))

Submission status: Accepted

Solution code:
n,ma,mb=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]
suma=0;sumb=0
for i in range(n):
  suma+=l[i][0]
  sumb+=l[i][1]
dp=[[[float('inf') for i in range(sumb+1)] for j in range(suma+1)] for i in range(n+1)]
dp[0][0][0]=0
for i in range(n):
  for j in range(suma):
    for k in range(sumb):
      dp[i+1][j][k]=min(dp[i+1][j][k],dp[i][j][k])
      if j+l[i][0]<=suma and k+l[i][1]<=sumb:
        dp[i+1][j+l[i][0]][k+l[i][1]]=min(dp[i+1][j+l[i][0]][k+l[i][1]],dp[i][j][k]+l[i][2])
ans=[]
for j in range(suma+1):
  for k in range(sumb+1):
    if j*mb==k*ma and j*j!=0:
      ans.append(dp[n][j][k])
if min(ans)==float('inf'):
  print(-1)
else:
  print(min(ans))

Submission status: Accepted

Solution code:
import numpy as np

n,ma,mb=map(int,input().split())
L=10*n

dp=np.zeros((L+1,L+1),dtype="int64")
dq=np.zeros((L+1,L+1),dtype="int64")

inf=10**18
dp+=inf
dp[0][0]=0

for i in range(n):
    a,b,c=map(int,input().split())
    dq=dp.copy()
    dq[a:,b:]=np.minimum(dq[a:,b:],dp[:-a,:-b]+c)
    dp=dq
    
ans=inf
for i in range(1,401):
    if ma*i > L or mb*i > L:
        break
    ans=min(ans,dp[ma*i][mb*i])
if ans==inf:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
import copy
n,ma,mb=map(int,input().split())
abc=[list(map(int,input().split())) for i in range(n)]
l=10*n
inf=float("inf")
data=[[inf]*(l+1) for i in range(l+1)]
data[0][0]=0
for u in abc:
    a,b,c=u
    h=copy.deepcopy(data)
    for i in range(l-a+1):
        for j in range(l-b+1):
            if data[i][j]!=inf:
                h[i+a][j+b]=min(h[i+a][j+b],data[i][j]+c)
    data=h
k=1
ans=inf
while ma*k<=l and mb*k<=l:
    if data[ma*k][mb*k]!=inf:
        ans=min(ans,data[ma*k][mb*k])
    k+=1
if ans!=inf:
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:

def examD(inf):
    N, Ma, Mb = LI()
    abc = [0,0,0]*N
    maxA = 0; maxB = 0
    for i in range(N):
        abc[i] = LI()
        if maxA<abc[i][0]:
            maxA=abc[i][0]
        if maxB<abc[i][1]:
            maxB=abc[i][1]
    dp = [[[inf]*(maxB*(N+1)+1) for _ in range(maxA*(N+1)+1)]for _ in range(N+1)]
    dp[0][0][0] = 0
    for i in range(N):
        for j in range(maxA*N+1):
            for k in range(maxB*N+1):
                dp[i+1][j][k] = min(dp[i][j][k],dp[i+1][j][k])
                dp[i+1][j+abc[i][0]][k+abc[i][1]] = min(dp[i][j+abc[i][0]][k+abc[i][1]],dp[i][j][k]+abc[i][2])
#    print(dp)
    ans = inf
    for j in range(1,maxA * N+1):
        for k in range(1,maxB * N+1):
            if j%Ma==0 and k%Mb==0:
                if j//Ma==k//Mb:
                    ans = min(ans,dp[N][j][k])
    if ans==inf:
        ans = -1
    print(ans)


import sys
import copy
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD(inf)

Submission status: Accepted

Solution code:
#masoyさんのコード

import numpy as np

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

dp = np.zeros((U + 1, U + 1), dtype = np.int64)
temp = np.zeros_like(dp)

INF = 10 ** 18
dp += INF
dp[0, 0] = 0

for _ in range(N):
    a, b, c = map(int, input().split())
    temp[:] = dp.copy()
    temp[a:, b:] = np.minimum(temp[a:, b:], dp[:-a, :-b] + c)
    dp = temp #毎回、一つ前の結果を残す

answer = INF
for t in range(1, 401):
    a = Ma * t
    b = Mb * t
    if max(a, b) >= U: #到達できない量に達したら終了
        break
    answer = min(answer, dp[a, b])

if answer == INF:
    answer = -1

print (answer)

Submission status: Accepted

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

INF = 10 ** 8

lst = [[INF] * 401 for i in range(401)]
lst[0][0] = 0

for _ in range(N):
    a, b, c = map(int, input().split())
    for i in range(400, a - 1, -1):
        for j in range(400, b - 1, -1):
            if lst[i- a][j - b] == INF:
                pass
            else:
                lst[i][j] = min(lst[i][j], lst[i - a][j - b] + c)

ans = INF
n = 400 // max(Ma, Mb)
for i in range(1, n + 1):
    ans = min(ans, lst[Ma * i][Mb * i])

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

# for i in range(7):
#     print (lst[i][:7])

Submission status: Accepted

Solution code:
n,ma,mb = map(int,input().split())
med = [tuple(map(int,input().split())) for _ in range(n)]
maa = sum(x[0] for x in med)
mab = sum(x[1] for x in med)
dp = [[10**6]*(mab+1) for _ in range(maa+1)]
dp[0][0] = 0
for a,b,c in med:
  for i in range(maa,a-1,-1):
    for j in range(mab,b-1,-1):
      if dp[i][j] > dp[i-a][j-b] + c:
        dp[i][j] = dp[i-a][j-b] + c
mi = min(dp[ma*i][mb*i] for i in range(1,min(maa//ma,mab//mb)+1))
if mi > 10**5:
  mi = -1
print(mi)

Submission status: Accepted

Solution code:
n,ma,mb=map(int,input().split())
dp=[[[10**30 for j in range(401)]for i in range(401)]for k in range(n+1)]
dp[0][0][0]=0 
l=[]
for i in range(n):
    a,b,c=map(int,input().split())
    l.append([a,b,c])
for i in range(1,n+1):
    a,b,c=l[i-1][0],l[i-1][1],l[i-1][2]
    for x in range(401):
        for y in range(401):
            if x<a or y<b:
                dp[i][x][y]=dp[i-1][x][y]
                continue 
            dp[i][x][y]=min(dp[i-1][x][y],dp[i-1][x-a][y-b]+c)
            
mini=10**30 
for i in range(1,40):
    mini=min(mini,dp[n][ma*i][mb*i])
if mini>=10**30 :
    print(-1)
else:
    print(mini)

Submission status: Accepted

Solution code:
def main():
    n,ma,mb = map(int,input().split())
    dp = [[[float("inf") for _ in range(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 = map(int,input().split())
        for j in range(401):
            for k in range(401):
                if a<=j and b<=k:
                    dp[i][j][k] = min(dp[i-1][j][k],dp[i-1][j-a][k-b]+c)
                else:
                    dp[i][j][k] = dp[i-1][j][k]
    ans = float("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==float("inf"):
        print(-1)
    else:
        print(ans)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def d():
    nmax, mmax = 40, 10
    n, ma, mb = map(int, input().split())
    a, b, c = [], [], []
    for i in range(n):
        a_, b_, c_ = map(int, input().split())
        a.append(a_)
        b.append(b_)
        c.append(c_)

    dp = [[[float('inf') for _ in range(n*mmax+1)] for _ in range(n*mmax+1) ] for _ in range(n+1)]
    dp[0][0][0] = 0

    for i in range(n):
        for ca in range(n*mmax+1):
            for cb in range(n*mmax+1):
                if dp[i][ca][cb] == float('inf'):
                    continue
                dp[i+1][ca][cb] = min(dp[i+1][ca][cb], dp[i][ca][cb])
                if ca + a[i] < n*mmax+1 and cb + b[i] < n*mmax+1:
                    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 = float('inf')
    for ca in range(1, n*mmax+1):
        for cb in range(1, n*mmax+1):
            if ma * cb == mb * ca:
                ans = min(ans, dp[n][ca][cb])

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


if __name__ == '__main__':
    d()

Submission status: Accepted

Solution code:
inf=10**10
n,ma,mb=map(int,input().split())
abc=[list(map(int,input().split())) for _ in range(n)]
memo=[[inf]*401 for _ in range(401)]
memo[0][0]=0
for a,b,c in abc:
  for i in range(400,-1,-1):
    for j in range(400,-1,-1):
      if memo[i][j]!= inf :
        memo[i+a][j+b]= min(memo[i+a][j+b], memo[i][j] + c)


ans=inf
for i in range(1,min(400//ma,400//mb)):
  ans=min(ans, memo[ma*i][mb*i])

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

Submission status: Accepted

Solution code:
n, ma, mb = map(int, input().split(" "))
info_med = []
for _ in range(n):
  info_med.append(list(map(int, input().split(" "))))
info_med.sort(key=lambda x: x[2])
dp = []
dp.append([(0, 0, [-1])])

for min_c in range(1, n * 100 + 1):
  temp_list = []
  for i, (a, b, c) in enumerate(info_med):
    if min_c - c >= 0:
      for pre_a, pre_b, pre_i in dp[min_c - c]:
        if pre_i[-1] < i:
          if (a + pre_a) * mb == (b + pre_b) * ma:
            print(min_c)
            exit()
          temp_list.append((a + pre_a, b + pre_b, pre_i + [i]))
  dp.append(temp_list)
print("-1")

Submission status: Accepted

Solution code:

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

lis = [[float("inf")] * 401 for i in range(401)]
lis[0][0] = 0

for x in range(N):

    a,b,c = map(int,input().split())

    olis = []
    for i in lis:
        new = []
        for j in i:
            new.append(j)
        olis.append(new)

    for i in range(401):

        for j in range(401):

            if olis[i][j] != float("inf"):

                if i + a < 400 and j + b < 400:

                    lis[i+a][j+b] = min( olis[i][j] + c , lis[i+a][j+b] )

ans = float("inf")

for i in range(400):

    i += 1

    for j in range(400):

        j += 1

        if i * Mb == j * Ma:

            ans = min(ans , lis[i][j])

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

Submission status: Accepted

Solution code:
def getlist():
	return list(map(int, input().split()))

INF = float("inf")
N, Ma, Mb = getlist()
L = [[INF] * 401 for i in range(401)]
L[0][0] = 0

#処理
for i in range(N):
	a, b, c = getlist()
	for j in range(401):
		for k in range(401):
			if L[400 - j][400 - k] != INF:
				L[400 - j + b][400 - k + a] = min(L[400 - j][400 - k] + c, L[400 - j + b][400 - k + a])


#最小値
ans = INF
x = Ma
y = Mb
while y <= 400 and x <= 400:
	ans = min(ans, L[y][x])
	x += Ma
	y += Mb

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

Submission status: Accepted

Solution code:
import sys
import numpy as np
input = sys.stdin.readline

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

    INF = int(1e6)
    MAX = (N+1) * 10 + 1
    dp = np.full((MAX, MAX), INF, dtype=np.int64)
    tmp = np.zeros_like(dp)

    dp[0][0] = 0
    for i in range(N):
        a, b, c = map(int, input().split())
        tmp = dp.copy()
        tmp[a:, b:] = np.minimum(tmp[a:, b:], dp[:-a, :-b] + c)
        dp = tmp
    
    ans = INF
    a, b = Ma, Mb
    while a < MAX and b < MAX:
        ans = min(ans, dp[a][b])
        a += Ma
        b += Mb
    
    if ans == INF:
        ans = -1
    
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, Ma, Mb = map(int, input().split())
l = []
for i in range(N):
    l.append(list(map(int, input().split())))
ans = float('inf')

Mab = 10 * N + 1
dp = [[[float('inf')] * Mab for _ in range(Mab)] for _ in range(N + 1)]
dp[0][0][0] = 0
for n in range(N):
    for a in range(10 * N):
        for b in range(10 * N):
            if dp[n][a][b] == float('inf'):
                continue
            dp[n + 1][a][b] = min(dp[n + 1][a][b], dp[n][a][b])
            dp[n + 1][a + l[n][0]][b + l[n][1]] = min(dp[n + 1][a + l[n][0]][b + l[n][1]], dp[n][a][b] + l[n][2])
# np.set_printoptions(threshold=np.inf)
# print(dp)
for a in range(1, 10 * N):
    for b in range(1, 10 * N):
        if a * Mb == b * Ma:
            ans = min(ans, dp[n + 1][a][b])
if ans == float('inf'):
    print(-1)
else:
    print(int(ans))

Submission status: Accepted

Solution code:
N,Ma,Mb = map(int,input().split())
ABC = [list(map(int,input().split())) for i in range(N)]

dp = [[[float("inf")]*(10*N+1) for i in range(10*N+1)] for j in range(N+1)]
for i in range(N+1):
    dp[i][0][0] = 0

for i in range(1,N+1):
    for A in range(10*N+1):
        for B in range(10*N+1):
            a,b,c = ABC[i-1]
            dp[i][A][B] = min(dp[i][A][B], dp[i-1][A][B], c+dp[i-1][A-a][B-b])
            
i = 1
ans = float("inf")
while i*Ma <= 10*N and i*Mb <= 10*N:
    ans = min(ans, dp[N][i*Ma][i*Mb])
    i += 1
if ans == float("inf"):
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
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)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, a_ratio, b_ratio = LI()
L = LIR(n)
A, B, cost = [[L[i][j] for i in range(n)] for j in range(3)]
dp = [[INF] * 401 for _ in range(401)]
dp[0][0] = 0
for i in range(n):
    for j in range(401, -1, -1):
        for k in range(401, -1, -1):
            if 0 <= j + A[i] <= 400 and 0 <= k + B[i] <= 400:
                dp[j + A[i]][k + B[i]] = min(dp[j + A[i]][k + B[i]], dp[j][k] + cost[i])



ans = INF
for l in range(1, min(400 // b_ratio, 400 // a_ratio)):
    ans = min(ans, dp[a_ratio * l][b_ratio * l])


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

Submission status: Accepted

Solution code:
import copy

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

INF, W = 10 ** 10, N * 10 * max(Ma, Mb)
dp = [[INF] * (W * 2 + 1)]

for a, b, c in ABC:
    dp.append(copy.deepcopy(dp[-1]))
    df = a * Mb - b * Ma
    dp[-1][df + W] = min(dp[-1][df + W], c)
    for d in range(-W, W + 1):
        if not dp[-2][d] == INF:
            dp[-1][df + d] = min(c + dp[-2][d], dp[-2][df + d], dp[-1][df + d])

print("-1" if dp[N][W] == INF else dp[N][W])

Submission status: Accepted

Solution code:
N, Ma, Mb = map(int, input().split())
ABC = [tuple(map(int, input().split())) for _ in range(N)]
ABC.sort(key=lambda x: x[2])

dp = [[(0, 0, [-1])]]

for p in range(1, N * 100 + 1):
    tmp = []
    for i, (a, b, c) in enumerate(ABC):
        if p - c >= 0:
            for ta, tb, ti in dp[p - c]:
                if ti[-1] < i:
                    if (a + ta) * Mb == (b + tb) * Ma:
                        print(p)
                        exit()
                    tmp.append((a + ta, b + tb, ti + [i]))
    dp.append(tmp)

print("-1")