Problem:


Score : 400 points

Problem Statement

Dolphin is planning to generate a small amount of a certain chemical substance C.

In order to generate the substance C, he must prepare a solution which is a mixture of two substances A and B in the ratio of M_a:M_b.

He does not have any stock of chemicals, however, so he will purchase some chemicals at a local pharmacy.

The pharmacy sells N kinds of chemicals. For each kind of chemical, there is exactly one package of that chemical in stock.

The package of chemical i contains a_i grams of the substance A and b_i grams of the substance B, and is sold for c_i yen (the currency of Japan).

Dolphin will purchase some of these packages. For some reason, he must use all contents of the purchased packages to generate the substance C.

Find the minimum amount of money required to generate the substance C.

If it is not possible to generate the substance C by purchasing any combination of packages at the pharmacy, report that fact.  

Constraints

1≦N≦40 

1≦a_i,b_i≦10 

1≦c_i≦100 

1≦M_a,M_b≦10 

gcd(M_a,M_b)=1

a_i, b_i, c_i, M_a and M_b are integers.

Input

The input is given from Standard Input in the following format:

N M_a M_b  
a_1 b_1 c_1  
a_2 b_2 c_2
:  
a_N b_N c_N  

Output

Print the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C, print -1 instead.

Sample Input 1

3 1 1
1 2 1
2 1 2
3 3 10

Sample Output 1

3

The amount of money spent will be minimized by purchasing the packages of chemicals 1 and 2.

In this case, the mixture of the purchased chemicals will contain 3 grams of the substance A and 3 grams of the substance B, which are in the desired ratio: 3:3=1:1.

The total price of these packages is 3 yen.  

Sample Input 2

1 1 10
10 10 10

Sample Output 2

-1

The ratio 1:10 of the two substances A and B cannot be satisfied by purchasing any combination of the packages. Thus, the output should be -1.



Sample input:
3 1 1
1 2 1
2 1 2
3 3 10


Sample output:
3

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
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)]

for i in range(n+1):
    dp[i][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])
            else:
                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: Wrong Answer

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)] for l in range(n+1)]

for i in range(n+1):
    dp[i][0][0][0] = 0
