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:
n, ma, mb = map(int, input().split())
INF = 100001
dp = [[[INF for k in range(401)] for j in range(401)] for i in range(n+1)]
a, b, c = [0 for i in range(n)], [0 for i in range(n)], [0 for i in range(n)]


def solve():
    dp[0][0][0] = 0

    for i in range(1, n+1):
        for j in range(1, 10*i+1):
            for k in range(1, 10*i+1):
                if a[i-1] <= j and b[i-1] <= k:
                    dp[i][j][k] = min(dp[i-1][j][k], dp[i-1][j-a[i-1]][k-b[i-1]]+c[i-1])
                else:
                    dp[i][j][k] = dp[i-1][j][k]

    min_cost = INF
    for i in range(401):
        for j in range(401):
            if i*mb == j*ma:
                min_cost = min(min_cost, dp[n][i][j])


    return min_cost


def main():
    for i in range(n):
        aa, bb, cc = map(int, input().split())
        a[i], b[i], c[i] = aa, bb, cc

    cost = solve()

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, ma, mb = map(int, input().split())
INF = 100001
dp = [[[INF for k in range(401)] for j in range(401)] for i in range(n+1)]
a, b, c = [0 for i in range(n)], [0 for i in range(n)], [0 for i in range(n)]


def solve():
    dp[0][0][0] = 0

    for i in range(1, n+1):
        for j in range(1, 10*i+1):
            for k in range(1, 10*i+1):
                if a[i-1] <= j and b[i-1] <= k:
                    dp[i][j][k] = min(dp[i-1][j][k], dp[i-1][j-a[i-1]][k-b[i-1]]+c[i-1])
                else:
                    dp[i][j][k] = dp[i-1][j][k]

    t, min_cost = 1, INF
    while t * ma <= 400 and t * mb <= 400:
        min_cost = min(dp[n][t*ma][t*mb], min_cost)
        t += 1

    return min_cost


def main():
    for i in range(n):
        aa, bb, cc = map(int, input().split())
        a[i], b[i], c[i] = aa, bb, cc

    cost = solve()

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, ma, mb = map(int, input().split())
INF = 100001
dp = [[[INF for k in range(401)] for j in range(401)] for i in range(n+1)]
a, b, c = [0 for i in range(n)], [0 for i in range(n)], [0 for i in range(n)]


def solve():
    dp[0][0][0] = 0

    for i in range(1, n+1):
        for j in range(1, 10*i+1):
            for k in range(1, 10*i+1):
                if a[i-1] < j and b[i-1] < k:
                    dp[i][j][k] = min(dp[i-1][j][k], dp[i-1][j-a[i-1]][k-b[i-1]]+c[i-1])
                else:
                    dp[i][j][k] = dp[i-1][j][k]

    t, min_cost = 1, INF
    while t * ma <= 400 and t * mb <= 400:
        min_cost = min(dp[n][t*ma][t*mb], min_cost)
        t += 1

    return min_cost


def main():
    for i in range(n):
        aa, bb, cc = map(int, input().split())
        a[i], b[i], c[i] = aa, bb, cc

    cost = solve()

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, ma, mb = map(int, input().split())
INF = 1 << 30
dp = [[[INF for k in range(401)] for j in range(401)] for i in range(n+1)]
a, b, c = [0 for i in range(n)], [0 for i in range(n)], [0 for i in range(n)]


def solve():
    dp[0][0][0] = 0

    for i in range(1, n+1):
        for j in range(a[i-1], 10*i+1):
            for k in range(b[i-1], 10*i+1):
                dp[i][j][k] = min(dp[i-1][j][k], dp[i-1][j-a[i-1]][k-b[i-1]]+c[i-1])

    t, min_cost = 1, INF
    while t * ma <= 400 and t * mb <= 400:
        min_cost = min(dp[n][t*ma][t*mb], min_cost)
        t += 1

    return min_cost


