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
def main():
    n,ma,mb = map(int, input().split())
    a,b,c = [],[],[]
    for i in range(n):
        ia,ib,ic = map(int, input().split())
        a.append(ia)
        b.append(ib)
        c.append(ic)
    sa = sum(a)
    sb = sum(b)
    INF = 10**5
    dp = np.full((n+1,sa+1,sb+1), INF, dtype=int)
    dp[0][0][0] = 0

    for i in range(n):
        for ca in range(sa):
            for cb in range(sb):
                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])



    res = INF
    for ca in range(1, sa):
        for cb in range(1, sb):
            if ca*mb==cb*ma:
                res = min(res, dp[n][ca][cb])

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n,ma,mb = map(int, input().split())
a,b,c = [],[],[]
for i in range(n):
    ia,ib,ic = map(int, input().split())
    a.append(ia)
    b.append(ib)
    c.append(ic)
sa = sum(a)
sb = sum(b)
INF = 10**5
dp = np.full((n+1,sa+1,sb+1), INF, dtype=int)
dp[0][0][0] = 0

for i in range(n):
    for ca in range(sa):
        for cb in range(sb):
            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])



res = INF
for ca in range(1, sa):
    for cb in range(1, sb):
        if ca*mb==cb*ma:
            res = min(res, dp[n][ca][cb])

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

Submission status: Time Limit Exceeded

Solution code:
def mixing_experiment(N: int, Ma: int, Mb: int, drags: list)->int:
    INF = float('inf')

    max_A = N * max(a for a, _, _ in drags)
    max_B = N * max(b for _, b, _ in drags)

    dp = [[INF] * (max_B+1) for _ in range(max_A+1)]
    dp[0][0] = 0

    for (a, b, c) in drags:
        for ca in range(max_A-a, -1, -1):
            for cb in range(max_B-b, -1, -1):
                dp[ca+a][cb+b] = \
                    min(dp[ca+a][cb+b], dp[ca][cb] + c)
        dp[a][b] = min(dp[a][b], c)

    ans = min(
        dp[ca][cb]
        for ca in range(1, max_A+1)
        for cb in range(1, max_B+1)
        if ca * Mb == cb * Ma)
    # print(dp)

    return ans if 0 < ans and ans < INF else -1


if __name__ == "__main__":
    N = 0
    N, Ma, Mb = map(int, input().split())
    drags = [tuple(int(s) for s in input().split()) for _ in range(N)]
    ans = mixing_experiment(N, Ma, Mb, drags)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,Ma,Mb=list(map(int, input().strip().split()))
abc=[list(map(int, input().strip().split())) for i in range(N)]
maxW=N*10
dp=[[[float("inf")]*(maxW+1) for _ in range(maxW+1)]for __ in range(N+1)]
dp[0][0][0]=0
for i in range(N):
    a,b,c=abc[i]
    for ai  in range(maxW):
        for bi in range(maxW):
            if dp[i][ai][bi]==float("inf"):continue
            dp[i+1][ai][bi] = min(dp[i+1][ai][bi],dp[i][ai][bi])
            dp[i+1][ai+a][bi+b] = min(dp[i][ai][bi]+c,dp[i+1][ai+a][bi+b])
cost=float("inf")
Mad=Ma
Mbd=Mb
while Ma<=maxW and Mb <=maxW:
    cost=min(dp[-1][Ma][Mb],cost)
    Ma+=Mad
    Mb+=Mbd
print(-1 if cost==float("inf") else cost)

Submission status: Time Limit Exceeded

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

c = n * 100 + 1

dp = [[[99999 for _ in range(1001)] for _ in range(1001)] for _ in range(n + 1)]

