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
from collections import deque
import copy


def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def bfs(X, Ma, Mb):
    #node_list = deque([0, 1])
    drug_dict = {}
    drug_dict[(0, 0)] = 0
    drug_dict[(X[0][0], X[0][1])] = X[0][2]
##    drug_dict[0] = ((0, 0) ,0)
##    drug_dict[1] = ((X[0][0], X[0][1]), X[0][2])
##    depth = 1
    min_cost = sys.maxint
    new_drug_dict = copy.deepcopy(drug_dict)
    for i in range(1, len(X)):
        for drug in drug_dict:
            cost = drug_dict[drug]
            new_drug = (drug[0] + X[i][0], drug[1] + X[i][1])
            if new_drug not in drug_dict:
                new_drug_dict[new_drug] = cost + X[i][2]
            else:
                new_drug_dict[new_drug] = min(drug_dict[new_drug], cost + X[i][2])
            if new_drug[1] * Ma == new_drug[0] * Mb:
                if new_drug_dict[new_drug] < min_cost:
                    min_cost = new_drug_dict[new_drug]
        drug_dict = copy.deepcopy(new_drug_dict)
    return min_cost


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, Ma, Mb] = [int(input_raw[0]), int(input_raw[1]), int(input_raw[2])]
    X = []
    for i in range(N):
        input_raw = read_func().strip().split()
        [a, b, c] = [int(input_raw[0]), int(input_raw[1]), int(input_raw[2])]
        X.append((a, b, c))

    cost = bfs(X, Ma, Mb)
    if cost < sys.maxint:
        print cost
    else:
        print -1


if __name__ == '__main__':
    main()

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(N):
    for a in range(401):
        for b in range(401):
            if dp[i][a][b] == INF:
                continue
            dp[i+1][a][b] = min(dp[i+1][a][b], dp[i][a][b])
            if a + A[i] <= 400 and b + B[i] <= 400:
                dp[i+1][a+A[i]][b+B[i]] = min(dp[i+1][a+A[i]][b+B[i]], dp[i][a][b]+C[i])

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:
INF = 1000000
NMAX=40
ABMAX=10
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)

dp = [[[INF]*(NMAX*ABMAX+1) for i in range(NMAX*ABMAX+1)] for i in range(n+1)]
dp[0][0][0]=0
for i in range(n):
    for j in range(NMAX*ABMAX+1):
        for k in range(NMAX*ABMAX+1):
            if j+a[i]>NMAX+ABMAX or k+b[i]>NMAX+ABMAX:
                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 = INF
for j in range(1,NMAX*ABMAX+1):
    for k in range(1,NMAX*ABMAX+1):
        if (j*mb==k*ma):
            ans = min(ans,dp[n][j][k])

if ans == INF:
    ans = -1

print(ans)

Submission status: Accepted

Solution code:
INF = 1000000
NMAX=40
ABMAX=10
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)

dp = [[[INF]*(NMAX*ABMAX+1) for i in range(NMAX*ABMAX+1)] for i in range(n+1)]
dp[0][0][0]=0
for i in range(n):
    for j in range(NMAX*ABMAX+1):
        for k in range(NMAX*ABMAX+1):
            if dp[i][j][k] == INF:
                continue
            dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k])
            dp[i+1][j+a[i]][k+b[i]] = min(dp[i+1][j+a[i]][k+b[i]], dp[i][j][k]+c[i])

ans = INF
for j in range(1,NMAX*ABMAX+1):
    for k in range(1,NMAX*ABMAX+1):
        if (j*mb==k*ma):
            ans = min(ans,dp[n][j][k])

if ans == INF:
    ans = -1

print(ans)

Submission status: Accepted

Solution code:
N,Ma,Mb=map(int,input().split())
Med=[]
cnt_a=0
cnt_b=0
for i in range(N):
  a=[int(x) for x in input().split()]
  Med.append(a)
  cnt_a+=a[0]
  cnt_b+=a[1]
R=max(cnt_a,cnt_b)
#[i]まででMa=[j],Mb=[k]を作る場合の最小のコストdp
#dp[i+1][j+Ma][k+Mb]=min(dp[i][j+Ma][k+Mb],dp[i][j][k]+Cost)
#リストをinfで初期化。inf=作れない配合なので、continue。初期値はdp[0][0][0]=0
dp=[[[float('inf') for j in range(R+1)] for k in range(R+1)] for i in range(N+1)]
dp[0][0][0]=0