def main():
    for i in range(n):
        aa, bb, cc = map(int, input().split())
        a[i], b[i], c[i] = aa, bb, cc

    cost = solve()

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, ma, mb = map(int, input().split())
INF = 1 << 21
dp = [[[INF for k in range(401)] for j in range(401)] for i in range(n+1)]
a, b, c = [0 for i in range(n)], [0 for i in range(n)], [0 for i in range(n)]


def solve():
    dp[0][0][0] = 0

    for i in range(1, n+1):
        for j in range(a[i-1], 10*i+1):
            for k in range(b[i-1], 10*i+1):
                dp[i][j][k] = min(dp[i-1][j][k], dp[i-1][j-a[i-1]][k-b[i-1]]+c[i-1])

    t, min_cost = 1, INF
    while t * ma <= 400 and t * mb <= 400:
        min_cost = min(dp[n][t*ma][t*mb], min_cost)
        t += 1

    return min_cost


def main():
    for i in range(n):
        aa, bb, cc = map(int, input().split())
        a[i], b[i], c[i] = aa, bb, cc

    cost = solve()

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = map(int, input().split(' '))
Chems = [list(map(int, input().split(' '))) for _ in range(N)]
MaxA = sum([Chems[i][0] for i in range(N)])
MaxB = sum([Chems[i][1] for i in range(N)])
dp = [[[float('INF') for i in range(MaxB + 1)] for j in range(MaxA + 1)] for k in range(N+1)]
dp[0][0][0] = 0

for i,(a, b, c) in enumerate(Chems):
    for ca in range(MaxA + 1):
        for cb in range(MaxB + 1):
            dp[i+1][ca][cb] = min(dp[i+1][ca][cb], dp[i][ca][cb])
            if ca + a <= MaxA and cb + b <= MaxB:
                dp[i+1][ca + a][cb + b] = min(dp[i+1][ca + a][cb + b], dp[i][ca][cb] + c)
res = float('INF')
for (a, b) in zip(range(MaxA + 1), range(MaxB + 1)):
    if a != 0 and b != 0 and a * Mb == b * Ma:
        res = min(res, dp[N][a][b])
if res == float('INF'): res = -1
print(res)

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = map(int, input().split())
L = [[float("inf")]*410 for i in range(410)]
P = [[0, 0]]
L[0][0] = 0

for i in range(N):
    a, b, c = map(int, input().split())
    OldP = P.copy()
    OldL = L.copy()
    for p in OldP:
        if L[p[0]+a][p[1]+b] == float("inf"):
            P.append([p[0]+a, p[1]+b])
        L[p[0]+a][p[1]+b] = min(OldL[p[0]+a][p[1]+b], OldL[p[0]][p[1]]+c)
g = [Ma, Mb]
ans = float("inf")
while g[0]<410 and g[1]<410:
    ans = min(ans, L[g[0]][g[1]])
    g[0] += Ma
    g[1] += Mb
print(ans if ans != float("inf") else -1)

Submission status: Wrong Answer

Solution code:
import copy
N, Ma, Mb = map(int, input().split())
L = [[float("inf")]*410 for i in range(410)]
P = [[0, 0]]
L[0][0] = 0

for i in range(N):
    a, b, c = map(int, input().split())
    OldP = copy.deepcopy(P)
    for p in OldP:
        if L[p[0]+a][p[1]+b] == float("inf"):
            P.append([p[0]+a, p[1]+b])
        L[p[0]+a][p[1]+b] = min(L[p[0]+a][p[1]+b], L[p[0]][p[1]]+c)
g = [Ma, Mb]
ans = float("inf")
while g[0]<410 and g[1]<410:
    ans = min(ans, L[g[0]][g[1]])
    g[0] += Ma
    g[1] += Mb
print(ans if ans != float("inf") else -1)

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = map(int, input().split())
L = [[float("inf")]*410 for i in range(410)]
P = [[0, 0]]
L[0][0] = 0

