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:
#       )                         .MMdMMM;                    .M#(,dM]                        .:
#       )                        .M@d@d#Mb                    dMGMM(MN                        .:
#       )                        dM1MCdbMM_                  .M@MSN;dM-                       .:
#       )                       .M#(#?d#(Mb.. ...............JMIMIMLdM]                       .:
#       )                      ..M#?NHM# ?MMMMMMMMMH"""""""HMM@(Nad#(Mb                       .:
#       )                  .(MMMMH=..-(Jg[   `               daggmp  MNm,                     .:
#       )                ..MMY`     !qN;                       (J     ?MMN,                   .:
#       )               (MMD`       .MM@                      .MM{      .WMN,                 .:
#       )              dM@`         dMM%                      .MM}        (MMm                .:
#       )             .M#`          dMN!                      .MM}        ~(MMr               .:
#       )            .MM:           dM#                       .MM}        ..JM#               .:
#       )      ``    .M@            dMF                       .MM}        .~(M#   `  .        .:
#       )      ?MMMMMMMNgJ,         .7`                        T"!       .JJjMNgNNMMMB^       .:
#       )            dMr ?Mm,                                            7YYMM#!`             .:
#       )            dMb   ?`                  .,   ` ..                 ~.(MM%               .:
#       )         ..JgMMNNN)                  .Mn.(Mm-JM{               dHHMMNag.,            .:
#       )         ("!  ?MN,..,                (NM@"`~OMM}             ..~-(M@` _?"M[          .:
#       )               ,MMM#_                .M#    .M#`           .~.~JMMMNg-..             .:
#       )           ..gMB"?TMMNJ..              `     _`         .~~.(JdM#^  _"TMMNgJ-,       .:
#       )        .gMB=`      _7WMMNaJ...                 ....__-(JJgMMMM^          ?T"^       .:
#       )           ............  ?TWMMMMNNNNgggggggggggggNNMMMMMMBY7`...                     .:
#       )           WMMMMMMMMMMMMMNgJ,   (dMMMM#"TYYYYYYMMMMMMMNgMMMMMMMMMMMNNgJ-,            .:
#       )            -?TMNmuuuuUWMMMMMMNNMM@g@MMmJ--_(JMMg@g@@gMMMMMMWUXXXUWMMMMMMNe.         .:

def push(k,v):
    if k in dic:
        dic[k] = min(dic[k],v)
    else:
        dic[k] = v
n,x,y = map(int,str(input()).split(' '))
dic = {}
for i in range(0,n):
    a = list(map(int,str(input()).split(' ')))
    k,v =a[0]*y-a[1]*x,a[2]
    l = []
    for j in dic:
        l.append([j,dic[j]])
    for jj in l:
        push(jj[0]+k,jj[1]+v)
    push(k,v)
if 0 in dic:
    print(dic[0])
else:
    print('-1')

Submission status: Accepted

Solution code:
n, ma, mb = map(int, input().split())
abc = [tuple(map(int, input().split())) for _ in range(n)]
csup = sum(_abc[2] for _abc in abc) + 1
dp = [[[csup for _ in range(400)] for _ in range(400)] for _ in range(n+1)]
dp[0][0][0] = 0

for i in range(n):
  for _a in range(400):
    for _b in range(400):
      if dp[i][_a][_b] != csup:
        dp[i+1][_a][_b] = min(dp[i+1][_a][_b], dp[i][_a][_b])
        dp[i+1][_a + abc[i][0]][_b + abc[i][1]] = min(dp[i+1][_a + abc[i][0]][_b + abc[i][1]], dp[i][_a][_b] + abc[i][2])

ans = csup
for ai in range(1, 400):
  for bi in range(1, 400):
    if ai * mb == bi * ma:
      ans = min(ans, dp[n][ai][bi])
if ans == csup:
  print(-1)
else:
  print(ans)

Submission status: Accepted

Solution code:
M_ab=400
M_n=40
inf=float('inf')
n,Ma,Mb=map(int,input().split())
A,B,C=[],[],[]
for _ in range(n):
    a,b,c=map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(c)