for i in range(N):
  for j in range(R+1):
    for k in range(R+1):
      if dp[i][j][k]==float('inf'):
        continue
      dp[i+1][j][k]=min(dp[i][j][k],dp[i+1][j][k])
      dp[i+1][j+Med[i][0]][k+Med[i][1]]=min(dp[i][j+Med[i][0]][k+Med[i][1]],dp[i][j][k]+Med[i][2])

ans=float('inf')
for i in range(1,N+1):
  if i*Ma<R+1 and i*Mb<R+1:
    ans=min(ans,dp[N][i*Ma][i*Mb])
if ans==float('inf'):
  print(-1)
else:
  print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
N,Ma,Mb=map(int,raw_input().split())

dp=defaultdict(lambda: float("inf"))
dp[(0,0)]=0

for i in range(N):
	a,b,c=map(int,raw_input().split())
	for j in dp.items():
		x=j[0][0]
		y=j[0][1]
		z=j[1]
		na, nb, nc = x+a, y+b, z+c 
		dp[(na, nb)]=min( dp[(na, nb)], nc)


ans=float("inf")
for i in dp.items():
	x=i[0][0]
	y=i[0][1]
	z=i[1]
	if x==0 and y==0:
		continue
	if x*Mb==y*Ma:
		ans=min(ans,z)

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

Submission status: Accepted

Solution code:
def d_MixingExperiment(N, Ma, Mb, Medicine):
    # 解説通り
    INF = float('inf')
    A = []
    B = []
    C = []
    for a, b, c in Medicine:
        A.append(a)
        B.append(b)
        C.append(c)
    a_max = max(A)
    b_max = max(B)
    dp = [[[INF for k in range(N * b_max + 1)]
           for j in range(N * a_max + 1)] for i in range(N + 1)]
    dp[0][0][0] = 0
    for i in range(N):
        for ca in range(N * a_max + 1):
            for cb in range(N * b_max + 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, N * a_max + 1):
        for cb in range(1, N * b_max + 1):
            if ca * Mb == cb * Ma:
                ans = min(ans, dp[N][ca][cb])
    if ans == INF:
        ans = -1
    return ans

N,Ma,Mb=[int(i) for i in input().split()]
Medicine=[[int(i) for i in input().split()] for j in range(N)]
print(d_MixingExperiment(N, Ma, Mb, Medicine))

Submission status: Accepted

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

S = 411
MAX_N = 10
MAX_SIZE = 400
INF = int(1e9)
dp = [[INF] * S for i in range(S)]
dp[0][0] = 0
for i in range(N):
    for j in range(MAX_SIZE, -1, -1):
        for k in range(MAX_SIZE, -1, -1):
            dp[j+a[i]][k+b[i]] = min((dp[j+a[i]][k+b[i]], dp[j][k] + c[i]))

ans = INF
for i in range(1, MAX_N):
    ans = min((ans, dp[i*Ma][i*Mb]))
if ans == INF:
    print(-1)
else:
    print(ans)

Submission status: Accepted

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

inf = float('inf')
for i in range(N):
    ai, bi, ci = map(int, input().split())
    ABC.append((ai,bi,ci))

dp = [[[inf] * 401 for i in range(401)] for j 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+1][j][k], dp[i][j][k])
            dp[i+1][j+ABC[i][0]][k+ABC[i][1]] = min(dp[i+1][j+ABC[i][0]][k+ABC[i][1]], dp[i][j][k]+ABC[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:
import sys
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache, reduce

INF = float("inf")
sys.setrecursionlimit(10 ** 7)

# 4近傍（右, 下, 左, 上）
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]


def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W


def main():
    N, Ma, Mb = map(int, input().split())
    dp = [[[INF] * 401 for j in range(401)] for i in range(N + 1)]
    dp[0][0][0] = 0
    a_b_c = [map(int, input().split()) for _ in range(N)]

    for i, (a, b, c) in enumerate(a_b_c):
        for j in range(len(dp[0])):
            for k in range(len(dp[0][0])):
                if dp[i][j][k] == INF:
                    continue

                # not use
                dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k])

                # use
                if j + a < len(dp[0]) and k + b < len(dp[0][0]):
                    dp[i + 1][j + a][k + b] = min(dp[i + 1][j + a][k + b], dp[i][j][k] + c)

    ans = INF
    for i in count(1):
        a, b = Ma * i, Mb * i
        if a >= len(dp[0]) or b >= len(dp[0][0]):
            break
        ans = min(ans, dp[-1][a][b])
    if ans != INF:
        print(ans)
    else:
        print(-1)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,MA,MB = map(int,raw_input().split())