for i in range(N):
    a, b, c = map(int, input().split())
    OldP = P.copy()
    for p in OldP:
        if L[p[0]+a][p[1]+b] == float("inf"):
            P.append([p[0]+a, p[1]+b])
        L[p[0]+a][p[1]+b] = min(L[p[0]+a][p[1]+b], L[p[0]][p[1]]+c)
g = [Ma, Mb]
ans = float("inf")
while g[0]<410 and g[1]<410:
    ans = min(ans, L[g[0]][g[1]])
    g[0] += Ma
    g[1] += Mb
print(ans if ans != float("inf") else -1)

Submission status: Wrong Answer

Solution code:
n, ma, mb = map(int, input().split())
a, b, c = [], [], []
for _ in range(n):
    at, bt, ct = map(int, input().split())
    a.append(at)
    b.append(bt)
    c.append(ct)
    
dp = [[float('inf') for _ in range(n*mb + 1)] for _ in range(n*ma + 1)]
dp[0][0] = 0
used = [[0, 0]]

for i in range(n):
    add_used = []
    for at, bt in used:
        if at+a[i] <= n and bt+b[i] <= n:
            if dp[at+a[i]][bt+b[i]] == float('inf'):
                add_used.append([at+a[i], bt+b[i]])
            
            dp[at+a[i]][bt+b[i]] = min(dp[at][bt]+c[i], dp[at+a[i]][bt+b[i]])
        
    used += add_used

if dp[-1][-1] == float('inf'):
    print(-1)
else :
    print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
n,ma,mb = (int(i) for i in input().split())
x = [[int(i) for i in input().split()] for i in range(n)]
dp = [[float("inf") for i in range(10*n+1)] for i in range(10*n+1)]
dp[0][0],ans = 0,float("inf")
for i in range(n):
	for j in range(10*(i-1)+1):
		for k in range(10*(i-1)+1):
			if dp[j][k]!=float("inf"): dp[j+x[i][0]][k+x[i][1]] = min(dp[j][k]+x[i][2],dp[j+x[i][0]][k+x[i][1]])
for i in range(ma,10*n+1,ma):
	if i*mb//ma<=10*n: ans=min(ans,dp[i][i*mb//ma])
if ans==float("inf"): print(-1)
else: print(ans)

Submission status: Wrong Answer

Solution code:
n,ma,mb=map(int,input().split())
lista=[]
listb=[]
listc=[]
for i in range(n):
    a,b,c=map(int,input().split())
    lista.append(a)
    listb.append(b)
    listc.append(c)
x=sum(lista)
y=sum(listb)
dp=[[[float('inf') for i in range(n+1)]for i\
 in range(y+1)]for i in range(x+1)]
for i in range(n+1):
    dp[0][0][i]=0
for i in range(1,n+1):
    for a in range(x+1):
        for b in range(y+1):
            if a-lista[i-1]>=0 and b-listb[i-1]>=0:
                if dp[a][b][i-1]!=float('inf') or dp[a-lista[i-1]][b-listb[i-1]][i-1]!=float('inf'):

                    dp[a][b][i]=min(dp[a][b][i-1],dp[a-lista[i-1]][b-listb[i-1]][i-1]+listc[i-1])
            else:
                dp[a][b][i]=dp[a][b][i-1]
cost=float('inf')
for a in range(1,x+1):
    if float.is_integer(a*mb/ma) and a*mb/ma<=y+1:
            cost=min(cost,dp[a][int(a*mb/ma)][n])
if cost==float('inf'):
    print(-1)
else:
    print(cost)
print(dp)

Submission status: Wrong Answer

Solution code:
n,ma,mb=map(int,input().split())
lista=[]
listb=[]
listc=[]
for i in range(n):
    a,b,c=map(int,input().split())
    lista.append(a)
    listb.append(b)
    listc.append(c)
x=sum(lista)
y=sum(listb)
dp=[[[float('inf') for i in range(n+1)]for i\
 in range(y+1)]for i in range(x+1)]
for i in range(n+1):
    dp[0][0][i]=0