ans = 99999
dp[0][0][0] = 0
for i in range(n):
    for j in range(1001):
        for k in range(1001):
            if dp[i][j][k] < 9999:
                dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k])
                dp[i+1][j+abc[i][0]][k+abc[i][1]] = min(dp[i+1][j+abc[i][0]][k+abc[i][1]], dp[i][j][k] + abc[i][2])
                if (j+abc[i][0]) * mb == (k+abc[i][1]) * ma:
                    ans = min(ans, dp[i+1][j+abc[i][0]][k+abc[i][1]])

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

Submission status: Time Limit Exceeded

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

c = n * 100 + 1

dp = [[[] for _ in range(c)] for _ in range(n + 1)]

dp[0][0].append([0,0])

ans = 999999

for i in range(n):
    for j in range(c):
        for a, b in dp[i][j]:
            dp[i+1][j].append([a, b])
            if (abc[i][0] + a) * mb == (abc[i][1] + b) * ma:
                ans = min(ans, abc[i][2] + j)
            dp[i+1][j + abc[i][2]].append([abc[i][0] + a, abc[i][1] + b])

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

Submission status: Time Limit Exceeded

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

m = ma, mb

ans = set()

def res(a, b, c, i):
    if i == n:
        return
    if (a + abc[i][0]) * mb == (b + abc[i][1]) * ma:
        #print(a,b,c,i, abc[i])
        ans.add(c + abc[i][2])
    res(a + abc[i][0], b + abc[i][1], c + abc[i][2], i + 1)
    res(a, b, c, i + 1)

res(0,0,0,0)

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

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

MAX = 40001


def main():
    N, Ma, Mb = [int(a) for a in input().split()]

    abc = [
        [int(a) for a in input().split()]
        for _ in range(N)
    ]

    dp = [
        [
            [MAX for b in range(10 * N + 1)]
            for a in range(10 * N + 1)
        ]
        for i in range(N)
    ]  # dp[i][a][b] = i番目までを使ってaグラムbグラムを作る最小コスト

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

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

    res = MAX
    l = dp[-1]
    for a in range(1,10 * N+1):
        aa = a * Mb
        if aa % Ma != 0:
            continue
        b = aa // Ma
        if b >= len(l[a]):
            continue
        c = l[a][b]
        res = min(res, c)

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


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

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

dp[0][0][0]=0
for i in range(N):
    for ca in range(361):
        for cb in range(361):
            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 = 10**18
for ca in range(401):
    for cb in range(401):
        if not ca*Mb == cb*Ma or ca == 0:
            continue
        if ans > dp[N][ca][cb]:
            ans = dp[N][ca][cb]
        
if ans == 10**18:
    print(-1)
else:
    print(ans)

Submission status: Time Limit Exceeded

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

dp = [[10**18 for _ in range(401)] for _ in range(401)]

dp[0][0]=0
for i in range(N):
    a,b,c=map(int,input().split())
    for ca in range(361, -1, -1):
        for cb in range(361, -1, -1):
            dp[ca+a][cb+b] = min(dp[ca+a][cb+b],dp[ca][cb]+c)

ans = 10**18
for i in range(1, 40):
    if ans > dp[Ma * i][Mb * i]:
        ans = dp[Ma * i][Mb * i]
        
if ans == 10**18:
    print(-1)
else:
    print(ans)

Submission status: Time Limit Exceeded

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

dp = [[10**18 for _ in range(401)] for _ in range(401)]

dp[0][0]=0
for i in range(N):
    a,b,c=map(int,input().split())
    for ca in range(361, -1, -1):
        for cb in range(361, -1, -1):
            dp[ca+a][cb+b] = min(dp[ca+a][cb+b],dp[ca][cb]+c)

ans = 10**18
for ca in range(401):
    for cb in range(401):
        if not ca*Mb == cb*Ma or ca == 0:
            continue
        if ans > dp[ca][cb]:
            ans = dp[ca][cb]
        
if ans == 10**18:
    print(-1)
else:
    print(ans)

Submission status: Time Limit Exceeded

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

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

dp = [[10**18 for _ in range(401)] for _ in range(401)]