src = []
sum_a = sum_b = 0
for i in range(N):
  a,b,c = map(int,raw_input().split())
  src.append((a,b,c))
  sum_a += a
  sum_b += b

INF = 10**9
dp = [[[INF for b in range(sum_b+1)] for a in range(sum_a+1)] for i in range(N+1)]
dp[0][0][0] = 0

for i in range(N):
  ai,bi,ci = src[i]
  for a in range(sum_a+1 - ai):
    for b in range(sum_b+1 - 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
n = 1
while True:
  a,b = n*MA,n*MB
  if a > sum_a or b > sum_b: break
  ans = min(ans, dp[N][a][b])
  n += 1

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

Submission status: Accepted

Solution code:
INF = float('inf')

def solve():
    N, Ma, Mb = map(int, input().split())
    l = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        l.append((a, b, c))
 
    dp = [[[INF] * 401 for _ in range(401)] for _ in range(N + 1)]
    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])
                    dp[i + 1][ca + l[i][0]][cb + l[i][1]] = min(dp[i + 1][ca + l[i][0]][cb + l[i][1]], dp[i][ca][cb] + l[i][2])
 
    ans = INF
    for i in range(N+1):
        for ca in range(1, 401):
            for cb in range(1, 401):
                if dp[i][ca][cb] != INF:
                    if Ma * cb == Mb * ca:
                        ans = min(ans, dp[i][ca][cb])
 
    print(-1 if ans == INF else ans)
 
 
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
INF = float('inf')

def solve():
    N, Ma, Mb = map(int, input().split())
    l = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        l.append((a, b, c))
 
    dp = [[[INF] * 401 for _ in range(401)] for _ in range(N + 1)]
    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])
                    dp[i + 1][ca + l[i][0]][cb + l[i][1]] = min(dp[i + 1][ca + l[i][0]][cb + l[i][1]], dp[i][ca][cb] + l[i][2])
 
    ans = INF
    for i in range(len(dp)):
        for ca in range(1, 401):
            for cb in range(1, 401):
                if dp[i][ca][cb] != INF:
                    if Ma * cb == Mb * ca:
                        ans = min(ans, dp[i][ca][cb])
 
    print(-1 if ans == INF else ans)
 
 
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
INF = float('inf')

def solve():
    N, Ma, Mb = map(int, input().split())
    l = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        l.append((a, b, c))
 
    dp = [[[INF] * 401 for _ in range(401)] for _ in range(N + 1)]
    dp[0][0][0] = 0
    for i in range(len(l)):
        for a in range(401):
            for b in range(401):
                if dp[i][a][b] != INF:
                    dp[i + 1][a][b] = min(dp[i + 1][a][b], dp[i][a][b])
                    dp[i + 1][a + l[i][0]][b + l[i][1]] = min(dp[i + 1][a + l[i][0]][b + l[i][1]], dp[i][a][b] + l[i][2])
 
    ans = INF
    for i in range(len(dp)):
        for a in range(1, 401):
            for b in range(1, 401):
                if dp[i][a][b] != INF:
                    if Ma * b == Mb * a:
                        ans = min(ans, dp[i][a][b])
 
    print(-1 if ans == INF else ans)
 
 
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
INF = float('inf')