for i in range(1,n+1):
    for a in range(x+1):
        for b in range(y+1):
            if a-lista[i-1]>=0 and b-listb[i-1]>=0:
                dp[a][b][i]=min(dp[a][b][i-1],dp[a-lista[i-1]][b-listb[i-1]][i-1]+listc[i-1])
            else:
                dp[a][b][i]=dp[a][b][i-1]
cost=float('inf')
for a in range(1,x+1):
    if float.is_integer(a*mb/ma) and a*mb/ma<=y+1:
            cost=min(cost,dp[a][int(a*mb/ma)][n])
if cost==float('inf'):
    print(-1)
else:
    print(cost)
print(dp)

Submission status: Wrong Answer

Solution code:
n,ma,mb=map(int,input().split())
lista=[]
listb=[]
listc=[]
for i in range(n):
    a,b,c=map(int,input().split())
    lista.append(a)
    listb.append(b)
    listc.append(c)
x=sum(lista)
y=sum(listb)
dp=[[[float('inf') for i in range(n+1)]for i\
 in range(y+1)]for i in range(x+1)]
for i in range(n+1):
    dp[0][0][i]=0
for i in range(1,n+1):
    for a in range(lista[i-1],x+1):
        for b in range(listb[i-1],y+1):
            dp[a][b][i]=min(dp[a][b][i-1],dp[a-lista[i-1]][b-listb[i-1]][i-1]+listc[i-1])

cost=float('inf')
for a in range(1,x+1):
    if float.is_integer(a*mb/ma) and a*mb/ma<=y+1:
            cost=min(cost,dp[a][int(a*mb/ma)][n])
if cost==float('inf'):
    print(-1)
else:
    print(cost)

Submission status: Wrong Answer

Solution code:
n,ma,mb=map(int,input().split())
lista=[]
listb=[]
listc=[]
for i in range(n):
    a,b,c=map(int,input().split())
    lista.append(a)
    listb.append(b)
    listc.append(c)
dp=[[[float('inf') for i in range(n+1)]for i\
 in range(sum(listb)+1)]for i in range(sum(lista)+1)]
for i in range(n+1):
    dp[0][0][i]=0
for i in range(1,n+1):
    for a in range(lista[i-1],sum(lista)+1):
        for b in range(listb[i-1],sum(listb)+1):
            dp[a][b][i]=min(dp[a][b][i-1],dp[a-lista[i-1]][b-listb[i-1]][i-1]+listc[i-1])

cost=float('inf')
for a in range(1,sum(lista)+1):
    for b in range(1,sum(listb)+1):
        if a*mb==b*ma:
            cost=min(cost,dp[a][b][n])
if cost==float('inf'):
    print(-1)
else:
    print(cost)

Submission status: Wrong Answer

Solution code:
n,ma,mb=map(int,input().split())
lista=[]
listb=[]
listc=[]
for i in range(n):
    a,b,c=map(int,input().split())
    lista.append(a)
    listb.append(b)
    listc.append(c)
dp=[[[float('inf') for i in range(n+1)]for i\
 in range(sum(listb)+1)]for i in range(sum(lista)+1)]
dp[0][0][0]=10000000
for i in range(1,n+1):
    for a in range(lista[i-1],sum(lista)+1):
        for b in range(listb[i-1],sum(listb)+1):
            dp[a][b][i]=min(dp[a][b][i-1],dp[a-lista[i-1]][b-listb[i-1]][i-1]+listc[i-1])

cost=float('inf')
for a in range(1,sum(lista)+1):
    for b in range(1,sum(listb)+1):
        if a*mb==b*ma:
            cost=min(cost,dp[a][b][n])
if cost==float('inf'):
    print(-1)
else:
    print(cost)

Submission status: Wrong Answer

Solution code:
n,ma,mb=map(int,input().split())
lista=[]
listb=[]
listc=[]
for i in range(n):
    a,b,c=map(int,input().split())
    lista.append(a)
    listb.append(b)
    listc.append(c)
dp=[[[float('inf') for i in range(n+1)]for i\
 in range(sum(listb)+1)]for i in range(sum(lista)+1)]