DP=[[[inf]*(M_ab+1) for _ in range(M_ab+1)] for _ in range(M_n+1)]
DP[0][0][0]=0
for i in range(n):
    for ca in range(M_ab+1):
        for cb in range(M_ab+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,M_ab+1):
    for cb in range(1,M_ab+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:
def push(k,v):
    if k in dic:
        dic[k] = min(dic[k],v)
    else:
        dic[k] = v
n,x,y = map(int,str(input()).split(' '))
dic = {}
for i in range(0,n):
    a = list(map(int,str(input()).split(' ')))
    k,v =a[0]*y-a[1]*x,a[2]
    l = []
    for j in dic:
        l.append([j,dic[j]])
    for jj in l:
        push(jj[0]+k,jj[1]+v)
    push(k,v)
if 0 in dic:
    print(dic[0])
else:
    print('-1')

Submission status: Accepted

Solution code:
from math import isinf

N, Ma, Mb = map(int, input().split())
a,b,c = [],[],[]
aappend = a.append
bappend = b.append
cappend = c.append
for i in range(N):
    ai,bi,ci = map(int, input().split())
    aappend(ai)
    bappend(bi)
    cappend(ci)

W_MAX = 40*10
dp = [[float('inf')]*(W_MAX+1) for _ in range(W_MAX+1)]
dp[0][0] = 0

for i in range(N):
    for wa in reversed(range(W_MAX+1)):
        for wb in reversed(range(W_MAX+1)):
            if isinf(dp[wa][wb]): continue
            if wa+a[i]<=W_MAX and wb+b[i]<=W_MAX:
                dp[wa+a[i]][wb+b[i]] = min(dp[wa+a[i]][wb+b[i]],
                                           dp[wa][wb]+c[i])

ans = float('inf')
for wa in range(1, W_MAX+1):
    for wb in range(1, W_MAX+1):
        if wa*Mb == wb*Ma:
            ans = min(ans, dp[wa][wb])

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

Submission status: Accepted

Solution code:
from math import isinf

N, Ma, Mb = map(int, input().split())
a,b,c = [],[],[]
for i in range(N):
    ai,bi,ci = map(int, input().split())
    a.append(ai)
    b.append(bi)
    c.append(ci)

W_MAX = 40*10
dp = [[float('inf')]*(W_MAX+1) for _ in range(W_MAX+1)]
dp[0][0] = 0

for i in range(N):
    for wa in reversed(range(W_MAX+1)):
        for wb in reversed(range(W_MAX+1)):
            if isinf(dp[wa][wb]): continue
            if wa+a[i]<=W_MAX and wb+b[i]<=W_MAX:
                dp[wa+a[i]][wb+b[i]] = min(dp[wa+a[i]][wb+b[i]],
                                           dp[wa][wb]+c[i])

ans = float('inf')
for wa in range(1, W_MAX+1):
    for wb in range(1, W_MAX+1):
        if wa*Mb == wb*Ma:
            ans = min(ans, dp[wa][wb])

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

Submission status: Accepted

Solution code:
from math import isinf

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

W_MAX = 40*10
dp = [[float('inf')]*(W_MAX+1) for _ in range(W_MAX+1)]
dp[0][0] = 0

for i in range(N):
    for wa in reversed(range(W_MAX+1)):
        for wb in reversed(range(W_MAX+1)):
            if isinf(dp[wa][wb]): continue
            if wa+a[i]<=W_MAX and wb+b[i]<=W_MAX:
                dp[wa+a[i]][wb+b[i]] = min(dp[wa+a[i]][wb+b[i]],
                                           dp[wa][wb]+c[i])

ans = float('inf')
for wa in range(1, W_MAX+1):
    for wb in range(1, W_MAX+1):
        if wa*Mb == wb*Ma:
            ans = min(ans, dp[wa][wb])

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

Submission status: Accepted

Solution code:
G_MAX = 40*10
N_MAX = 40
INF = float('inf')

dp = [[[INF]*(G_MAX+1) for _ in range(G_MAX+1)] 
      for _ in range(N_MAX+1)]
N,Ma,Mb = map(int, input().split())
a,b,c = [],[],[]
for _ in range(N):
    ai,bi,ci = map(int, input().split())
    a.append(ai)
    b.append(bi)
    c.append(ci)

dp[0][0][0] = 0
from math import isinf
for i in range(N):
    for ca in range(G_MAX+1):
        for cb in range(G_MAX+1):
            if isinf(dp[i][ca][cb]): 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,G_MAX+1):
    for cb in range(1,G_MAX+1):
        if ca*Mb==cb*Ma:
            ans = min(ans, dp[N][ca][cb])
            