def solve():
    N, Ma, Mb = map(int, input().split())
    l = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        l.append((a, b, c))
 
    dp = [[[INF] * 401 for _ in range(401)] for _ in range(N + 1)]
    dp[0][0][0] = 0
    for i in range(len(l)):
        for a in range(len(dp[0])):
            for b in range(len(dp[0][0])):
                if dp[i][a][b] != INF:
                    dp[i + 1][a][b] = min(dp[i + 1][a][b], dp[i][a][b])
                    dp[i + 1][a + l[i][0]][b + l[i][1]] = min(dp[i + 1][a + l[i][0]][b + l[i][1]], dp[i][a][b] + l[i][2])
 
    ans = INF
    for i in range(len(dp)):
        for a in range(1, len(dp[0])):
            for b in range(1, len(dp[0][0])):
                if dp[i][a][b] != INF:
                    if Ma * b == Mb * a:
                        ans = min(ans, dp[i][a][b])
 
    print(-1 if ans == INF else ans)
 
 
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
N, Ma, Mb = [int(i) for i in input().split()]
a, b, c = [], [], []
for i in range(N):
    a_, b_, c_ = [int(i) for i in input().split()]
    a.append(a_)
    b.append(b_)
    c.append(c_)
cost = [[float('inf')] * 401 for _ in range(401)]


def solve():
    max_a, max_b = 1, 1
    for num in range(N):
        for ca in range(max_a, -1, -1):
            for cb in range(max_b, -1, -1):
                cost[ca + a[num]][cb + b[num]] = min(cost[ca][cb] + c[num], cost[ca + a[num]][cb + b[num]])
        cost[a[num]][b[num]] = min(cost[a[num]][b[num]], c[num])
        max_a += a[num]
        max_b += b[num]


solve()
ans = float('inf')
most = ans
for i in range(1, 401):
    for n in range(1, 401):
        if i * Mb is n * Ma:
            ans = min(ans, cost[i][n])
if ans is most:
    print(-1)
else:
    print(ans)

Submission status: Accepted

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

#iまでの薬品で、aがj(g),bがk(g)となるときの最小コスト
dp = [[[10 ** 15 for i in range(n + 1)] for j in range(401)] \
    for k in range(401)]

dp[0][0][0] = 0
sa = [0] * (n + 1)
sb = [0] * (n + 1)

for i in range(1, n + 1):
    sa[i] = sa[i - 1] + a[i]
    sb[i] = sb[i - 1] + b[i]

for i in range(1, n + 1):
    for j in range(sa[i] + 1):
        for k in range(sb[i] + 1):
            if j < a[i] or k < b[i]:
                dp[k][j][i] = dp[k][j][i - 1]
            else:
                dp[k][j][i] = min(dp[k][j][i - 1], \
                    dp[k - b[i]][j - a[i]][i - 1] + c[i])

i = 1
ans = 10 ** 15
while True:
    if i * ma > 400 or i * mb > 400:
        break
    ans = min(ans, dp[i * mb][i * ma][n])
    i += 1

if ans == 10 ** 15:
    ans = -1

print(ans)

Submission status: Accepted

Solution code:
import fractions

n, ma, mb = map(int,input().split())
a = [0] * (n + 1)
b = [0] * (n + 1)
c = [0] * (n + 1)
for i in range(1, n + 1):
    a[i], b[i], c[i] = map(int,input().split())

#iまでの薬品で、aがj(g),bがk(g)となるときの最小コスト
dp = [[[10 ** 15 for i in range(n + 1)] for j in range(401)] \
    for k in range(401)]

dp[0][0][0] = 0
sa = [0] * (n + 1)
sb = [0] * (n + 1)

for i in range(1, n + 1):
    for j in range(401):
        for k in range(401):
            if j < a[i] or k < b[i]:
                dp[k][j][i] = dp[k][j][i - 1]
            else:
                dp[k][j][i] = min(dp[k][j][i - 1], \
                    dp[k - b[i]][j - a[i]][i - 1] + c[i])

i = 1
ans = 10 ** 15
while True:
    if i * ma > 400 or i * mb > 400:
        break
    ans = min(ans, dp[i * mb][i * ma][n])
    i += 1

if ans == 10 ** 15:
    ans = -1

print(ans)

