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

Solution code:
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意

n,A,B = [int(i) for i in readline().split()]
abc = [[int(i) for i in readline().split()] for j in range(n)]


res1 = []
res2 = []
eq = 10**9


def dfs(s,g,amari,yen,List):
    global eq
    if s != g:
        a,b,c = abc[s]
        if a*B == A*b:
            eq = min(eq,c)
            dfs(s+1,g,amari,yen,List)
        else: 
            dfs(s+1,g,amari+a*B-b*A,yen+c,List)
            dfs(s+1,g,amari        ,yen,List)
    else:
        if amari != 0:
            List.append((amari,yen))
        else:
            if yen != 0:
                eq = min(eq,yen)
        

dfs(0,n//2,0,0,res1)
dfs(n//2,n,0,0,res2)

#res1.sort()
res2.sort()
#print(res1)
#print(res2)


from bisect import *

ans = min(10**9,eq)

L = len(res2)
for amari,yen in res1:
    i = bisect(res2,(-amari,-10000000))
    if i < L and res2[i][0] == -amari:
        if 0 < yen+res2[i][1] < ans:
            ans = yen+res2[i][1]


if ans == 10**9:
    print(-1)
else:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, Ma, Mb = map(int, input().split())
table = []
for i in range(N):
  a,b,c = map(int, input().split())
  table.append([a,b,c])
inf = 10**9
dp = [[[inf]*401 for i in range(401)] for i in range(N+1)]
dp[0][0][0] = 0

for i in range(1,N+1):
  a,b,c = table[i-1]
  for j in range(401):
    for k in range(401):
      dp[i][j][k] = dp[i-1][j][k]
      if 400>=j-a>=0 and 400>=k-b>=0:
        dp[i][j][k] = min(dp[i-1][j][k],dp[i-1][j-a][k-b]+c)
ans = 10**9
i = 1
while max(Ma,Mb)*i <= 400:
  ans = min(ans,dp[N][Ma*i][Mb*i])
  i += 1
if ans == 10**9:
  ans = -1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, Ma, Mb = map(int, input().split())
A = []
B = []
C = []
for _ in range(N):
    a, b, c = map(int, input().split())
    A.append(a)
    B.append(b)
    C.append(c)
M = 401
dp = [[[float('INF') for _ in range(M)]
       for _ in range(M)] for _ in range(N + 1)]
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] == float('INF'):
                continue
            dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k])
            dp[i + 1][j + A[i]][k + B[i]
                                ] = min(dp[i + 1][j + A[i]][k + B[i]], dp[i][j][k] + C[i])
ans = float('INF')
for j in range(1, M):
    for k in range(1, M):
        if j * Mb == k * Ma:
            ans = min(ans, dp[N][j][k])
if ans == float('INF'):
    print(-1)
else:
    print(ans)

Submission status: Time Limit Exceeded

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

memo=[[10**5 for _ in [0]*401] for _ in [0]*401]
memo[0][0]=0

for N in range(n):
    for i in range(390,-1,-1):
        for j in range(390,-1,-1):
            a,b,c=abc[N]
            memo[i+a][j+b]=min(memo[i+a][j+b],memo[i][j]+c)

m=10**4
for i in range(1,400):
    for j in range(1,400):
        if i*mb==ma*j:
            m=min(m,memo[i][j])
if m==10**4:
    print(-1)
else:
    print(m)

Submission status: Time Limit Exceeded

Solution code:
N,Ma,Mb=map(int,input().split())
abc = list(list(map(int,input().split()))for i in range(N))
DP = [[[10**9]*(10*N+1) for i in range(10*N+1)]for i in range(N+1)]
DP[0][0][0] = 0
for i in range(N):
    for j in range(10*N+1):
        for k in range(10*N+1):
            if j >= abc[i][0] and k >= abc[i][1]:
                DP[i+1][j][k] = min(DP[i][j][k],DP[i][j-abc[i][0]][k-abc[i][1]]+abc[i][2])
            else:
                DP[i+1][j][k] = DP[i][j][k]