for i in range(n):
    for j in range(n)[::-1]:
        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][j+1][a+ai][b+bi] = min(dp[i+1][j+1][a+ai][b+bi],dp[i][j][a][b]+ci,dp[i][j+1][a+ai][b+bi])
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 i in range(1,n+1):
    for a,b in li:
        ans = min(ans,dp[n][i][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: Wrong Answer

Solution code:
L = [[float("inf") for i in range(401)] for j in range(401)]
L[0][0] = 0

ans = float("inf")

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

for x in range(N):
	a, b, c = list(map(int, input().split()))
	for i in range(401):
		for j in range(401):
			if L[-1 - i][-1 - j] != float("inf"):
				if L[-1 - i][-1 - j] + c < L[-1 - i + a][-1 - j + b]:
					L[-1 - i + a][-1 - j + b] = L[-1 - i][-1 - j] + c


for i in range(401):
	for j in range(401):
		if L[i][j] != 0 and j * Mb == i * Ma and L[i][j]!= float("inf"):
			if L[i][j] <= ans:
				ans = L[i][j]

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

Submission status: Wrong Answer

Solution code:
inf = 9999999999
N, Ma, Mb = map(int, input().split())
d = [{} for i in range(N + 1)]
for i in range(N):
    a, b, c = map(int, input().split())
    d[i + 1][a, b] = min(c, d[i].get((a, b), inf))
    for (j, k) in d[i]:
        d[i + 1][j, k] = d[i][j, k]
    for (j, k) in d[i]:
        d[i + 1][j + a, k + b] = min(d[i][j, k] + c, d[i].get((j + a, k + b), inf))
ans = inf
for (j, k) in d[N]:
    if j * Mb == k * Ma:
        ans = min(ans, d[N][j, k])
if ans == inf:
    ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
inf = 9999999999
N, Ma, Mb = map(int, input().split())
d = [{} for i in range(N + 1)]
for i in range(N):
    a, b, c = map(int, input().split())
    d[i + 1][a, b] = min(c, d[i].get((a, b), inf))
    for (j, k) in d[i]:
        d[i + 1][j, k] = d[i][j, k]
        d[i + 1][j + a, k + b] = min(d[i][j, k] + c, d[i].get((j + a, k + b), inf))
ans = inf
for (j, k) in d[N]:
    if j * Mb == k * Ma:
        ans = min(ans, d[N][j, k])
if ans == inf:
    ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
inf = 9999999999
N, Ma, Mb = map(int, input().split())
d = [{} for i in range(N + 1)]
for i in range(N):
    a, b, c = map(int, input().split())
    d[i + 1][a, b] = min(c, d[i].get((a, b), inf))
    for (j, k) in d[i]:
        d[i + 1][j + a, k + b] = min(d[i][j, k] + c, d[i].get((j + a, k + b), inf))
ans = inf
for i in range(1, N + 1):
    for (j, k) in d[i]:
        if j * Mb == k * Ma:
            ans = min(ans, d[i][j, k])
if ans == inf:
    ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
inf = 9999999999
N, Ma, Mb = map(int, input().split())
d = [{} for i in range(N + 1)]
for i in range(N):
    a, b, c = map(int, input().split())
    d[i][0, 0] = 0
    for (j, k) in d[i]:
        d[i + 1][j + a, k + b] = min(d[i][j, k] + c, d[i].get((j + a, k + b), inf))
ans = inf
for i in range(1, N + 1):
    for (j, k) in d[i]:
        if (j, k) == (0, 0):
            continue
        if j * Mb == k * Ma:
            ans = min(ans, d[i][j, k])
if ans == inf:
    ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
inf = 9999999999
N, Ma, Mb = map(int, input().split())
d = [{} for i in range(N + 1)]
for i in range(N):
    a, b, c = map(int, input().split())
    d[i][0, 0] = 0
    for (j, k) in d[i]:
        d[i + 1][j + a, k + b] = min(d[i][j, k] + c, d[i + 1].get((j + a, k + b), inf))
ans = inf
for i in range(1, N + 1):
    for (j, k) in d[i]:
        if (j, k) == (0, 0):
            continue
        if j * Mb == k * Ma:
            ans = min(ans, d[i][j, k])
if ans == inf:
    ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = map(int, input().split())
d = [{} for i in range(N + 1)]
for i in range(N):
    a, b, c = map(int, input().split())
    d[i][0, 0] = 0
    for (j, k) in d[i]:
        d[i + 1][j + a, k + b] = min(d[i][j, k] + c, d[i + 1].get((j + a, k + b), 999999999))
ans = 9999999999
for i in range(1, N + 1):
    for (j, k) in d[i]:
        if (j, k) == (0, 0):
            continue
        if j * Mb == k * Ma:
            ans = min(ans, d[i][j, k])
if ans == 9999999999:
    ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
from math import isinf
M_ab=400
M_n=40
INF=float('inf')
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)
DP=[[[INF]*(M_ab+1) for _ in range(M_ab+1)] for _ in range(M_n+1)]
DP[0][0][0]=0
for i in range(n):
    for ca in range(M_ab+1):
        for cb in range(M_ab+1):
            if isinf(DP[i][ca][cb]):
                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+A[i]],DP[i][ca][cb]+C[i])
ans=INF
for ca in range(1,M_ab+1):
    for cb in range(1,M_ab+1):
        if ca*Mb==cb*Ma:
            ans=min(ans,DP[n][ca][cb])
if isinf(ans):
    ans=-1
print(ans)

Submission status: Wrong Answer

Solution code:
M_ab=400
M_n=40
INF=float('inf')
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)
DP=[[[INF]*(M_ab+1) for _ in range(M_ab+1)] for _ in range(M_n+1)]
DP[0][0][0]=0
for i in range(n):
    for ca in range(M_ab+1):
        for cb in range(M_ab+1):
            if DP[i][ca][cb]==float('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+A[i]],DP[i][ca][cb]+C[i])
ans=INF
for ca in range(1,M_ab+1):
    for cb in range(1,M_ab+1):
        if ca*Mb==cb*Ma:
            ans=min(ans,DP[n][ca][cb])
if ans==float('inf'):
    ans=-1
print(ans)

Submission status: Wrong Answer

Solution code:
M_ab=400
M_n=40
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)
DP=[[[float('inf')]*(M_ab+1) for _ in range(M_ab+1)] for _ in range(M_n+1)]
DP[0][0][0]=0
for i in range(n):
    for ca in range(M_ab+1):
        for cb in range(M_ab+1):
            if DP[i][ca][cb]==float('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+A[i]],DP[i][ca][cb]+C[i])
ans=float('inf')
for ca in range(1,M_ab+1):
    for cb in range(1,M_ab+1):
        if ca*Mb==cb*Ma:
            ans=min(ans,DP[n][ca][cb])
if ans==float('inf'):
    ans=-1
print(ans)

Submission status: Wrong Answer

Solution code:
M_ab=400
M_n=40
inf=float('inf')
DP=[[[inf]*(M_ab+1) for _ in range(M_ab+1)] for _ in range(M_n+1)]
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)
DP[0][0][0] = 0
for i in range(n):
    for ca in range(M_ab+1):
        for cb in range(M_ab+1):
            if DP[i][ca][cb]==inf:
                continue
            DP[i+1][ca][cb]=min(DP[i+1][ca][cb],DP[i][ca][cb])
            DP[i+1][ca+A[i]][cb+B[i]]=min(DP[i+1][ca+A[i]][cb+A[i]],DP[i][ca][cb]+C[i])