"""
import fractions

n, ma, mb = map(int,input().split())
a = [0] * (n + 1)
b = [0] * (n + 1)
c = [0] * (n + 1)
for i in range(1, n + 1):
    a[i], b[i], c[i] = map(int,input().split())

#iまでの薬品で、aがj(g),bがk(g)となるときの最小コスト
dp = [[[10 ** 15 for i in range(n + 1)] for j in range(401)] \
    for k in range(401)]

dp[0][0][0] = 0
sa = [0] * (n + 1)
sb = [0] * (n + 1)

for i in range(1, n + 1):
    sa[i] = sa[i - 1] + a[i]
    sb[i] = sb[i - 1] + b[i]

for i in range(1, n + 1):
    for j in range(sa[i] + 1):
        for k in range(sb[i] + 1):
            if j < a[i] or k < b[i]:
                dp[k][j][i] = dp[k][j][i - 1]
            else:
                dp[k][j][i] = min(dp[k][j][i - 1], \
                    dp[k - b[i]][j - a[i]][i - 1] + c[i])

i = 1
ans = 10 ** 15
while True:
    if i * ma > 400 or i * mb > 400:
        break
    ans = min(ans, dp[i * mb][i * ma][n])
    i += 1

if ans == 10 ** 15:
    ans = -1

print(ans)
"""

Submission status: Accepted

Solution code:
import fractions

n, ma, mb = map(int,input().split())
a = [0] * (n + 1)
b = [0] * (n + 1)
c = [0] * (n + 1)
for i in range(1, n + 1):
    a[i], b[i], c[i] = map(int,input().split())

#iまでの薬品で、aがj(g),bがk(g)となるときの最小コスト
dp = [[[10**15 for i in range(n + 1)] for j in range(401)] \
    for k in range(401)]

dp[0][0][0] = 0
sa = [0] * (n + 1)
sb = [0] * (n + 1)

for i in range(1, n + 1):
    sa[i] = sa[i - 1] + a[i]
    sb[i] = sb[i - 1] + b[i]

for i in range(1, n + 1):
    for j in range(sa[i] + 1):
        for k in range(sb[i] + 1):
            # i番目を買うか、買わないか
            if j < a[i] or k < b[i]:
                dp[k][j][i] = dp[k][j][i - 1]
            else:
                dp[k][j][i] = min(dp[k][j][i - 1], \
                    dp[k - b[i]][j - a[i]][i - 1] + c[i])

i = 1
ans = 10 ** 15
while True:
    if i * ma > 400 or i * mb > 400:
        break
    ans = min(ans, dp[i * mb][i * ma][n])
    i += 1

if ans == 10 ** 15:
    ans = -1

print(ans)

Submission status: Accepted

Solution code:
import fractions

n, ma, mb = map(int,input().split())
a = [0] * (n + 1)
b = [0] * (n + 1)
c = [0] * (n + 1)
for i in range(1, n + 1):
    a[i], b[i], c[i] = map(int,input().split())

#iまでの薬品で、aがj(g),bがk(g)となるときの最小コスト
dp = [[[10**15 for i in range(n + 1)] for j in range(401)] \
    for k in range(401)]

dp[0][0][0] = 0
sa = [0] * (n + 1)
sb = [0] * (n + 1)

for i in range(1, n + 1):
    sa[i] = sa[i - 1] + a[i]
    sb[i] = sb[i - 1] + b[i]

for i in range(1, n + 1):
    for j in range(401):
        if j > sa[i]:
            break
        for k in range(401):
            if k > sb[i]:
                break
            # i番目を買うか、買わないか
            if j < a[i] or k < b[i]:
                dp[k][j][i] = dp[k][j][i - 1]
            else:
                dp[k][j][i] = min(dp[k][j][i - 1], \
                    dp[k - b[i]][j - a[i]][i - 1] + c[i])

i = 1
ans = 10 ** 15
while True:
    if i * ma > 400 or i * mb > 400:
        break
    ans = min(ans, dp[i * mb][i * ma][n])
    i += 1

if ans == 10 ** 15:
    ans = -1

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict


INF = 10 ** 10 // 3
def main():
    N, Ma, Mb = map(int, input().split())
    l = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        l.append((a, b, c))

    dp = [[[INF] * 401 for _ in range(401)] for _ in range(N + 1)]
    dp[0][0][0] = 0
    for i in range(len(l)):
        for a in range(len(dp[0])):
            for b in range(len(dp[0][0])):
                if dp[i][a][b] != INF:
                    dp[i + 1][a][b] = min(dp[i + 1][a][b], dp[i][a][b])
                    dp[i + 1][a + l[i][0]][b + l[i][1]] = min(dp[i + 1][a + l[i][0]][b + l[i][1]], dp[i][a][b] + l[i][2])

    ans = INF
    for i in range(len(dp)):
        for a in range(1, len(dp[0])):
            for b in range(1, len(dp[0][0])):
                if dp[i][a][b] != INF:
                    if Ma * b == Mb * a:
                        ans = min(ans, dp[i][a][b])

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
maxm = 10000
def makelist(l, n, m):
	return [[[maxm for i in range(m)] for j in range(n)] for k in range(l)]

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

