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:
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 = 10 ** 5
nums = 401

dp = [[[INF for _ in range(nums)] for _ in range(nums)] for _ in range(N+1)]

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

for i in range(N):
    for sum_a in range(nums):
        for sum_b in range(nums):
            if dp[i][sum_a][sum_b] == INF:
                continue
            dp[i+1][sum_a][sum_b] = min(dp[i+1][sum_a][sum_b], dp[i][sum_a][sum_b])
            dp[i+1][sum_a+a[i]][sum_b+b[i]] = min(dp[i+1][sum_a+a[i]][sum_b+b[i]], dp[i][sum_a][sum_b] + c[i])
            # print(i, dp[i][sum_a][sum_b], sum_a, sum_b)


res = INF
for i in range(1, nums):
    for j in range(1, nums):
        if i * Mb == j * Ma:
            res = min(res, dp[N][i][j])

if res == INF:
    res = -1

print(res)

Submission status: Accepted

Solution code:

def main():
    num, wariai_a, wariai_b = map(int, input().split())
    data = [list(map(int, input().split())) for i in range(num)]

    inf = 100 * num + 1
    dp = [[[inf for i in range(400)] for j in range(400)] for k in range(num + 1)]
    dp[0][0][0] = 0
    for i in range(1, num + 1):
        a, b, c = data[i - 1]
        for j in range(400):
            for k in range(400):
                dp[i][j][k] = min(dp[i - 1][j][k], dp[i][j][k])
                if j + a < 400 and k + b < 400:
                    dp[i][j + a][k + b] = min(dp[i][j + a][k + b], dp[i - 1][j][k] + c)

    ans = inf
    for j in range(1, 400):
        for k in range(1, 400):
            if j * wariai_b == k * wariai_a:
                ans = min(ans, dp[num][j][k])

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def smaller(x, y):
	if x < y:
		return x
	return y

N, Ma, Mb = map(int, input().split())
dp = []
for i in range(0, 401):
	lst = []
	for j in range(0, 401):
		lst.append(-1)
	dp.append(lst)
dp[0][0] = 0
for i in range(0, N):
	a, b, c = map(int, input().split())
	next = []
	for j in range(0, 401):
		for k in range(0, 401):
			if dp[j][k] != -1:
				if dp[j+a][k+b] == -1:
					next.append([j+a, k+b, dp[j][k]+c])
				else:
					if dp[j][k]+c == smaller(dp[j+a][k+b], dp[j][k]+c):
						next.append([j+a, k+b, dp[j][k]+c])
	for j in range(0, len(next)):
		if next[j][0] <= 400 and next[j][1] <= 400:
			dp[next[j][0]][next[j][1]] = next[j][2]
min = 5000000000000000
index = 1
while Ma*index <= 400 and Mb*index <= 400:
	if dp[Ma*index][Mb*index] != -1:
		min = smaller(min, dp[Ma*index][Mb*index])
	index += 1
if min == 5000000000000000:
	print(-1)
else:
	print(min)

Submission status: Accepted

Solution code:
def calc():
    N,Ma,Mb = map(int, input().split())
    arr=[list(map(int,input().split())) for _ in range(N)]
    dp=[[[5000]*401 for _ in range(401)] for _ in range(41)]
    dp[0][0][0] = 0
    for i in range(N):
        a,b,c=arr[i]
        for j in range(10*N+1):
            for k in range(10*N+1):
                if dp[i][j][k] == 5000:
                    continue
                dp[i+1][j][k]=min(dp[i+1][j][k],dp[i][j][k])
                dp[i+1][j+a][k+b]=min(dp[i+1][j+a][k+b],dp[i][j][k]+c)

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

calc()

Submission status: Accepted

Solution code:
N,Ma,Mb = map(int, input().split())
arr=[list(map(int,input().split())) for _ in range(N)]
dp=[[[5000]*401 for _ in range(401)] for _ in range(41)]
dp[0][0][0] = 0
for i in range(N):
    a,b,c=arr[i]
    for j in range(10*N+1):
        for k in range(10*N+1):
            if dp[i][j][k] == 5000:
                continue
            dp[i+1][j][k]=min(dp[i+1][j][k],dp[i][j][k])
            dp[i+1][j+a][k+b]=min(dp[i+1][j+a][k+b],dp[i][j][k]+c)

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

Submission status: Accepted