dp[0][0]=0
for i in range(N):
    for ca in range(361, -1, -1):
        for cb in range(361, -1, -1):
            dp[ca+A[i]][cb+B[i]] = min(dp[ca+A[i]][cb+B[i]],dp[ca][cb]+C[i])

ans = 10**18
for ca in range(401):
    for cb in range(401):
        if not ca*Mb == cb*Ma or ca == 0:
            continue
        if ans > dp[ca][cb]:
            ans = dp[ca][cb]
        
if ans == 10**18:
    print(-1)
else:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

#A

#B

#C
"""
n,m = LI()
v = LIR(m)
p2 = [1]
for i in range(n):
    p2.append(p2[-1]*2)
dp = [[0 for i in range(p2[n])] for j in range(n)]
dp[0][1] = 1
for i in range(1,p2[n]):
    for k in range(n):
        for a,b in v:
            a -= 1
            b -= 1
            if k==a or k == b:
                c = p2[a]
                d = p2[b]
                if i&c and not i&d:
                    dp[b][i|d] += dp[k][i]
                if (not i&c) and i&d:
                    dp[a][i|c] += dp[k][i]
ans = 0
for i in range(1,n):
    ans += dp[i][-1]
print(ans)
"""

#D
n,p,q = LI()
v = [None for j in range(n)]
sa = 0
sb = 0
for i in range(n):
    a,b,c = LI()
    v[i] = [a,b,c]
    sa += a
    sb += b
dp = [[[float("inf") for i in range(sb+1)] for j in range(sa+1)] for k in range(n+1)]
dp[0][0][0] = 0
for i in range(n):
    for a in range(sa):
        for b in range(sb):
            ai,bi,ci = v[i]
            if a + ai <= sa and b + bi <= sb:
                dp[i+1][a+ai][b+bi] = min(dp[i+1][a+ai][b+bi],dp[i][a][b]+ci,dp[i][a+ai][b+bi])
            dp[i+1][a][b] = min(dp[i+1][a][b],dp[i][a][b])
li = [[p,q]]
while li[-1][0] <= sa and li[-1][1] <= sb:
    li.append([li[-1][0]+p,li[-1][1]+q])
li.pop(-1)
ans = float("inf")
for a,b in li:
    ans = min(ans,dp[n][a][b])
if ans == float("inf"):
    print(-1)
else:
    print(ans)
#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T

Submission status: Time Limit Exceeded

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

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

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

dp[0][0][0]=0
for i in range(N):
    for ca in range(361):
        for cb in range(361):
            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 = 10**18
for ca in range(1,401):
    for cb in range(1,401):
        if not ca*Mb == cb*Ma:
            continue
        ans = min(ans,dp[N][ca][cb])
        
if ans == 10**18:
    print(-1)
else:
    print(ans)

Submission status: Time Limit Exceeded

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

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

def f(i,j):
    global A
    global B
    global C
    if i % 2 == 1:
        A += n[j-1][0]
        B += n[j-1][1]
        C += n[j-1][2]
    if not i // 2 == 0:
        i //= 2
        j -= 1
        f(i,j)

ans = 10**18
for i in range(2**N):
    A,B,C = 0,0,0
    f(i,N)
    if A != 0:
        if A*Mb == B*Ma:
            if C < ans:
                ans = C
if ans == 10**18:
    print(-1)
else:
    print(ans)

Submission status: Time Limit Exceeded

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

from copy import deepcopy

ABMAX = 400
HUGE = 10 ** 12

def main():
    n, ma, mb = map(int, input().split())
    sold = []
    for i in range(n):
        a, b, c = map(int, input().split())
        sold.append((a, b, c))
    mincost = dp(sold, n)
    print(find_mincost(mincost, ma, mb))

def find_mincost(mincost, ma, mb):
    res = HUGE
    for mult in range(1, ABMAX):
        if ma * mult > ABMAX:
            break
        if mb * mult > ABMAX:
            break
        res = min(res, mincost[ma * mult][mb * mult])
    return -1 if res == HUGE else res

