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:
import numpy as np

if __name__=="__main__":
    inputs_number  = lambda : [int(x) for x in input().split()]
    N, Ma, Mb = inputs_number()
    drugs = [inputs_number() for i in range(N)]
    inf = 100*N + 1
    dp = np.ones((N+1,N*10+1,N*10+1)).astype(np.int32) * inf
    dp = dp.tolist()
    dp[0][0][0] = 0.0

    for i in range(N):
        for ca in range(N*10+1):
            for cb in range(N*10+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+drugs[i][0]][cb+drugs[i][1]]=min([dp[i+1][ca+drugs[i][0]][cb+drugs[i][1]], dp[i][ca][cb]+drugs[i][2]])
    ans = inf
    for ca in range(1, N*10+1):
        for cb in range(1, N*10+1):
            if(Ma*cb==Mb*ca and dp[N][ca][cb]<ans):
                ans = dp[N][ca][cb]
    if(ans==inf):
        ans = -1
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
INF = 1010101010

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

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

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

        for A in range(401):
            for B in range(401):
                nx[A][B] = min(nx[A][B], dp[A][B])
                if A+a<=400 and B+b<=400:
                    nx[A+a][B+b] = min(nx[A+a][B+b], dp[A][B]+c)

        dp = nx[:]

    ans = INF
    for A in range(1,401):
        for B in range(1,401):
            if Ma*B == Mb*A:
                ans = min(ans, dp[A][B])

    return -1 if ans == INF else ans

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

Submission status: Time Limit Exceeded

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

dp=[[[float('inf') for _ in range(401)] for _ in range(401)] for _ in range(N+1)]
dp[0][0][0]=0
for i in range(N):
    for ma in range(400):
        for mb in range(400):
            if dp[i][ma][mb]<float('inf'):
                dp[i+1][ma][mb]=min(dp[i+1][ma][mb],dp[i][ma][mb])
                dp[i+1][ma+D[i][0]][mb+D[i][1]]=min(dp[i+1][ma+D[i][0]][mb+D[i][1]],dp[i][ma][mb]+D[i][2])


ans=float('inf')
for i in range(400):
    for j in range(400):
        if float(i)/Ma==float(j)/Mb:
            if dp[N][i][j]!=0:
                ans=min(ans,dp[N][i][j])
if ans<float('inf'):
    print ans
else :
    print "-1"

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy

N, MA, MB = map(int, raw_input().split())
X = [map(int, raw_input().split()) for _ in range(N)]

DP = [[100000 for _ in range(401)] for _ in range(401)]
DP[0][0] = 0

S = {(0, 0)}
for a, b, c in X:
    DP2 = deepcopy(DP)
    for sa, sb in S:
        if DP[sa+a][sb+b] > DP2[sa][sb] + c:
            DP[sa+a][sb+b] = DP2[sa][sb] + c
    T = {(sa+a, sb+b) for sa, sb in S}
    S |= T

a, b = MA, MB
ans = 100000
while a <= 400 and b <= 400:
    ans = min(ans, DP[a][b])
    a += MA
    b += MB

if ans == 100000:
    print -1
else:
    print ans

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy

# fin = open('test4.txt')
# input = fin.readline


def wfs(tree, root, node_num):
    lost_list = [root]

    def _wfs(tree, root, lost_list) -> int:
        if len(lost_list) == node_num:
            return 1
        counter = 0
        for i in tree[root]:
            if i not in lost_list:
                tmp = deepcopy(lost_list)
                tmp.append(i)
                counter += _wfs(tree, i, tmp)
        return counter

    return _wfs(tree, 0, lost_list)


N, MA, MB = [int(i) for i in input().rstrip().split()]
TGT = MA / MB
med = [[int(i) for i in input().rstrip().split()] for _ in range(N)]
flag_list = [0] * N
amount = [0, 0]
all_list = list()


def solve(amount, flag_list, money, i):
    global TGT
    global all_list
    global med
    global N
    try :
        if amount[0] / amount[1] == TGT:
            all_list.append(money)
    except ZeroDivisionError as e:
        pass
    if i == N:
        return
    solve(amount, flag_list, money, i + 1)
    flag_list[i] = 1
    amount[0] += med[i][0]
    amount[1] += med[i][1]
    solve(amount, flag_list, money + med[i][2], i + 1)
    flag_list[i] = 0
    amount[0] -= med[i][0]
    amount[1] -= med[i][1]