ans = 10**9
for i in range(1,10*N//max(Ma,Mb)+1):
    ans = min(DP[-1][i*Ma][i*Mb],ans)
if ans >= 10**8:
    print(-1)
else:
    print(ans)

Submission status: Time Limit Exceeded

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

INF = 10 ** 5
nums = 401

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

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

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


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

if res == INF:
    res = -1

print(res)

Submission status: Time Limit Exceeded

Solution code:
N,Ma,Mb = map(int,input().split())
ABC = [list(map(int,input().split())) for _ in [0]*N]
INFTY = 10**8
dp = [[[INFTY]*10*(N+1) for _ in [0]*10*(N+1)] for _ in [0]*(N+1)]
dp[0][0][0] = 0
for i in range(1,N+1):
    a,b,c = ABC[i-1]
    for j in range(10*(N+1)):
        for k in range(10*(N+1)):
            dp[i][j][k] = dp[i-1][j][k]
            if j-a>=0 and k-b>=0:
                dp[i][j][k] = min(dp[i][j][k],dp[i-1][j-a][k-b]+c)
ans = min(dp[-1][Ma*k][Mb*k] for k in range(1,(10*(N+1)-1)//max(Ma,Mb)+1))
if ans == INFTY : ans = -1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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

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

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

dp[0][0][0] = 0
for i in range(N):
    for j in range(10*N+1):
        for k in range(10*N+1):
            if dp[i][j][k] == float('inf'):
                continue
            
            # 選ばなかった場合
            dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k])
            # 選んだ場合
            dp[i+1][j+a[i]][k+b[i]] = min(dp[i+1][j+a[i]][k+b[i]], dp[i][j][k] + c[i])


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

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

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

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

Submission status: Time Limit Exceeded

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

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

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

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

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

dp[0][0][0] = 0
for i in range(N):
    for j in range(10*N+1):
        for k in range(10*N+1):
            if dp[i][j][k] == float('inf'):
                continue
            
            # 選ばなかった場合
            dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k])
            # 選んだ場合
            dp[i+1][j+a[i]][k+b[i]] = min(dp[i+1][j+a[i]][k+b[i]], dp[i][j][k] + c[i])


ans = float('inf')
# 0スタートは何を選んでいない状態となり最小値が0になってしまうため1から開始する
for i in range(1,10 * N + 1):
    for j in range(1,10 * N + 1):
        if i * Mb != j * Ma:
            continue
        ans = min(ans, dp[N][i][j])

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

Submission status: Time Limit Exceeded

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

inf = 1000 * N
M = N * 10 + 1
DP = [[[inf] * M for _ in range(M)] for _ in range(N)]

a, b, c = abc[0]
DP[0][a][b] = c

for i in range(1, N):
  a, b, c = abc[i]
  DP[i][a][b] = min(DP[i - 1][a][b], c)
  for j in range(M - 10):
    for k in range(M - 10):
      DP[i][j][k] = min(DP[i][j][k], DP[i - 1][j][k])
      if DP[i - 1][j][k] < inf:
        jj = j + a
        kk = k + b
        DP[i][jj][kk] = min(DP[i - 1][jj][kk], DP[i - 1][j][k] + c)
ans = inf
i = MA
j = MB
while i < M and j < M:
  ans = min(ans, DP[-1][i][j])
  i += MA
  j += MB

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

Submission status: Time Limit Exceeded

Solution code:
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)

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

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

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

ans = float('inf')
for i in range(1 << N):
    tmp_a = 0
    tmp_b = 0
    tmp_c = 0
    for j in range(N):
        if ((i >> j) & 1) == 1:
            tmp_a += a[j]
            tmp_b += b[j]
            tmp_c += c[j]
    if tmp_a == 0 or tmp_b == 0:
        continue
    gcd_ab = gcd(tmp_a, tmp_b)
    if tmp_a / gcd_ab == Ma and tmp_b / gcd_ab == Mb:
        if ans > tmp_c:
            ans = tmp_c

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import copy