def dp(sold, n):
    # mincost[a][b]: Minimum cost of achieving a, b
    mincost = [[HUGE for b in range(ABMAX + 1)] for a in range(ABMAX + 1)]
    mincost[0][0] = 0
    for a, b, c in sold:
        mincost = update(mincost, a, b, c)
    return mincost

def update(mincost, a, b, c):
    for ia in reversed(range(ABMAX + 1)):  # 配る
        for ib in reversed(range(ABMAX + 1)):
            na = ia + a
            nb = ib + b
            if na <= ABMAX and nb <= ABMAX:
                mincost[na][nb] = min(mincost[na][nb], mincost[ia][ib] + c)
    return mincost

main()

Submission status: Time Limit Exceeded

Solution code:
class abc054D:
    def __init__(self, n, a, b, thing):
        self.n = n
        self.a = a
        self.b = b
        self.thing = thing
        _sumA = 0
        _sumB = 0
        for i in range(len(thing)):
            _sumA += thing[i][0]
            _sumB += thing[i][1]
        self.table = [[[float("inf") for _ in range(_sumB)] for _ in range(_sumA)] for _ in range(n+1)]

    def func(self):
        self.table[0][0][0] = 0
        for i in range(1, len(self.table)):
            for sumA in range(len(self.table[0])):
                for sumB in range(len(self.table[0][0])):
                    if sumA - self.thing[i-1][0] >= 0 and sumB - self.thing[i-1][1] >= 0:
                        self.table[i][sumA][sumB] \
                            = min(self.table[i - 1][sumA][sumB],
                                  self.table[i - 1][sumA - self.thing[i-1][0]][sumB - self.thing[i-1][1]]
                                  + self.thing[i-1][2])
                    else:
                        self.table[i][sumA][sumB] = self.table[i-1][sumA][sumB]
        _min = float("inf")
        for sumA in range(1, len(self.table[0])):
            for sumB in range(1, len(self.table[0][0])):
                if sumA / sumB == self.a / self.b:
                    _min = min(_min, self.table[-1][sumA][sumB])
        if _min == float("inf"):
            return str(-1)
        else:
            return str(_min)

n, a, b = [int(i) for i in input().split()]
thing = [[0, 0, 0] for _ in range(n)]
for i in range(n):
    thing[i] = [int(i) for i in input().split()]
print(abc054D(n, a, b, thing).func())
"""
# print("print(\" vs \" + func({}, {}, {}, {}))".format(n, m, a, thing))
print("3  vs " + abc054D(3, 1, 1, [[1, 2, 1], [2, 1, 2], [3, 3, 10]]).func())
print("-1 vs " + abc054D(1, 1, 10, [[10, 10, 10]]).func())
"""

Submission status: Time Limit Exceeded