solve(amount, flag_list, 0, 0)

if len(all_list) == 0:
    print(-1)
else:
    print(min(all_list))

Submission status: Time Limit Exceeded

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

def recursion(ma,mb,c,i):
    if i==N:
        return float("inf")
    nma=ma+D[i][0]
    nmb=mb+D[i][1]
    nc=c+D[i][2]
    if nma/float(Ma)==nmb/float(Mb):
        return min(nc,recursion(ma,mb,c,i+1))
    return min(recursion(nma,nmb,nc,i+1),recursion(ma,mb,c,i+1))
v=recursion(0,0,0,0)
if v<float('inf'):
    print v
else:
    print -1

Submission status: Time Limit Exceeded

Solution code:
n, aa, bb = map(int,input().split())
lis = [list(map(int,input().split())) for i in range(n) ]
ans = -1
for i in range(1,2**n):
    i = list(str(bin(i)))
    madea = 0
    madeb = 0
    c = 0
    for j in range(2,len(i)):
        if int(i[j]) == 1:
            madea = madea + lis[j-2][0]
            madeb = madeb + lis[j-2][1]
            c = c + lis[j-2][2]
    if madea*bb == madeb*aa and ans == -1 :
        ans = c
    if madea*bb == madeb*aa and ans > c:
        ans = c

print(ans)

Submission status: Time Limit Exceeded

Solution code:
Unknown = 1234567
best = Unknown

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

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

def search(choice, a, b, c):
    if c > 0 and a % A == b % B == 0 and a // A == b // B:
        global best
        best = min(best, c)
        choice.pop()
        return
    for i in range(N):
        if i in choice:
            continue
        if best <= c + abc[i][2]:
            continue
        choice.append(i)
        search(choice, a + abc[i][0], b + abc[i][1], c + abc[i][2])
    if len(choice) > 0:
        choice.pop()

search([], 0, 0, 0)

if best == Unknown:
    best = -1

print(best)

Submission status: Time Limit Exceeded

Solution code:
Unknown = 1234567
best = Unknown

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

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

def search(choice, a, b, c, start):
    if a % A == b % B == 0 and a // A == b // B:
        global best
        best = min(best, c)
        choice.pop()
        return
    for i in range(start, N):
        if i in choice:
            continue
        if best <= c + abc[i][2]:
            continue
        choice.append(i)
        search(choice, a + abc[i][0], b + abc[i][1], c + abc[i][2], start)
    choice.pop()

for i in range(N):
    search([i], abc[i][0], abc[i][1], abc[i][2], i + 1)

if best == Unknown:
    best = -1

print(best)

Submission status: Time Limit Exceeded

Solution code:
Unknown = 1234567
best = Unknown

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

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

def search(choice, a, b, c):
    if a % A == b % B == 0 and a // A == b // B:
        global best
        best = min(best, c)
        choice.pop()
        return
    for i in range(N):
        if i in choice:
            continue
        if (a / b > A / B and abc[i][0] < abc[i][1]) or (a / b < A / B and abc[i][0] > abc[i][1]):
            choice.append(i)
            search(choice, a + abc[i][0], b + abc[i][1], c + abc[i][2])
    choice.pop()

for i in range(N):
    search([i], abc[i][0], abc[i][1], abc[i][2])            

if best == Unknown:
    best = -1

print(best)

Submission status: Time Limit Exceeded

Solution code:
Unknown = 1234567
best = Unknown

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

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

def search(choice, a, b, c):
    if a%A==b%B==0 and a//A==b//B:
        global best
        best = min(best, c)
    for i in range(N):
        if i in choice:
            continue
        if (a / b > A / B   and abc[i][0] < abc[i][1]) or (a / b < A / B and abc[i][0] > abc[i][1]):
            choice.append(i)
            search(choice, a+abc[i][0], b+abc[i][1], c+abc[i][2])
    choice.pop()

for i in range(N):
    search([i], abc[i][0],  abc[i][1], abc[i][2])            

if best == Unknown:
    best = -1

print(best)