dp[0][0][0]=0
for i in range(1,n+1):
    for a in range(lista[i-1],sum(lista)+1):
        for b in range(listb[i-1],sum(listb)+1):
            dp[a][b][i]=min(dp[a][b][i-1],dp[a-lista[i-1]][b-listb[i-1]][i-1]+listc[i-1])

cost=float('inf')
for a in range(1,sum(lista)+1):
    for b in range(1,sum(listb)+1):
        if a*mb==b*ma:
            cost=min(cost,dp[a][b][n])
if cost==float('inf'):
    print(-1)
else:
    print(cost)

Submission status: Wrong Answer

Solution code:
n,ma,mb=map(int,input().split())
lista=[]
listb=[]
listc=[]
for i in range(n):
    a,b,c=map(int,input().split())
    lista.append(a)
    listb.append(b)
    listc.append(c)
dp=[[[float('inf') for i in range(n+1)]for i\
 in range(sum(listb)+1)]for i in range(sum(lista)+1)]
dp[0][0][0]=0
for i in range(1,n+1):
    for a in range(lista[i-1],sum(lista)+1):
        for b in range(listb[i-1],sum(listb)+1):
            dp[a][b][i]=min(dp[a][b][i-1],dp[a-lista[i-1]][b-listb[i-1]][i-1]+listc[i-1])

cost=float('inf')
for a in range(1,sum(lista)):
    for b in range(1,sum(listb)):
        if a*mb==b*ma:
            cost=min(cost,dp[a][b][n])
if cost==float('inf'):
    print(-1)
else:
    print(cost)

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-1][a][b], dp[i-1][a-A[i-1]][b-B[i-1]] + C[i-1])
            else:
                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)

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):
        if A[i-1] > a:
            continue
        for b in range(1, 401):
            if B[i-1] <= b:
                dp[i][a][b] = min(dp[i-1][a][b], dp[i-1][a-A[i-1]][b-B[i-1]] + C[i-1])
            else:
                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-1][target_b-1])

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

Submission status: Wrong Answer

Solution code:
def gcd(a,b):
    if a<b:
        a,b = b,a
    if b == 0:
        return a
    c = a%b
    return gcd(b,c)

dp = []
dp.append({(0,0):0})
N,A,B = map(int,input().split())

for i in range(N):
    a,b,c = map(int,input().split())
    temp = dp[-1].copy()
    for key in dp[-1].keys():
        left = key[0] + a
        right = key[1] + b
        g = gcd(left, right)
        if g != 1:
            left = int(left/g)
            right = int(right/g)
        n_key = (left,right)
        if not (n_key in temp):
            temp[n_key] = dp[-1][key]+c
        elif temp[key]+c < temp[n_key]:
            temp[n_key] = dp[-1][key]+c
    dp.append(temp)

if (A,B) in dp[N-1]:
    print(dp[N-1][(A,B)])
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
N,Ma,Mb=map(int,raw_input().split())

dp=defaultdict(lambda: float("inf"))
dp[(0,0)]=0

for i in range(N):
	a,b,c=map(int,raw_input().split())
	for j in dp.items():
		x=j[0][0]
		y=j[0][1]
		z=j[1]
		dp[(x+a, y+b)]=min( dp[(x+a, y+b)], dp[(x,y)]+c )


ans=float("inf")
for i in dp.items():
	x=i[0][0]
	y=i[0][1]
	z=i[1]
	if x==0 and y==0:
		continue
	if x*Mb==y*Ma:
		ans=min(ans,z)

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
N,Ma,Mb=map(int,raw_input().split())

dp=defaultdict(lambda: float("inf"))
dp[(0,0)]=0

for i in range(N):
	a,b,c=map(int,raw_input().split())
	for j in dp.items():
		x=j[0][0]
		y=j[0][1]
		z=j[1]
		dp[(x+a, y+b)]=min( dp[(x+a, y+b)], dp[(x,y)]+c )


