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:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from fractions import gcd

def readln():
    _res = list(map(int,str(input()).split(' ')))
    return _res

def dp(a):
    res = [100000 for i in range(0,3000)]
    res[0] = 0
    for e in a:
        for i in range(e[0],3000):
            res[i] = min(res[i], e[1] + res[i - e[0]])
    return res

a = readln()
n,x,y = a[0],a[1],a[2]
up = []
down = []
ans = 100000
for i in range(0,n):
    a = readln()
    s = [a[0]*y-a[1]*x,a[2]]
    if s[0] > 0:
        up.append(s)
    elif s[0] < 0:
        down.append(s)
    else:
        ans = min(ans,s[1])
down = list(map(lambda x: [-x[0],x[1]],down))
fup = dp(up)
fdown = dp(down)
for i in range(1,3000):
    ans = min(ans,fup[i]+fdown[i])
if ans < 100000 : print(ans)
else : print('-1')

Submission status: Wrong Answer

Solution code:
N, ma, mb = map(int, raw_input().split())
A = [map(int, raw_input().split()) for _ in xrange(N)]

if N == 1:
    if ma * A[0][0] == mb * A[0][1]:
        print A[0][2]
    else:
        print -1
    exit()

n = N / 2
m = N / 2 + N % 2

B = [[0, 0, 0] for _ in xrange(2**n)]
C = [[0, 0, 0] for _ in xrange(2**m)]

for i in xrange(2**n):
    for j in xrange(n):
        if i & (1 << j):
            B[i][0] += A[j][0]
            B[i][1] += A[j][1]
            B[i][2] += A[j][2]
for i in xrange(2**m):
    for j in xrange(m):
        if i & (1 << j):
            C[i][0] += A[n+j][0]
            C[i][1] += A[n+j][1]
            C[i][2] += A[n+j][2]

d = {}
for a, b, c in C:
    x = ma * b - mb * a
    if x in d:
        d[x] = min(d[x], c)
    else:
        d[x] = c

ans = float('inf')
for a, b, c in B:
    x = mb * a - ma * b
    if x in d and c + d[x] > 0:
        ans = min(ans, c + d[x])
print ans if ans != float('inf') else -1

Submission status: Time Limit Exceeded