dp = makelist(N, 401, 401)
a0, b0, c0 = drugs[0]
dp[0][a0][b0] = c0
dp[0][0][0] = 0
for i in range(1, N):
	a, b, c = drugs[i]
	for ia in range(401):
		for ib in range(401):
			if ia - a < 0 or ib - b < 0:
				dp[i][ia][ib] = dp[i-1][ia][ib]
			else:
				dp[i][ia][ib] = \
					min(dp[i-1][ia][ib], dp[i-1][ia-a][ib-b] + c)

i = 1
ans = maxm
while Ma * i <= 400 and Mb * i <= 400:
	now = dp[N-1][Ma*i][Mb*i]
	ans = min(ans, now)
	i += 1

if ans < maxm:
	print(ans)
else:
	print(-1)

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict

# sys.stdin = open('d1.in')


def read_int_list():
    return list(map(int, input().split()))


def read_str_list():
    return input().split()


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


def read_str():
    return input()


M = 10
inf = 10 ** 20


def solve():
    n, ma, mb = read_int_list()
    p = [read_int_list() for i in range(n)]
    min_c = defaultdict(lambda: inf)
    min_c[(0, 0)] = 0

    for i, (a, b, c) in enumerate(p):
        for u in range(M * i, -1, -1):
            for v in range(M * i, -1, -1):
                if (u, v) in min_c:
                    cost = min_c[(u, v)]
                    if cost + c < min_c[((u + a), (v + b))]:
                        min_c[((u + a), (v + b))] = cost + c

    res = inf
    for u in range(0, M * n + 1):
        for v in range(0, M * n + 1):
            if u == 0 and v == 0:
                continue
            if u * mb == v * ma:
                if (u, v) in min_c:
                    cost = min_c[(u, v)]
                    if cost < res:
                        res = cost
    if res == inf:
        res = -1
    return res


def main():
    res = solve()
    print(res)


main()

Submission status: Accepted

Solution code:
import sys

# sys.stdin = open('d1.in')


def read_int_list():
    return list(map(int, input().split()))


def read_str_list():
    return input().split()


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


def read_str():
    return input()


def solve():
    M = 10
    n, ma, mb = read_int_list()
    p = [read_int_list() for i in range(n)]
    min_c = {(0, 0, 0): 0}

    for i in range(1, n + 1):
        a, b, c = p[i - 1]
        for u in range(0, M * (i - 1) + 1):
            for v in range(0, M * (i - 1) + 1):
                if (i - 1, u, v) in min_c:
                    cost = min_c[(i - 1, u, v)]
                    cc = cost + c
                    uu = u + a
                    vv = v + b
                    if (i, uu, vv) not in min_c:
                        min_c[(i, uu, vv)] = cc
                    if cc < min_c[(i, uu, vv)]:
                        min_c[(i, uu, vv)] = cc
                        
                    cc = cost
                    uu = u
                    vv = v
                    if (i, uu, vv) not in min_c:
                        min_c[(i, uu, vv)] = cc
                    if cc < min_c[(i, uu, vv)]:
                        min_c[(i, uu, vv)] = cc

    inf = 10 ** 20
    res = inf
    for u in range(0, M * n + 1):
        for v in range(0, M * n + 1):
            if u == 0 and v == 0:
                continue
            if u * mb == v * ma:
                if (n, u, v) in min_c:
                    cost = min_c[(n, u, v)]
                    if cost < res:
                        res = cost
    if res == inf:
        res = -1
    return res


def main():
    res = solve()
    print(res)


main()

Submission status: Accepted

Solution code:
N, M_a, M_b = map(int, raw_input().split())
v = [map(int, raw_input().split()) for i in range(N)]
def solve():
    dp = {(0, 0): 0}
    for i in range(N):
        a, b, c = v[i]
        for (A, B), C in dp.items():
            nA, nB, nC = A+a, B+b, C+c
            if not (nA, nB) in dp:
                dp[nA, nB] = nC
            else:
                dp[nA, nB] = min(dp[nA, nB], nC)
    for (a, b), c in sorted(dp.items(), key=lambda x: x[1]):
        if (a, b) == (0, 0):
            continue
        if a*M_b == b*M_a:
            return c
    return -1
