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: Wrong Answer

Solution code:
from copy import deepcopy

def getval():
    n,ma,mb = map(int,input().split())
    p = [list(map(int,input().split())) for i in range(n)]
    return n,ma,mb,p 

def main(n,ma,mb,p):
    dp = [[[-1 for i in range(401)] for i in range(401)]]
    dp[0][0][0] = 0
    for i in range(n):
        temp = deepcopy(dp[-1])
        temp[0][0] = 0
        cur = p[i]
        prev = dp[-1]
        for a in range(401):
            for b in range(401):
                if cur[0]>a or cur[1]>b:
                    continue
                if prev[a-cur[0]][b-cur[1]]==-1:
                    continue
                elif prev[a][b]==-1:
                    temp[a][b] = prev[a-cur[0]][b-cur[1]] + cur[2]
                    #print(i,a,b,temp[a][b],prev[a-cur[0]][b-cur[1]])
                else:
                    temp[a][b] = min(prev[a][b], prev[a-cur[0]][b-cur[1]] + cur[2])
        dp.append(temp)
    ans = -1
    for i in range(1,11):
        if i*ma>10 or i*mb>10:
            break
        if dp[n][i*ma][i*mb]!=-1:
            if ans==-1:
                ans = dp[n][i*ma][i*mb]
            else:
                ans = min(ans,dp[n][i*ma][i*mb])
    print(ans)
    #print(dp[n])

if __name__=="__main__":
    n,ma,mb,p = getval()
    main(n,ma,mb,p)

Submission status: Wrong Answer

Solution code:
from copy import deepcopy

def getval():
    n,ma,mb = map(int,input().split())
    p = [list(map(int,input().split())) for i in range(n)]
    return n,ma,mb,p 

def main(n,ma,mb,p):
    dp = [[[-1 for i in range(11)] for i in range(11)]]
    dp[0][0][0] = 0
    for i in range(n):
        temp = deepcopy(dp[-1])
        temp[0][0] = 0
        cur = p[i]
        prev = dp[-1]
        for a in range(11):
            for b in range(11):
                if cur[0]>a or cur[1]>b:
                    continue
                if prev[a-cur[0]][b-cur[1]]==-1:
                    continue
                elif prev[a][b]==-1:
                    temp[a][b] = prev[a-cur[0]][b-cur[1]] + cur[2]
                    #print(i,a,b,temp[a][b],prev[a-cur[0]][b-cur[1]])
                else:
                    temp[a][b] = min(prev[a][b], prev[a-cur[0]][b-cur[1]] + cur[2])
        dp.append(temp)
    ans = -1
    for i in range(1,11):
        if i*ma>10 or i*mb>10:
            break
        if dp[n][i*ma][i*mb]!=-1:
            if ans==-1:
                ans = dp[n][i*ma][i*mb]
            else:
                ans = min(ans,dp[n][i*ma][i*mb])
    print(ans)
    #print(dp[0])

if __name__=="__main__":
    n,ma,mb,p = getval()
    main(n,ma,mb,p)

Submission status: Wrong Answer

Solution code:
from copy import copy

def getval():
    n,ma,mb = map(int,input().split())
    p = [list(map(int,input().split())) for i in range(n)]
    return n,ma,mb,p 

def main(n,ma,mb,p):
    dp = [[[-1 for i in range(11)] for i in range(11)]]
    dp[0][0][0] = 0
    for i in range(n):
        temp = copy(dp[-1])
        temp[0][0] = 0
        cur = p[i]
        prev = dp[-1]
        for a in range(11):
            for b in range(11):
                if cur[0]>a or cur[1]>b:
                    continue
                if prev[a-cur[0]][b-cur[1]]==-1:
                    continue
                elif prev[a][b]==-1:
                    temp[a][b] = prev[a-cur[0]][b-cur[1]] + cur[2]
                else:
                    temp[a][b] = min(prev[a][b], prev[a-cur[0]][b-cur[1]] + cur[2])
        dp.append(temp)
    ans = -1
    for i in range(1,11):
        if i*ma>10 or i*mb>10:
            break
        if dp[n][i*ma][i*mb]!=-1:
            if ans==-1:
                ans = dp[n][i*ma][i*mb]
            else:
                ans = min(ans,dp[n][i*ma][i*mb])
    print(ans)
    #print(dp[n])