def main():
    N, Ma, Mb = map(int, input().split())
    a_arr = []
    b_arr = []
    c_arr = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        a_arr.append(a)
        b_arr.append(b)
        c_arr.append(c)
    assert len(a_arr) == len(b_arr) == len(c_arr) == N

    a = np.array(a_arr)
    b = np.array(b_arr)
    c = np.array(c_arr)

    INF = 1000000
    dp = np.full((N+1, np.sum(a)+1+10, np.sum(b)+1+10), INF, dtype=np.int)
    dp[0, 0, 0] = 0

    for i in range(N):
        for ca in range(np.sum(a) + 1):
            for cb in range(np.sum(b) + 1):
                if dp[i, ca, cb] == INF:
                    continue
                dp[i + 1, ca, cb] = min([dp[i + 1, ca, cb], dp[i, ca, cb]])
                dp[i + 1, ca + a[i], cb + b[i]
                   ] = min([dp[i + 1, ca + a[i], cb + b[i]],
                            dp[i, ca, cb] + c[i]])
    mask = (np.arange(dp.shape[1])[:, np.newaxis] *
            Mb) == (np.arange(dp.shape[2])[np.newaxis, :] * Ma)
    mask[0, :] = False
    mask[:, 0] = False
    ans = np.min(dp[:, mask])
    if ans == INF:
        print(-1)
    else:
        print(int(ans))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
from bisect import bisect_right
INF = float('inf')
N, Ma, Mb = map(int, input().split())
a, b, c = [None] * N, [None] * N, [None] * N
for i in range(N):
    a[i], b[i], c[i] = map(int, input().split())
hN = N // 2
dic1 = defaultdict(lambda : INF)
for i in range(1 << hN):
    _a, _b, _c = 0, 0, 0
    for j in range(hN):
        if (i >> j) & 1:
            _a += a[j]
            _b += b[j]
            _c += c[j]
    if _c < dic1[(_a, _b)]:
        dic1[(_a, _b)] = _c
dic2 = defaultdict(lambda : INF)
for i in range(1 << (N - hN)):
    _a, _b, _c = 0, 0, 0
    for j in range(N - hN):
        if (i >> j) & 1:
            _a += a[hN+j]
            _b += b[hN+j]
            _c += c[hN+j]
    if _c < dic2[(_a, _b)]:
        dic2[(_a, _b)] = _c
ans = INF
for k in dic1:
    _a, _b = k
    v = dic1[k]
    for i in range(1, N+1):
        ans = min(ans, v + dic2[(Ma*i-_a,Mb*i-_b)])
ans = ans if ans < INF else -1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, Ma, Mb = map(int, input().split())
A = [0]*N
B = [0]*N
C = [0]*N

for i in range(N):
    A[i], B[i], C[i] = map(int, input().split())

# print(N, Ma, Mb, A, B, C)

A = [Mb * a for a in A]
B = [Ma * b for b in B]
D = []
for i in range(N):
    D.append((A[i] - B[i], C[i]))
# print(N, Ma, Mb, A, B, C, D)

Dp = []
Dn = []
Dz = []
for d in D:
    if d[0] == 0:
        Dz.append(d)
    elif d[0] > 0:
        Dp.append(d)
    else:
        Dn.append(d)

Dp.sort(key=lambda x: x[0])
Dn.sort(key=lambda x: x[0])
Dz.sort(key=lambda x: x[1])
min_c = 100*41
if len(Dz) > 0:
    min_c = Dz[0][1]
# print(Dp, Dn, Dz, min_c)

Ds = Dn
Dl = Dp
# if len(Dp) < len(Dn):
#     Ds, Dl = Dp, Dn
# else:
#     Ds, Dl = Dn, Dp

if len(Ds) == 0 or len(Dl) == 0:
    if min_c == 100*41:
        print(-1)
    else:
        print(min_c)
    exit()

def calc(D, l):
    s = 0
    c = 0
    for i in range(len(D)):
        if (l >> i) & 1 != 0:
            s += D[i][0]
            c += D[i][1]
    return (s, c)

Ls = len(Ds)
Ll = len(Dl)
Ds_memo = []
Dl_memo = []

for ls in range(1, 2 << (Ls - 1)):
    Ds_memo.append(calc(Ds, ls))
for ll in range(1, 2 << (Ll - 1)):
    Dl_memo.append(calc(Dl, ll))

Ds_memo.sort(key=lambda x: x[1])
Dl_memo.sort(key=lambda x: x[0])

# print(Ds_memo, Dl_memo)

for ls in Ds_memo:
    s = ls[0]
    c = ls[1]
    # print('ls', ls, s, c)
    if c >= min_c:
        break
    for ll in Dl_memo:
        sl = ll[0]
        cl = ll[1]
        # print('ll', ll, sl, cl)
        if s + sl > 0:
            break
        if c + cl >= min_c:
            continue
        if s + sl == 0:
            min_c = c + cl

if min_c == 100*41:
    print(-1)