print solve()

Submission status: Accepted

Solution code:
# ABC54 D
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)
    
max_sum = n * 10
max_cost = 100000000
dp = [[[max_cost for k in range(max_sum+1)] for j in range(max_sum+1)] for i in range(n+1)]

dp[0][0][0] = 0
for i in range(n):
    for ca in range(max_sum+1):
        for cb in range(max_sum+1):
            if(dp[i][ca][cb] == max_cost):
                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 = max_cost
for ca in range(1,max_sum+1):
    for cb in range(1,max_sum+1):
        if(ca*mb == cb*ma):
            ans = min(ans, dp[n][ca][cb])

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

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin
def na(): return map(int, stdin.readline().split())

n, ma, mb = na()
abcs = []
for i in range(n):
    abcs.append(list(na()))
I = 9999999999
dp = [[I]*420 for _ in range(420)]
dp[0][0] = 0
sa = 1
sb = 1
for abc in abcs:
    for i in range(sa-1, -1, -1):
        for j in range(sb-1, -1, -1):
            dp[i+abc[0]][j+abc[1]] = min(dp[i+abc[0]][j+abc[1]], dp[i][j] + abc[2])
    sa += abc[0]
    sb += abc[1]

x = ma
y = mb
ret = I
while x< 420 and y < 420:
    ret = min(ret, dp[x][y])
    x += ma
    y += mb

if ret == I:
 print(-1)
else:
 print(ret)

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

dp = [[[4001 for i in range(411)] for j in range(411)] for l in range(N+1)]

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

for i in range(1,N+1):
    for j in range(400):
        for k in range(400):
            if dp[i-1][j][k] == 4001:
                continue
            dp[i][j+a[i-1]][k+b[i-1]] = min(dp[i-1][j][k] + c[i-1],dp[i-1][j+a[i-1]][k+b[i-1]])
            dp[i][j][k] = min(dp[i - 1][j][k], dp[i][j][k])
s = min(400//Ma,400//Mb)

min = 4001
for i in range(1,s+1):
    x = dp[N][i*Ma][i*Mb]
    if x>0 and min > x:
        min = x

if min != 4001:
    print(min)
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys
 
def solve():
    def dfs(idx, end, value, cost, g):
        if idx == end:
            if cost == 0:
                return

            if value not in g:
                g[value] = cost
            else:
                g[value] = min(g[value], cost)

            return

        a, b, c = table[idx]
        dfs(idx + 1, end, value + b * Ma - a * Mb, cost + c, g)
        dfs(idx + 1, end, value, cost, g)

    N, Ma, Mb = map(int, input().split())
    table = [tuple(map(int, input().split())) for i in range(N)]
 
    g1, g2 = dict(), dict()
    dfs(0, N // 2, 0, 0, g1)
    dfs(N // 2, N, 0, 0, g2)

    inf = 100*40 + 1
    ans = inf
 
    if 0 in g1:
        ans = min(ans, g1[0])
    if 0 in g2:
        ans = min(ans, g2[0])
 
    for v1, c1 in g1.items():
        if -v1 in g2:
            tmp = c1 + g2[-v1]
            ans = min(ans, tmp)
 
    if ans < inf:
        print(ans)
    else:
        print(-1)

        
def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None
 
 
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:

def solve():
    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)

    nvals = 99 * N * 2 + 1
    inf = 40 * 100 + 1
    dp = [inf] * nvals

    for i in range(N):
        v = Mb * a[i] - Ma * b[i]

        if i == 0:
            dp[v] = c[i]
            continue

        if v > 0:
            for j in range(99 * N, -99 * N + v - 1, -1):
                dp[j] = min(dp[j], dp[j - v] + c[i])
        else:
            for j in range(-99 * N, 99 * N + v + 1):
                dp[j] = min(dp[j], dp[j - v] + c[i])

        dp[v] = min(dp[v], c[i])

    ans = dp[0]

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

if __name__ == '__main__':
    solve()