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:
from copy import deepcopy
n,ma,mb=map(int,input().split())
el=[list(map(int,input().split())) for i in range(n)]
sa=sum(i[0] for i in el)
sb=sum(i[1] for i in el)
sa1=sa+1
sb1=sb+1

inf=10000000000
x=[[inf]*(sb1) for i in range(sa1)]
y=[[0]*(sb1) for i in range(sa1)]
x[0][0]=0
for i in range(n):
    now=el[i]
    x_sub=deepcopy(y)
    for k in range(sa1):
        for l in range(sb1):
            if x[k][l]!=inf:
                if k+now[0]<sa1:
                    if l+now[1]<sb1:
                        x_sub[k+now[0]][l+now[1]]=x[k][l]+now[2]
    for k in range(sa1):
        for l in range(sb1):
                if x_sub[k][l]!=0:
                    x[k][l]=min(x[k][l],x_sub[k][l])
mi=min(sa//ma,sb//mb)
ans=inf
for i in range(1,mi+1):
    ans=min(ans,x[ma*i][mb*i])
if ans==inf:
    print(-1)
else:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,ma,mb,*L=map(int,open(0).read().split())
INF=float('inf')
dp=[[INF]*420 for _ in range(420)]
dp[0][0]=0
for a,b,c in zip(*[iter(L)]*3):
	for i in range(400,-1,-1):
		for j in range(400,-1,-1):
			t=dp[i][j]+c
			if dp[i+a][j+b]>t:
				dp[i+a][j+b]=t
ans=INF
_ma,_mb=ma,mb
while _ma<410>_mb:
	ans=min(ans,dp[_ma][_mb])
	_ma+=ma
	_mb+=mb
print(ans if ans<INF else -1)

Submission status: Time Limit Exceeded

Solution code:
n,a,b=[int(x) for x in str(input()).split()]
A=[]
for i in range(n):
  A.append([int(x) for x in str(input()).split()])



#print(A)
def f(ma,mb,cost,index):

  newa=ma+A[index][0]
  newb=mb+A[index][1]
  newc=cost+A[index][2]
  
  #print(index,newa,newb,newc)
  if newa*b==a*newb:
    return newc
  elif index==n-1:
    return 100000000000000000000
  else:
    ith=f(newa,newb,newc,index+1)
    return min(ith,f(ma,mb,cost,index+1))



if f(0,0,0,0)<100000000000000000000:
  print(f(0,0,0,0))
else:
  print(-1)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, X, Y = MAP()
ABC = []
for i in range(N):
    a, b, c = MAP()
    ABC.append((a, b, c))

MAX = 407
# dp[i][j][k] := i個目まで見て、物質Aをj、物質Bをk含んだ状態での最小コスト
dp = list3d(N+1, MAX, MAX, INF)
dp[0][0][0] = 0
for i in range(N):
    a, b, c = ABC[i]
    for j in range(MAX):
        for k in range(MAX):
            dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k])
            if j+a < MAX and k+b < MAX:
                dp[i+1][j+a][k+b] = min(dp[i+1][j+a][k+b], dp[i][j][k] + c)

j = X
k = Y
ans = INF
while j < MAX and k < MAX:
    # 混合比がX:Yになるところから最小を取る
    ans = min(ans, dp[N][j][k])
    j += X
    k += Y
if ans == INF:
    print(-1)
else:
    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 = list(map(int, input().split()))
D = [tuple(map(int, input().split())) for _ in range(N)]

def dfs(L, D, i, ta, tb, tc):
    if i==len(D):
        key = (ta, tb)
        L[key] = min(L[key], tc) if key in L else tc
        return
    a, b, c = D[i]
    dfs(L, D, i+1, ta  , tb  , tc  )
    dfs(L, D, i+1, ta+a, tb+b, tc+c)