ans=float("inf")
for i in dp.items():
	x=i[0][0]
	y=i[0][1]
	z=i[1]
	if x*Mb==y*Ma and x!=0:
		ans=min(ans,z)

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

Submission status: Wrong Answer

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

dp=[ [ [ float("inf") for y in range(b_sum+1) ] for x in range(a_sum+1)] for k in range(N+1) ]
dp[0][A[0]][B[0]]=C[0]


for k in range(1,N+1):
	for x in range(a_sum+1):
		for y in range(b_sum+1):
			dp[k][x][y]=min( dp[k][x][y], dp[k-1][x][y] )
			if x-A[k-1]>=0 and y-B[k-1]>=0:
				dp[k][x][y]=min( dp[k-1][x][y], dp[k-1][x-A[k-1]][y-B[k-1]]+C[k-1] )


ans=float("inf")
for x in range(1,a_sum+1):
	for y in range(1,b_sum+1):
		if x*Mb==y*Ma:
			ans=min(ans,dp[N][x][y])

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

Submission status: Wrong Answer

Solution code:
N,Ma,Mb=map(int,raw_input().split())
A=[]
B=[]
C=[]
a_sum=0
b_sum=0
for i in range(N):
	a,b,c=map(int,raw_input().split())
	A.append(a)
	B.append(b)
	C.append(c)
	a_sum+=1
	b_sum+=1


dp=[ [ [ float("inf") for y in range(b_sum+1) ] for x in range(a_sum+1)] for k in range(N+1) ]
dp[0][0][0]=0


for k in range(1,N+1):
	for x in range(1,a_sum+1):
		for y in range(1,b_sum+1):
			dp[k][x][y]=min( dp[k][x][y], dp[k-1][x][y] )
			if x-A[k-1]>=0 and y-B[k-1]>=0:
				dp[k][x][y]=min( dp[k-1][x][y], dp[k-1][x-A[k-1]][y-B[k-1]]+C[k-1] )



ans=float("inf")
for x in range(a_sum+1):
	for y in range(b_sum+1):
		if x*Mb==y*Ma:
			ans=min(ans,dp[N][x][y])

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

Submission status: Wrong Answer

Solution code:
N,Ma,Mb=map(int,raw_input().split())
A=[]
B=[]
C=[]
a_sum=0
b_sum=0
for i in range(N):
	a,b,c=map(int,raw_input().split())
	A.append(a)
	B.append(b)
	C.append(c)
	a_sum+=1
	b_sum+=1


dp=[ [ [ float("inf") for y in range(b_sum+1) ] for x in range(a_sum+1)] for k in range(N+1) ]
dp[0][0][0]=0


for k in range(1,N+1):
	for x in range(1,a_sum+1):
		for y in range(1,b_sum+1):
			if x-A[k-1]>=0 and y-B[k-1]>=0:
				dp[k][x][y]=min( dp[k-1][x][y], dp[k-1][x-A[k-1]][y-B[k-1]]+C[k-1] )



ans=float("inf")
for x in range(a_sum+1):
	for y in range(b_sum+1):
		if x*Mb==y*Ma:
			ans=min(ans,dp[N][x][y])

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

Submission status: Wrong Answer

Solution code:
N,Ma,Mb=map(int,raw_input().split())
A=[0.1]
B=[0.1]
C=[0.1]
a_sum=0
b_sum=0
for i in range(N):
	a,b,c=map(int,raw_input().split())
	A.append(a)
	B.append(b)
	C.append(c)
	a_sum+=1
	b_sum+=1


dp=[ [ [ float("inf") for y in range(b_sum+1) ] for x in range(a_sum+1)] for k in range(N+1) ]
dp[0][0][0]=0


for k in range(1,N+1):
	for x in range(1,a_sum+1):
		for y in range(1,b_sum+1):
			dp[k][x][y]=min( dp[k][x][y], dp[k-1][x][y] )
			if x-A[k]>=0 and y-B[k]>=0:
				dp[k][x][y]=min( dp[k-1][x][y], dp[k-1][x-A[k]][y-B[k]]+C[k] )