ans=inf
for ca in range(1,M_ab+1):
    for cb in range(1,M_ab+1):
        if ca*Mb==cb*Ma:
            ans=min(ans,DP[n][ca][cb])
if ans==inf:
    ans=-1
print(ans)

Submission status: Wrong Answer

Solution code:
from math import isinf

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

W_MAX = 40*10
dp = [[float('inf')]*(W_MAX+1) for _ in range(W_MAX+1)]
dp[0][0] = 0

for i in range(N):
    for wa in range(W_MAX+1):
        for wb in range(W_MAX+1):
            if isinf(dp[wa][wb]): continue
            if wa+a[i]<=W_MAX and wb+b[i]<=W_MAX:
                dp[wa+a[i]][wb+b[i]] = min(dp[wa+a[i]][wb+b[i]],
                                           dp[wa][wb]+c[i])

ans = float('inf')
for wa in range(1, W_MAX+1):
    for wb in range(1, W_MAX+1):
        if wa*Mb == wb*Ma:
            ans = min(ans, dp[wa][wb])

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

Submission status: Wrong Answer

Solution code:
from math import isinf

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

W_MAX = 40*10
dp = [[float('inf')]*(W_MAX+1) for _ in range(W_MAX+1)]
dp[0][0] = 0

for i in range(N):
    for wa in range(W_MAX):
        for wb in range(W_MAX):
            if isinf(dp[wa][wb]): continue
            if wa+a[i]<=W_MAX and wb+b[i]<=W_MAX:
                dp[wa+a[i]][wb+b[i]] = min(dp[wa+a[i]][wb+b[i]],
                                           dp[wa][wb]+c[i])
ans = float('inf')
for wa in range(1, W_MAX+1):
    for wb in range(1, W_MAX+1):
        if wa*Mb == wb*Ma:
            ans = min(ans, dp[wa][wb])

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

Submission status: Wrong Answer

Solution code:
from math import isinf

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

W_MAX = 40*10
dp = [[float('inf')]*(W_MAX+1) for _ in range(W_MAX+1)]
dp[0][0] = 0

for i in range(N):
    for wa in range(W_MAX):
        for wb in range(W_MAX):
            if isinf(dp[wa][wb]): continue
            if wa+a[i]<=W_MAX and wb+b[i]<=W_MAX:
                dp[wa+a[i]][wb+b[i]] = min(dp[wa+a[i]][wb+b[i]],
                                           dp[wa][wb]+c[i])
ans = float('inf')
for wa in range(1, W_MAX+1):
    for wb in range(1, W_MAX+1):
        if wa*Mb == wb*Ma:
            ans = min(ans, dp[wa][wb])
print(ans)

Submission status: Wrong Answer

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):
    for mult in range(1, ABMAX):
        if ma * mult > ABMAX:
            break
        if mb * mult > ABMAX:
            break
        if mincost[ma * mult][mb * mult] < HUGE:
            return mincost[ma * mult][mb * mult]
    return -1

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(prevcost, a, b, c):
    mincost = deepcopy(prevcost)
    for ia in range(ABMAX + 1):  # 配る
        for ib in range(ABMAX + 1):
            na = ia + a
            nb = ib + b
            if na <= ABMAX and nb <= ABMAX:
                mincost[na][nb] = min(mincost[na][nb], prevcost[ia][ib] + c)
    return mincost

main()