Submission status: Time Limit Exceeded

Solution code:
from sys import exit, setrecursionlimit
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

setrecursionlimit(1000000)
INF = 10**3

(N, Ma, Mb) = reads()

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

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


# dp[i, j, k]: 番号i以下の品物だけを使って物質Aをjグラム物質Bをkグラム買うときのコスト
dp = [[[INF]*410 for _ in range(410)] for _ in range(N+1)]
# dp = defaultdict(lambda: INF)

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

for i, j, k in product(range(N), range(410), range(410)):
  if j-a[i] < 0 or k-b[i] < 0:
    prev = INF
  else:
    prev = dp[i][j-a[i]][k-b[i]]

  dp[i+1][j][k] = min(prev + c[i], dp[i][j][k])

# for i in range(N):
#   for j in range(10):
#     print(" ".join("%4d"%(dp[i, j, k],) for k in range(10)))
#   print()

result = INF

for n in range(1, 410):
  if Ma * n >= 410 or Mb * n >= 410:
    break
  result = min(result, dp[N][Ma * n][Mb * n])

print(result if result < INF else -1)

Submission status: Time Limit Exceeded

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

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

M = 401
INF = 1000000000
dp = [INF] * (M * M)
dp[0] = 0
for a, b, c in G:
  i = M - a - 1
  while i >= 0:
    j = M - b - 1
    while j >= 0:
      dp[(i+a)*M+(j+b)] = min(dp[(i+a)*M+(j+b)], dp[i*M+j] + c)
      j -= 1
    i -= 1

ans = INF
x = 1
while Ma * x < M and Mb * x < M:
  ans = min(ans, dp[Ma * x * M + Mb * x])
  x += 1
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())

M = 401
INF = 10 ** 15
min_cost = [[INF] * M for k in range(M)]
min_cost[0][0] = 0

for k in range(n):
    for i in range(M - 1, a[k]-1, -1):
        for j in range(M - 1, b[k] - 1, -1):
            cc = min_cost[i - a[k]][j - b[k]] + c[k]
            if cc < min_cost[i][j]:
                min_cost[i][j] = cc

res = INF
for i in range(1, M):
    for j in range(1, M):
        if i * mb == j * ma:
            if min_cost[i][j] < res:
                res = min_cost[i][j]

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

Submission status: Time Limit Exceeded

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

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

M = 401
INF = 1000000000
dp = [INF] * (M * M)
dp[0] = 0
for a, b, c in G:
  for i in reversed(range(0, M - a)):
    for j in reversed(range(0, M - b)):
      dp[(i+a)*M+(j+b)] = min(dp[(i+a)*M+(j+b)], dp[i*M+j] + c)

ans = INF
x = 1
while Ma * x < M and Mb * x < M:
  ans = min(ans, dp[Ma * x * M + Mb * x])
  x += 1
if ans >= INF:
  ans = -1
print(ans)

Submission status: Time Limit Exceeded

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

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

M = 401
INF = 1000000000
dp = {}
for i in range(M):
  for j in range(M):
    dp[i,j] = INF
dp[0,0] = 0
for a, b, c in G:
  for i in reversed(range(0, M - a)):
    for j in reversed(range(0, M - b)):
      dp[i+a,j+b] = min(dp[i+a,j+b], dp[i,j] + c)

ans = INF
x = 1
while Ma * x < M and Mb * x < M:
  ans = min(ans, dp[Ma * x,Mb * x])
  x += 1
if ans >= INF:
  ans = -1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import exit, setrecursionlimit
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

setrecursionlimit(1000000)
INF = 10**3

(N, Ma, Mb) = reads()

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

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


# dp[i, j, k]: 番号i以下の品物だけを使って物質Aをjグラム物質Bをkグラム買うときのコスト
dp = defaultdict(lambda: INF)

dp[-1, 0, 0] = 0

for i, j, k in product(range(N), range(410), range(410)):
  dp[i,j,k] = min(dp[i-1, j-a[i], k-b[i]] + c[i], dp[i-1, j, k])

# for i in range(N):
#   for j in range(10):
#     print(" ".join("%4d"%(dp[i, j, k],) for k in range(10)))
#   print()

result = INF