Solution code:
N,Ma,Mb = map(int, input().split())
arr=[list(map(int,input().split())) for _ in range(N)]
INF = 5000
dp=[[[INF]*(10*N+1) for _ in range(10*N+1)] for _ in range(N+1)]
dp[0][0][0] = 0
for i in range(N):
    a,b,c=arr[i]
    for j in range(10*N+1):
        for k in range(10*N+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][k+b]=min(dp[i+1][j+a][k+b],dp[i][j][k]+c)

ans=INF
for i in range(1,10*N+1):
        if i*Ma>10*N or i*Mb>10*N:
            break
        ans = min(ans, dp[N][i*Ma][i*Mb])
if ans == INF:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
def calc():
  n,ma,mb=map(int,input().split())
  dp=[[[5000]*401 for _ in range(401)] for _ in range(41)]
  arr=[list(map(int,input().split())) for _ in range(n)]
  dp[0][0][0]=0
  for t in range(n):
    a,b,c=arr[t]
    for i in range(401):
      for j in range(401):
        if dp[t][i][j]==5000:
          continue
        dp[t+1][i][j]=min(dp[t+1][i][j],dp[t][i][j])
        dp[t+1][i+a][j+b]=min(dp[t+1][i+a][j+b],dp[t][i][j]+c)
  ans=5000
  for i in range(1,401):
    if i*ma>400 or i*mb>400:
      break
    ans=min(ans,dp[n][i*ma][i*mb])
  if ans==5000:
    print(-1)
  else:
    print(ans)
    
calc()

Submission status: Accepted

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

# 入力値の受け取り方を変える

arr=[list(map(int,input().split())) for _ in range(N)]

#a = [0] * N
#b = [0] * N
#c = [0] * N

#for i in range(N):
#    a[i], b[i], c[i] = map(int, input().split())



# TLEになってしまうためinfを5000とかで設定する
INF = 5000
# さらに計算量を落とすため10 * N + 1などを定数で設定する →　関係なかったため戻す

# 最小値を求める問題なのでinfで初期化
# dp[i][sun_a][sum_b]で考えていく
dp = [[[INF] * (10 * N + 1) for _ in range(10 * N + 1)] for _ in range(N+1)]

# 初期選択状態を0とする

dp[0][0][0] = 0
for i in range(N):
    a,b,c=arr[i]
    for j in range(10*N+1):
        for k in range(10*N+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][k+b] = min(dp[i+1][j+a][k+b], dp[i][j][k] + c)


ans = INF
# 0スタートは何を選んでいない状態となり最小値が0になってしまうため1から開始する

# TLEになったため変更
#for i in range(1,10 * N + 1):
#    for j in range(1,10 * N + 1):
#        if i * Mb != j * Ma:
#            continue
#        ans = min(ans, dp[N][i][j])
#

for i in range(1, 10*N+1):
        if i*Ma>10*N or i*Mb>10*N:
            break
        ans = min(ans, dp[N][i*Ma][i*Mb])

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

Submission status: Accepted

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

# 入力値の受け取り方を変える

arr=[list(map(int,input().split())) for _ in range(N)]

#a = [0] * N
#b = [0] * N
#c = [0] * N

#for i in range(N):
#    a[i], b[i], c[i] = map(int, input().split())



# TLEになってしまうためinfを5000とかで設定する
INF = 5000
# さらに計算量を落とすため10 * N + 1などを定数で設定する →　関係なかったため戻す

# 最小値を求める問題なのでinfで初期化
# dp[i][sun_a][sum_b]で考えていく
dp = [[[INF for _ in range(10 * N + 1)] for _ in range(10 * N + 1)] for _ in range(N+1)]

# 初期選択状態を0とする

dp[0][0][0] = 0
for i in range(N):
    a,b,c=arr[i]
    for j in range(10*N+1):
        for k in range(10*N+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][k+b] = min(dp[i+1][j+a][k+b], dp[i][j][k] + c)


ans = INF
# 0スタートは何を選んでいない状態となり最小値が0になってしまうため1から開始する

# TLEになったため変更
#for i in range(1,10 * N + 1):
#    for j in range(1,10 * N + 1):
#        if i * Mb != j * Ma:
#            continue
#        ans = min(ans, dp[N][i][j])
#

for i in range(1, 10*N+1):
        if i*Ma>10*N or i*Mb>10*N:
            break
        ans = min(ans, dp[N][i*Ma][i*Mb])

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

