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

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

import sys
INT_MAX = sys.maxsize


drugstores = ABC[:]
drugstores = [(c, a, b) for a,b,c in drugstores]
drugstores = sorted(drugstores)


mix_list = [(0, 0, 0)]

ans = INT_MAX
import heapq
for d_c, d_a, d_b in drugstores:
    for m_c, m_a, m_b in mix_list[:]:
        a = d_a + m_a
        b = d_b + m_b
        c = d_c + m_c
        
        if a * Mb == b * Ma:
            #print(a, b, c)
            ans = min(ans, c)
        elif ans > c:
            heapq.heappush(mix_list, (c, a, b))

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

Submission status: Accepted

Solution code:
n, ma, mb = map(int,input().split())
INF = 10**9
dp = {(0,0):0}
for _ in range(n):
  a, b, c = map(int, input().split())
  #print('chk!',a,b,c)
  for (ea, eb), ec in dp.copy().items():
    dp[ea+a,eb+b] = min(dp.get((ea+a,eb+b),INF), ec+c)
    #print(dp)
ans = min(dp.get((i*ma,i*mb),INF) for i in range(1,401))
print(ans if ans != INF else -1)

Submission status: Accepted

Solution code:
import sys
from functools import lru_cache

sys.setrecursionlimit(2*10**5)

# 入力
N, Ma, Mb = map(int, input().split())
a, b, c = (
    zip(*(map(int, input().split()) for _ in range(N))) if N else
    ((), ())
)


# タイプAがxグラム, タイプBがyグラムの物質の最小生成コストを動的計画法により求める
@lru_cache(maxsize=None)
def f(i, x, y):
    return (
        10**18 if (i < 0 and not (x == y == 0)) or x < 0 or y < 0 else
        0 if x == 0 and y == 0 else
        min(
            f(i - 1, x - a[i - 1], y - b[i - 1]) + c[i - 1],
            f(i - 1, x, y)
        )
    )