Submission status: Wrong Answer

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) - 1):
            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")
        i = -1
        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:
                    if sumA - self.thing[i - 1][0] >= 0 and sumB - self.thing[i - 1][1] >= 0:
                        _min = min(_min, 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:
                        _min = min(_min, self.table[i - 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: Wrong Answer

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) - 1):
            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")
        i = -1
        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:
                    if sumA - self.thing[i - 1][0] >= 0 and sumB - self.thing[i - 1][1] >= 0:
                        _min = min(_min, 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 _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: Wrong Answer

Solution code:
# coding:utf-8

import sys
# from collections import Counter, defaultdict

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


n, ma, mb = LI()
abc = [LI() for _ in range(n)]
ab_max = n * 10 + 1

dp = [[[INF] * ab_max for _ in range(ab_max)] for _ in range(n + 1)]
dp[0][0][0] = 0


for i in range(n):
    ai, bi, ci = abc[i]
    for a in range(ab_max - ai):
        for b in range(ab_max - bi):
            if dp[i][a][b] == INF:
                continue
            dp[i + 1][a][b] = min(dp[i + 1][a][b], dp[i][a][b])
            dp[i + 1][a + ai][b + bi] = min(dp[i][a + ai][b + bi], dp[i][a][b] + ci)


ans = INF
c = 1
while True:
    a, b = c * ma, c * mb
    if a >= ab_max or b >= ab_max or c > n:
        break
    ans = min(ans, dp[c][a][b])
    c += 1

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

Submission status: Wrong Answer

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

"""
参考：https://img.atcoder.jp/abc054/editorial.pdf
・3次元DP
・送るDP
"""
 
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())

# 不可能な組み合わせは0とは区別するのでINF初期化
dp = [[[INF] * 401  for i in range(401)] for i in range(N+1)]
dp[0][0][0] = 0
for i in range(N):
    for j in range(401):
        for k in range(401):
            # あり得ない組み合わせの時は送らない
            if dp[i][j][k] == INF: continue
            dp[i+1][j][k] = min(dp[i][j][k], dp[i+1][j][k])
            dp[i+1][j+l[i][0]][k+l[i][1]] = min(dp[i][j][k] + l[i][2], dp[i+1][j+l[i][0]][k+l[i][1]])

ans = INF
for i in range(1, N):
    for j in range(1, 401):
        for k in range(1, 401):
            # j/k == Ma/Mb だと分母0が心配なので
            if j * Mb == k * Ma:
                ans = min(dp[i][j][k], ans)

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

Submission status: Wrong Answer

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 = [[[-1] * 401  for i in range(401)] for i in range(N+1)]
def dfs(cur, a, b, c):
    if memo[cur][a][b] != -1:
        return memo[cur][a][b]
    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)
    memo[cur][a][b] = mn
    return mn

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

Submission status: Wrong Answer

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] != INF:
        return memo[cur][a][b]
    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)
    memo[cur][a][b] = mn
    return mn

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

Submission status: Wrong Answer

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] != INF:
        return memo[cur][a][b]
    mn = INF
    if b != 0 and a / b == Ma / Mb:
        mn = min(c, mn)
    if cur == N:
        return INF
    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 == INF:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

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] != INF:
        return memo[cur][a][b]
    if cur == N:
        return INF
    mn = INF
    if b != 0 and a / b == Ma / Mb:
        mn = min(c, 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 == INF:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

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

ans = INF
for i in range(N):
    sm = [0, 0, 0]
    for j in range(i, N):
        sm[0] += l[j][0]
        sm[1] += l[j][1]
        sm[2] += l[j][2]
        if sm[0] / sm[1] == Ma / Mb:
            ans = min(sm[2], ans)

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

Submission status: Wrong Answer

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-9):
        for b in range(b_sum-9):
            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: Wrong Answer

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
print(a_sum, b_sum)
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: Wrong Answer

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(1,10*n+1):
        if j-m[i][0]<0:
            dp[i][j]=dp[i-1][j]
            continue
        for k in range(1,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 and dp[-1][i][int(j)]<10**18:
        ans=min(ans,dp[-1][i][int(j)])
print(-(ans==10**18)or ans)

Submission status: Wrong Answer

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(1,10*n+1):
        if j-m[i][0]<0:
            dp[i][j]=dp[i-1][j]
            continue
        for k in range(1,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])
for i in range(1,10*n+1):
    j=Mb*i/Ma
    if j==int(j) and j<10*n+2 and dp[-1][i][int(j)]<10**18:
        print(dp[-1][i][int(j)])
        break
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = map(int, input().split())
A, B, C = [], [], []
for i in range(N):
    ai, bi, ci = map(int, input().split())
    A.append(ai)
    B.append(bi)
    C.append(ci)

INF = float('inf')
dp = [[[INF] * 401 for i in range(401)] for j in range(N + 1)]
dp[0][0][0] = 0

for i in range(1, N + 1):
    for a in range(1, 401):
        for b in range(1, 401):
            if A[i - 1] <= a and B[i - 1] <= b:
                dp[i][a][b] = min(dp[i][a][b], dp[i - 1][a][b], dp[i - 1][a - A[i - 1]][b - B[i - 1]] + C[i - 1])
            else:
                dp[i][a][b] = min(dp[i][a][b], dp[i - 1][a][b])
ans = INF
for i in range(1, 401):
    target_a = Ma * i
    target_b = Mb * i
    if target_a > 400 or target_b > 400:
        break
    ans = min(ans, dp[N][target_a][target_b])

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