# TLEになってしまうためinfを5000とかで設定する
INF = 5000
# さらに計算量を落とすため10 * N + 1などを定数で設定する

# 最小値を求める問題なのでinfで初期化
# dp[i][sun_a][sum_b]で考えていく
dp = [[[INF for _ in range(401)] for _ in range(401)] for _ in range(41)]

# 初期選択状態を0とする

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+a[i]][k+b[i]] = min(dp[i+1][j+a[i]][k+b[i]], dp[i][j][k] + c[i])


ans = INF
# 0スタートは何を選んでいない状態となり最小値が0になってしまうため1から開始する

# TLEになったため変更
#for i in range(1,10 * N + 1):
#    for j in range(1,10 * N + 1):
#        if i * Mb != j * Ma:
#            continue
#        ans = min(ans, dp[N][i][j])
#

for i in range(1, 401):
        if i*Ma>10*N or i*Mb>10*N:
            break
        ans = min(ans, dp[N][i*Ma][i*Mb])

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

# TLEになってしまうためinfを5000とかで設定する
INF = 5000

# 最小値を求める問題なのでinfで初期化
# dp[i][sun_a][sum_b]で考えていく
dp = [[[INF for _ in range(10 * N + 1)] for _ in range(10 * N + 1)] for _ in range(N+1)]

# 初期選択状態を0とする

dp[0][0][0] = 0
for i in range(N):
    for j in range(10*N+1):
        for k in range(10*N+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
# 0スタートは何を選んでいない状態となり最小値が0になってしまうため1から開始する

# TLEになったため変更
#for i in range(1,10 * N + 1):
#    for j in range(1,10 * N + 1):
#        if i * Mb != j * Ma:
#            continue
#        ans = min(ans, dp[N][i][j])
#

for i in range(1, 10*N+1):
        if i*Ma>10*N or i*Mb>10*N:
            break
        ans = min(ans, dp[N][i*Ma][i*Mb])

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

Submission status: Accepted

Solution code:

def read_input():
    n, ma, mb = map(int, input().split())

    medicines = []
    for i in range(n):
        a, b, c = map(int, input().split())
        medicines.append((a, b, c))

    return n, ma, mb, medicines


def submit():
    n, ma, mb, medicines = read_input()

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

    for i in range(1, n + 1):
        # 薬品iを足す場合と足さない場合をそれぞれのa, bについて計算
        m = medicines[i - 1]
        for ca in range(401 - m[0]):
            for cb in range(401 - m[1]):
                # 足さない場合
                dp[i][ca][cb] = min(dp[i][ca][cb], dp[i - 1][ca][cb])

                # 足す場合
                dp[i][ca + m[0]][cb + m[1]] = min(dp[i][ca + m[0]][cb + m[1]], dp[i - 1][ca][cb] + m[2])


    cost = float('inf')
    for ca in range(1, 401):
        for cb in range(1, 401):
            if ca*mb == cb*ma:
                if cost > dp[n][ca][cb]:
                    cost = dp[n][ca][cb]

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



if __name__ == '__main__':
    submit()

Submission status: Accepted

Solution code:
from collections import defaultdict

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

dp = [defaultdict(lambda : float('inf')) for _ in range(2)]
zero = float('inf')

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

for _ in range(N) :
    a, b, c = map(int, input().split())
    d = a * Mb - b * Ma
    if d == 0 :
        zero = min(zero, c)
    else :
        i = 0 if d > 0 else 1
        d = abs(d)
        item = list(dp[i].items())[::-1]
        for k, v in item :
            dp[i][k+d] = min(dp[i][k+d], dp[i][k] + c)

ret = zero       
for k, v in dp[0].items() :
    if k in dp[1] and k != 0 :
        ret = min(ret, v + dp[1][k])
        
if ret == float('inf') :
    print(-1)
else :
    print(ret)

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") for i in range(401)] for j in range(401)] for k in range(N+1)]
dp[0][0][0] = 0
for i in range(N):
    for j in range(401):
        for k in range(401):
            if j - abc[i][0] >= 0 and k - abc[i][1] >= 0:
                dp[i+1][j][k] = min(dp[i][j][k],dp[i][j-abc[i][0]][k-abc[i][1]] + abc[i][2])

            else:
                dp[i+1][j][k] = dp[i][j][k]


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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)