if __name__=="__main__":
    n,ma,mb,p = getval()
    main(n,ma,mb,p)

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = map(int, input().split())
abc = [list(map(int, input().split())) for _ in range(N)]
INF = 10 ** 9 + 7
dp = [[[INF] * (Mb * N + 1) for _ in range(N * Ma + 1)] for _ in range(N + 1)]
dp[0][0][0] = 0
for i in range(N):
    for j in range(Ma * N + 1):
        for k in range(Mb * N + 1):
            if j - abc[i][0] >= 0 and k - abc[i][1] >=0:
                dp[i + 1][j][k] = min(dp[i + 1][j - abc[i][0]][k - abc[i][1]] + abc[i][2],dp[i][j][k])
            else:
                dp[i + 1][j][k] = min(dp[i][j][k], dp[i + 1][j][k])

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

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

Submission status: Wrong Answer

Solution code:
n,ma,mb=map(int,input().split())
dp=[[[10**4 for _ in range(401)] for __ in range(401)] for ___ in range(n+1)]
for i in range(n+1):
    dp[i][0][0]=0
for i in range(n):
    a,b,c=map(int,input().split())
    for j in range(a,401):
        for k in range(b,401):
            dp[i+1][j][k]=min(dp[i][j][k],dp[i][j-a][k-b]+c)
ans=10**4
for j in range(1,401):
    for k in range(1,401):
        if ma*k==mb*j:
            ans=min(ans,dp[n][j][k])
if ans==10**4:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
n,ma,mb=map(int,input().split())
dp=[[[10**4 for _ in range(401)] for __ in range(401)] for ___ in range(n+1)]
for i in range(n+1):
    dp[i][0][0]=0
for i in range(n):
    a,b,c=map(int,input().split())
    for j in range(a,401):
        for k in range(b,401):
            dp[i+1][j][k]=min(dp[i][j][k],dp[i][j-a][k-b]+c)
ans=float("INF")
for j in range(1,401):
    for k in range(1,401):
        if ma*k==mb*j:
            ans=min(ans,dp[n][j][k])
if ans==10**4:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
n,ma,mb=map(int,input().split())
dp=[[[float("INF") for _ in range(401)] for __ in range(401)] for ___ in range(n+1)]
for i in range(n+1):
    dp[i][0][0]=0
for i in range(n):
    a,b,c=map(int,input().split())
    for j in range(a,401):
        for k in range(b,401):
            dp[i+1][j][k]=min(dp[i][j][k],dp[i][j-a][k-b]+c)
ans=float("INF")
for j in range(1,401):
    for k in range(1,401):
        if ma*k==mb*j:
            ans=min(ans,dp[n][j][k])
if ans==float("INF"):
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
n, ma, mb = (int(x) for x in input().split())
ABC = [tuple(int(x) for x in input().split()) for _ in range(n)]
m = n // 2
D = dict()
E = dict()
for bit in range(1, 1<<m):
    sum_a = 0
    sum_b = 0
    sum_c = 0
    for i in range(m):
        if bit & 1<<i:
            a, b, c = ABC[i]
            sum_a += a
            sum_b += b
            sum_c += c
    s = ma * sum_a - mb * sum_b
    if s in D:
        D[s] = min(D[s], sum_c)
    else:
        D[s] = sum_c

k = n - m
for bit in range(1, 1<<k):
    sum_a = 0
    sum_b = 0
    sum_c = 0
    for i in range(k):
        if bit & 1<<i:
            a, b, c = ABC[m + i]
            sum_a += a
            sum_b += b
            sum_c += c
    s = ma * sum_a - mb * sum_b
    if s in E:
        E[s] = min(E[s], sum_c)
    else:
        E[s] = sum_c

ans = 10**18
if 0 in D:
    ans = min(ans, D[0])
if 0 in E:
    ans = min(ans, E[0])
for s, c in D.items():
    if -s in E:
        ans = min(ans, D[s] + E[-s])
if ans == 10**18:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

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

wmax=400
inf=10**10
dp=[[[inf]*(wmax+1) for _ in range(wmax+1)] for _ in range(n+1)]
dp[0][0][0]=0