ans=float("inf")
for x in range(a_sum+1):
	for y in range(b_sum+1):
		if x*Mb==y*Ma:
			ans=min(ans,dp[N][x][y])

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

Submission status: Wrong Answer

Solution code:
N,Ma,Mb=map(int,raw_input().split())
A=[0.1]
B=[0.1]
C=[0.1]
a_sum=0
b_sum=0
for i in range(N):
	a,b,c=map(int,raw_input().split())
	A.append(a)
	B.append(b)
	C.append(c)
	a_sum+=1
	b_sum+=1


dp=[ [ [ float("inf") for y in range(b_sum+1) ] for x in range(a_sum+1)] for k in range(N+1) ]
dp[0][0][0]=0


for k in range(1,N+1):
	for x in range(a_sum+1):
		for y in range(b_sum+1):
			if x-A[k]>=0 and y-B[k]>=0:
				dp[k][x][y]=min( dp[k-1][x][y], dp[k-1][x-A[k]][y-B[k]]+C[k] )


ans=float("inf")
for x in range(a_sum+1):
	for y in range(b_sum+1):
		if x*Mb==y*Ma:
			ans=min(ans,dp[N][x][y])

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

Submission status: Wrong Answer

Solution code:
N,Ma,Mb=map(int,raw_input().split())
A=[0]
B=[0]
C=[0]
a_sum=0
b_sum=0
for i in range(N):
	a,b,c=map(int,raw_input().split())
	A.append(a)
	B.append(b)
	C.append(c)
	a_sum+=1
	b_sum+=1

dp=[ [ [ float("inf") for y in range(b_sum+1) ] for x in range(a_sum+1)] for k in range(N+1) ]
dp[0][0][0]=0


for k in range(1,N+1):
	for x in range(a_sum+1):
		for y in range(b_sum+1):
			if x-A[k-1]>=0 and y-B[k-1]>=0:
				dp[k][x][y]=min( dp[k-1][x][y], dp[k-1][x-A[k-1]][y-B[k-1]]+C[k-1])


ans=float("inf")
for x in range(1,a_sum+1):
	for y in range(1,b_sum+1):
		if x*Mb==y*Ma:
			ans=min(ans,dp[N][x][y])

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
N,Ma,Mb=map(int,raw_input().split())
A=[0]
B=[0]
C=[0]
a_sum=0
b_sum=0
for i in range(N):
	a,b,c=map(int,raw_input().split())
	A.append(a)
	B.append(b)
	C.append(c)
	a_sum+=1
	b_sum+=1

dp=[ [ [ float("inf") for y in range(b_sum+1) ] for x in range(a_sum+1)] for k in range(N+1) ]
dp[0][0][0]=0

for k in range(1,N+1):
	for x in range(a_sum+1):
		for y in range(b_sum+1):
			#print k
			if x-A[k]>=0 and y-B[k]>=0:
				dp[k][x][y]=min( dp[k-1][x][y], dp[k-1][x-A[k]][y-B[k]]+C[k])

ans=float("inf")
for x in range(a_sum+1):
	for y in range(b_sum+1):
		if x*Mb==y*Ma:
			ans=min(ans,dp[N][x][y])

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
N,Ma,Mb=map(int,raw_input().split())
item=[]
i_sum=0
for i in range(N):
	a,b,c=map(int,raw_input().split())
	i_sum+=c
	item.append( (a,b,c) )

cost=defaultdict(lambda: 0)


for i in item:
	x=i[0]
	y=i[1]
	w=i[2]
	if len(cost)==0:
		cost[0]=(0,0)
		cost[w]=(x,y)
	else:
		for j in cost.items():
			tw=j[0]
			tx=j[1][0]
			ty=j[1][1]
			cost[tw+w]=(x+tx,y+ty)


for i in cost.items():
	w=i[0]
	x=i[1][0]
	y=i[1][1]
	if x*Mb==y**Ma and x*Mb!=0:
		print w
		quit()