def solve(N: int, M_a: int, M_b: int, a: "List[int]", b: "List[int]", c: "List[int]"):
    INF = float('inf')
    dp = [[INF] * 401 for _ in range(401)]
    dp[0][0] = 0
    for i in range(N):
        for x in range(400, -1, -1):
            for y in range(400, -1, -1):
                if dp[x][y] < INF:
                    dp[x + a[i]][y + b[i]] = min(dp[x + a[i]][y + b[i]], dp[x][y] + c[i])
    ret = INF
    for i in range(1, 401):
        for j in range(1, 401):
            if i % M_a == 0 and j % M_b == 0 and i // M_a == j // M_b:
                ret = min(ret, dp[i][j])
    if ret == INF:
        print(-1)
    else:
        print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M_a = int(next(tokens))  # type: int
    M_b = int(next(tokens))  # type: int
    a = [int()] * (N)  # type: "List[int]" 
    b = [int()] * (N)  # type: "List[int]" 
    c = [int()] * (N)  # type: "List[int]" 
    for i in range(N):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
        c[i] = int(next(tokens))
    solve(N, M_a, M_b, a, b, c)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
N, Ma, Mb = map(int, input().split())
INF, MAX = 1000000000, 10 * N + 1
dp = defaultdict(lambda : INF)
dp[(0, 0)] = 0
for i in range(N):
    a, b, c = map(int, input().split())
    predp = dp.copy()
    for ab, v in predp.items():
        _a, _b = ab
        dp[(_a+a, _b+b)] = min(dp[(_a+a, _b+b)], v+c)
ans = INF
for i in range(1, N+1):
    ans = min(ans, dp[(Ma*i, Mb*i)])
ans = -1 if ans == INF else ans
print(ans)

Submission status: Accepted

Solution code:
"""
13:56
"""
import os
import sys
from collections import defaultdict
from fractions import Fraction

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18

N, MA, MB = list(map(int, sys.stdin.readline().split()))
A, B, C = list(zip(*[map(int, sys.stdin.readline().split()) for _ in range(N)]))

A = np.array(A)
B = np.array(B)
C = np.array(C)

# dp[a][b]: 薬品を a と b 揃えるときの最小のコスト
SUMMAX = 400
dp = np.full((SUMMAX + 1, SUMMAX + 1), IINF)
dp[0, 0] = 0
for a, b, c in zip(A, B, C):
    dp[a:, b:] = np.minimum(dp[a:, b:], dp[:-a, :-b] + c)

# MA / MB のやつを探す
ans = IINF
ma, mb = MA, MB
while ma <= SUMMAX and mb <= SUMMAX:
    ans = min(dp[ma, mb], ans)
    ma += MA
    mb += MB
if ans < IINF:
    print(int(ans))
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from subprocess import*
call(('pypy3','-c',"""
I=lambda:list(map(int,input().split()))
n,Ma,Mb=I()
m=[[0,0,0]]+[I()for _ in range(n)]
dp=[[[10**18]*(10*n+1)for _ in range(10*n+1)]for _ in range(n+1)]
dp[0][0][0]=0
for i in range(1,n+1):
    for j in range(10*n+1):
        if j-m[i][0]<0:
            dp[i][j]=dp[i-1][j]
            continue
        for k in range(10*n+1):
            if k-m[i][1]<0:
                dp[i][j][k]=dp[i-1][j][k]
            else:
                dp[i][j][k]=min(dp[i-1][j-m[i][0]][k-m[i][1]]+m[i][2],dp[i-1][j][k])
ans=10**18
for i in range(1,10*n+1):
    j=Mb*i/Ma
    if j==int(j) and j<10*n+2:
        ans=min(ans,dp[-1][i][int(j)])
print(-(ans==10**18)or ans)
"""))

Submission status: Accepted

Solution code:
inf = float('inf')

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

# dp[i][j][k]: i番目までみてj(g), k(g)となるような最小予算
dp = [[[inf]*401 for _ in range(401)] for _ in range(N+1)]
dp[0][0][0] = 0

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

ans = inf

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

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

inf = float('inf')

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

# dp[i][j][k]: i番目までみてj(g), k(g)となるような最小予算
dp = [[[inf]*401 for _ in range(401)] for _ in range(N+1)]
dp[0][0][0] = 0

for i in range(N):
    a,b,c = map(int,input().split())
    for j in range(401):
        for k in range(401):
            dp[i+1][j][k] = dp[i][j][k]
            if j - a >= 0 and k - b >= 0 and dp[i+1][j][k] > dp[i][j-a][k-b] + c:
                dp[i+1][j][k] = dp[i][j-a][k-b] + c