else:
    print(min_c)

Submission status: Time Limit Exceeded

Solution code:
N, Ma, Mb = map(int, input().split())
A = [0]*N
B = [0]*N
C = [0]*N

for i in range(N):
    A[i], B[i], C[i] = map(int, input().split())

# print(N, Ma, Mb, A, B, C)

A = [Mb * a for a in A]
B = [Ma * b for b in B]
D = []
for i in range(N):
    D.append((A[i] - B[i], C[i]))
# print(N, Ma, Mb, A, B, C, D)

Dp = []
Dn = []
Dz = []
for d in D:
    if d[0] == 0:
        Dz.append(d)
    elif d[0] > 0:
        Dp.append(d)
    else:
        Dn.append(d)

Dp.sort(key=lambda x: x[0])
Dn.sort(key=lambda x: x[0])
Dz.sort(key=lambda x: x[1])
min_c = 100*41
if len(Dz) > 0:
    min_c = Dz[0][1]
# print(Dp, Dn, Dz, min_c)

Ds = []
Dl = []
if len(Dp) < len(Dn):
    Ds, Dl = Dp, Dn
else:
    Ds, Dl = Dn, Dp

if len(Ds) == 0:
    if min_c == 100*41:
        print(-1)
    else:
        print(min_c)
    exit()

def calc(D, l):
    s = 0
    c = 0
    for i in range(len(D)):
        if (l >> i) & 1 != 0:
            s += D[i][0]
            c += D[i][1]
    return s, c

Ls = len(Ds)
Ll = len(Dl)
for ls in range(1 << (Ls - 1)):
    s, c = calc(Ds, ls + 1)
    # print('ls', ls, s, c)
    if c >= min_c:
        continue
    for ll in range(1 << (Ll - 1)):
        sl, cl = calc(Dl, ll + 1)
        # print('ll', ll, sl, cl)
        if c + cl >= min_c:
            continue
        if s + sl == 0:
            min_c = c + cl

if min_c == 100*41:
    print(-1)
else:
    print(min_c)

Submission status: Time Limit Exceeded

Solution code:
INF = float('inf')
N,Ma,Mb = map(int,input().split())
abc = [list(map(int,input().split())) for _ in range(N)]
dp = [[[INF]*(10*N+11) for _ in range(10*N+11)] for _ in range(N+1)]
dp[0][0][0] = 0

for i in range(N):
    for a1 in range(10*N+1):
        for b1 in range(10*N+1):
            a,b,c = abc[i]
            dp[i+1][a1][b1] = min(dp[i][a1][b1],dp[i][a1-a][b1-b]+c)

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

if ans == INF:
    ans = -1

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N,Ma,Mb=map(int,input().split())
a,b,c=map(list,zip(*[list(map(int,input().split())) for i in range(N)]))
ma,mb=sum(a),sum(b)
MAX=5000
dp=[[[MAX]*(mb+1) for i in range(ma+1)] for j in range(N+1)]
dp[0][0][0]=0
for i in range(N):
    for j in range(ma):
        for k in range(mb):
            dp[i+1][j][k]=min(dp[i][j][k],dp[i+1][j][k])
            if j+a[i]<=ma and k+b[i]<=mb:
                dp[i+1][j+a[i]][k+b[i]]=min(dp[i+1][j+a[i]][k+b[i]],dp[i][j][k]+c[i])
ans=MAX
x=1
while x*Ma<=ma and x*Mb<=mb:
    ans=min(ans,dp[N][x*Ma][x*Mb])
    x+=1
print(ans if ans<MAX else -1)

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    inf = float('inf')

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

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

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

    ans = inf

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

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

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from fractions import gcd

inf = float('inf')

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

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

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

ans = inf

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

abc = [list(map(int, input().split())) for i in range(N)]
cost = 4001


for i in range(1, 2**N):
  a, b = 0, 0
  tmp = 0
#  flag = 0
  for j in range(N):
    if (i>>j) & 1:
#      flag = 1
      tmp += abc[j][2]
      a += abc[j][0]
      b += abc[j][1]
  if a/Ma == b/Mb:
    cost = min(tmp, cost)
if cost <= 4000:
  print(cost)
else:
  print(-1)

Submission status: Time Limit Exceeded

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

abc = [list(map(int, input().split())) for i in range(N)]
tmp = []