else:
	print -1

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
N,Ma,Mb=map(int,raw_input().split())
item=[]
i_sum=0
for i in range(N):
	a,b,c=map(int,raw_input().split())
	i_sum+=c
	item.append( (a,b,c) )

cost=defaultdict(lambda: 0)


for i in item:
	x=i[0]
	y=i[1]
	w=i[2]
	if len(cost)==0:
		cost[0]=(0,0)
		cost[w]=(x,y)
		if x*Mb==y**Ma:
			quit()
	else:
		for j in cost.items():
			tw=j[0]
			tx=j[1][0]
			ty=j[1][1]
			if (x+tx)*Mb==(y+ty)**Ma:
				print tw+w
				quit()
			else:
				cost[tw+w]=(x+tx,y+ty)
else:
	print -1

Submission status: Wrong Answer

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

S = 411
MAX_N = 10
MAX_SIZE = 400
INF = int(1e9)
dp = [[INF] * S for i in range(S)]
dp[0][0] = 0
for i in range(N):
    for j in range(MAX_SIZE):
        for k in range(MAX_SIZE):
            dp[j+a[i]][k+b[i]] = min((dp[j+a[i]][k+b[i]], dp[j][k] + c[i]))

ans = INF
for i in range(1, MAX_N):
    ans = min((ans, dp[i*Ma][i*Mb]))
if ans == INF:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

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

inf = float('inf')
for i in range(N):
    ai, bi, ci = map(int, input().split())
    ABC.append((ai,bi,ci))

dp = [[[inf] * 401 for i in range(401)] for j 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:
                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])

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

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

Submission status: Wrong Answer

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

inf = 1000000
for i in range(N):
    ai, bi, ci = map(int, input().split())
    ABC.append((ai,bi,ci))

dp = [[[inf] * 401 for i in range(401)] for j 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:
                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])

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

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

Submission status: Wrong Answer

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

inf = 1000000
for i in range(N):
    ai, bi, ci = map(int, input().split())
    ABC.append((ai,bi,ci))

dp = [[[inf] * 401 for i in range(401)] for j 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+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])

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

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

Submission status: Wrong Answer

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

inf = 40001
for i in range(N):
    ai, bi, ci = map(int, input().split())
    ABC.append((ai,bi,ci))

dp = [[[inf] * 401 for i in range(401)] for j 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+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])

ans = inf

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

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

Submission status: Wrong Answer

Solution code:
from numpy import full

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

dp = full((N + 1, 1000, 1000), inf)
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+1,j,k], dp[i,j,k])
            dp[i+1,j+A[i],k+B[i]] = min(dp[i+1,j+A[i],k+B[i]], dp[i,j,k]+C[i])

ans = inf

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

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

Submission status: Wrong Answer

Solution code:
import numpy as np

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

dp = np.full((N + 1, 1000, 1000), inf)
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+1,j,k], dp[i,j,k])
            dp[i+1,j+A[i],k+B[i]] = min(dp[i+1,j+A[i],k+B[i]], dp[i,j,k]+C[i])

ans = inf

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

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

Submission status: Wrong Answer

Solution code:
N,MA,MB = map(int,raw_input().split())
src = []
sum_a = sum_b = 0
for i in range(N):
  a,b,c = map(int,raw_input().split())
  src.append((a,b,c))
  sum_a += a
  sum_b += b

INF = 10**9
dp = [[[INF for b in range(sum_b+1)] for a in range(sum_a+1)] for i in range(N+1)]
dp[0][0][0] = 0

for i in range(N):
  ai,bi,ci = src[i]
  for a in range(sum_a+1 - ai):
    for b in range(sum_b+1 - bi):
      dp[i+1][a+ai][b+bi] = min(dp[i][a+ai][b+bi], dp[i][a][b] + ci)

ans = INF
n = 1
while True:
  a,b = n*MA,n*MB
  if a > sum_a or b > sum_b: break
  ans = min(ans, dp[N][a][b])
  n += 1

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