for i in range(n):
    a,b,c=abc[i]
    for j in range(wmax+1):
        for k in range(wmax+1):
            dp[i+1][j][k]=dp[i][j][k]

            if j>=a:
                dp[i+1][j][k]=min(dp[i+1][j][k],dp[i][j-a][k]+c)
            if k>=b:
                dp[i+1][j][k]=min(dp[i+1][j][k],dp[i][j][k-b]+c)

ans=inf

for j in range(1,wmax+1):
    for k in range(1,wmax+1):
        if dp[n][j][k]>=inf:
            continue

        if ma*k==mb*j:
            ans=min(ans,dp[n][j][k])

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

Submission status: Wrong Answer

Solution code:
import sys
from itertools import product
from collections import defaultdict

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, Ma, Mb = map(int, input().split())
    ABC = [list(map(int, input().split())) for _ in range(n)]
    s = (n + 1) // 2
    t = n - s

    S = defaultdict(int)
    for pattern in product([0, 1], repeat=s):
        a = b = c = 0
        for idx, p in enumerate(pattern):
            if p == 1:
                a += ABC[idx][0]
                b += ABC[idx][1]
                c += ABC[idx][2]
        S[(a, b)] = min(S.get((a, b), f_inf), c)

    T = defaultdict(int)
    for pattern in product([0, 1], repeat=t):
        a = b = c = 0
        for idx, p in enumerate(pattern):
            if p == 1:
                a += ABC[s + idx][0]
                b += ABC[s + idx][1]
                c += ABC[s + idx][2]
        T[(a, b)] = min(T.get((a, b), f_inf), c)

    MAX_A = sum([a for a, _, _ in ABC])
    MAX_B = sum([b for _, b, _ in ABC])

    res = f_inf
    for k, v in S.items():
        if k == (0, 0):
            continue
        a, b = k
        x, y = Ma, Mb
        while x <= MAX_A and y <= MAX_B:
            if T.get((x - a, y - b), -1) != -1:
                cost = v + T[(x - a, y - b)]
                res = min(res, cost)
            x += Ma
            y += Mb
    print(res if res != f_inf else -1)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = map(int, input().split())
Med = [tuple(map(int, input().split())) for _ in range(N)]
N *= 100
import numpy as np
dp = np.full((N+1,N+1),np.inf) #dp[i,j]:Aがiグラム、Bがjグラムの時の最小予算
dp[0,0] = 0
from itertools import product
R = lambda x:reversed(range(x,N+1))
for a,b,c in Med:
    for i,j in product(R(a),R(b)):
        dp[i,j] = min(dp[i,j], dp[i-a,j-b]+c)
        