for i in range(2**N):
  a, b = 0, 0
  cost = []
  for j in range(N):
    if (i>>j) & 1:
      cost.append(abc[j][2])
      a += abc[j][0]
      b += abc[j][1]
  if a/Ma == b/Mb and sum(cost) > 0:
    tmp.append(sum(cost))
try:
  print(min(tmp))
except ValueError:
  print(-1)

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
from collections import defaultdict
N,Ma,Mb=map(int,raw_input().split())
A=[];B=[];C=[]
for i in range(N):
    a,b,c=map(int,raw_input().split())
    A.append(a);B.append(b);C.append(c)

# dp[i][ma,mb]:=min(cost)
dp=[defaultdict(lambda:114514) for _ in range(41)]
dp[0][(0,0)]=0
dp[0][(A[0],B[0])]=C[0]

for i in range(1,N):
    for ma in range(401):
        for mb in range(401):
            dp[i][(ma,mb)] = min(dp[i-1][(ma,mb)], dp[i-1][(ma-A[i], mb-B[i])]+C[i],114514)

ans=114514
for ma in range(1,401):
    for mb in range(1,401):
        if ma*Mb!=mb*Ma:continue
        ans=min(ans, dp[N-1][ma,mb])
if ans>=114514: print(-1)
else: print(ans)

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
from collections import defaultdict
N,Ma,Mb=map(int,raw_input().split())
A=[];B=[];C=[]
for i in range(N):
    a,b,c=map(int,raw_input().split())
    A.append(a);B.append(b);C.append(c)

# dp[i][ma,mb]:=min(cost)
dp=[defaultdict(lambda:1145141919810) for _ in range(41)]
dp[0][(0,0)]=0
dp[0][(A[0],B[0])]=C[0]

for i in range(1,N):
    for ma in range(401):
        for mb in range(401):
            dp[i][(ma,mb)] = min(dp[i-1][(ma,mb)], dp[i-1][(ma-A[i], mb-B[i])]+C[i])
ans=1145141919810
for ma in range(1,401):
    for mb in range(1,401):
        if ma*Mb!=mb*Ma:continue
        ans=min(ans, dp[N-1][ma,mb])
if ans>=1145141919810: print(-1)
else: print(ans)

Submission status: Time Limit Exceeded

Solution code:

n,a,b = map(int,input().split())
l = [list(map(int,input().split())) for i in range(n)]
INF = 500
dp = [[[INF for i in range(411)] for j in range(411)] for k in range(n+1)]
dp[0][0][0] = 0
for i in range(n):
    for j in range(a*n+1):
        for k in range(b*n+1):
            if j - l[i][0]>=0 and k - l[i][1]>=0:
                dp[i+1][j][k] = min(dp[i][j][k],dp[i][j - l[i][0]][k - l[i][1]]+l[i][2])
            else:
                dp[i+1][j][k] = dp[i][j][k]
i =1
ans = INF
while i*max(a,b)<=400:
    ans = min(ans, dp[n][i*a][i*b])
    i+=1
if ans==INF:
    print(-1)
else:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import numpy as np
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)
ans=float("inf")
for key in itertools.product(range(2),repeat=n):
  aa=sum(list(map(np.prod,zip(A,key))))
  bb=sum(list(map(np.prod,zip(B,key))))
  cc=sum(list(map(np.prod,zip(C,key))))
  if aa*mb==bb*ma and aa!=0:
    ans=min(ans,cc)
print(-1 if ans==float("inf") else ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n,ma,mb=map(int,input().split())
A=[]
for i in range(n):
  a,b,c=map(int,input().split())
  A.append([a,b,c])
ans=float("inf")
for key in itertools.product(range(2),repeat=n):
  aa,bb,cc=0,0,0
  for i in range(n):
    if key[i]==1:
      aa+=A[i][0]
      bb+=A[i][1]
      cc+=A[i][2]
  if aa*mb==bb*ma and aa!=0:
    ans=min(ans,cc)
print(-1 if ans==float("inf") else ans)

Submission status: Time Limit Exceeded

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

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

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

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

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

if ans == INF:
    ans = -1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

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

inf = N_max * c_max + 1
dp = np.full((N_max + 1, N_max * a_max + 1, N_max * b_max + 1), inf, dtype='int16')
dp[0][0][0] = 0

# print(dp[N])

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

# print(dp[N])

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

print(ans if ans < inf else -1)

Submission status: 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())
  if a/b>ma/mb:
    A.append([a,b,c])
  elif a/b<ma/mb:
    B.append([a,b,c])
  else:
    C.append([a,b,c])