Solution code:
class abc054D:
    def __init__(self, n, a, b, thing):
        self.n = n
        self.a = a
        self.b = b
        self.thing = thing
        self.table = [[[float("inf") for _ in range(400)] for _ in range(400)] for _ in range(n+1)]

    def func(self):
        """
        for sumA in range(len(self.table[0])):
            for sumB in range(len(self.table[0][0])):
                if sumA > 0 and sumB > 0 and sumA / sumB == self.a / self.b:
                    self.table[0][sumA][sumB] = 0
                elif sumA == 0 and sumB == 0 and self.a == self.b:
                    self.table[0][sumA][sumB] = 0
        """
        self.table[0][0][0] = 0
        for i in range(1, len(self.table)):
            for sumA in range(len(self.table[0])):
                for sumB in range(len(self.table[0][0])):
                    # print("i;{} sumA:{} sumB:{}".format(i, sumA, sumB))
                    """
                    if sumA - self.thing[i-1][0] >= 0 and sumB - self.thing[i-1][1] >= 0:
                        if self.table[i - 1][sumA][sumB] == 0:
                            self.table[i][sumA][sumB] \
                                = self.table[i - 1][sumA - self.thing[i - 1][0]][sumB - self.thing[i - 1][1]]\
                                  + self.thing[i - 1][2]
                        else:
                            self.table[i][sumA][sumB] \
                                = min(self.table[i - 1][sumA][sumB],
                                      self.table[i - 1][sumA - self.thing[i-1][0]][sumB - self.thing[i-1][1]]
                                      + self.thing[i-1][2])
                    else:
                        self.table[i][sumA][sumB] = self.table[i - 1][sumA][sumB]
                    """
                    if sumA - self.thing[i-1][0] >= 0 and sumB - self.thing[i-1][1] >= 0:
                        self.table[i][sumA][sumB] \
                            = min(self.table[i - 1][sumA][sumB],
                                  self.table[i - 1][sumA - self.thing[i-1][0]][sumB - self.thing[i-1][1]]
                                  + self.thing[i-1][2])
                    else:
                        self.table[i][sumA][sumB] = self.table[i-1][sumA][sumB]

        """
        for i in range(len(self.table)):
            for sumA in range(20):#len(self.table[0])):
                for sumB in range(20):#len(self.table[0][0])):
                    if self.table[i][sumA][sumB] != float("inf"):
                        print("table[{}][{}][{}]:{}".format(i, sumA, sumB, self.table[i][sumA][sumB]))
        """
        _min = float("inf")
        for sumA in range(1, len(self.table[0])):
            for sumB in range(1, len(self.table[0][0])):
                if sumA / sumB == self.a / self.b:
                    _min = min(_min, self.table[-1][sumA][sumB])
        if _min == float("inf"):
            return str(-1)
        else:
            return str(_min)

n, a, b = [int(i) for i in input().split()]
thing = [[0, 0, 0] for _ in range(n)]
for i in range(n):
    thing[i] = [int(i) for i in input().split()]
print(abc054D(n, a, b, thing).func())
# print("print(\" vs \" + func({}, {}, {}, {}))".format(n, m, a, thing))
# print("3  vs " + abc054D(3, 1, 1, [[1, 2, 1], [2, 1, 2], [3, 3, 10]]).func())
# print("-1 vs " + abc054D(1, 1, 10, [[10, 10, 10]]).func())

Submission status: Time Limit Exceeded

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

main()

Submission status: Time Limit Exceeded

Solution code:
def inpl(): return list(map(int, input().split()))


N, Ma, Mb = inpl()
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] = inpl()

sumA = sum(A)
sumB = sum(B)
sumC = sum(C)
init = sum(C)*2
dp = [[[init for _ in range(sumB+1)] for _ in range(sumA+1)] for _ in range(N+1)]
dp[0][0][0] = 0
for i in range(N):
    for j in range(sumA+1):
        for k in range(sumB+1):
            dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k])
            if j+A[i]<=sumA and k+B[i]<=sumB:
                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])

a = Ma
b = Mb
ans = init
while a <= sumA and b <= sumB:
    ans = min(ans, dp[N][a][b])
    a+=Ma
    b+=Mb

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

Submission status: Time Limit Exceeded

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

import sys
def input(): return sys.stdin.readline().strip()
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, Ma, Mb = map(int, input().split())
l = [[0] * 3 for i in range(N)]
for i in range(N):
    l[i][0], l[i][1], l[i][2] = map(int, input().split())

memo = [[[INF] * 401  for i in range(401)] for i in range(N+1)]
def dfs(cur, a, b, c):
    if memo[cur][a][b] < c:
        return memo[cur][a][b]
    mn = 10000
    if b != 0 and a / b == Ma / Mb:
        mn = min(c, mn)
    if cur == N:
        return mn
    mn = min(dfs(cur+1, a+l[cur][0], b+l[cur][1], c+l[cur][2]), mn)
    mn = min(dfs(cur+1, a, b, c), mn)
    memo[cur][a][b] = mn
    return mn