for n in range(1, 410):
  result = min(result, dp[N-1, Ma * n, Mb * n])

print(result if result < INF else -1)

Submission status: Time Limit Exceeded

Solution code:
n, ma, mb = map(int, raw_input().split())
N = 403
memo = [[10**18]*(N+20) for i in xrange(N+20)]
memo[0][0] = 0
for t in xrange(n):
    a, b, c = map(int, raw_input().split())
    for i in xrange(N, -1, -1):
        for j in xrange(N, -1, -1):
            memo[i+a][j+b] = min(memo[i+a][j+b], memo[i][j] + c)
ans = 10**18
a = ma; b = mb
while a < N and b < N:
    ans = min(ans, memo[a][b])
    a += ma; b += mb
print -1 if ans > 50000 else ans

Submission status: Time Limit Exceeded

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

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

M = 401
INF = 1000000000
dp = [ [ INF ] * M for _ in range(M) ]

dp[0][0] = 0
for a, b, c in G:
  for i in reversed(range(0, M - a)):
    for j in reversed(range(0, M - b)):
      dp[i+a][j+b] = min(dp[i+a][j+b], dp[i][j] + c)

ans = INF
x = 1
while Ma * x < M and Mb * x < M:
  ans = min(ans, dp[Ma * x][Mb * x])
  x += 1
if ans >= INF:
  ans = -1
print(ans)

Submission status: Time Limit Exceeded

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

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

M = 401
INF = 1000000000
dp = [ [ INF ] * M for _ in range(M) ]

dp[0][0] = 0
for a, b, c in G:
  for i in reversed(range(0, M)):
    for j in reversed(range(0, M)):
      if i + a < M and j + b < M:
        dp[i+a][j+b] = min(dp[i+a][j+b], dp[i][j] + c)

ans = INF
x = 1
while Ma * x < M and Mb * x < M:
  ans = min(ans, dp[Ma * x][Mb * x])
  x += 1
if ans >= INF:
  ans = -1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
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())

M = 401
INF = 1000000000
dp = [ [ INF ] * 500 for _ in range(M) ]

dp[0][0] = 0
for k in range(N):
  for i in reversed(range(0, M)):
    for j in reversed(range(0, M)):
      if i + a[k] < M and j + b[k] < M:
        dp[i+a[k]][j+b[k]] = min(dp[i+a[k]][j+b[k]], dp[i][j] + c[k])

ans = INF
x = 1
while Ma * x < M and Mb * x < M:
  ans = min(ans, dp[Ma * x][Mb * x])
  x += 1
if ans >= INF:
  ans = -1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
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 = 1000000000
dp = [ [ INF ] * 500 for _ in range(500) ]

dp[0][0] = 0
for k in range(N):
  for i in reversed(range(0, 500)):
    for j in reversed(range(0, 500)):
      if i + a[k] < 500 and j + b[k] < 500:
        dp[i+a[k]][j+b[k]] = min(dp[i+a[k]][j+b[k]], dp[i][j] + c[k])

ans = INF
x = 1
while Ma * x < 500 and Mb * x < 500:
  ans = min(ans, dp[Ma * x][Mb * x])
  x += 1
if ans >= INF:
  ans = -1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def count(data, i, a, b, c, ma, mb):
  if a and b and a*mb == b*ma: return c
  elif i >= len(data): return -1
  else:
    v1 = count(data, i+1, a, b, c, ma, mb)
    v2 = count(data, i+1, a+data[i][0], b+data[i][1], c+data[i][2], ma, mb)
    if v1*v2 < 0: return max(v1, v2)
    else: return min(v1, v2)

n, ma, mb = map(int, input().split())
data = [list(map(int, input().split())) for i in range(n)]
print(count(data, 0, 0, 0, 0, ma, mb))

Submission status: Time Limit Exceeded

Solution code:
INF = 1010101010

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

    a = [0 for _ in range(N)]
    b = [0 for _ in range(N)]
    c = [0 for _ in range(N)]

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

    dp = [[[INF for k in range(401)] for j in range(401)] for i in range(41)]
    dp[0][0][0] = 0

    for i in range(N):
        for A in range(401):
            for B in range(401):
                dp[i+1][A][B] = min(dp[i+1][A][B], dp[i][A][B])
                if A+a[i]<=400 and B+b[i]<=400:
                    dp[i+1][A+a[i]][B+b[i]] = min(dp[i+1][A+a[i]][B+b[i]], dp[i][A][B]+c[i])

    ans = 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])

    return -1 if ans == INF else ans

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

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
from fractions import Fraction