ans=float("inf")
na,nb,nc=len(A),len(B),len(C)
for i in range(1,2**na):
  for j in range(1,2**nb):
    for k in range(2**nc):
      key_a=str(bin(i))[2:].zfill(na)
      key_b=str(bin(j))[2:].zfill(nb)
      key_c=str(bin(k))[2:].zfill(nc)
      x,y,price=0,0,0
      for ii in range(na):
        if key_a[ii]=="1":
          x+=A[ii][0]
          y+=A[ii][1]
          price+=A[ii][2]
      for jj in range(nb):
        if key_b[jj]=="1":
          x+=B[jj][0]
          y+=B[jj][1]
          price+=B[jj][2]
      for kk in range(nc):
        if key_c[kk]=="1":
          x+=C[kk][0]
          y+=C[kk][1]
          price+=C[kk][2]
      if x*mb-y*ma==0:
        ans=min(ans,price)
print(-1 if ans==float("inf") else ans)

Submission status: Time Limit Exceeded

Solution code:
n,ma,mb=map(int,input().split())
A=[]
for i in range(n):
  a,b,c=map(int,input().split())
  A.append([a,b,c])
ans=float("inf")
for i in range(1,2**n):
  key=str(bin(i))[2:].zfill(n)
  xa,xb,price=0,0,0
  for j in range(n):
    if key[j]=="1":
      xa+=A[j][0]
      xb+=A[j][1]
      price+=A[j][2]
  if xa*mb-xb*ma==0:
    ans=min(ans,price)
print(-1 if ans==float("inf") else ans)

Submission status: Time Limit Exceeded

Solution code:
n,ma,mb=map(int,input().split())
A=[]
for i in range(n):
  a,b,c=map(int,input().split())
  A.append([a,b,c])
ans=float("inf")
for i in range(1,2**n):
  key=str(bin(i))[2:].zfill(n)
  xa,xb,price=0,0,0
  for j in range(n):
    if key[j]=="1":
      xa+=A[j][0]
      xb+=A[j][1]
      price+=A[j][2]
  if xa*mb-xb*ma==0:
    ans=min(ans,price)
print(-1 if ans==float("inf") else ans)

Submission status: Time Limit Exceeded

Solution code:
N,*M = map(int,input().split())
abc = [list(map(int,input().split())) for _ in [0]*N]
INF = 100*40*100
dp = [[[INF]*500 for _ in [0]*500] for _ in [0]*(N+1)]
dp[0][0][0] = 0
for i in range(1,N+1):
    a,b,c = abc[i-1]
    for j in range(410):
        for k in range(410):
            dp[i][j][k] = min(dp[i-1][j][k],dp[i-1][j-a][k-b]+c)
ans = min([dp[-1][M[0]*i][M[1]*i] for i in range(1,1+400//(max(M)))])
if ans == INF:ans=-1
print(ans)

Submission status: Time Limit Exceeded

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となるような物質の最小コストを求める
opt = min(
    f(N, x, Mb * (x // Ma))
    for x in range(Ma, sum(a) + 1)
    if x % Ma == 0
)
ans = (
    opt if opt < 10**18 else
    -1
)

# 出力
print(ans)

Submission status: Time Limit Exceeded

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

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)
amax,bmax,cmax,=max(A),max(B),max(C)
dp=[[[N*cmax for broop in range(NM*ABM+1)] for aroop in range(NM*ABM+1)] for nroop in range(NM+1)]
dp[0][0][0]=0
for i in range(N):
    for cb in range(NM*ABM):
        for ca in range(NM*ABM):
            dp[i+1][ca][cb]=min(dp[i+1][ca][cb],dp[i][ca][cb])
            if ca+A[i]<NM*ABM and cb+B[i]<NM*ABM:
                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=N*cmax
for cb in range(NM*ABM):
    CB=cb+1
    for ca in range(NM*ABM):
        CA=ca+1
        if ca*M2 == cb*M1:
            ans=min(ans,dp[N][CA][CB])

if ans == N*cmax:
    print(-1)
else:
    print(ans)