Solution code:
n,ma,mb=map(int,input().split())
zen=[]
kou=[]
A=[ list(map(int,input().split())) for i in range(n//2)];a=len(A)
B=[ list(map(int,input().split())) for i in range(n-n//2)];b=len(B)
for bit in range(1<<a):
    now=[0,0,0]
    for i in range(a):
        if (bit>>i)&1:
            now[0]+=A[i][0]
            now[1]+=A[i][1]
            now[2]+=A[i][2]
    zen.append(now)
for bit in range(1<<b):
    now=[0,0,0]
    for i in range(b):
        if (bit>>i)&1:
            now[0]+=B[i][0]
            now[1]+=B[i][1]
            now[2]+=B[i][2]
    kou.append(now)

ans=10**10
for li in zen:
    for lj in kou:
        if li[0]+lj[0]==0 :continue
        if (li[0]+lj[0])*mb==ma*(li[1]+lj[1]):
            ans=min(ans,li[2]+lj[2])
print(ans if ans!=10**10 else -1)

Submission status: Time Limit Exceeded

Solution code:
# dp[i][j] := Aをiグラム、Bをjグラムちょうど買うための最小予算。
# i <= N * a_i <= 40*10=400
# j <= N * b_i <= 40*10=400
# ……だとダメだわ。マスの遷移が許されるかどうかの情報が入ってない。
# （ある薬品を2回使うことはできないから、薬品を買ったか否かの情報を入れなきゃいけない）
# dp[k][i][j] := k番目までの薬品を使って、Aをiグラム、Bをjグラムちょうど買うための最小予算。
# 40*400*400で、各マスの計算は2個のマスを参照すれば求まるから、
# 40*400*400*2 = 12800000 = 1280万 間に合う。

n, a_ratio, b_ratio = list(map(int, input().split()))
chemicals = [list(map(int, input().split())) for _ in range(n)]

inf = 10 ** 10
ab_max = 10
dp = [[[inf for j in range(n * ab_max + 1)] for i in range(n * ab_max + 1)] for k in range(n)]

dp[0][0][0] = 0
dp[0][chemicals[0][0]][chemicals[0][1]] = chemicals[0][2]

for k in range(1, n):
    for i in range(n * ab_max + 1):
        for j in range(n * ab_max + 1):
            if i-chemicals[k][0] >= 0 and j-chemicals[k][1] >= 0:
                dp[k][i][j] = min(dp[k-1][i][j],  dp[k-1][i-chemicals[k][0]][j-chemicals[k][1]] + chemicals[k][2])
            else:
                dp[k][i][j] = dp[k-1][i][j]

ans = inf
bai = 1
while(a_ratio * bai <= n * ab_max and b_ratio * bai <= n * ab_max):
    ans = min(ans, dp[n-1][a_ratio * bai][b_ratio * bai])
    bai += 1

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

Submission status: Time Limit Exceeded

Solution code:
def dfs(i,ax,bx,kakaku):
    global ans
    if i == n:
        if ax * mb == bx * ma and kakaku != 0:
            ans.append(kakaku)
        return
    dfs(i+1,ax,bx,kakaku)
    dfs(i+1,ax+a[i],bx+b[i],kakaku+c[i])

n,ma,mb = map(int,input().split())
a,b,c = [],[],[]
for i in range(n):
    ai,bi,ci = map(int,input().split())
    a.append(ai)
    b.append(bi)
    c.append(ci)
#print(a,b,c)
ans = []
dfs(0,0,0,0)
if len(ans)==0:
    print(-1)
else:
    print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
N,Ma,Mb=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)
sa = sum(A)
sb = sum(B)

dp = [[[10000]*(sb+1) for _ in range(sa+1)] for _ in range(N+1)]
dp[0][0][0] = 0

ta = 0
tb = 0
for i in range(N):
  a = A[i]
  b = B[i]
  for j in range(sa):
    for k in range(sb):
      if j - a >= 0 and k - b >= 0:
        dp[i+1][j][k] = min(dp[i][j-a][k-b]+C[i], dp[i][j][k])
      else:
        dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k])

dnp = dp[N]
ans = 10000
for i in range(sa):
  for j in range(sb):
    if Ma * (j+1) == Mb * (i+1):
      ans = min(ans, dnp[i+1][j+1])
if ans == 10000:
  ans = -1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from numba import njit

@njit
def main(n,ma,mb,abc):
    inf=10**5
    cost=inf*np.ones((401,401))
    maxa=np.sum(abc,axis=0)[0]
    maxb=np.sum(abc,axis=0)[1]

    left=6*n//8
    right=n
    cnt=right-left

    for i in range(2**cnt):
        ta=0
        tb=0
        tc=0
        for j in range(cnt):
            if (i>>j)&1==1:
                a,b,c=abc[left+j]
                ta+=a
                tb+=b
                tc+=c
        
        cost[ta,tb]=min(cost[ta,tb],tc)
    
    ans=inf

    for i in range(1,401):
        targeta=ma*i
        targetb=mb*i
        if targeta>maxa or targetb>maxb:
            break

        for j in range(2**left):
            ta=0
            tb=0
            tc=0
            for k in range(left):
                if (j>>k)&1==1:
                    a,b,c=abc[k]
                    ta+=a
                    tb+=b
                    tc+=c
        
            ua=targeta-ta
            ub=targetb-tb

            if 0<=ua<=400 and 0<=ub<=400 and cost[ua,ub]!=inf:
                ans=min(ans,tc+cost[ua,ub])
    
    if ans==inf:
        return -1
    else:
        return int(ans)

n,ma,mb=map(int,input().split())
abc=np.array([list(map(int,input().split())) for _ in range(n)])
print(main(n,ma,mb,abc))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from numba import njit

@njit
def main(n,ma,mb,abc):
    inf=10**5
    cost=inf*np.ones((401,401))
    maxa=np.sum(abc,axis=0)[0]
    maxb=np.sum(abc,axis=0)[1]

    left=4*n//8
    right=n
    cnt=right-left

    for i in range(2**cnt):
        ta=0
        tb=0
        tc=0
        for j in range(cnt):
            if (i>>j)&1==1:
                a,b,c=abc[left+j]
                ta+=a
                tb+=b
                tc+=c
        
        cost[ta,tb]=min(cost[ta,tb],tc)
    
    ans=inf

    for i in range(1,401):
        targeta=ma*i
        targetb=mb*i
        if targeta>maxa or targetb>maxb:
            break

        for j in range(2**left):
            ta=0
            tb=0
            tc=0
            for k in range(left):
                if (j>>k)&1==1:
                    a,b,c=abc[k]
                    ta+=a
                    tb+=b
                    tc+=c
        
            ua=targeta-ta
            ub=targetb-tb

            if 0<=ua<=400 and 0<=ub<=400 and cost[ua,ub]!=inf:
                ans=min(ans,tc+cost[ua,ub])
    
    if ans==inf:
        return -1
    else:
        return int(ans)

n,ma,mb=map(int,input().split())
abc=np.array([list(map(int,input().split())) for _ in range(n)])
print(main(n,ma,mb,abc))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from numba import njit

@njit
def main(n,ma,mb,abc):
    inf=10**5
    cost=inf*np.ones((401,401))
    

    left=5*n//8
    right=n
    cnt=right-left

    for i in range(2**cnt):
        ta=0
        tb=0
        tc=0
        for j in range(cnt):
            if (i>>j)&1==1:
                a,b,c=abc[left+j]
                ta+=a
                tb+=b
                tc+=c
        
        cost[ta,tb]=min(cost[ta,tb],tc)
    
    ans=inf

    for i in range(1,401):
        targeta=ma*i
        targetb=mb*i

        for j in range(2**left):
            ta=0
            tb=0
            tc=0
            for k in range(left):
                if (j>>k)&1==1:
                    a,b,c=abc[k]
                    ta+=a
                    tb+=b
                    tc+=c
        
            ua=targeta-ta
            ub=targetb-tb

            if 0<=ua<=400 and 0<=ub<=400 and cost[ua,ub]!=inf:
                ans=min(ans,tc+cost[ua,ub])
    
    if ans==inf:
        return -1
    else:
        return int(ans)

n,ma,mb=map(int,input().split())
abc=np.array([list(map(int,input().split())) for _ in range(n)])
print(main(n,ma,mb,abc))

Submission status: Time Limit Exceeded

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

ans = INF
for i,j in prd(R(1),R(1)):
    if i*Mb == j*Ma:
        ans = min(ans, dp[i][j])
print(ans if ans != INF else -1)

Submission status: Time Limit Exceeded

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

ans = INF
for i,j in prd(R(1),R(1)):
    if i*Mb == j*Ma:
        ans = min(ans, dp[i,j])
print(ans if ans != INF else -1)

Submission status: Time Limit Exceeded

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 i,j in product(R(1),R(1)):
    if i*Mb == j*Ma:
        ans = min(ans, dp[i,j])
print(int(ans) if ans != np.inf else -1)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from collections import defaultdict
from collections import deque
from itertools import combinations
import bisect

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def main():
    N, MA, MB = NMI()
    D = []
    for _ in range(N):
        a, b, c = NMI()
        t = a*MB - b*MA
        D.append((t, c))
    D.sort()

    D1 = D[:N//2]
    D2 = D[N//2:]
    ld1 = len(D1)
    ld2 = len(D2)

    S1 = defaultdict(lambda: 10**5)
    for case in range(2**ld1):
        if case == 0: continue
        s, c = 0, 0
        for i in range(ld1):
            if (case>>i)&1:
                s += D1[i][0]
                c += D1[i][1]
        S1[s] = min(S1[s], c)

    S2 = defaultdict(lambda: 10**5)
    for case in range(2 ** ld2):
        if case == 0: continue
        s, c = 0, 0
        for i in range(ld2):
            if (case >> i) & 1:
                s += D2[i][0]
                c += D2[i][1]
        S2[s] = min(S2[s], c)

    ans = 10**5
    for s, c in list(S1.items()):
        ans = min(ans, c + S2[-s])
    ans = min(ans, S1[0], S2[0])
    if ans >= 10**5:
        print(-1)
    else:
        print(ans)



if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import math

def f(i):
    if i == ns:
        suma, sumb, sumcost = 0, 0, 0
        for j in range(ns):
            if s[j] == 1:
                suma += x[j][0]
                sumb += x[j][1]
                sumcost += x[j][2]
        mincost[suma][sumb] = min(mincost[suma][sumb], sumcost)
        return
    s[i] = 1
    f(i + 1)
    s[i] = 0
    f(i + 1)

def g(i):
    if i == nt:
        global ans
        suma, sumb, sumcost = 0, 0, 0
        for j in range(nt):
            if t[j] == 1:
                suma += y[j][0]
                sumb += y[j][1]
                sumcost += y[j][2]
        k = 1
        while True:
            ss, tt = k * ma - suma, k * mb - sumb
            if 0 <= ss <= l and 0 <= tt <= l:
                ans = min(ans, mincost[ss][tt] + sumcost)
            elif l < ss or l < tt:
                break
            k += 1
        return
    t[i] = 1
    g(i + 1)
    t[i] = 0
    g(i + 1)

n, ma, mb = map(int, input().split())
ns, nt = int(math.ceil(n / 2)), n // 2
l = 10 * ns
mincost = [[114514] * (l + 5) for _ in range(l + 5)]
ans = 114514
x = [list(map(int, input().split())) for _ in range(ns)]
y = [list(map(int, input().split())) for _ in range(nt)]
s = [0] * ns
t = [0] * nt
f(0)
g(0)
print(ans if not ans == 114514 else -1)

Submission status: Time Limit Exceeded

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(1,nmax*abmax+1):
        for cb in range(1,nmax*abmax+1):
            if ca*Mb == cb*Ma:
                ans = min(ans,dp[N,ca,cb])

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

Submission status: Time Limit Exceeded

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

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


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

Submission status: Time Limit Exceeded

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: Time Limit Exceeded

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

def dp(n, medichine, Ma, Mb):
    INF =  5000
    if n == 0: #終端
        List = [[0, 0, 0], medichine[0]]
        if medichine[0][0] * Mb == medichine[0][1] * Ma:
            cost = medichine[0][2]
        else:
            cost = INF
    else: #n-1をもとにnについて計算
        previous = dp(n-1, medichine, Ma, Mb)
        List = previous[0].copy()
        cost = previous[1]
        for x in previous[0]:
            C = x[2] + medichine[n][2]
            if C < cost:
                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: Time Limit Exceeded

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

def dp(n, medichine, Ma, Mb):
    INF =  5000
    if n == 0: #終端
        List = [[0, 0, 0], medichine[0]]
        if medichine[n][0] * Mb == medichine[n][1] * Ma:
            cost = medichine[0][2]
        else:
            cost = INF
    else: #n-1をもとにnについて計算
        previous = dp(n-1, medichine, Ma, Mb)
        List = previous[0][:]
        cost = previous[1]
        for x in previous[0]:
            A = x[0] + medichine[n][0]
            B = x[1] + medichine[n][1]
            C = x[2] + medichine[n][2]
            if C < cost:
                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[i] = Lst
ans = dp(N-1, medichine, Ma, Mb)
cost = ans[1]
if cost != INF:
    print(cost)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

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



N, Ma, Mb = map(int, input().split())
medichine = []
for _ 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: Time Limit Exceeded

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

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



N, Ma, Mb = map(int, input().split())
medichine = []
for _ 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: Time Limit Exceeded

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

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



N, Ma, Mb = map(int, input().split())
medichine = []
for _ 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: Time Limit Exceeded

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

N, Ma, Mb = map(int, input().split())
a, b, c = [], [], []
for _ in range(N):
    Lst = [int(x) for x in input().split()]
    a.append(Lst[0])
    b.append(Lst[1])
    c.append(Lst[2])

cost = INF
for n in range(1,2**N):
    A, B, C = 0, 0, 0
    for i in range(N):
        if n & (1<<i):
            A += a[i]
            B += b[i]
            C += c[i]
            if C > cost:
                break
    if A * Mb == B * Ma:
        cost = min(cost, C)
if cost != INF:
    print(cost)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

N, Ma, Mb = map(int, input().split())
a, b, c = [], [], []
for _ in range(N):
    Lst = [int(x) for x in input().split()]
    a.append(Lst[0])
    b.append(Lst[1])
    c.append(Lst[2])

cost = INF
for n in range(1,2**N):
    A, B, C = 0, 0, 0
    for i in range(N):
        if n & (1<<i):
            A += a[i]
            B += b[i]
            C += c[i]
    if A * Mb == B * Ma:
        cost = min(cost, C)
if cost != INF:
    print(cost)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()


def main():
	n,ma,mb=MI()
	dp=[[10**6]*(n*10+15) for i in range(n*10+15)]
	for i in range(n+1):
		dp[0][0]=0

	for _ in range(n):
		a,b,c=MI()
		for aa in range(n*10+1,-1,-1):
			for bb in range(n*10+1,-1,-1):
				dp[a+aa][b+bb]=min(dp[aa][bb]+c,dp[a+aa][b+bb])

	ans=10**6

	for i in range(1,1000):
		try:
			ans=min(ans,dp[ma*i][mb*i])
		except:
			break

	print(ans if ans<10**6 else -1)





























if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

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

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

INF = 5000
dp = np.full((n + 1, 444, 444), INF, dtype=np.int)

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

ans = 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][i][j])
print(ans if ans != INF else -1)

Submission status: Time Limit Exceeded

Solution code:
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
def main(N, Ma, Mb, ABC):
    ans = 10**10
    for p in range(1, 1 << N):
        ta, tb, tc = 0, 0, 0
        for i in range(N):
            if p & (1 << i):
                a, b, c = ABC[i]
                ta += a
                tb += b
                tc += c
        if ta * Mb == tb * Ma:
            ans = min(ans, tc)
    if ans == 10**10:
        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: Time Limit Exceeded

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(MAX_A, -1, -1):
            if a + da > MAX_A:
                continue
            for b in range(MAX_B, -1, -1):
                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: Time Limit Exceeded

Solution code:
import sys
import itertools

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

budget = N * 100
for I in range(1,N+1):
    for K in list(itertools.combinations(array,I)):
        a = 0
        b = 0
        c = 0
        for L in K:
            a += L[0]
            b += L[1]
            c += L[2]
        if a/b == Ma/Mb and budget >= c: budget = c
print(budget) if budget != N * 100 else print(-1)

Submission status: Time Limit Exceeded

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

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

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

    for i in range(N):
        for j in range(400,-1,-1):# 後ろから更新すること
            for k in range(400,-1,-1):
                if j - a[i] >= 0 and k - b[i] >= 0:
                    dp[j][k] = min(dp[j][k], dp[j-a[i]][k-b[i]]+c[i])
    
    ans = INF
    for j in range(400):
        for k in range(400):
            if j*Mb == k*Ma != 0:
                ans = min(ans,dp[j][k])
    print(ans) if ans < INF else print(-1)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

inf = 10 ** 9
lst = [[inf for _ in range(401)] for _ in range(401)]
lst[0][0] = 0
for a, b, c in abc:
    for i in range(400, -1, -1):
        if i - a < 0:
            break
        for j in range(400, -1, -1):
            if j - b < 0:
                break
            lst[i][j] = min(lst[i][j], lst[i - a][j - b] + c)

ans = inf
a = ma
b = mb
while 1:
    try:
        ans = min(ans, lst[a][b])
        a += ma
        b += mb
    except IndexError:
        break
if ans == inf:
    print(-1)
else:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,ma,mb=map(int,input().split())
subs=[list(map(int,input().split())) for _ in range(n)]
infi=10**18
dp=[[[infi]*401 for _ in range(401)] for _ in range(n+1)]
dp[0][0][0]=0
for i in range(1,n+1):
    a,b,c=subs[i-1]
    for j in range(401):
        for k in range(401):
            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)
            dp[i][j][k]=min(dp[i][j][k],dp[i-1][j][k])
ans=infi
for i in range(1,401):
    for j in range(1,401):
        if i%ma==0 and j%mb==0:
            if i//ma==j//mb:
                ans=min(ans,dp[n][i][j])
if ans==infi:
    ans=-1
print(ans)

Submission status: Time Limit Exceeded

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

# dp[iまでの薬品を使用して][aの重量][bの重量]を達成する最小の予算
dp = [[[float('inf')]*401 for j in range(401)] for i in range(41)]
dp[0][0][0] = 0

for i in range(n):
    a, b, c = items[i]
    for anow in range(400):
        for bnow in range(400):
            # 使わない
            dp[i+1][anow][bnow] = min(dp[i+1][anow][bnow], dp[i][anow][bnow])
            # 使う
            if 0<=anow+a<401 and 0<=bnow+b<401:
                dp[i+1][anow+a][bnow+b] = min(dp[i+1][anow+a][bnow+b], dp[i][anow][bnow]+c)
                

ans = float('inf')
for a in range(1, 401):
    for b in range(1, 401):
        if ma*b==mb*a:
            ans = min(ans, dp[n][a][b])
if ans == float('inf'):
    print(-1)
else:
    print(ans)