L1, L2 = {}, {}
dfs(L1, D[:N//2], 0, 0, 0, 0)
dfs(L2, D[N//2:], 0, 0, 0, 0)

ans = 10**9
for m in range(1,401):
    ma = Ma * m
    mb = Mb * m
    if ma>400 or mb>400: break
    for key1 in L1:
        ta1, tb1 = key1
        ta2 = ma - ta1
        tb2 = mb - tb1
        key2 = (ta2, tb2)
        if key2 in L2: ans = min(ans, L1[key1]+L2[key2])

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

Submission status: Time Limit Exceeded

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

def dfs(L, D, i, ta, tb, tc):
    if i==len(D):
        key = (ta, tb)
        L[key] = min(L[key], tc) if key in L else tc
        return
    a, b, c = D[i]
    dfs(L, D, i+1, ta  , tb  , tc  )
    dfs(L, D, i+1, ta+a, tb+b, tc+c)

L1, L2 = {}, {}
dfs(L1, D[:N//2], 0, 0, 0, 0)
dfs(L2, D[N//2:], 0, 0, 0, 0)

ans = 10**9
for m in range(1,401):
    ma = Ma * m
    mb = Mb * m
    for key1 in L1:
        ta1, tb1 = key1
        ta2 = ma - ta1
        tb2 = mb - tb1
        key2 = (ta2, tb2)
        if key2 in L2: ans = min(ans, L1[key1]+L2[key2])

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

Submission status: Time Limit Exceeded

Solution code:
import itertools
N,A,B=map(int,input().split())
X=[]
Y=[]
Z=[]
answer=float('inf')
for i in range(N):
  a,b,c=map(int,input().split())
  X +=[a]
  Y +=[b]
  Z +=[c]
for x in itertools.product([0,1],repeat=N):
  p=0
  q=0
  r=0
  for i in range(N):
    if x[i]==1:
      p +=X[i]
      q +=Y[i]
      r +=Z[i]
  if p*B==q*A and not p*q==0:
    answer=min(answer,r)
if answer==float('inf'):
  answer=-1
print(answer)

Submission status: Time Limit Exceeded

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

m = n * 10 + 1

dp = [[[float('inf')] * m for j in range(m)] for i in range(n + 1)]
for i in range(n + 1):
    dp[i][0][0] = 0

for i in range(n):
    for j in range(m):
        for k in range(m):
            dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k])

            # i個目を買う
            jj = j + abc[i][0]
            kk = k + abc[i][1]
            if jj < m and kk < m:
                dp[i+1][jj][kk] = min(dp[i+1][jj][kk], dp[i][j][k] + abc[i][2])

from fractions import gcd
ans = float('inf')
for i in range(1, m):
    for j in range(1, m):
        g = gcd(i, j)
        if i // g == ma and j // g == mb:
            ans = min(ans, dp[-1][i][j])

if ans == float('inf'):
    print(-1)
else:
    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)

INF = 4001
Na = sum(A)
Nb = sum(B)
dp = [[[INF]*(Nb+1) for _ in range(Na+1)] for _ in range(N+1)]
dp[0][0][0] = 0

for i in range(N):
    for j in range(Na+1):
        for k in range(Nb+1):
            if j < A[i] or k < B[i]:
                dp[i+1][j][k] = dp[i][j][k]
            else:
                dp[i+1][j][k] = min(dp[i][j][k], dp[i][j-A[i]][k-B[i]]+C[i])
            
ans = INF
for i in range(1, N+1):
    for j in range(1, Na+1):
        for k in range(1, Nb+1):
            if j*Mb == k*Ma:
                ans = min(ans, dp[i][j][k])
                
if ans == INF:
    ans = -1
    
print(ans)

Submission status: Time Limit Exceeded

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

INF = 10**8
MAX_M = 400
dp = [[[INF] * (MAX_M + 1) for _ in range(MAX_M + 1)] for _ in range(2)]
dp[0][0][0] = 0
for i, (a, b, c) in enumerate(med):
    i0 = (i + 0) % 2
    i1 = (i + 1) % 2
    for x in range(MAX_M + 1):
        for y in range(MAX_M + 1):
            if x - a >= 0 and y - b >= 0:
                dp[i1][x][y] = min(dp[i0][x][y], dp[i0][x - a][y - b] + c)
            else:
                dp[i1][x][y] = dp[i0][x][y]

ans = INF
for i in range(1, MAX_M + 1):
    if ma * i > MAX_M or mb * i > MAX_M:
        break
    ans = min(ans, dp[n % 2][ma * i][mb * i])
if ans == INF:
    ans = -1

print(ans)

Submission status: Time Limit Exceeded

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

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**9

# dp[i][ca][cb] := i番目までの(ca, cb)の最小費用
dp = [[[INF]*401 for j in range(401)] for i in range(n+1)]

dp[0][0][0] = 0
for i in range(1, n+1) :
    dp[i][A[i-1]][B[i-1]] = C[i-1]

for i in range(1, n+1) :
    for ca in range(1, 401) :
        for cb in range(1, 401) :
            if not (ca-A[i-1] >= 0 and cb-B[i-1] >= 0) :
                dp[i][ca][cb] = min(dp[i][ca][cb], dp[i-1][ca][cb])
            else :
                dp[i][ca][cb] = min(dp[i][ca][cb], dp[i-1][ca][cb], dp[i-1][ca-A[i-1]][cb-B[i-1]]+C[i-1])

ans = INF

for ca in range(1, 401) :
    for cb in range(1, 401) :
        if mb*ca == ma*cb :
            ans = min(ans, dp[n][ca][cb])

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

INF = 10**9

# dp[i][ca][cb] := i番目までの(ca, cb)の最小費用
dp = [[[INF]*401 for j in range(401)] for i in range(n+1)]

dp[0][0][0] = 0
for i in range(1, n+1) :
    dp[i][A[i-1]][B[i-1]] = C[i-1]

for i in range(1, n+1) :
    for ca in range(1, 401) :
        for cb in range(1, 401) :
            if not (ca-A[i-1] >= 0 and cb-B[i-1] >= 0) :
                dp[i][ca][cb] = min(dp[i][ca][cb], dp[i-1][ca][cb])
            else :
                dp[i][ca][cb] = min(dp[i][ca][cb], dp[i-1][ca][cb], dp[i-1][ca-A[i-1]][cb-B[i-1]]+C[i-1])

ans = INF

for ca in range(1, 401) :
    for cb in range(1, 401) :
        if mb*ca == ma*cb :
            ans = min(ans, dp[n][ca][cb])

if ans == INF :
    ans = -1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n, ma, mb = map(int, input().split(" "))
info_med = []
for _ in range(n):
  info_med.append(list(map(int, input().split(" "))))

min_cost = 10000
for num_med in range(1, n + 1):
  list_med = [i for i in range(n)]
  list_select = itertools.combinations(list_med, num_med)
  for one_select in list_select:
    sum_ma = sum([info_med[one_med][0] for one_med in one_select])
    sum_mb = sum([info_med[one_med][1] for one_med in one_select])
    if sum_ma / sum_mb == ma / mb:
      sum_c = sum([info_med[one_med][2] for one_med in one_select])
      if sum_c < min_cost:
        min_cost = sum_c

if min_cost == 10000:
  print(-1)
else:
  print(min_cost)

Submission status: Time Limit Exceeded

Solution code:

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

lis = [[float("inf")] * 401 for i in range(401)]
lis[0][0] = 0

for x in range(N):

    a,b,c = map(int,input().split())

    olis = []
    for i in lis:
        olis.append(i.copy())

    for i in range(401):

        for j in range(401):

            if olis[i][j] != float("inf"):

                if i + a < 400 and j + b < 400:

                    lis[i+a][j+b] = min( olis[i][j] + c , lis[i+a][j+b] )

ans = float("inf")

for i in range(400):

    i += 1

    for j in range(400):

        j += 1

        if i * Mb == j * Ma:

            ans = min(ans , lis[i][j])

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N, Ma, Mb = map(int, input().split())
l = []
for i in range(N):
    l.append(list(map(int, input().split())))
ans = np.inf

dp = np.full((N + 1, 10 * N + 1, 10 * N + 1), np.inf)
dp[0][0][0] = 0
for n in range(N):
    for a in range(10 * N):
        for b in range(10 * N):
            if dp[n][a][b] == np.inf:
                continue
            dp[n + 1][a][b] = min(dp[n + 1][a][b], dp[n][a][b])
            dp[n + 1][a + l[n][0]][b + l[n][1]] = min(dp[n + 1][a + l[n][0]][b + l[n][1]], dp[n][a][b] + l[n][2])
# np.set_printoptions(threshold=np.inf)
# print(dp)
for a in range(1, 10 * N):
    for b in range(1, 10 * N):
        if a * Mb == b * Ma:
            ans = min(ans, dp[n + 1][a][b])
if ans == np.inf:
    print(-1)
else:
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
n, ma, mb = map(int, input().split())
ls = [list(map(int, input().split())) for _ in range(n)]
MAX_N = 10 * n + 1
dp = [[[float('inf')] * MAX_N for _ in range(MAX_N)] for _ in range(n + 1)]
dp[0][0][0] = 0

for k, (a, b, c) in enumerate(ls):
    for i in range(MAX_N):
        for j in range(MAX_N):
            dp[k + 1][i][j] = min(dp[k + 1][i][j], dp[k][i][j])
            if i + a < MAX_N and j + b < MAX_N:
                dp[k + 1][i + a][j + b] = min(dp[k][i][j] + c,
                                              dp[k][i + a][j + b])
p = min([
    dp[n][ma * i][mb * i] for i in range(1, MAX_N)
    if ma * i < MAX_N and mb * i < MAX_N
])
if p < float('inf'):
    print(p)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
inf = float("inf")
N, Ma, Mb = map(int, input().split())
ABC = []
for _ in range(N):
    a, b, c = map(int, input().split())
    ABC.append((a, b, c))


def cost(bits):
    sa = sb = sc = 0
    for i, (a, b, c) in enumerate(ABC):
        if not bits & 1 << i:
            continue
        sa += a
        sb += b
        sc += c
    return sc if sa * Mb == sb * Ma else inf


ans = min([cost(bits) for bits in range(1, 1 << N)])
print(ans if ans < inf else -1)

Submission status: Time Limit Exceeded

Solution code:
##SPEED TEST##
n,ma,mb=map(int,input().split())
ab=[[[10**6]*401 for _ in range(401)] for _ in range(n+1)]
ab[0][0][0]=0

for p in range(1,n+1):
    a,b,c=map(int,input().split())
    for i in range(400):
        for j in range(400):
            ab[p][i][j]=min(ab[p][i][j],ab[p-1][i][j])
            if i+a<=400 and j+b<=400:
                ab[p][i+a][j+b]=min(ab[p][i+a][j+b],ab[p-1][i][j]+c)

ans=10**6-1
for i in range(401):
    for j in range(401):
        if i!=0 and j!=0:
            if i*mb==j*ma:
                ans=min(ans,ab[n][i][j])

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

Submission status: Time Limit Exceeded

Solution code:
n,ma,mb=map(int,input().split())
ab=[[[10**6]*401 for _ in range(401)] for _ in range(n+1)]
ab[0][0][0]=0

for p in range(1,n+1):
    a,b,c=map(int,input().split())
    for i in range(400):
        for j in range(400):
            if i+a<=400 and j+b<=400:
                ab[p][i+a][j+b]=min(ab[p][i+a][j+b],ab[p-1][i][j]+c)

ans=10**6-1
for p in range(1,n+1):
    for i in range(401):
        for j in range(401):
            if i!=0 and j!=0:
                if i*mb==j*ma:
                    ans=min(ans,ab[p][i][j])

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

Submission status: Time Limit Exceeded

Solution code:
n,ma,mb=map(int,input().split())
abc=[]
for i in range(n):
    abc.append([int(j) for j in input().split()])
from operator import itemgetter
abc.sort(key=itemgetter(2))

dp=[[10**18 for i in range(401)]for j in range(401)]
dp[0][0]=0

q=[[0,0]]
for a,b,c in abc:
    l=[]
    for i,j in q:
        l.append([i,j])
        l.append([i+a,j+b])
        dp[a+i][b+j]=min(dp[a+i][b+j],dp[i][j]+c)
    q=l

ans=1145141919
i=1
while ma*i<401 and mb*i<401:
    ans=min(ans,dp[ma*i][mb*i])
    i+=1
if ans==1145141919:
    print(-1)
else:
    print(ans)

Submission status: Time Limit Exceeded

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

from operator import itemgetter
abc.sort(key=itemgetter(2))

dp=[[10**100 for i in range(500)]for j in range(500)]
dp[0][0]=0
from collections import deque
q=deque([(0,0)])
p=0
for a,b,c in abc:
    l=[]
    while q:
        i,j=q.popleft()
        l.append([i,j])
        l.append([i+a,j+b])
        dp[a+i][b+j]=min(dp[a+i][b+j],dp[i][j]+c)
    q=deque(l)

ans=1145141919
i=1
while ma*i<500 and mb*i<500:
    ans=min(ans,dp[ma*i][mb*i])
    i+=1
if ans==1145141919:
    print(-1)
else:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
nmax, abmax, inf = 40, 10, 1000000

a, b, c = [0] * nmax, [0] * nmax, [0] * nmax
dp = [[[0] * (nmax * abmax + 1) for _ in range(nmax * abmax + 1)] for _ in range(nmax + 1)]

n, ma, mb = map(int, input().split())
for i in range(n):
  x, y, z = map(int, input().split())
  a[i], b[i], c[i] = x, y, z

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(0, 401):
    for cb in range(0, 401):
      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])

ans = inf
for ca in range(1, 401):
  for cb in range(1, 401):
    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:
import sys
n, ma, mb = map(int, input().split())
tc = [[] for _ in range(100 * n + 1)]
tn = [[] for _ in range(100 * n + 1)]
high = 100 * n + 1
for i in range(n):
  a, b, c = map(int, input().split())
  if a * mb == b * ma:
    high = c + 1
  tn[c].append((a, b))
  for j in range(1, high):
    for aa, bb in tc[j]:
      tn[j].append((aa, bb))
      tn[c + j].append((a + aa, b + bb))
      if (a + aa) * mb == (b + bb) * ma:
        high = c + j + 1
  tc = tn
  tn = [[] for _ in range(100 * n + 1)]
for i in range(1, 100 * n + 1):
  for a, b in tc[i]:
    if a * mb == b * ma:
      print(i)
      sys.exit()
print(-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
n, ma, mb = map(int, input().split())
tc = [[] for _ in range(100 * n + 1)]
tn = [[] for _ in range(100 * n + 1)]
high = 100 * n + 1
for i in range(n):
  a, b, c = map(int, input().split())
  if a % ma == 0 and b % mb == 0 and a // ma == b // mb:
    high = c + 1
  tn[c].append((a, b))
  for j in range(1, high):
    for aa, bb in tc[j]:
      tn[j].append((aa, bb))
      tn[c + j].append((a + aa, b + bb))
      if (a + aa) % ma == 0 and (b + bb) % mb == 0 and (a + aa) // ma == (b + bb) // mb:
        high = c + j + 1
  tc = tn
  tn = [[] for _ in range(100 * n + 1)]
for i in range(1, 100 * n + 1):
  for a, b in tc[i]:
    if a % ma == 0 and b % mb == 0 and a // ma == b // mb:
      print(i)
      sys.exit()
print(-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
n, ma, mb = map(int, input().split())
tc = [[] for _ in range(100 * n + 1)]
tn = [[] for _ in range(100 * n + 1)]
for i in range(n):
  a, b, c = map(int, input().split())
  tn[c].append((a, b))
  for j in range(1, 100 * n + 1):
    for aa, bb in tc[j]:
      tn[j].append((aa, bb))
      tn[c + j].append((a + aa, b + bb))
  tc = tn
  tn = [[] for _ in range(100 * n + 1)]
for i in range(1, 100 * n + 1):
  for a, b in tc[i]:
    if a % ma == 0 and b % mb == 0 and a // ma == b // mb:
      print(i)
      sys.exit()
print(-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
n, ma, mb = map(int, input().split())
t = [[[] for _ in range(100 * n + 1)] for _ in range(n + 1)]
for i in range(n):
  a, b, c = map(int, input().split())
  t[i + 1][c].append((a, b))
  for j in range(1, 100 * n + 1):
    for aa, bb in t[i][j]:
      t[i + 1][j].append((aa, bb))
      t[i + 1][c + j].append((a + aa, b + bb))
for i in range(1, 100 * n + 1):
  for a, b in t[n][i]:
    if a % ma == 0 and b % mb == 0 and a // ma == b // mb:
      print(i)
      sys.exit()
print(-1)

Submission status: Time Limit Exceeded

Solution code:
n,a,b=[int(x) for x in str(input()).split()]
A=[]
for i in range(n):
  A.append([int(x) for x in str(input()).split()])



#print(A)
def f(ma,mb,cost,index):

  newa=ma+A[index][0]
  newb=mb+A[index][1]
  newc=cost+A[index][2]
  
  #print(index,newa,newb,newc)
  if newa*b==a*newb:
    return newc
  elif index==n-1:
    return 100000000000000000000
  else:
    ith=f(newa,newb,newc,index+1)
    return min(ith,f(ma,mb,cost,index+1))



if f(0,0,0,0)<100000000000000000000:
  print(f(0,0,0,0))
else:
  print(-1)

Submission status: Time Limit Exceeded

Solution code:
import itertools
N, Ma, Mb = map(int, input().split())
L = [[int(i) for i in input().split()] for _ in range(N)]

inf = N * 101
dp = [[[inf for _ in range(N*10+11)] for _ in range(N*10+11)] for _ in range(N+1)]

dp[0][0][0] = 0
cmb = itertools.product(range(N), range(N*10), range(N*10))
for i, j, k in cmb:
    dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k])
    dp[i+1][j+L[i][0]][k+L[i][1]] = min(dp[i+1][j+L[i][0]][k+L[i][1]], dp[i][j][k] + L[i][2])
ans = inf
for i in range(1, N*10//max(Ma, Mb)):
    ans = min(ans, dp[N][i*Ma][i*Mb])

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

Submission status: Time Limit Exceeded

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

inf = N * 101
dp = [[[inf for _ in range(N*10+11)] for _ in range(N*10+11)] for _ in range(N+1)]

dp[0][0][0] = 0
for i in range(N):
    for j in range(N*10):
        for k in range(N*10):
            dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k])
            dp[i+1][j+L[i][0]][k+L[i][1]] = min(dp[i+1][j+L[i][0]][k+L[i][1]], dp[i][j][k] + L[i][2])
ans = inf
for i in range(1, N*10//max(Ma, Mb)):
    ans = min(ans, dp[N][i*Ma][i*Mb])

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

dp = 99999999*np.ones((N+1,402,402),dtype=int)
dp[0][0][0] = 0
for n in range(N):
  a,b,c = map(int,input().split())
  for i in range(401):
    for j in range(401):
      if dp[n][i][j]!=99999999:
        dp[n+1][i  ][j  ] = min(dp[n+1][i  ][j  ],dp[n][i][j])
        dp[n+1][i+a][j+b] = min(dp[n+1][i+a][j+b],dp[n][i][j]+c)

ans = 99999999
i,j = Ma,Mb
while i<=401 and j<=401:
  ans = min(ans,dp[N][i][j])
  i += Ma
  j += Mb
print(ans if ans!=99999999 else -1)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: int, M_a: int, M_b: int, a: "List[int]", b: "List[int]", c: "List[int]"):
    M = (max(a+b)+1)*N
    DP = [[INF]*(M) for _ in range(M)]
    for aa, bb, cc in zip(a, b, c):
        DP_next = [DP[i][:] for i in range(M)]
        DP_next[aa][bb] = min(cc, DP_next[aa][bb])
        for i in range(1, M):
            for j in range(1, M):
                if i+aa < M and j + bb < M and DP[i][j] > 0:
                    DP_next[i+aa][j+bb] = min(DP[i][j]+cc, DP_next[i+aa][j+bb])
        DP = DP_next

    m = INF
    for i in range(1, M//M_a):
        anum = M_a*i
        bnum = M_b*i
        if anum >= M or bnum >= M:
            break
        if DP[anum][bnum] != 0:
            if m > DP[anum][bnum]:
                m = DP[anum][bnum]
    if m == INF:
        print(-1)
    else:
        print(m)
    return


def main():

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def sol():
    N, Ma, Mb = map(int, input().split())
    items = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        items.append((a, b, c))

    dp = [[set([()]) for _ in range(Ma)] for _ in range(N + 1)]

    for i, (a, _, _) in enumerate(items):
        for mod in range(Ma):
            for prev in dp[i][mod]:
                dp[i + 1][mod].add(prev)
            for prev in dp[i][(mod - a) % Ma]:
                dp[i + 1][mod].add(tuple(list(prev) + [i]))

    ans = float('inf')
    for addList in dp[N][0]:
        sumA = 0
        sumB = 0
        cost = 0
        for i in addList:
            sumA += items[i][0]
            sumB += items[i][1]
            cost += items[i][2]
        if sumA * sumB > 0 and sumA * Mb == sumB * Ma:
            ans = min(ans, cost)

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

sol()

Submission status: Time Limit Exceeded

Solution code:
def sol():
    N, Ma, Mb = map(int, input().split())
    items = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        items.append((a, b, c))

    dp = [[set([()]) for _ in range(Ma)] for _ in range(N + 1)]

    for i, (a, _, _) in enumerate(items):
        for mod in range(Ma):
            for prev in dp[i][mod]:
                dp[i + 1][mod].add(prev)
            for prev in dp[i][(mod - a) % Ma]:
                dp[i + 1][mod].add(tuple(list(prev) + [i]))

    maList = filter(lambda A : len(A) > 0, dp[N][0])

    ans = float('inf')
    for addList in maList:
        sumA = 0
        sumB = 0
        cost = 0
        for i in addList:
            sumA += items[i][0]
            sumB += items[i][1]
            cost += items[i][2]
        if sumA * Mb == sumB * Ma:
            ans = min(ans, cost)

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

sol()

Submission status: Time Limit Exceeded

Solution code:
def sol():
    N, Ma, Mb = map(int, input().split())
    items = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        items.append((a, b, c))

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

    for i, (a, _, _) in enumerate(items):
        for mod in range(Ma):
            for prev in dp[i][mod]:
                dp[i + 1][mod].append(prev)
            for prev in dp[i][(mod - a) % Ma]:
                dp[i + 1][mod].append(prev + [i])

    maList = filter(lambda A : len(A) > 0, dp[N][0])

    ans = float('inf')
    for addList in maList:
        sumA = 0
        sumB = 0
        cost = 0
        for i in addList:
            sumA += items[i][0]
            sumB += items[i][1]
            cost += items[i][2]
        if sumA * Mb == sumB * Ma:
            ans = min(ans, cost)

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

sol()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n,Ma,Mb = map(int,input().split())
first = np.array(list(map(int,input().split())))
L =[first]

for i in range(1,n):
  abc =  np.array(list(map(int,input().split())))
  x = len(L)
  for lis in L[:x]:
    L.append(lis+abc)
    L.append(abc)

nums = [] 
for l in L:
  if l[0]*Mb == l[1]*Ma:
    nums.append(l[2])
if nums:
  print(min(nums))
else:
  print("-1")

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

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

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

# それぞれの薬品を購入するかしないかでbit全探索
can = False
min = -1
n = len(d)
for i in range(2**n):
    sum_a = 0
    sum_b = 0
    sum_c = 0
    for j in range(n):
        if (i >> j) & 1:
            sum_a += d[j][0]
            sum_b += d[j][1]
            sum_c += d[j][2]
    if sum_a > 0 and Ma*sum_b == Mb*sum_a:
        if min < 0 or sum_c < min:
            min = sum_c

print(min)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N, Ma, Mb = map(int,input().split())
C = [map(int,input().split()) for i in range(N)]
INF = 40000
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 = C[i-1]
  for j in range(401):
      for k in range(401):
        dp[i][j][k] = dp[i-1][j][k]
        if j >= a and k >= b and dp[i-1][j-a][k-b] != INF:
            dp[i][j][k] = min(dp[i][j][k],dp[i-1][j-a][k-b]+c)
ans = INF
Ma2,Mb2 = Ma,Mb
while max(Ma2,Mb2) < 401:
  ans = min(ans,dp[-1][Ma2][Mb2])
  Ma2 += Ma
  Mb2 += Mb    

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