memo = {}

def dfs(i, a, b):
    if a != 0 and b != 0 and Fraction(a, b) == Fraction(Ma, Mb):
        return 0
    if i == N:
        return 10 ** 10

    if (i, a, b) in memo:
        return memo[(i, a, b)]

    ans = dfs(i + 1, a + l[i][0], b + l[i][1]) + l[i][2]
    ans = min(ans, dfs(i + 1, a, b))

    memo[(i, a, b)] = ans
    return ans


if __name__ == '__main__':

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

    s = dfs(0, 0, 0)
    if s >= 10 ** 10:
        print(-1)
    else:
        print(s)

Submission status: Memory Limit Exceeded

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


if __name__ == "__main__":
    main()

Submission status: Memory Limit Exceeded

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

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

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

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

ans = float("inf")

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

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

Submission status: Memory Limit Exceeded

Solution code:
import numpy as np

if __name__=="__main__":
    inputs_number  = lambda : [int(x) for x in input().split()]
    N, Ma, Mb = inputs_number()
    drugs = [inputs_number() for i in range(N)]
    inf = 100*N + 1
    dp = np.ones((N+1,N*10+1,N*10+1)).astype(np.int32) * inf
    dp = dp.tolist()
    dp[0][0][0] = 0.0

    for i in range(N):
        for ca in range(N*i+1):
            for cb in range(N*i+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+drugs[i][0]][cb+drugs[i][1]]=min([dp[i+1][ca+drugs[i][0]][cb+drugs[i][1]], dp[i][ca][cb]+drugs[i][2]])
    ans = inf
    for ca in range(1, N*10+1):
        for cb in range(1, N*10+1):
            if(Ma*cb==Mb*ca and dp[N][ca][cb]<ans):
                ans = dp[N][ca][cb]
    if(ans==inf):
        ans = -1
    print(int(ans))

Submission status: Memory Limit Exceeded

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]

ans = float('inf')
d = {}
for a, b, c in C:
    x = ma * b - mb * a
    if x == 0 and c > 0:
        ans = min(ans, c)
    if x in d:
        d[x] = min(d[x], c)
    else:
        d[x] = c

for a, b, c in B:
    x = mb * a - ma * b
    if x == 0 and c > 0:
        ans = min(ans, c)
    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: Accepted

Solution code:
n,ma,mb = map(int,input().split())
l = []
sum_a = 0
sum_b = 0
for _ in range(n):
    a,b,c = map(int,input().split())
    sum_a += a
    sum_b += b
    l.append([a,b,c])

INF = 10**10
dp = list( list([INF for i in range(450)] for i in range(450)) for i in range(45))
dp[0][0][0] = 0
for i in range(n):
    for s in range(sum_a+1):
        for k in range(sum_b+1):
            a,b,c = l[i]
            dp[i+1][s][k] = min(dp[i][s][k], dp[i+1][s][k],dp[i][s-a][k-b]+c)

ans = 10**10
k = 1
while ma*k < 450 and mb*k < 450 :
    ans = min(ans, dp[n][ma*k][mb*k])
    k += 1
if ans != 10**10:
  print(ans)
else:
  print(-1)

Submission status: Accepted

Solution code:
n,ma,mb=map(int,input().split())
zen=[[10**10]*401 for i in range(401)]
kou=[[10**10]*401 for j in range(401)]
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[now[0]][now[1]]=min(zen[now[0]][now[1]],now[2])
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[now[0]][now[1]]=min(kou[now[0]][now[1]],now[2])


ans=10**10
for base in range(1,401):
    na=base*ma ; nb=base*mb 
    if max(na,nb)>400: break
    for i in range(na+1):
        for j in range(nb+1):
            ans=min(ans,zen[i][j]+ kou[na-i][nb-j])


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

Submission status: Accepted

Solution code:
from copy import deepcopy

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

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

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

Submission status: Accepted