ans = inf

for j in range(1,401):
    for k in range(1,401):
        if j * Mb == Ma * k and ans > dp[N][j][k]:
            ans = dp[N][j][k]

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

Submission status: Accepted

Solution code:
n,ma,mb=map(int,input().split())
inf=10**4
A=[]
append=A.append
for i in range(n):
  a,b,c=map(int,input().split())
  append([a,b,c])
DP=[[[inf]*(10*n+1) for _ in range(10*n+1)] for _ in range(n+1)]
DP[0][0][0]=0
for i in range(n):
  a,b,c=A[i][0],A[i][1],A[i][2]
  for j in range(10*(i+1)+1):
    for k in range(10*(i+1)+1):
      if j>=a and k>=b:
        DP[i+1][j][k]=min(DP[i][j-a][k-b]+c,DP[i][j][k])
      else:
        DP[i+1][j][k]=DP[i][j][k]
ans=inf
for i in range(1,10*n+1):
  if i*mb%ma==0 and i*mb//ma<10*n+1:
    j=i*mb//ma
    ans=min(ans,DP[n][i][j])
print(ans if ans!=inf else -1)

Submission status: Accepted

Solution code:
n,ma,mb=map(int,input().split())
inf=10**4
A=[]
append=A.append
for i in range(n):
  a,b,c=map(int,input().split())
  append([a,b,c])
DP=[[[inf]*(10*n+1) for _ in range(10*n+1)] for _ in range(n+1)]
DP[0][0][0]=0
for i in range(n):
  a,b,c=A[i][0],A[i][1],A[i][2]
  for j in range(10*(i+1)+1):
    for k in range(10*(i+1)+1):
      if j>=a and k>=b:
        DP[i+1][j][k]=min(DP[i][j-a][k-b]+c,DP[i][j][k])
      else:
        DP[i+1][j][k]=DP[i][j][k]
ans=inf
for i in range(10*n+1):
  for j in range(10*n+1):
    if i*mb==j*ma and i*j!=0:
      ans=min(ans,DP[n][i][j])
print(ans if ans!=inf else -1)

Submission status: Accepted

Solution code:
N,Ma,Mb=map(int,input().split())
medic=[tuple(map(int,input().split())) for i in range(N)]
from collections import defaultdict
dp=defaultdict(lambda : float('inf'))
dp[(0,0)]=0
for a,b,c in medic:
    dpc=dp.copy()
    for k,v in dpc.items():
        x,y=k
        dp[(x+a,y+b)]=min(dp[(x+a,y+b)],v+c)
ans=10**10
for i in range(1,1+N):
    ans=min(ans,dp[(Ma*i,Mb*i)])
if ans>10**9:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
def smaller(x, y):
	if x < y: return x
	return y

N, Ma, Mb = map(int, input().split())
a = []
b = []
c = []
suma = 0
sumb = 0
for i in range(0, N):
	inpt = list(map(int, input().split()))
	a.append(inpt[0])
	b.append(inpt[1])
	c.append(inpt[2])
	suma += inpt[0]
	sumb += inpt[1]
dp = []
for i in range(0, N+1):
	lst1 = []
	for j in range(0, suma+1):
		lst2 = []
		for k in range(0, sumb+1):
			lst2.append(-1)
		lst1.append(lst2)
	dp.append(lst1)
dp[0][0][0] = 0
for i in range(0, N):
	for j in range(0, suma+1):
		for k in range(0, sumb+1):
			if dp[i][j][k] != -1:
				if dp[i+1][j][k] == -1:
					dp[i+1][j][k] = dp[i][j][k]
				else:
					dp[i+1][j][k] = smaller(dp[i][j][k], dp[i+1][j][k])
				if dp[i+1][j+a[i]][k+b[i]] == -1:
					dp[i+1][j+a[i]][k+b[i]] = dp[i][j][k] + c[i]
				else:
					dp[i+1][j+a[i]][k+b[i]] = smaller(dp[i][j][k]+c[i], dp[i+1][j+a[i]][k+b[i]])
ans = -1
index = 1
while index * Ma <= suma and index * Mb <= sumb:
	if dp[N][index*Ma][index*Mb] != -1:
		if ans == -1: ans = dp[N][index*Ma][index*Mb]
		else: ans = smaller(ans, dp[N][index*Ma][index*Mb])
	index += 1