if isinf(ans): ans = -1
print(ans)

Submission status: Accepted

Solution code:
N, Ma, Mb = map(int, input().split())
a = [0 for _ in range(N)]
b = [0 for _ in range(N)]
c = [0 for _ in range(N)]
for i in range(N):
    a[i], b[i], c[i] = map(int, input().split())
inf = 1000000
nmax = 40
abmax = 10
dp = [[[inf for _ in range(nmax * abmax + 1)] for _ in range(nmax * abmax + 1)] for _ in range(nmax + 1)]
dp[0][0][0] = 0
for i in range(N):
    for ca in range(nmax * abmax + 1):
        for cb in range(nmax * abmax + 1):
            if(dp[i][ca][cb] == inf):
                continue
            dp[i + 1][ca][cb] = min(dp[i + 1][ca][cb], dp[i][ca][cb])
            dp[i + 1][ca + a[i]][cb + b[i]] = min(dp[i + 1][ca + a[i]][cb + b[i]], dp[i][ca][cb] + c[i])
ans = inf
for ca in range(1, nmax * abmax + 1):
    for cb in range(1, nmax * abmax + 1):
        if ca * Mb == cb * Ma:
            ans = min(ans, dp[N][ca][cb])
print(-1 if ans == inf else ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

from copy import deepcopy

ABMAX = 400
HUGE = 10 ** 12

def main():
    n, ma, mb = map(int, input().split())
    sold = []
    for i in range(n):
        a, b, c = map(int, input().split())
        sold.append((a, b, c))
    mincost = dp(sold, n)
    print(find_mincost(mincost, ma, mb))

def find_mincost(mincost, ma, mb):
    res = HUGE
    for mult in range(1, ABMAX):
        if ma * mult > ABMAX:
            break
        if mb * mult > ABMAX:
            break
        res = min(res, mincost[ma * mult][mb * mult])
    return -1 if res == HUGE else res

def dp(sold, n):
    # mincost[a][b]: Minimum cost of achieving a, b
    mincost = [[HUGE for b in range(ABMAX + 1)] for a in range(ABMAX + 1)]
    mincost[0][0] = 0
    for a, b, c in sold:
        update(mincost, a, b, c)
    return mincost

def update(mincost, a, b, c):
    for ia in reversed(range(ABMAX + 1)):  # 配る
        for ib in reversed(range(ABMAX + 1)):
            na = ia + a
            nb = ib + b
            if na <= ABMAX and nb <= ABMAX:
                mincost[na][nb] = min(mincost[na][nb], mincost[ia][ib] + c)

main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

from copy import deepcopy

ABMAX = 400
HUGE = 10 ** 12

def main():
    n, ma, mb = map(int, input().split())
    sold = []
    for i in range(n):
        a, b, c = map(int, input().split())
        sold.append((a, b, c))
    mincost = dp(sold, n)
    print(find_mincost(mincost, ma, mb))

def find_mincost(mincost, ma, mb):
    res = HUGE
    for mult in range(1, ABMAX):
        if ma * mult > ABMAX:
            break
        if mb * mult > ABMAX:
            break
        res = min(res, mincost[ma * mult][mb * mult])
    return -1 if res == HUGE else res

def dp(sold, n):
    # mincost[a][b]: Minimum cost of achieving a, b
    mincost = [[HUGE for b in range(ABMAX + 1)] for a in range(ABMAX + 1)]
    mincost[0][0] = 0
    for a, b, c in sold:
        mincost = update(mincost, a, b, c)
    return mincost

def update(prevcost, a, b, c):
    mincost = deepcopy(prevcost)
    for ia in range(ABMAX + 1):  # 配る
        for ib in range(ABMAX + 1):
            na = ia + a
            nb = ib + b
            if na <= ABMAX and nb <= ABMAX:
                mincost[na][nb] = min(mincost[na][nb], prevcost[ia][ib] + c)
    return mincost

main()

Submission status: Accepted

Solution code:
n,Ma,Mb=map(int,input().split())
inf=float("inf")
dp=[[[inf]*401 for _ in range(401)] for _ in range(n+1)]
dp[0][0][0]=0

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

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

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

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

Submission status: Accepted

Solution code:
n,Ma,Mb=map(int,input().split())
inf=float("inf")
dp=[[[inf]*401 for _ in range(401)] for _ in range(n+1)]
dp[0][0][0]=0

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

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

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

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

Submission status: Accepted

Solution code:
n,Ma,Mb=map(int,input().split())
inf=float("inf")
dp=[[[inf]*401 for _ in range(401)] for _ in range(n+1)]
dp[0][0][0]=0

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

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

ans=inf
for i in range(1,401):
    for j in range(1,401):
        if i*Mb==j*Ma:
            ans=min(ans,dp[n][i][j])

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

Submission status: Accepted

Solution code:
def main():
    INF = 10 ** 5
    n, ma, mb = map(int, input().split())
    dp = [[INF] * 401 for _ in range(401)]
    dp[0][0] = 0
    for _ in range(n):
        a, b, c = map(int, input().split())
        for i in range(400 - a, -1, -1):
            dpi = dp[i]
            dpia = dp[i + a]
            for j in range(400 - b, -1, -1):
                if dpia[j + b] > dpi[j] + c:
                    dpia[j + b] = dpi[j] + c
    
    ans = INF
    for i in range(1, 400):
        if ma * i > 400 or mb * i > 400:
            break
        ans = min(ans, dp[ma * i][mb * i])
    if ans >= INF:
        print(-1)
    else:
        print(ans)

main()

Submission status: Accepted

Solution code:
# coding:utf-8

import sys

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


n, ma, mb = LI()
src = []
a_max, b_max = 1, 1
for _ in range(n):
    a, b, c = LI()
    src.append((a, b, c))
    a_max += a
    b_max += b

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


for i in range(n):
    ai, bi, ci = src[i]
    for a in range(a_max - ai):
        for b in range(b_max - bi):
            if dp[i][a][b] == INF:
                continue
            dp[i + 1][a][b] = min(dp[i + 1][a][b], dp[i][a][b])
            dp[i + 1][a + ai][b + bi] = min(dp[i][a + ai][b + bi], dp[i][a][b] + ci)


ans = INF
c = 1
while True:
    a, b = c * ma, c * mb
    if a >= a_max or b >= b_max:
        break
    ans = min(ans, dp[n][a][b])
    c += 1

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

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
# from collections import Counter, defaultdict

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


n, ma, mb = LI()
abc = [tuple(LI()) for _ in range(n)]
ab_max = n * 10 + 1

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


for i in range(n):
    ai, bi, ci = abc[i]
    for a in range(ab_max - ai):
        for b in range(ab_max - bi):
            if dp[i][a][b] == INF:
                continue
            dp[i + 1][a][b] = min(dp[i + 1][a][b], dp[i][a][b])
            dp[i + 1][a + ai][b + bi] = min(dp[i][a + ai][b + bi], dp[i][a][b] + ci)


ans = INF
c = 1
while True:
    a, b = c * ma, c * mb
    if a >= ab_max or b >= ab_max:
        break
    ans = min(ans, dp[n][a][b])
    c += 1

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

"""
参考：https://img.atcoder.jp/abc054/editorial.pdf
・3次元DP
・送るDP
"""
 
import sys
def input(): return sys.stdin.readline().strip()
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7
 
N, Ma, Mb = map(int, input().split())
l = [[0] * 3 for i in range(N)]
for i in range(N):
    l[i][0], l[i][1], l[i][2] = map(int, input().split())

# 不可能な組み合わせは0とは区別するのでINF初期化
dp = [[[INF] * 401  for i in range(401)] for i 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: continue
            dp[i+1][j][k] = min(dp[i][j][k], dp[i+1][j][k])
            dp[i+1][j+l[i][0]][k+l[i][1]] = min(dp[i][j][k] + l[i][2], dp[i+1][j+l[i][0]][k+l[i][1]])

ans = INF
for j in range(1, 401):
    for k in range(1, 401):
        # j/k == Ma/Mb だと分母0が心配なので
        if j * Mb == k * Ma:
            ans = min(dp[N][j][k], ans)

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

Submission status: Accepted

Solution code:
N, Ma, Mb = (list)(map(int, input().split()))
l = []
inf = 10**6
dp = [[[inf]*401 for _ in range(401)] for _ in range(41)]


for _ in range(N):
    l.append((list)(map(int, input().split())))

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

for i in range(N):
    for ca in range(401):
        for cb in range(401):
            if dp[i][ca][cb] != inf:
                dp[i+1][ca][cb] = min(dp[i+1][ca][cb], dp[i][ca][cb])
                t_a = l[i][0]
                t_b = l[i][1]
                dp[i+1][ca+t_a][cb+t_b] =\
                    min(dp[i+1][ca+t_a][cb+t_b], dp[i][ca][cb]+l[i][2])

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

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

Submission status: Accepted

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

a_sum = sum([item[0] for item in items])
b_sum = sum([item[1] for item in items])
inf = float("inf")
dp = [[[inf for _ in range(a_sum + 50)] for _ in range(b_sum + 50)] for i in range(N + 1)]
dp[0][0][0] = 0

for i in range(N):
    for a in range(a_sum):
        for b in range(b_sum):
            if dp[i + 1][a][b] > dp[i][a][b]:
                dp[i + 1][a][b] = dp[i][a][b]
            if dp[i + 1][a + items[i][0]][b + items[i][1]] > dp[i][a][b] + items[i][2]:
                dp[i + 1][a + items[i][0]][b + items[i][1]] = dp[i][a][b] + items[i][2]

ans = inf
for a in range(1, a_sum + 1):
    for b in range(1, b_sum + 1):
        if a * Mb == b * Ma:
            ans = min(ans, dp[N][a][b])
if ans != inf:
    print(ans)
else:
    print(-1)

Submission status: Accepted

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

a_sum = sum([item[0] for item in items])
b_sum = sum([item[1] for item in items])
inf = float("inf")
dp = [[[inf for _ in range(a_sum + 60)] for _ in range(b_sum + 60)] for i in range(N + 1)]
dp[0][0][0] = 0

for i in range(N):
    for a in range(a_sum):
        for b in range(b_sum):
            if dp[i + 1][a][b] > dp[i][a][b]:
                dp[i + 1][a][b] = dp[i][a][b]
            if dp[i + 1][a + items[i][0]][b + items[i][1]] > dp[i][a][b] + items[i][2]:
                dp[i + 1][a + items[i][0]][b + items[i][1]] = dp[i][a][b] + items[i][2]

ans = inf
for a in range(1, a_sum + 1):
    for b in range(1, b_sum + 1):
        if a * Mb == b * Ma:
            ans = min(ans, dp[N][a][b])
if ans != inf:
    print(ans)
else:
    print(-1)

Submission status: Accepted

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

a_sum = sum([item[0] for item in items])
b_sum = sum([item[1] for item in items])
inf = float("inf")
dp = [[[inf for _ in range(a_sum + 100)] for _ in range(b_sum + 100)] for i in range(N + 1)]
dp[0][0][0] = 0

for i in range(N):
    for a in range(a_sum + 1):
        for b in range(b_sum + 1):
            if dp[i + 1][a][b] > dp[i][a][b]:
                dp[i + 1][a][b] = dp[i][a][b]
            if dp[i + 1][a + items[i][0]][b + items[i][1]] > dp[i][a][b] + items[i][2]:
                dp[i + 1][a + items[i][0]][b + items[i][1]] = dp[i][a][b] + items[i][2]

ans = inf
for a in range(1, a_sum + 1):
    for b in range(1, b_sum + 1):
        if a * Mb == b * Ma:
            ans = min(ans, dp[N][a][b])
if ans != inf:
    print(ans)
else:
    print(-1)

Submission status: Accepted

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

a_sum = sum([item[0] for item in items])
b_sum = sum([item[1] for item in items])
inf = float("inf")
dp = [[[inf for _ in range(401)] for _ in range(401)] for i in range(N+1)]
dp[0][0][0] = 0

for i in range(N):
    for a in range(a_sum + 1):
        for b in range(b_sum + 1):
            dp[i + 1][a][b] = min(dp[i + 1][a][b], dp[i][a][b])
            dp[i + 1][a + items[i][0]][b + items[i][1]] = min(dp[i + 1][a + items[i][0]][b + items[i][1]],
                                                              dp[i][a][b] + items[i][2])
ans = inf
for a in range(1, a_sum + 1):
    for b in range(1, b_sum + 1):
        if a * Mb == b * Ma:
            ans = min(ans, dp[N][a][b])
if ans != inf:
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
N, Ma, Mb = map(int, input().split())
A, B, C = [], [], []
for i in range(N):
    ai, bi, ci = map(int, input().split())
    A.append(ai)
    B.append(bi)
    C.append(ci)

INF = float('inf')
dp = [[[INF] * 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 a in range(401):
        for b in range(401):
            if a - A[i - 1] >= 0 and b - B[i - 1] >= 0:
                dp[i][a][b] = min(dp[i][a][b], dp[i - 1][a][b], dp[i - 1][a - A[i - 1]][b - B[i - 1]] + C[i - 1])
            else:
                dp[i][a][b] = min(dp[i][a][b], dp[i - 1][a][b])
ans = INF
for i in range(1, 401):
    target_a = Ma * i
    target_b = Mb * i
    if target_a > 400 or target_b > 400:
        break
    ans = min(ans, dp[N][target_a][target_b])

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

Submission status: Accepted

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

# dp[i][a][b] := i個目の薬品までで薬品Aがaグラム、薬品Bがbグラムの混合液を作るために必要な最小予算
dp = [[[float('inf')] * 401 for j in range(401)] for i in range(N + 1)]
dp[0][0][0] = 0

for i in range(1, N + 1):
    ai, bi, ci = Items[i - 1]
    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][a][b], dp[i - 1][a][b], dp[i - 1][a - ai][b - bi] + ci)
            else:
                dp[i][a][b] = min(dp[i][a][b], dp[i - 1][a][b])

ans = float('inf')
i = 1
while Ma * i <= 400 and Mb * i <= 400:
    ans = min(ans, dp[N][Ma * i][Mb * i])
    i += 1

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

Submission status: Accepted

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

# dp[i][a][b] := i個目の薬品までで薬品Aがaグラム、薬品Bがbグラムの混合液を作るために必要な最小予算
dp = [[[float('inf')] * 401 for j in range(401)] for i in range(N + 1)]
dp[0][0][0] = 0

for i in range(N):
    ai, bi, ci = Items[i]
    for a in range(401):
        for b in range(401):
            if a + ai <= 400 and b + bi <= 400:
                dp[i + 1][a + ai][b + bi] = min(dp[i][a][b] + ci, dp[i + 1][a + ai][b + bi])
            dp[i + 1][a][b] = min(dp[i][a][b], dp[i + 1][a][b])

ans = float('inf')
i = 1
while Ma * i <= 400 and Mb * i <= 400:
    ans = min(ans, dp[N][Ma * i][Mb * i])
    i += 1

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

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n,ma,mb = li()
drugs = []
for _ in range(n):
    drugs.append(tuple(li()))
    
dp = [[[10**10]*(401) for _ in range(401)] for _ in range(n+1)]
dp[0][0][0] = 0

for i in range(n):
    ca, cb, cc = drugs[i]
    for aa in range(401):
        for bb in range(401):
            if 0 <= aa - ca <= 400 and 0 <= bb - cb <= 400:
                dp[i+1][aa][bb] = min(dp[i][aa-ca][bb-cb] + cc,
                                      dp[i][aa][bb])
                
            else:
                dp[i+1][aa][bb] = dp[i][aa][bb]

ans = 10**10     
for aa in range(1,401):
    for bb in range(1,401):
        if (aa/bb) == (ma/mb):
            ans = min(ans, dp[n][aa][bb])
            
if ans == 10**10:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
N, Ma, Mb = [int(_) for _ in input().split()]
ABC = [[int(_) for _ in input().split()] for _ in range(N)]
dp = {}
dp[0] = 0
dp[ABC[0][0] + 1000 * ABC[0][1]] = ABC[0][2]
ans = 5000
for i in range(N - 1):
    dp_old = dp.copy()
    for ab in dp_old.keys():
        a = ab % 1000
        b = ab // 1000
        if a * Mb == b * Ma > 0:
            ans = min(ans, dp_old[ab])
        dp[a + ABC[i + 1][0] + 1000 * (b + ABC[i + 1][1])] = min(
            dp_old[ab] + ABC[i + 1][2],
            dp_old.get(a + ABC[i + 1][0] + 1000 * (b + ABC[i + 1][1]), 5000))
for ab in dp.keys():
    a = ab % 1000
    b = ab // 1000
    if a * Mb == b * Ma > 0:
        ans = min(ans, dp[ab])
if ans == 5000:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
n, ma, mb = map(int, input().split())
d = []
for i in range(n):#h:高さ
    d.append([int(m) for m in input().split()])

def gcd(a,b):
    if b == 0:
        return a
    return gcd(b, a % b)

#dptable,dp[n][a][b][c](a = dp[n][0])
"""dp = []
for i in range(401):
    for j in range(401):
        dp.append([i,j])"""

cost = []
for i in range(401):
    cost.append([])
for i in range(401):
    for j in range(401):
        cost[i].append([])
for i in range(401):
    for j in range(401):
        cost[i][j] = float("INF")
cost[0][0] = 0
pos = []
pos.append([0,0])
pos2 = set()
pos2.add((0,0))
#cppos = []
#cppos.append([0,0])
cppos2 = set()
cppos2.add((0,0))

cpcost = {}
cpcost = defaultdict(lambda: float("INF"))
for i in range(n):
    for v in range(2):
        for w in range(len(pos)):
            p = pos[w][0] + v * d[i][0]
            q = pos[w][1] + v * d[i][1]
            if (p, q) not in pos2:
                #cppos.append([p, q])
                cppos2.add((p, q))
            if cost[p][q] > cost[pos[w][0]][pos[w][1]] + d[i][2]:
                if cpcost[p, q] > cost[pos[w][0]][pos[w][1]] + d[i][2]:
                    cpcost[p, q] = cost[pos[w][0]][pos[w][1]] + d[i][2]
    #pos += cppos
    for u in cppos2:
        pos2.add(u)
        pos.append(list(u))
    for u in cpcost:
        cost[u[0]][u[1]] = cpcost[u]
    cppos = []
    cppos2 = set()
    cpcost = {}
    cpcost = defaultdict(lambda: float("INF"))

realcost = []
for i in range(401):
    for j in range(401):
        if gcd(i, j) != 0:
            if i // gcd(i, j) == ma and j // gcd(i, j) == mb and cost[i][j] != float("INF"):
                realcost.append(cost[i][j])

if len(realcost) > 0:
    print(min(realcost))
else:
    print(-1)

Submission status: Accepted

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

a, b, c = [], [], []

for i in range(n):
    aa,bb,cc = list(map(int,input().split()))
    a.append(aa)
    b.append(bb)
    c.append(cc)

dp = [[float("inf")]*(sum(b)+1) for i in range(sum(a)+1)]
dp[0][0] = 0
max_a = 0
max_b = 0
for i in range(n):
    max_a += a[i]
    max_b += b[i]
    for ai in range(a[i], max_a+1)[::-1]:
        for bi in range(b[i], max_b+1)[::-1]:
            dp[ai][bi] = min(dp[ai][bi], dp[ai-a[i]][bi-b[i]] + c[i])

ans = float("inf")
for i in range(1, min(max_a//ma, max_b//mb)):
    ans = min(ans, dp[i*ma][i*mb])
if ans == float("inf"):
    print(-1)
else:
    print(ans)

Submission status: Accepted

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

a, b, c = [], [], []

for i in range(n):
    aa,bb,cc = list(map(int,input().split()))
    a.append(aa)
    b.append(bb)
    c.append(cc)

dp = [[0]*(sum(b)+1) for i in range(sum(a)+1)]

max_a = 0
max_b = 0
for i in range(n):
    max_a += a[i]
    max_b += b[i]
    for ai in range(a[i], max_a+1)[::-1]:
        for bi in range(b[i], max_b+1)[::-1]:
            if dp[ai - a[i]][bi - b[i]]:
                if dp[ai][bi]:
                    dp[ai][bi] = min(dp[ai-a[i]][bi-b[i]]+c[i],dp[ai][bi])
                else:
                    dp[ai][bi] = dp[ai-a[i]][bi-b[i]]+c[i]
            elif ai == a[i] and bi == b[i]:
                if dp[ai][bi]:
                    dp[ai][bi] = min(c[i],dp[ai][bi])
                else:
                    dp[ai][bi] = c[i]

ans = float("inf")
for i in range(1, min(max_a//ma, max_b//mb)):
    if dp[i*ma][i*mb]:
        ans = min(ans, dp[i*ma][i*mb])
if ans == float("inf"):
    print(-1)
else:
    print(ans)