Solution code:
# A,Bそれぞれの最大値は40 * 10である
# dp[i][j] = Aがiグラム、Bがjグラム含まれた薬品の最小コスト

import sys
readline = sys.stdin.readline

N,Ma,Mb = map(int,readline().split())
INF = 10 ** 10
dp = [[INF] * (401) for i in range(401)]
dp[0][0] = 0

for _ in range(N):
  a,b,c = map(int,readline().split())
  for i in range(len(dp) - 1, -1, -1):
    for j in range(len(dp[i]) - 1, -1, -1):
      if dp[i][j] == INF:
        continue
      if dp[i + a][j + b] > dp[i][j] + c:
        dp[i + a][j + b] = dp[i][j] + c

ans = INF
import math
for i in range(1, len(dp)):
  for j in range(1, len(dp)):
    g = math.gcd(i,j)
    if i // g == Ma and j // g == Mb:
      if ans > dp[i][j]:
        ans = dp[i][j]
      
if ans == INF:
  print(-1)
else:
  print(ans)

Submission status: Accepted

Solution code:
# A,Bそれぞれの最大値は40 * 10である
# dp[i][j] = Aがiグラム、Bがjグラム含まれた薬品の最小コスト

import sys
readline = sys.stdin.readline

N,Ma,Mb = map(int,readline().split())
INF = 10 ** 10
dp = [[INF] * (401) for i in range(401)]
dp[0][0] = 0

for _ in range(N):
  a,b,c = map(int,readline().split())
  for i in range(len(dp) - 1, -1, -1):
    for j in range(len(dp[i]) - 1, -1, -1):
      if dp[i][j] == INF:
        continue
      dp[i + a][j + b] = min(dp[i + a][j + b], dp[i][j] + c)

ans = INF
import math
for i in range(1, len(dp)):
  for j in range(1, len(dp)):
    g = math.gcd(i,j)
    if i // g == Ma and j // g == Mb:
      if ans > dp[i][j]:
        ans = dp[i][j]
      
if ans == INF:
  print(-1)
else:
  print(ans)

Submission status: Accepted

Solution code:
N, Ma, Mb = map(int, input().split())
L = []
sum_a = 0
sum_b = 0
for _ in range(N):
    a, b, c = map(int, input().split())
    sum_a += a
    sum_b += b
    L.append((a, b, c))

inf = 10 ** 9 + 7
dp = [[[inf] * (sum_b + 1) for _ in range(sum_a + 1)] for _ in range(N + 1)]

dp[0][0][0] = 0
for i in range(N):
    for a in range(sum_a + 1):
        for b in range(sum_b + 1):
            if 0 <= a - L[i][0] and 0 <= b - L[i][1]:
                # print(a, b, L[i])
                dp[i + 1][a][b] = min(dp[i][a - L[i][0]]
                                      [b - L[i][1]] + L[i][2], dp[i][a][b])
            else:
                dp[i + 1][a][b] = dp[i][a][b]

min_cost = inf
for a in range(sum_a + 1):
    for b in range(sum_b + 1):
        if a * Mb == b * Ma and dp[N][a][b] != 0:
            # print(a, b, dp[N][a][b])
            min_cost = min(dp[N][a][b], min_cost)

print(min_cost if min_cost != inf else -1)

Submission status: Accepted

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

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

Submission status: Accepted

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

sum_a = 0
sum_b = 0
A = []
B = []
C = []
for _ in range(N):
    a, b, c = map(int, input().split())
    sum_a += a
    sum_b += b
    A.append(a)
    B.append(b)
    C.append(c)

inf = 10 ** 9 + 7
# 3次元dpを考える
dp = [[[inf] * (sum_b + 1) for _ in range(sum_a + 1)] for _ in range(N + 1)]

dp[0][0][0] = 0
for i in range(N):
    for sa in range(sum_a):
        for sb in range(sum_b):
            if sa - A[i] >= 0 and sb - B[i] >= 0:
                dp[i + 1][sa][sb] = min(dp[i][sa - A[i]]
                                        [sb - B[i]] + C[i], dp[i][sa][sb])
            else:
                dp[i + 1][sa][sb] = min(dp[i][sa][sb], dp[i + 1][sa][sb])

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

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

Submission status: Accepted

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

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

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