print(ans)

Submission status: Accepted

Solution code:

N,A,B = list(map(int,input().split()))

item=[]
for i in range(N):
    a,b,c = list(map(int,input().split()))
    item.append([a,b,c])

D = [[[10**27 for i in range(401)] for j in range(401)] for k in range(N+1)]
D[0][0][0] = 0
for i in range(N):
    for j in range(401):
        for k in range(401):
            if D[i][j][k]<10**26:
                D[i+1][j][k]=min(D[i+1][j][k],D[i][j][k])
                D[i+1][j+item[i][0]][k+item[i][1]] = min(D[i+1][j+item[i][0]][k+item[i][1]],D[i][j][k]+item[i][2])

i=1
min_cost=10**27
while A*i<=400 and B*i<=400:
    min_cost = min(D[N][A*i][B*i],min_cost)
    i+=1
if min_cost==10**27:
	print(-1)
else:
	print(min_cost)

Submission status: Accepted

Solution code:
N,Ma,Mb=map(int,input().split())
abc=[]
INF=float("inf")
for i in range(N):
    a,b,c=map(int,input().split())
    abc.append((a,b,c))
stack=set([(0,0)])
dp=[[[INF]*(401) for j in range(401)] for i in range(N+1)]
dp[0][0][0]=0
for i in range(N):
    dp[i+1][0][0]=0
    a,b,c=abc[i]
    stack_=set()
    for x,y in stack:
        dp[i+1][x][y]=min(dp[i][x][y],dp[i+1][x][y])
        dp[i+1][x+a][y+b]=min(dp[i+1][x+a][y+b],dp[i][x+a][y+b],dp[i][x][y]+c)
        stack_.add((x+a,y+b))
        stack_.add((x,y))
    stack=stack_
k=1
ans=INF
while(Ma*k<=400 and Mb*k<=400):
    ans=min(dp[N][Ma*k][Mb*k],ans)
    k+=1