# タイプAとタイプBの物質の比率がMa:Mbとなるような物質の最小コストを求める
A = sum(a)
B = sum(b)
opt = min(
    f(N, x, Mb * (x // Ma))
    for x in range(Ma, A + 1)
    if x % Ma == 0 and Mb * (x // Ma) <= B
)
ans = (
    opt if opt < 10**18 else
    -1
)

# 出力
print(ans)

Submission status: Accepted

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


dp = [[[float('inf')] * 401 for j in range(401)] for i in range(N + 1)]
dp[0][0][0] = 0
for i, (a, b, c) in enumerate(Drug):
    for ma in range(401):
        for mb in range(401):
            if (ma + a < 401) and (mb + b < 401):
                dp[i + 1][ma + a][mb + b] = min(dp[i + 1][ma + a][mb + b], dp[i][ma][mb] + c)
            dp[i + 1][ma][mb] = min(dp[i + 1][ma][mb], dp[i][ma][mb])

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

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

Submission status: Accepted

Solution code:
n, ma, mb = map(int, input().split())
x = [list(map(int, input().split())) for _ in range(n)]
a_limit = [0]
b_limit = [0]
for i in range(n):
    a_limit.append(a_limit[-1] + x[i][0])
    b_limit.append(b_limit[-1] + x[i][1])

INF = 10 ** 9
DP = [None for _ in range(n + 1)]
for i in range(n + 1):
    DP[i] = [[INF for k in range(b_limit[-1] + 1)] for j in range(a_limit[-1] + 1)]

DP[0][0][0] = 0
for i in range(n):
    for j in range(a_limit[i] + 1):
        for k in range(b_limit[i] + 1):
            DP[i + 1][j][k] = min(DP[i + 1][j][k], DP[i][j][k])
            DP[i + 1][j + x[i][0]][k + x[i][1]] = min(DP[i + 1][j + x[i][0]][k + x[i][1]], DP[i][j][k] + x[i][2])

ans = INF
for j in range(1, a_limit[-1] + 1):
    for k in range(1, b_limit[-1] + 1):
        if k * ma == j * mb:
            ans = min(ans, DP[-1][j][k])
if ans == INF:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import numpy as np


def solve(N: int, M_a: int, M_b: int, a: "List[int]", b: "List[int]", c: "List[int]"):
    c_max = 100
    a_max = 10
    total_max = (N+1) * a_max

    t = np.full((N+1, total_max + 1, total_max + 1), c_max * N, dtype=int)
    t[0][0][0] = 0

    for i in range(N):
        t[i+1] = t[i]
        buy = t[i, :-a[i], :-b[i]] + c[i]
        not_buy = t[i, a[i]:, b[i]:]
        cheaper = buy < not_buy
        t[i+1, a[i]:, b[i]:] = np.where(cheaper, buy, not_buy)
    
    t_a, t_b = M_a, M_b
    cheapest = total_max + 1
    while t_a <= total_max and t_b <= total_max:
        total = t[N][t_a][t_b]
        if total < cheapest:
            cheapest = total
        t_a += M_a
        t_b += M_b
    
    if cheapest == total_max + 1:
        print(-1)
    else:
        print(cheapest)

    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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:
n, ma, mb = map(int,input().split())
drag_list = []
a_sum = 0
b_sum = 0
for i in range(n):
    a, b, c = map(int,input().split())
    a_sum +=a
    b_sum +=b
    drag = [a,b,c]
    drag_list.append(drag)

INF = 100000
dp = [[[INF for k in range(401)]\
              for j in range(401)]\
              for i in range(n+1)]
dp[0][0][0] = 0
for i in range(n):
    A, B, C = drag_list[i]
    for a in range(a_sum+1):
        for b in range(b_sum+1):
            if dp[i][a][b] == INF:
                continue
            if A + a <= a_sum + 1 and B + b <= b_sum + 1:
                dp[i+1][a][b] = min(dp[i+1][a][b], dp[i][a][b])
                dp[i+1][a+A][b+B] = min(dp[i+1][a+A][b+B], dp[i][a][b] + C)
ans = INF
for a in range(1,401):
    for b in range(1,401):
        if a*mb == b*ma:
            ans = min(ans, dp[n][a][b])
print(-1 if ans == INF else ans)
import numpy as np
dp = np.array(dp)

Submission status: Accepted

Solution code:
INF = 10**10
n, ma, mb = [int(item) for item in input().split()]
abc = [[int(item) for item in input().split()] for _ in range(n)]

memo = [[INF] * 401 for _ in range(401)]
memo[0][0] = 0 
for a, b, c in abc:
    for i in range(400,-1,-1):
        for j in range(400,-1,-1):
            if memo[i][j] != INF:
                memo[i+a][j+b] = min(memo[i+a][j+b], memo[i][j] + c)
ans = INF
for i in range(1,min(400//ma, 400//mb)):
    ans = min(ans, memo[ma*i][mb*i])
if ans == INF:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
n, ma, mb = map(int,input().split())
INF = 10**6
abmax = 10
nmax = 40
medi = []
sumb = 0
suma = 0
for i in range(n):
    a, b, c = map(int,input().split())
    suma += a
    sumb += b
    medi.append([a,b,c])
dp = [[[INF for k in range(nmax*abmax+1)] for j in range(nmax*abmax+1)] for i in range(nmax+1)]
dp[0][0][0] = 0
for i in range(n):
    a, b, c = medi[i]
    for x in range(suma+1):
        for y in range(sumb+1):
            if dp[i][x][y] == INF:
                continue
            if x + a <= suma + 1 and y + b <= sumb + 1:
                dp[i + 1][x][y] = min(dp[i + 1][x][y], dp[i][x][y])
                dp[i + 1][x + a][y + b] = min(dp[i][x][y] + c, dp[i + 1][x + a][y + b])

t = 1
ok = False
ans = INF
for ca in range(1,nmax*abmax+1):
    for cb in range(1,nmax*abmax+1):
        if ma*cb == mb*ca:
            ans = min(ans, dp[n][ca][cb])
    
print(ans if ans != INF else -1)

Submission status: Accepted

Solution code:
n, ma, mb = map(int,input().split())
INF = 10**6
abmax = 10
nmax = 40
medi = []
sumb = 0
suma = 0
for i in range(n):
    a, b, c = map(int,input().split())
    suma += a
    sumb += b
    medi.append([a,b,c])
dp = [[[INF for k in range(nmax*abmax+1)] for j in range(nmax*abmax+1)] for i in range(nmax+1)]
dp[0][0][0] = 0
for i in range(n):
    a, b, c = medi[i]
    for x in range(suma+1 - a):
        for y in range(sumb+1 - b):
            if dp[i][x][y] == INF:
                continue
            dp[i + 1][x][y] = min(dp[i + 1][x][y], dp[i][x][y])
            dp[i + 1][x + a][y + b] = min(dp[i][x][y] + c, dp[i + 1][x + a][y + b])

t = 1
ok = False
ans = INF
for ca in range(1,nmax*abmax+1):
    for cb in range(1,nmax*abmax+1):
        if ma*cb == mb*ca:
            ans = min(ans, dp[n][ca][cb])
    
print(ans if ans != INF else -1)

Submission status: Accepted

Solution code:
n, ma, mb = map(int,input().split())
INF = 1000000
abmax = 10
nmax = 40
medi = []
sumb = 0
suma = 0
for i in range(n):
    a, b, c = map(int,input().split())
    suma += a
    sumb += b
    medi.append([a,b,c])
dp = [[[INF for k in range(nmax*abmax+1)] for j in range(nmax*abmax+1)] for i in range(nmax+1)]
dp[0][0][0] = 0
for i in range(n):
    a, b, c = medi[i]
    for x in range(suma+1 - a):
        for y in range(sumb+1 - b):
            if dp[i][x][y] == INF:
                continue
            dp[i + 1][x][y] = min(dp[i + 1][x][y], dp[i][x][y])
            dp[i + 1][x + a][y + b] = min(dp[i][x][y] + c, dp[i + 1][x + a][y + b])

t = 1
ok = False
ans = INF
for ca in range(1,nmax*abmax+1):
    for cb in range(1,nmax*abmax+1):
        if ma*cb == mb*ca:
            ans = min(ans, dp[n][ca][cb])
    
print(ans if ans != INF else -1)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 8)

N, Ma, Mb = map(int, input().split())
A = [[int(i) for i in input().split()] for i in range(N)]
memo = {i : {} for i in range(N)}

def dfs(i, a, b):
    if b > 0 and a / b == Ma / Mb:
        return 0
    elif i == N:
        return float('inf')
    elif (a, b) in memo[i]:
        return memo[i][(a, b)]
    else:
        ans = min(dfs(i + 1, a, b), dfs(i + 1, a + A[i][0], b + A[i][1]) + A[i][2])
    memo[i][(a, b)] = ans
    return ans

if dfs(0, 0, 0) == float('inf'):
    print(-1)
else:
    print(dfs(0, 0, 0))

Submission status: Accepted

Solution code:
import sys
from collections import deque

def main():
    input = sys.stdin.readline
    N, Ma, Mb = map(int, input().split())
    A = []
    a_sum = 1
    b_sum = 1
    for _ in range(N):
        a, b, c = map(int, input().split())
        A.append((a, b, c))
        a_sum += a
        b_sum += b

    DP = [[10**5 for _ in range(b_sum)] for _ in range(a_sum)]
    q = deque()
    for a, b, c in A:
        DP[a][b] = min(DP[a][b], c)

        q_ = deque()
        q_.append((a, b, c))
        while len(q) > 0:
            x, y, z = q.popleft()
            q_.append((x, y, z))

            s, t, u = x+a, y+b, z+c
            if DP[s][t] > u:
                DP[s][t] = u
                q_.append((s, t, u))

        q = q_

    n = min(a_sum // Ma, b_sum // Mb)
    ans = 10**5
    for i in range(1, n):
        x, y = Ma * i, Mb * i
        ans = min(ans, DP[x][y])

    if ans == 10**5:
        return -1
    else:
        return ans


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

Submission status: Accepted

Solution code:
#D問題
NM=40
ABM=10
MM=400

N,M1,M2=map(int,input().split())
A=[]
B=[]
C=[]
for i in range(N):
    a,b,c=map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(c)
cmax=max(C)
CM=N*cmax+10
dp=[[[CM for broop in range(401)] for aroop in range(401)] for nroop in range(N+1)]
dp[0][0][0]=0
for i in range(N):
    for cb in range(401):
        for ca in range(401):
            if dp[i][ca][cb]==CM:
                continue
            dp[i+1][ca][cb]=min(dp[i+1][ca][cb],dp[i][ca][cb])
            if ca+A[i]<401 and cb+B[i]<401:
                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=CM
for cb in range(400):
    for ca in range(400):
        CB=cb+1
        CA=ca+1
        if CA*M2 == CB*M1:
            ans=min(ans,dp[N][CA][CB])

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

Submission status: Accepted

Solution code:
N,A,B=map(int,input().split())

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

dp=[[100000 for i in range(401)]for j in range(401)]
dp[0][0]=0

for n in range(N):
    for i in range(401):
        for j in range(401):
            if dp[400-i][400-j]<100000:
                if 400-i+L[n][0]<=400 and 400-j+L[n][1]<=400:
                    if dp[400-i+L[n][0]][400-j+L[n][1]]>dp[400-i][400-j]+L[n][2]:
                        dp[400-i+L[n][0]][400-j+L[n][1]]=dp[400-i][400-j]+L[n][2]
ans=100000
for i in range(1,401):
    for j in range(1,401):
        if i*B==j*A and ans>dp[i][j]:
            ans=dp[i][j]
if ans<100000:
    print(ans)
else:
    print(-1)
#print(dp)

Submission status: Accepted

Solution code:
def main():
    n,ma,mb = map(int, input().split())
    a,b,c = [],[],[]
    for i in range(n):
        ia,ib,ic = map(int, input().split())
        a.append(ia)
        b.append(ib)
        c.append(ic)
    sa = sum(a)
    sb = sum(b)
    INF = 10**5
    dp = [[ [INF] * (sb+1) for i in range(sa+1)] for i in range(n+1)]
    dp[0][0][0] = 0

    for i in range(n):
        for ca in range(sa):
            for cb in range(sb):
                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])



    res = INF
    for ca in range(1, sa):
        for cb in range(1, sb):
            if ca*mb==cb*ma:
                res = min(res, dp[n][ca][cb])

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def mixing_experiment(N: int, Ma: int, Mb: int, drags: list)->int:
    INF = float('inf')

    max_A = min(100, N * max(a for a, _, _ in drags))
    max_B = min(100, N * max(b for _, b, _ in drags))

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

    for (a, b, c) in drags:
        for ca in range(max_A-a, -1, -1):
            for cb in range(max_B-b, -1, -1):
                dp[ca+a][cb+b] = \
                    min(dp[ca+a][cb+b], dp[ca][cb] + c)
        dp[a][b] = min(dp[a][b], c)

    ans = INF
    for ca in range(1, max_A+1):
        for cb in range(1, max_B+1):
            if ca * Mb != cb * Ma:
                continue
            ans = min(ans, dp[ca][cb])

    return ans if 0 < ans and ans < INF else -1


if __name__ == "__main__":
    N = 0
    N, Ma, Mb = map(int, input().split())
    drags = [tuple(int(s) for s in input().split()) for _ in range(N)]
    ans = mixing_experiment(N, Ma, Mb, drags)
    print(ans)

Submission status: Accepted

Solution code:
# import time


def mixing_experiment(N: int, Ma: int, Mb: int, drags: list)->int:
    INF = float('inf')

    max_A = min(100, N * max(a for a, _, _ in drags))
    max_B = min(100, N * max(b for _, b, _ in drags))

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

    # print(max_A, max_B)
    # t1 = time.time()

    # op = 0
    for (a, b, c) in drags:
        for ca in range(max_A-a, -1, -1):
            for cb in range(max_B-b, -1, -1):
                dp[ca+a][cb+b] = \
                    min(dp[ca+a][cb+b], dp[ca][cb] + c)
                # dp[ca+a][cb+b] = 0
                # op += 1
        dp[a][b] = min(dp[a][b], c)

    # t2 = time.time()
    # print('dp: {} [s]'.format(t2 - t1))

    ans = INF
    for ca in range(1, max_A+1):
        for cb in range(1, max_B+1):
            if ca * Mb != cb * Ma:
                continue
            ans = min(ans, dp[ca][cb])

    # t3 = time.time()
    # print('min: {} [s]'.format(t3 - t2))

    return ans if 0 < ans and ans < INF else -1


if __name__ == "__main__":
    N = 0
    N, Ma, Mb = map(int, input().split())
    drags = [tuple(int(s) for s in input().split()) for _ in range(N)]
    ans = mixing_experiment(N, Ma, Mb, drags)
    print(ans)

Submission status: Accepted

Solution code:
mod=1777777777
n,ma,mb=map(int,input().split())
d=[[mod for j in range(100)] for i in range(100)]
for i in range(n):
    a,b,c=map(int,input().split())
    for j in range(100-a-1,-1,-1):
        for k in range(100-b-1,-1,-1):
            d[j+a][k+b]=min(d[j+a][k+b],d[j][k]+c)
    d[a][b]=min(d[a][b],c)
ans=mod
for i in range(100):
    if i%ma==0 and mb*i//ma<100:
        ans=min(ans,d[i][mb*i//ma])

print([ans,-1][ans==mod])

Submission status: Accepted

Solution code:
mod=1777777777
n,ma,mb=map(int,input().split())
d=[[mod for j in range(100)] for i in range(100)]
for i in range(n):
    a,b,c=map(int,input().split())
    x=a
    y=b
    for j in range(100-a-1,0,-1):
        for k in range(100-b-1,0,-1):
            if d[j][k]!=mod:
                d[j+a][k+b]=min(d[j+a][k+b],d[j][k]+c)
    d[x][y]=min(d[x][y],c)
ans=mod
for i in range(100):
    if i%ma==0 and mb*i//ma<100:
        ans=min(ans,d[i][mb*i//ma])

print([ans,-1][ans==mod])

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 = [tuple(li()) for _ in range(n)]

INF = 10**18

dp = [[[INF]*401 for _ in range(401)] for _ in range(41)]
dp[0][0][0] = 0

for i in range(n):
    ai, bi, ci = drugs[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 = INF
for ai in range(1,401):
    for bi in range(1,401):
        if ai/bi == ma/mb and dp[n][ai][bi] < INF:
            ans = min(ans, dp[n][ai][bi])
            
print(ans if ans < INF else -1)

Submission status: Accepted

Solution code:
n,ma,mb = map(int,input().split())
info =[list(map(int,input().split())) for i in range(n)]

max_a = sum(list(zip(*info))[0]) + 1
max_b = sum(list(zip(*info))[1]) + 1

dp = [[[float("inf")]*(max_b) for i in range(max_a)] for i in range(n+1)]

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

for ni in range(1,len(info)+1):
  for ai in range(0,max_a):
    for bi in range(0,max_b):
      if ai-info[ni-1][0] < 0 or bi-info[ni-1][1] < 0:
        dp[ni][ai][bi] = dp[ni-1][ai][bi]
      else:
        dp[ni][ai][bi] = min(dp[ni-1][ai][bi],dp[ni-1][ai-info[ni-1][0]][bi-info[ni-1][1]]+info[ni-1][2])
    
ans = float("inf")
for ai in range(1,max_a):
  for bi in range(1,max_b):
    if ai*mb == bi*ma:
      ans = min(ans,dp[n][ai][bi])
#print(dp[n])
if ans == float("inf"):
  print(-1)
else:
  print(ans)

Submission status: Accepted

Solution code:
n,a,b=map(int,input().split())
d=[[0]*401 for i in range(401)]
h=[]
for i in range(n):
    s=[]
    c=d[:]
    p,q,r=map(int,input().split())
    if d[p][q]!=0:
        d[p][q]=min(r,c[p][q])
    else:
        s.append([p,q])
        d[p][q]=r
    h=sorted(h,key=lambda x:x[1]+x[0])
    for j in range(len(h)-1,-1,-1):
        if h[j][0]+p<401 and h[j][1]+q<401:
            if c[h[j][0]+p][h[j][1]+q]!=0:
                d[h[j][0]+p][h[j][1]+q]=min(r+c[h[j][0]][h[j][1]],d[h[j][0]+p][h[j][1]+q])
            else:
                s.append([h[j][0]+p,h[j][1]+q])
                d[h[j][0]+p][h[j][1]+q]=r+c[h[j][0]][h[j][1]]
    else:
        h+=s
else:
    c=float("inf")
    k=1
    while a*k<401 and b*k<401:
        if d[a*k][b*k]!=0:
            c=min(c,d[a*k][b*k])
        k+=1
    else:
        if c==float("inf"):
            print(-1)
        else:
            print(c)

Submission status: Accepted

Solution code:
N,Ma,Mb=list(map(int, input().strip().split()))
abc=[list(map(int, input().strip().split())) for i in range(N)]
maxW=N*10
dp=[{} for __ in range(N+1)]
dp[0][(0,0)]=0
for i in range(N):
    a,b,c=abc[i]
    for k,v in dp[i].items():
        dp[i+1][k]=min(dp[i][k], dp[i+1][k]if k in dp[i+1] else float("inf"))
        dp[i+1][k[0]+a,k[1]+b]=min(dp[i][k]+c, dp[i+1][k[0]+a,k[1]+b] if (k[0]+a,k[1]+b)in dp[i+1] else float("inf"))

cost=float("inf")
Mad=Ma
Mbd=Mb
while Ma<=maxW and Mb <=maxW:
    if (Ma,Mb) in dp[-1]:
        cost=min(dp[-1][Ma,Mb],cost)
    Ma+=Mad
    Mb+=Mbd
print(-1 if cost==float("inf") else cost)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import itertools
import sys
import math
from functools import lru_cache

# 1整数
# n = int(input())

# 空白区切り2変数
from queue import Queue

n, a, b = list(map(int, input().split()))

ll = [[-1 for i in range(401)] for j in range(401)]

# queue = Queue()
ll[0][0] = 0
for i in range(n):
    ap, bp, price = list(map(int, input().split()))
    #    next = Queue()
    #    for q in queue:
    #        ll[q[0] + ap][q[1] + bp] = min(ll[q[0]][q[1]] + price, ll[q[0] + ap][q[1] + bp])
    #        next.put_nowait([q[0] + ap, q[1] + bp])
    for aa in reversed(range(390)):
        for bb in reversed(range(390)):
            if ll[aa][bb] >= 0:
                if ll[aa + ap][bb + bp] < 0:
                    ll[aa + ap][bb + bp] = 99999
                ll[aa + ap][bb + bp] = min(ll[aa + ap][bb + bp], ll[aa][bb] + price)
              #  print(aa+ap,bb+bp,ll[aa+ap][bb+bp])
ret = 9999999
for aa in range(401):
    for bb in range(1, 401):
        if abs(float(aa) / bb - float(a) / b) < 0.001 and ll[aa][bb] > 0:
            ret = min(ret, ll[aa][bb])
if ret > 900000:
    print('-1')
else:
    print(ret)

Submission status: Accepted

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

inf = float("inf")

dp = [ [ [inf] * 401 for _ in range(401)] for _ in range(N+1)]
dp[0][0][0] = 0

for i in range(N):
  a,b,c = ABC[i]
  for ma in range(400):
    for mb in range(400):
      dp[i+1][ma][mb] = min(dp[i+1][ma][mb],
                            dp[i][ma][mb])
      if ma + a > 400: continue
      if mb + b > 400: continue
        
      dp[i+1][ma+a][mb+b] = \
      min(dp[i+1][ma+a][mb+b],
          dp[i][ma][mb]+c)
      
ans = inf
for i in range(1,40000):
  if Ma*i > 400 or Mb*i > 400: break
  ans = min(ans, dp[N][Ma*i][Mb*i])

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

Submission status: Accepted

Solution code:
def main():
    N, MA, MB = map(int, input().split())
    dp = [[[float("inf")] * (401) for _ in range(401)] for _ in range(2)]
    dp[0][0][0] = 0
    dp[1][0][0] = 0
    for i in range(N):
        a, b, c = map(int, input().split())
        prev = (i-1) % 2
        current = i % 2
        for j in range(401):
            for k in range(401):
                if j-a >= 0 and k-b >= 0:
                    dp[current][j][k] = min(dp[prev][j][k],
                                            dp[prev][j-a][k-b] + c)
                else:
                    dp[current][j][k] = dp[prev][j][k]
    ans = float("inf")
    for i in range(1, 401):
        a = MA * i
        b = MB * i
        if a <= 400 and b <= 400:
            ans = min(ans, dp[(N-1) % 2][a][b])
        else:
            continue
    if ans == float("inf"):
        print(-1)
    else:
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n, ma, mb = map(int, input().split())
abc = [tuple(int(x) for x in input().split()) for _ in range(n)]

c = n * 100 + 1

m = 401

dp = [[[99999 for _ in range(m)] for _ in range(m)] for _ in range(n + 1)]

ans = 99999
dp[0][0][0] = 0
for i in range(n):
    for j in range(m):
        for k in range(m):
            if dp[i][j][k] < 9999:
                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])
                if (j+abc[i][0]) * mb == (k+abc[i][1]) * ma:
                    ans = min(ans, dp[i+1][j+abc[i][0]][k+abc[i][1]])

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

Submission status: Accepted

Solution code:
N,Ma,Mb=map(int,input().split())
medic=[tuple(map(int,input().split())) for _ in range(N)]
from collections import defaultdict
inf = float('inf')
dp=defaultdict(lambda :inf)
dp[(0,0)]=0
for n in range(N):
    dpc=dp.copy()
    a,b,c=medic[n]
    for d,cost in dpc.items():
        da,db=d
        dp[(da+a,db+b)]=min(dp[(da+a,db+b)],c+cost)
ans=inf
nowa,nowb=Ma,Mb
while nowa<=400 and nowb<=400:
    ans=min(ans,dp[nowa,nowb])
    nowa+=Ma
    nowb+=Mb
if ans>4000:ans=-1
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict

MAX = 40001


def main():
    N, Ma, Mb = [int(a) for a in input().split()]

    abc = [
        [int(a) for a in input().split()]
        for _ in range(N)
    ]

    dp = [
        [
            [MAX for b in range(10 * N + 1)]
            for a in range(10 * N + 1)
        ]
        for i in range(N)
    ]  # dp[i][a][b] = i番目までを使ってaグラムbグラムを作る最小コスト

    A, B, C = abc[0]
    # dp[0][a][b] = c
    # dp[0][0][0] = 0

    mem = {(0, A, B): C, (0, 0, 0): 0}

    def solve(i, a, b):
        if (i, a, b) in mem:
            return mem[(i, a, b)]
        if i == 0:
            return MAX
        ai, bi, ci = abc[i]
        tukawanai = solve(i - 1, a, b)
        if a - ai >= 0 and b - bi >= 0:
            pre = solve(i - 1, a - ai, b - bi)
            if pre != MAX:
                mem[(i, a, b)] = min(tukawanai, pre + ci)
                return mem[(i, a, b)]
        mem[(i, a, b)] = tukawanai
        return mem[(i, a, b)]

    res = MAX
    for a in range(1, 10 * N + 1):
        aa = a * Mb
        if aa % Ma != 0:
            continue
        b = aa // Ma
        if b >= 10 * N + 1:
            continue
        c = solve(N - 1, a, b)
        res = min(res, c)

    if res == MAX:
        res = -1
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

dp = [[10**18 for _ in range(401)] for _ in range(401)]

dp[0][0]=0
for i in range(N):
    a,b,c=map(int,input().split())
    for ca in range(i * 10, -1, -1):
        for cb in range(i * 10, -1, -1):
            dp[ca+a][cb+b] = min(dp[ca+a][cb+b],dp[ca][cb]+c)

ans = 10**18
for ca in range(401):
    for cb in range(401):
        if not ca*Mb == cb*Ma or ca == 0:
            continue
        if ans > dp[ca][cb]:
            ans = dp[ca][cb]
        
if ans == 10**18:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
N,MA,MB=map(int,input().split())
S=[list(map(int,input().split())) for i in range(N)]

DPLIST=[[10**6 for i in range(401)] for j in range(401)]
DPLIST[0][0]=0

for a,b,c in S:
    for i in range(400,a-1,-1):
        for j in range(400,b-1,-1):
            if DPLIST[i][j]>DPLIST[i-a][j-b]+c:
                DPLIST[i][j]=DPLIST[i-a][j-b]+c
                
ANS=10**6
for k in range(1,400//max(MA,MB)):
    if DPLIST[MA*k][MB*k]<ANS:
        ANS=DPLIST[MA*k][MB*k]

if ANS==10**6:
    print(-1)
else:
    print(ANS)

Submission status: Accepted

Solution code:
inf = 9999999999
N, Ma, Mb = map(int, input().split())
d = [{} for i in range(N + 1)]
for i in range(N):
    a, b, c = map(int, input().split())
    for (j, k) in d[i]:
        d[i + 1][j, k] = d[i][j, k]
    d[i + 1][a, b] = min(c, d[i].get((a, b), inf))
    for (j, k) in d[i]:
        d[i + 1][j + a, k + b] = min(d[i][j, k] + c, d[i].get((j + a, k + b), inf))
ans = inf
for (j, k) in d[N]:
    if j * Mb == k * Ma:
        ans = min(ans, d[N][j, k])
if ans == inf:
    ans = -1
print(ans)

Submission status: Accepted

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

dp = [ [ [float("inf")] * (25*Mb+1) for _ in range(25*Ma+1)] for _ in range(N+1)]
dp[0][0][0] = 0
# dp[i][ma][mb] 

for i in range(0, N):
    a,b,c = ABC[i]

    for ma in range(0,10*Ma+1):
        for mb in range(0,10*Mb+1):
            
            dp[i+1][ma+a][mb+b] = min(dp[i+1][ma+a][mb+b],
                    dp[i][ma][mb] + c) 
            dp[i+1][ma][mb] = min(dp[i+1][ma][mb],
                                  dp[i][ma][mb])

ans = float("inf")

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

# print(dp[3][1][2])
# print(dp[N][Ma][Mb])

Submission status: Accepted

Solution code:
n, ma, mb = list(map(int, input().split()))
negs, poss, ans = [], [], 100 * n + 1
for _ in range(n):
    a, b, c = list(map(int, input().split()))
    sub = a * mb - b * ma
    if sub > 0:
        poss.append((sub, c))
    elif sub < 0:
        negs.append((-sub, c))
    else:
        ans = min(c, ans)

def sums(tups):
    ret = {0: 0}
    for x, c in tups:
        tmp = ret.copy()
        for x0, c0 in tmp.items():
            if x + x0 in ret:
                ret[x + x0] = min(c + c0, ret[x + x0])
            else:
                ret[x + x0] = c + c0
    del ret[0]
    return ret

nsum = sums(negs)
psum = sums(poss)

for v in nsum:
    if v in psum:
        ans = min(ans, nsum[v] + psum[v])
if ans == 100 * n + 1:
    ans = -1
print(ans)