ans = dfs(0, 0, 0, 0)
if ans == 10000:
    print(-1)
else:
    print(ans)

Submission status: Time Limit Exceeded

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

import sys
def input(): return sys.stdin.readline().strip()
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, Ma, Mb = map(int, input().split())
l = [[0] * 3 for i in range(N)]
for i in range(N):
    l[i][0], l[i][1], l[i][2] = map(int, input().split())

def dfs(cur, a, b, c):
    mn = INF
    if b != 0 and a / b == Ma / Mb:
        mn = min(c, mn)
    if cur == N:
        return mn
    mn = min(dfs(cur+1, a+l[cur][0], b+l[cur][1], c+l[cur][2]), mn)
    mn = min(dfs(cur+1, a, b, c), mn)
    return mn

ans = dfs(0, 0, 0, 0)
if ans == INF:
    print(-1)
else:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, Ma, Mb = map(int, input().split())
items = []
ap = items.append
a_sum =0
b_sum = 0
for i in range(N):
    a, b, c = map(int, input().split())
    ap((a, b, c))
    a_sum += a
    b_sum += b

inf = float("inf")
dp = [[[inf for _ in range(b_sum + 10)] for _ in range(a_sum + 10)] for i in range(N + 1)]
dp[0][0][0] = 0
for i in range(N):
    for a in range(a_sum):
        for b in range(b_sum):
            dp[i + 1][a][b] = min(dp[i + 1][a][b], dp[i][a][b])
            dp[i + 1][a + items[i][0]][b + items[i][1]] = min(dp[i + 1][a + items[i][0]][b + items[i][1]],
                                                              dp[i][a][b] + items[i][2])

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

Submission status: Time Limit Exceeded

Solution code:
N, Ma, Mb = map(int, input().split())
items = []
ap = items.append
a_sum =0
b_sum = 0
for i in range(N):
    a, b, c = map(int, input().split())
    ap((a, b, c))
    a_sum += a
    b_sum += b

inf = float("inf")
dp = [[[inf for _ in range(b_sum + 10)] for _ in range(a_sum + 10)] for i in range(N + 1)]
dp[0][0][0] = 0
for i in range(N):
    for a in range(a_sum):
        for b in range(b_sum):
            if dp[i + 1][a][b] > dp[i][a][b]:
                dp[i + 1][a][b] = dp[i][a][b]
            if dp[i + 1][a + items[i][0]][b + items[i][1]] > dp[i][a][b] + items[i][2]:
                dp[i + 1][a + items[i][0]][b + items[i][1]] = dp[i][a][b] + items[i][2]

ans = inf
for a in range(1, a_sum + 1):
    for b in range(1, b_sum + 1):
        if a * Mb == b * Ma:
            if ans > dp[N][a][b]:
                ans = dp[N][a][b]
if ans != inf:
    print(ans)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
N, Ma, Mb = map(int, input().split())
items = []
ap = items.append
a_sum =0
b_sum = 0
for i in range(N):
    a, b, c = map(int, input().split())
    ap((a, b, c))
    a_sum += a
    b_sum += b

inf = float("inf")
dp = [[[inf for _ in range(b_sum + 10)] for _ in range(a_sum + 10)] for i in range(N + 1)]
dp[0][0][0] = 0
for i in range(N):
    for a in range(a_sum):
        for b in range(b_sum):
            if dp[i + 1][a][b] > dp[i][a][b]:
                dp[i + 1][a][b] = dp[i][a][b]
            if dp[i + 1][a + items[i][0]][b + items[i][1]] > dp[i][a][b] + items[i][2]:
                dp[i + 1][a + items[i][0]][b + items[i][1]] = dp[i][a][b] + items[i][2]

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

Submission status: Time Limit Exceeded