if ans==INF:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
n,Ma, Mb = map(int, input().split())
dp=[[[1000000]*510 for i in range(510)] for j 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(410):
        for k in range(410):
            dp[i][j][k]=min(dp[i-1][j][k], dp[i][j][k])
            p=j+a
            q=k+b
            dp[i][p][q]=min(dp[i][p][q], dp[i-1][j][k]+c)
ans=1000000
for i in range(1, 410//max(Ma, Mb)+1):
    ans=min(dp[n][i*Ma][i*Mb], ans)
if ans==1000000:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
D - Mixing Experiment
https://atcoder.jp/contests/abc054/tasks/abc054_d

"""
import sys


def solve(N, Ma, Mb, items):
    d = dict()
    d[(0, 0)] = 0
    for a, b, c in items:
        nd = dict()
        for k, v in d.items():
            t = d.get((k[0], k[1]), float('inf')) + c
            if d.get((k[0]+a, k[1]+b), float('inf')) > t:
                nd[(k[0]+a, k[1]+b)] = t
        d.update(nd)
    res = []
    for k, v in d.items():
        if k == (0, 0):
            continue
        a, b = k
        if a*Mb == b*Ma:
            res.append(v)
    return min(res) if res else -1


def main(args):
    N, Ma, Mb = map(int, input().split())
    items = [[int(i) for i in input().split()] for _ in range(N)]
    ans = solve(N, Ma, Mb, items)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
# a,bが小さい
# 合計のa,bでdp

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:
inf = float('inf')

N, Ma, Mb = map(int, input().split())
dp = [[inf] * (400 + 1) for _ in range(400 + 1)]
dp[0][0] = 0

max_a = 0
max_b = 0
for i in range(N):
    a, b, c = map(int, input().split())
    max_a += a
    max_b += b
    for j in reversed(range(max_a + 1 - a)):
        for k in range(max_b + 1 - b):
            if dp[j][k] == inf: continue
            dp[j + a][k + b] = min(dp[j][k] + c, dp[j + a][k + b])

j = Ma
k = Mb
ans = inf
while j < 400 + 1 and k < 400 + 1:
    ans = min(ans, dp[j][k])
    j += Ma
    k += Mb

print(-1 if ans == inf else ans)
# print(dp)

Submission status: Accepted

Solution code:
def solve():
    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 = sum(c)
    dp = [[[INF]*(10*N+1) for _ in range(10*N+1)] for _ in range(N+1)]
    
    dp[0][0][0] = 0
    for i in range(N):
        for j in range(10*N+1-a[i]):
            for k in range(10*N+1-b[i]):
                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 i in range(1, 10*N+1):
        if i*Ma>10*N or i*Mb>10*N:
            break
        ans = min(ans, dp[N][i*Ma][i*Mb])
    if ans==INF:
        ans = -1

    print(ans)


solve()

Submission status: Accepted

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

INF = 10 ** 18
dp = [[[INF] * (10*N+1) for _ in range(10*N+1)] for _ in range(N+1)]

dp[0][0][0] = 0
for i in range(N):
    a, b, c = map(int, input().split())
    for j in range(10*N+1-a):
        for k in range(10*N+1-b):
            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][k+b] = min(dp[i+1][j+a][k+b], dp[i][j][k]+c)

ans = INF
for j in range(1, 10*N+1):
    if j * Ma > 10 * N  or j * Mb > 10 * N:
        break

    ans = min(ans, dp[N][j*Ma][j*Mb])

if ans == INF:
    ans = -1

print(ans)

Submission status: Accepted

Solution code:
N, Ma, Mb = map(int, input().split())
e = [tuple(map(int, input().split())) for _ in range(N)]

N_max = 40
a_max = 10
b_max = 10
c_max = 100

inf = N_max * c_max + 1
dp = [[inf] * (N_max * b_max + 1) for _ in range(N_max * a_max + 1)]
dp[0][0] = 0

for i, (aa, bb, cc) in enumerate(e):
    for a in reversed(range(a_max * i + 1)):
        for b in reversed(range(b_max * i + 1)):
            if dp[a][b] == inf: continue
            if a + aa < N_max * a_max + 1 and b + bb < N_max * b_max + 1:
                dp[a + aa][b + bb] = min(dp[a + aa][b + bb], dp[a][b] + cc)

ans = inf
for i in range(1, N_max * a_max + 1):
    if i * Ma >= N_max * a_max + 1 or i * Mb >= N_max * b_max + 1: break
    # a:b=Ma:Mb
    # b*Ma=a*Mb
    ans = min(ans, dp[i * Ma][i * Mb])

print(ans if ans < inf else -1)

Submission status: Accepted

Solution code:
N, Ma, Mb = map(int, input().split())
e = [tuple(map(int, input().split())) for _ in range(N)]

N_max = 40
a_max = 10
b_max = 10
c_max = 100

inf = N_max * c_max + 1
dp = [[[inf for _ in range(N_max * b_max + 1)] for _ in range(N_max * a_max + 1)] for _ in range(N_max + 1)]
dp[0][0][0] = 0

# print(dp[N])

for i, (aa, bb, cc) in enumerate(e, 1):
    for a in range(a_max * i + 1):
        for b in range(b_max * i + 1):
            dp[i][a][b] = min(dp[i][a][b], dp[i - 1][a][b])
            if a + aa < N_max * a_max + 1 and b + bb < N_max * b_max + 1:
                dp[i][a + aa][b + bb] = min(dp[i][a + aa][b + bb], dp[i - 1][a][b] + cc)

# print(dp[N])

ans = inf
for a in range(N_max * a_max + 1):
    for b in range(N_max * b_max + 1):
        # a:b=Ma:Mb
        # b*Ma=a*Mb
        if b * Ma == a * Mb and a * b > 0:
            ans = min(ans, dp[N][a][b])

print(ans if ans < inf else -1)

Submission status: Accepted

Solution code:
N,Ma,Mb = map(int,input().split())
abc = [list(map(int,input().split())) for _ in range(N)]
inf = 10**9
dp = [[inf for i in range(N*10+1)] for i in range(N*10+1)]
dp[0][0] = 0
max_a = 0
max_b = 0
for i in range(N):
    max_a += abc[i][0]
    max_b += abc[i][1]
    for ai in range(abc[i][0],max_a+1)[::-1]:
        for bi in range(abc[i][1],max_b+1)[::-1]:
            dp[ai][bi] = min(dp[ai][bi],dp[ai-abc[i][0]][bi-abc[i][1]]+abc[i][2])
ans = inf
for i in range(1,min(max_a//Ma,max_b//Mb)):
    ans = min(ans,dp[i*Ma][i*Mb])
if ans == inf:
    print(-1)
else:
    print(ans)