ans = np.inf
for n in range(1,min(N//Ma, N//Mb)):
    ans = min(ans, dp[n*Ma, n*Mb])
print(ans if ans != np.inf else -1)

Submission status: Wrong Answer

Solution code:
import sys
from math import gcd
def input(): return sys.stdin.readline().strip()
sys.setrecursionlimit(10**9)

def main():
    N, Ma, Mb = map(int, input().split())
    """
    dp[i][j] = (0~iまでの薬品を使って、物質Bがjグラム余るようにするときの最小金額)
    jは整数になるとは限らないのでj=p/qと既約分数表示して(p, q)を辞書のキーとして保持する。
    """
    dp = {}
    a, b, c = map(int, input().split())
    p, q = b * Ma - a * Mb, Ma
    div = gcd(a, b)
    p, q = p // div, q // div
    dp[(p, q)] = c
    #print("i=0=>dp={}".format(dp))

    for i in range(1, N):
        dp2 = {}
        a, b, c = map(int, input().split())
        p, q = b * Ma - a * Mb, Ma
        div = gcd(a, b)
        p, q = p // div, q // div
        dp2[(p, q)] = c
        for p, q in dp:
            cost = dp[(p, q)]
            if (p, q) in dp2:
                dp2[(p, q)] = min(dp2[(p, q)], cost)
            else:
                dp2[(p, q)] = cost
            np, nq = p * Ma + b * q * Ma - a * q * Mb, q * Ma
            if np == 0:
                np, nq = 0, 1
            else:
                div = gcd(np, nq)
                np, nq = np // div, nq // div
            if (np, nq) in dp2:
                dp2[(np, nq)] = min(dp2[(np, nq)], cost + c)
            else:
                dp2[(np, nq)] = cost + c
        dp = dp2
        #print("i={}=>dp={}".format(i, dp))
    if (0, 1) in dp:
        print(dp[(0, 1)])
    else:
        print(-1)



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from math import gcd
def input(): return sys.stdin.readline().strip()
sys.setrecursionlimit(10**9)

def main():
    N, Ma, Mb = map(int, input().split())
    """
    dp[i][j] = (0~iまでの薬品を使って、物質Bがjグラム余るようにするときの最小金額)
    jは整数になるとは限らないのでj=p/qと既約分数表示して(p, q)を辞書のキーとして保持する。
    """
    dp = {}
    a, b, c = map(int, input().split())
    p, q = b * Ma - a * Mb, Ma
    div = gcd(a, b)
    p, q = p // div, q // div
    dp[(p, q)] = c
    #print("i=0=>dp={}".format(dp))

    for i in range(1, N):
        dp2 = {}
        a, b, c = map(int, input().split())
        p, q = b * Ma - a * Mb, Ma
        div = gcd(a, b)
        p, q = p // div, q // div
        dp[(p, q)] = c
        for p, q in dp:
            cost = dp[(p, q)]
            if (p, q) in dp2:
                dp2[(p, q)] = min(dp2[(p, q)], cost)
            else:
                dp2[(p, q)] = cost
            np, nq = p * Ma + b * q * Ma - a * q * Mb, q * Ma
            if np == 0:
                np, nq = 0, 1
            else:
                div = gcd(np, nq)
                np, nq = np // div, nq // div
            if (np, nq) in dp2:
                dp2[(np, nq)] = min(dp2[(np, nq)], cost + c)
            else:
                dp2[(np, nq)] = cost + c
        dp = dp2
        #print("i={}=>dp={}".format(i, dp))
    if (0, 1) in dp:
        print(dp[(0, 1)])
    else:
        print(-1)



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from math import gcd
def input(): return sys.stdin.readline().strip()
sys.setrecursionlimit(10**9)

def main():
    N, Ma, Mb = map(int, input().split())
    """
    dp[i][j] = (0~iまでの薬品を使って、物質Bがjグラム余るようにするときの最小金額)
    jは整数になるとは限らないのでj=p/qと既約分数表示して(p, q)を辞書のキーとして保持する。
    """
    dp = {}
    a, b, c = map(int, input().split())
    p, q = b * Ma - a * Mb, Ma
    div = gcd(a, b)
    p, q = p // div, q // div
    dp[(p, q)] = c
    #print("i=0=>dp={}".format(dp))

    for i in range(1, N):
        dp2 = {}
        a, b, c = map(int, input().split())
        for p, q in dp:
            cost = dp[(p, q)]
            if (p, q) in dp2:
                dp2[(p, q)] = min(dp2[(p, q)], cost)
            else:
                dp2[(p, q)] = cost
            np, nq = p * Ma + b * q * Ma - a * q * Mb, q * Ma
            if np == 0:
                np, nq = 0, 1
            else:
                div = gcd(np, nq)
                np, nq = np // div, nq // div
            if (np, nq) in dp2:
                dp2[(np, nq)] = min(dp2[(np, nq)], cost + c)
            else:
                dp2[(np, nq)] = cost + c
        dp = dp2
        #print("i={}=>dp={}".format(i, dp))
    if (0, 1) in dp:
        print(dp[(0, 1)])
    else:
        print(-1)



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
def input():
    return stdin.readline().strip()

n, ma, mb = map(int, input().split())
l = []
for _ in range(n):
    i, j, k = map(int, input().split())
    # 順番注意
    l.append([k, i, j])

l.sort()

heap = l.copy()
for i in range(n):
    heap[i].append(i)

import heapq
heapq.heapify(heap)

for _ in range(10 ** 6):
    k, i, j, index = heapq.heappop(heap)
    if i * mb == j * ma:
        print(k)
        exit()

    for a in range(index, n):
        heapq.heappush(heap, [k + l[a][0], i + l[a][1], j + l[a][2], a])

print(-1)

Submission status: Wrong Answer

Solution code:
import sys
readlines = sys.stdin.buffer.readlines
input = sys.stdin.buffer.readline
N, Ma, Mb = map(int, input().split())
X = [map(int, line.split()) for line in readlines()]
X = [(a*Mb-b*Ma, c) for a, b, c in X]

INF = 1 << 30
MAX = 10000
MID = MAX//2
dp = [INF]*(MAX+1)
ans = INF
for i, (x, c) in enumerate(X):
    if x == 0:
        ans = min(ans, c)
        continue

    dp[MID+x] = min(dp[MID+x], c)
    if x > 0:
        for j in range(MAX, x-1, -1):
            dp[j] = min(dp[j], dp[j-x]+c)
    else:
        for j in range(MAX+x):
            dp[j] = min(dp[j], dp[j-x]+c)

ans = min(ans, dp[MID])
print(ans if ans < INF else -1)

Submission status: Wrong Answer

Solution code:
import sys
readlines = sys.stdin.buffer.readlines
input = sys.stdin.buffer.readline
N, Ma, Mb = map(int, input().split())
X = [tuple(map(int, line.split())) for line in readlines()]

INF = 1 << 30
x = [[INF]*201 for _ in [0]*201]
y = [[INF]*201 for _ in [0]*201]

n = N//2
X, Y = X[:n], X[n:]
for bit in range(1 << n):
    totA = totB = cost = 0
    for i in range(n):
        if bit >> i & 1:
            a, b, c = X[i]
            totA += a
            totB += b
            cost += c

    x[totA][totB] = min(x[totA][totB], cost)

for bit in range(1 << (N-n)):
    totA = totB = cost = 0
    for i in range(N-n):
        if bit >> i & 1:
            a, b, c = Y[i]
            totA += a
            totB += b
            cost += c

    y[totA][totB] = min(y[totA][totB], cost)

ans = INF
M = 405//max(Ma, Mb)
for i, t in enumerate(x):
    for j, s in enumerate(t):
        if s == INF:
            continue
        for k in range(1, M+1):
            I = k*Ma-i
            J = k*Mb-j
            if 0 < I <= 200 and 0 < J <= 200:
                ans = min(ans, s + y[I][J])

print(ans if ans < INF else -1)

Submission status: Wrong Answer

Solution code:
import collections
import copy
INF = 10000

#入力
N, Ma, Mb = map(int, input().split())
medichine = [tuple(map(int, input().split())) for _ in range(N)]

dp = {}
#dp初期化
dp = collections.defaultdict(lambda:INF)
dp[0,0] = 0

for a, b, c in medichine:
    dp_ = copy.copy(dp)
    for ca,cb in dp.keys():
        dp_[ca+a, cb+b] = min(dp_[ca+a, cb+b], dp_[ca,cb]+c)
    dp = dp_    

ans = INF
for ca,cb in dp.keys():
    if ca == cb == 0: continue
    if ca*Mb == cb*Ma:
        ans = min(ans, dp[ca,cb])
        
print(-1 if ans == INF else ans)

Submission status: Wrong Answer

Solution code:
import collections
import copy
INF = 10000

#入力
N, Ma, Mb = map(int, input().split())
medichine = [tuple(map(int, input().split())) for _ in range(N)]

dp = {}
#dp初期化
dp = collections.defaultdict(lambda:INF)
dp[0,0] = 0

for a, b, c in medichine:
    for ca,cb in dp.keys():
        dp_ = copy.copy(dp)
        dp_[ca+a, cb+b] = min(dp_[ca+a, cb+b], dp_[ca,cb]+c)
    dp = dp_    

ans = INF
for ca,cb in dp.keys():
    if ca == cb == 0: continue
    if ca*Mb == cb*Ma:
        ans = min(ans, dp[ca,cb])
        
print(-1 if ans == INF else ans)

Submission status: Wrong Answer

Solution code:
import math
import collections
nmax = 40
abmax = 10
INF = 10000

#入力
N, Ma, Mb = map(int, input().split())
medichine = [tuple(map(int, input().split())) for _ in range(N)]

dp = {}
#dp初期化
dp = collections.defaultdict(lambda:INF)
dp[0,0] = 0

for a, b, c in medichine:
    for ca,cb in dp.keys():
        dp_ = dp.copy()
        dp_[ca+a, cb+b] = min(dp_[ca+a, cb+b], dp_[ca,cb]+c)
    dp = dp_    

ans = INF
for ca,cb in dp.keys():
    if ca == cb == 0: continue
    if ca*Mb == cb*Ma:
        ans = min(ans, dp[ca,cb])
        
print(-1 if ans == INF else ans)

Submission status: Wrong Answer

Solution code:
import math
nmax = 40
abmax = 10
INF = 10000

#入力
N, Ma, Mb = map(int, input().split())
medichine = []
for i in range(N):
    Lst = [int(x) for x in input().split()]
    medichine.append(Lst)

dp = {}
#dp初期化
for i in range(N+1):
    for ca in range(nmax*abmax+1):
        for cb in range(nmax*abmax+1):
            dp[i,ca,cb] = INF
dp[0,0,0] = 0

for i in range(N):
    for ca in range(nmax*abmax+1):
        for cb in range(nmax*abmax+1):
            if dp[i,ca,cb] != INF:
                dp[i+1,ca,cb] = min(dp[i+1,ca,cb],dp[i,ca,cb])
                dp[i+1,ca+medichine[i][0],cb+medichine[i][1]] = min(dp[i+1,ca+medichine[i][0],cb+medichine[i][1]], dp[i,ca,cb]+medichine[i][2])

ans = INF
for ca in range(nmax*abmax+1):
        for cb in range(nmax*abmax+1):
            if ca*Mb == cb*Ma:
                ans = min(ans,dp[N,ca,cb])

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

Submission status: Wrong Answer

Solution code:
#bit全探索する
INF = 5000

def dp(n, medichine, Ma, Mb):
    INF =  5000
    if n == 0: #終端
        List = [[0, 0, 0], medichine[n]]
        if medichine[n][0] * Mb == medichine[n][1] * Ma:
            cost = medichine[n][2]
        else:
            cost = INF
    else: #n-1をもとにnについて計算
        previous = dp(n-1, medichine, Ma, Mb)
        List = previous[0].copy()
        cost = previous[1]
        if medichine[n][2] > cost:
            for x in previous[0]:
                C = x[2] + medichine[n][2]
                if C > cost:
                    continue
                A = x[0] + medichine[n][0]
                B = x[1] + medichine[n][1]
                List.append([A, B, C])
                if A * Mb == B * Ma:
                    cost = C         
    return List, cost



N, Ma, Mb = map(int, input().split())
medichine = []
for i in range(N):
    Lst = [int(x) for x in input().split()]
    medichine.append(Lst)
ans = dp(N-1, medichine, Ma, Mb)
cost = ans[1]
if cost != INF:
    print(cost)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import numpy as np
n, ma, mb = map(int, input().split())

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

dp = np.full((2, 444, 444), float('inf'))
dp[0][0][0] = 0

for i in range(n):
    for j in range(401):
        for k in range(401):
            a, b, c = item[i]
            dp[(i + 1) % 2][j + a][k + b] = min(dp[(i + 1) %
                                                   2][j + a][k + b], dp[i % 2][j][k] + c)
            dp[(i + 1) % 2][j][k] = min(dp[(i + 1) % 2][j][k], dp[i % 2][j][k])

ans = float('inf')
for i in range(1, 401):
    for j in range(1, 401):  # i : j == ma : mb
        if ma * j == i * mb:
            ans = min(ans, dp[n & 1][i][j])
print(ans if ans != float('inf') else -1)

Submission status: Wrong Answer

Solution code:
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
INF = 10**10
def main(N, Ma, Mb, ABC):
    MX = 10 * N
    dp = [[[INF] * (MX + 1) for _ in range(MX + 1)] for __ in range(N + 1)]
    dp[0][0][0] = 0
    for i in range(N):
        ni = i + 1
        ma, mb, mc = ABC[i]
        for a in range(MX + 1 - ma):
            na = a + ma
            for b in range(MX + 1 - mb):
                nb = b + mb
                dp[ni][na][nb] = min(dp[i][na][nb], dp[i][a][b] + mc)

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

if __name__ == '__main__':
    input = sys.stdin.readline
    N, Ma, Mb = map(int, input().split())
    ABC = [tuple(map(int, input().split())) for _ in range(N)]
    main(N, Ma, Mb, ABC)

Submission status: Wrong Answer

Solution code:
def main():
    N, MA, MB = list(map(int, input().split()))
    chemicals = [list(map(int, input().split())) for _ in range(N)]
    INF_COST = 1 + sum([chemical[2] for chemical in chemicals])
    MAX_A, MAX_B = 400, 400
    # dp[a][b]: 物質Aをaグラム、物質Bをbグラム入手するのに必要な最小コスト
    #           状態数は400 * 400 = 160000
    #           薬品の個数は40個
    #           → 更新ステップ数は 160000 * 40 = 6400000 = 6.4 * (10**6)
    dp = [[INF_COST for _ in range(1 + MAX_B)] for _ in range(1 + MAX_A)]
    dp[0][0] = 0
    for chemical in chemicals:
        da, db, dc = chemical
        for a in range(1 + MAX_A):
            if a + da > MAX_A:
                continue
            for b in range(1 + MAX_B):
                if b + db > MAX_B:
                    continue
                dp[a + da][b + db] = min(dp[a + da][b + db], dc + dp[a][b])
    ans = INF_COST
    scale = 1
    while scale * MA <= MAX_A and scale * MB <= MAX_B:
        ans = min(ans, dp[scale * MA][scale * MB])
        scale += 1
    if ans == INF_COST:
        print(-1)
    else:
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import itertools

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

res=list(itertools.combinations(array,2))
for I in res:
    if (I[0][0]+I[1][0])/(I[0][1]+I[1][1]) == Ma/Mb: print(I[0][2]+I[1][2]);sys.exit()
print(-1)

Submission status: Wrong Answer

Solution code:
n,ma,mb=map(int,input().split())
INF=10**18
dp=[[[INF for _ in range(401)] for _ in range(401)] for _ in range(n+1)]
ce=[[] for _ in range(n)]
for i in range(n):
  a,b,c=map(int,input().split())
  dp[i][a][b]=c
  ce[i]=(a,b,c)
dp[0][0][0]=0
for k in range(n):
  for i in range(400):
    for j in range(400):
      a,b,c=ce[k]
      dp[k+1][i][j]=min(dp[k+1][i][j],dp[k][i][j])
      if i+a<401 and j+b<401:
        dp[k+1][i+a][j+b]=min(dp[k+1][i+a][j+b],dp[k][i][j]+c)
ans=INF
for i in range(1,401):
  for j in range(1,401):
    if i*mb==j*ma:ans=min(ans,dp[n][i][j])
if ans==INF:ans=-1
print(ans)

Submission status: Wrong Answer

Solution code:
n,ma,mb=map(int,input().split())
INF=10**18
dp=[[[INF for _ in range(401)] for _ in range(401)] for _ in range(n+1)]
ce=[[] for _ in range(n)]
for i in range(n):
  a,b,c=map(int,input().split())
  dp[i][a][b]=c
  ce[i]=(a,b,c)
dp[0][0][0]=0
for k in range(n):
  for i in range(400):
    for j in range(400):
      a,b,c=ce[k]
      dp[k+1][i][j]=min(dp[k+1][i][j],dp[k][i][j])
      if i>=a and j>=b:
        dp[k+1][i][j]=min(dp[k+1][i][j],dp[k][i-a][j-b]+c)
ans=INF
for i in range(1,min(401//ma,401//mb)):
  ans=min(ans,dp[n][ma*i][mb*i])
if ans==INF:ans=-1
print(ans)

Submission status: Wrong Answer

Solution code:
n,ma,mb=map(int,input().split())
INF=10**18
dp=[[[INF for _ in range(401)] for _ in range(401)] for _ in range(n+1)]
ce=[[] for _ in range(n)]
for i in range(n):
  a,b,c=map(int,input().split())
  dp[i][a][b]=c
  ce[i]=(a,b,c)
dp[0][0][0]=0
for k in range(n):
  for i in range(400):
    for j in range(400):
      a,b,c=ce[k]
      if i>=a and j>=b:
        dp[k+1][i][j]=min(dp[k][i][j],dp[k][i-a][j-b]+c)
ans=INF
for i in range(1,min(401//ma,401//mb)):
  ans=min(ans,dp[n][ma*i][mb*i])
if ans==INF:ans=-1
print(ans)

Submission status: Wrong Answer

Solution code:
dp_n = 410
n, ma, mb = map(int, input().split())
dp = [[float('inf')]*dp_n for _ in range(dp_n)]
dp[0][0] = 0
for i in range(n):
    a, b, c = map(int, input().split())
    updated = [[False]*dp_n for _ in range(dp_n)]
    for j in range(dp_n):
        for i in range(dp_n):
            if dp[i][j] != float('inf'):
                try:
                    if not updated[i][j]:
                        dp[i+a][j+b] = min(dp[i+a][j+b],dp[i][j]+c)
                        updated[i+a][j+b] = True
                except:
                    pass
res = float('inf')
for j in range(1,dp_n):
    for i in range(1,dp_n):
        if i*mb == j*ma:
            res = min(res,dp[i][j])
print(res if res != float('inf') else -1)

Submission status: Wrong Answer

Solution code:
dp_n = 410
n, ma, mb = map(int, input().split())
dp = [[float('inf')]*dp_n for _ in range(dp_n)]
dp[0][0] = 0
for i in range(n):
    a, b, c = map(int, input().split())
    updated = [[False]*dp_n for _ in range(dp_n)]
    for j in range(dp_n):
        for i in range(dp_n):
            if dp[i][j] != float('inf'):
                try:
                    if not updated[i][j]:
                        dp[i+a][j+b] = min(dp[i+a][j+b],dp[i][j]+c)
                        updated[i+a][j+b] = True
                except:
                    pass
res = float('inf')
for j in range(1,dp_n):
    for i in range(1,dp_n):
        if i/ma*mb == j or mb/j*i == ma or ma/mb == i/j or mb/ma == j/i:
            res = min(res,dp[i][j])
print(res if res != float('inf') else -1)

Submission status: Wrong Answer

Solution code:
dp_n = 410
n, ma, mb = map(int, input().split())
dp = [[float('inf')]*dp_n for _ in range(dp_n)]
dp[0][0] = 0
for i in range(n):
    a, b, c = map(int, input().split())
    updated = [[False]*dp_n for _ in range(dp_n)]
    for j in range(dp_n):
        for i in range(dp_n):
            if dp[i][j] != float('inf'):
                try:
                    if not updated[i][j]:
                        dp[i+a][j+b] = min(dp[i+a][j+b],dp[i][j]+c)
                        updated[i+a][j+b] = True
                except:
                    pass
res = float('inf')
for j in range(1,dp_n):
    for i in range(1,dp_n):
        if i/ma*mb == j or mb/j*i == ma or ma/mb == i/j:
            res = min(res,dp[i][j])
print(res if res != float('inf') else -1)

Submission status: Wrong Answer

Solution code:
dp_n = 10
n, ma, mb = map(int, input().split())
dp = [[float('inf')]*dp_n for _ in range(dp_n)]
dp[0][0] = 0
for i in range(n):
    a, b, c = map(int, input().split())
    updated = [[False]*dp_n for _ in range(dp_n)]
    for j in range(dp_n):
        for i in range(dp_n):
            if dp[i][j] != float('inf'):
                try:
                    if not updated[i][j]:
                        dp[i+a][j+b] = min(dp[i+a][j+b],dp[i][j]+c)
                        updated[i+a][j+b] = True
                except:
                    pass
res = float('inf')
for j in range(1,dp_n):
    for i in range(1,dp_n):
        if i/ma*mb == j or mb/j*i == ma:
            res = min(res,dp[i][j])
print(res if res != float('inf') else -1)

Submission status: Wrong Answer

Solution code:
dp_n = 10
n, ma, mb = map(int, input().split())
dp = [[float('inf')]*dp_n for _ in range(dp_n)]
dp[0][0] = 0
for i in range(n):
    a, b, c = map(int, input().split())
    updated = [[False]*dp_n for _ in range(dp_n)]
    for j in range(dp_n):
        for i in range(dp_n):
            if dp[i][j] != float('inf'):
                try:
                    if not updated[i][j]:
                        dp[i+a][j+b] = min(dp[i+a][j+b],dp[i][j]+c)
                        updated[i+a][j+b] = True
                except:
                    pass
res = float('inf')
for j in range(1,dp_n):
    for i in range(1,dp_n):
        if i/ma*mb == j or mb/j*i == ma or ma/mb == i/j:
            res = min(res,dp[i][j])
print(res if res != float('inf') else -1)