Solution code:
N, Ma, Mb = map(int, input().split())
items = []
ap = items.append
a_sum =0 
b_sum = 0
for i in range(N):
    a, b, c = map(int, input().split())
    ap((a, b, c))
    a_sum += a
    b_sum += b

inf = float("inf")
dp = [[[inf for _ in range(b_sum + 11)] for _ in range(a_sum + 11)] for i in range(N + 1)]
dp[0][0][0] = 0
for i in range(N):
    for a in range(a_sum):
        for b in range(b_sum):
            if dp[i + 1][a][b] > dp[i][a][b]:
                dp[i + 1][a][b] = dp[i][a][b]
            if dp[i + 1][a + items[i][0]][b + items[i][1]] > dp[i][a][b] + items[i][2]:
                dp[i + 1][a + items[i][0]][b + items[i][1]] = dp[i][a][b] + items[i][2]

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

Submission status: Time Limit Exceeded

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

a_sum = sum([item[0] for item in items])
b_sum = sum([item[1] for item in items])
inf = float("inf")
dp = [[[inf for _ in range(b_sum + 11)] for _ in range(a_sum + 11)] for i in range(N + 1)]
dp[0][0][0] = 0
for i in range(N):
    for a in range(a_sum):
        for b in range(b_sum):
            if dp[i + 1][a][b] > dp[i][a][b]:
                dp[i + 1][a][b] = dp[i][a][b]
            if dp[i + 1][a + items[i][0]][b + items[i][1]] > dp[i][a][b] + items[i][2]:
                dp[i + 1][a + items[i][0]][b + items[i][1]] = dp[i][a][b] + items[i][2]

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

Submission status: Time Limit Exceeded

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

inf = float("inf")
dp = [[[inf for _ in range(411)] for _ in range(411)] for i in range(N+1)]
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])
            dp[i + 1][a + items[i][0]][b + items[i][1]] = min(dp[i + 1][a + items[i][0]][b + items[i][1]],
                                                              dp[i][a][b] + items[i][2])
ans = inf
for a in range(1, 401):
    for b in range(1, 401):
        if a * Mb == b * Ma:
            ans = min(ans, dp[N][a][b])
if ans != inf:
    print(ans)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

a_sum = sum([item[0] for item in items])
b_sum = sum([item[1] for item in items])
inf = float("inf")
dp = [[[inf for _ in range(a_sum + 100)] for _ in range(b_sum + 100)] for i in range(N+1)]
dp[0][0][0] = 0

for i in range(N):
    for a in range(a_sum + 1):
        for b in range(b_sum + 1):
            dp[i + 1][a][b] = min(dp[i + 1][a][b], dp[i][a][b])
            dp[i + 1][a + items[i][0]][b + items[i][1]] = min(dp[i + 1][a + items[i][0]][b + items[i][1]],
                                                              dp[i][a][b] + items[i][2])
ans = inf
for a in range(1, a_sum + 1):
    for b in range(1, b_sum + 1):
        if a * Mb == b * Ma:
            ans = min(ans, dp[N][a][b])
if ans != inf:
    print(ans)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
I=lambda:list(map(int,input().split()))
n,Ma,Mb=I()
m=[[0,0,0]]+[I()for _ in range(n)]
dp=[[[10**18]*(10*n+1)for _ in range(10*n+1)]for _ in range(n+1)]
dp[0][0][0]=0
for i in range(1,n+1):
    for j in range(10*n+1):
        if j-m[i][0]<0:
            dp[i][j]=dp[i-1][j]
            continue
        for k in range(10*n+1):
            if k-m[i][1]<0:
                dp[i][j][k]=dp[i-1][j][k]
            else:
                dp[i][j][k]=min(dp[i-1][j-m[i][0]][k-m[i][1]]+m[i][2],dp[i-1][j][k])
ans=10**18
for i in range(1,10*n+1):
    j=Mb*i/Ma
    if j==int(j) and j<10*n+2:
        ans=min(ans,dp[-1][i][int(j)])
print(-(ans==10**18)or ans)