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())
r = Ma/Mb
a = 0
b = 0
costlist = [500000]
data = [list(map(int, input().split())) for i in range(N)]
for i in range(N):
    a += data[i][0]
    b += data[i][1]


dp = [[[500000 for i in range(N+1)] for i in range(b+10)] for i in range(a+10)]
dp[0][0][0] = 0

#Aがiグラム、Bがjグラム、k番目までの薬を用いたときの最小コスト
for k in range(N):
    for j in range(b):
        for i in range(a):
            ai = data[k][0]
            bj = data[k][1]
            dp[i+ai][j+bj][k+1] = min(dp[i+ai][j+bj][k+1], dp[i][j][k] + data[k][2])
            dp[i][j][k+1] = min(dp[i][j][k], dp[i][j][k+1])

for i in range(1, a):
    for j in range(1, b):
        if i/j == r:
            costlist.append(dp[i][j][N])
if N == 1:
    if data[0][0]/data[0][1] == r:
        print(data[0][2])
elif min(costlist) == 500000:
    print(-1)
else:
    print(min(costlist))

Submission status: Wrong Answer

Solution code:

N, Ma, Mb = map(int, input().split())
r = Ma/Mb
a = 0
b = 0
costlist = [500000]
data = [list(map(int, input().split())) for i in range(N)]
for i in range(N):
    a += data[i][0]
    b += data[i][1]


dp = [[[500000 for i in range(N+1)] for i in range(b+10)] for i in range(a+10)]
dp[0][0][0] = 0

#Aがiグラム、Bがjグラム、k番目までの薬を用いたときの最小コスト
for k in range(N):
    for j in range(b):
        for i in range(a):
            ai = data[k][0]
            bj = data[k][1]
            dp[i+ai][j+bj][k+1] = min(dp[i+ai][j+bj][k+1], dp[i][j][k] + data[k][2])
            dp[i][j][k+1] = min(dp[i][j][k], dp[i][j][k+1])

for i in range(1, a):
    for j in range(1, b):
        if i/j == r:
            costlist.append(dp[i][j][k])
if min(costlist) == 500000:
    print(-1)
else:
    print(min(costlist))

Submission status: Wrong Answer

Solution code:
import itertools
import numpy
N, Ma, Mb = map(int, input().split())
r = Ma/Mb
a = 0
b = 0
costlist = [50000]
data = [list(map(int, input().split())) for i in range(N)]
for i in range(N):
    a += data[i][0]
    b += data[i][1]


dp = [[[50000 for i in range(N+1)] for i in range(b+10)] for i in range(a+10)]
dp[0][0][0] = 0

#Aがiグラム、Bがjグラム、k番目までの薬を用いたときの最小コスト
for k in range(N):
    for j in range(b):
        for i in range(a):
            ai = data[k][0]
            bj = data[k][1]
            dp[i+ai][j+bj][k+1] = min(dp[i+ai][j+bj][k+1], dp[i][j][k] + data[k][2])
            dp[i][j][k+1] = min(dp[i][j][k], dp[i][j][k+1])

for i in range(1, a):
    for j in range(1, b):
        if i/j == r:
            costlist.append(dp[i][j][k])
if min(costlist) == 50000:
    print(-1)
else:
    print(min(costlist))

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = map(int, input().split())
INF = 10 ** 9
# dp[a][b] = min_cost
dp = {(0, 0): 0}

for i in range(1, N + 1):
    new_dp = {}
    a, b, c = map(int, input().split())
    for (pa, pb), pc in dp.items():
        new_dp[pa, pb] = pc
        new_dp[pa + a, pb + b] = min(dp.get((pa + a, pb + b), INF), pc + c)

    dp = new_dp

ans = min(dp.get((i * Ma, i * Mb), INF) for i in range(1, 401))
print(ans if ans != INF else -1)

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = map(int, input().split())
abc = []
ans = float('inf')
for i in range(N):
    abc.append(list(map(int, input().split())))


def buy(a, b, c, t):
    global ans
    if a != 0:
        if a / b == Ma / Mb and c != 0:
            ans = min(ans, c)
    if c < ans and t < N:
        buy(a + abc[t][0], b + abc[t][1], c + abc[t][1], t + 1)
        buy(a, b, c, t + 1)


buy(0, 0, 0, 0)

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

Submission status: Wrong Answer

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

Inf = 10**4
DP = [[[Inf for b in range(btotal+1)] for a in range(atotal+1)] for i in range(N+1)]
DP[0][0][0] = 0
for i in range(N):
    for a in range(atotal+1):
        for b in range(btotal+1):
            if D[i][0] + a <= atotal and D[i][1] + b <= btotal:
                DP[i+1][a + D[i][0]][b + D[i][1]] = min(DP[i+1][a + D[i][0]][b+D[i][1]], \
                    DP[i][a][b] + D[i][2])
                DP[i+1][a][b] = min(DP[i+1][a][b], DP[i][a][b])

ans = Inf
for a in range(1, atotal+1):
    for b in range(1, btotal+1):
        if a*Ma == b*Mb:
            ans = min(ans, DP[N][a][b])

print(ans if ans < Inf else -1)

Submission status: Wrong Answer

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

Inf = 10**4
DP = [[[Inf for b in range(btotal+1)] for a in range(atotal+1)] for i in range(N+1)]
DP[0][0][0] = 0
for i in range(N):
    for a in range(atotal+1):
        for b in range(btotal+1):
            if D[i][0] + a <= atotal and D[i][1] + b <= btotal:
                DP[i+1][a + D[i][0]][b + D[i][1]] = min(DP[i+1][a + D[i][0]][b+D[i][1]], \
                    DP[i][a][b] + D[i][2])
                DP[i+1][a][b] = min(DP[i+1][a][b], DP[i][a][b])

ans = Inf
for i in range(1, N+1):
    for a in range(1, atotal+1):
        for b in range(1, btotal+1):
            if a*Ma == b*Mb:
                ans = min(ans, DP[i][a][b])

print(ans if ans < Inf else -1)

Submission status: Wrong Answer

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

Inf = 10**4
DP = [[[Inf for b in range(bmax)] for a in range(amax)] for i in range(N)]
DP[0][0][0] = 0
for i in range(N-1):
    for a in range(amax):
        for b in range(bmax):
            if D[i][0] + a < amax and D[i][1] + b < bmax:
                DP[i+1][a + D[i][0]][b + D[i][1]] = min(DP[i+1][a + D[i][0]][b+D[i][1]], \
                    DP[i][a][b] + D[i][2])
                DP[i+1][a][b] = min(DP[i+1][a][b], DP[i][a][b])

ans = Inf
for i in range(N):
    for a in range(amax):
        for b in range(bmax):
            if a*Ma == b*Mb:
                if a != 0 or b != 0:
                    ans = min(ans, DP[i][a][b])

print(ans if ans < Inf else -1)

Submission status: Wrong Answer

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

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

dp[0][0][0] = 0
for i in range(N):
    for j in range(ma):
        for k in range(ma):
            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 + rec[i][0]][k + rec[i][1]] = min(dp[i + 1][j + rec[i][0]][k + rec[i][1]], dp[i][j][k] + rec[i][2])

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

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

Submission status: Wrong Answer

Solution code:
N,Ma,Mb = map(int,input().split())
first_half = []
last_half = []
for i in range(N):
	a,b,c = map(int,input().split())
	if i < N // 2:
		first_half.append((a,b,c))
	else:
		last_half.append((a,b,c))

first_half_combination = {}
last_half_combination = {}
def make_all_combination(i,a,b,c,d_list,flag):
	if i == len(d_list):
		if c == 0:
			return

		w = Ma * b - Mb * a
		if flag:
			if w not in first_half_combination or c < first_half_combination[w]:
				first_half_combination[w] = c
		else:
			if -w not in last_half_combination or c < last_half_combination[-w]:
				last_half_combination[-w] = c
		return
	make_all_combination(i+1,a,b,c,d_list,flag)
	make_all_combination(i+1,a+d_list[i][0],b+d_list[i][1],c+d_list[i][2],d_list,flag)

make_all_combination(0,0,0,0,first_half,True)
make_all_combination(0,0,0,0,last_half,False)

ans = last_half_combination[0] if 0 in last_half_combination else -1
for w in first_half_combination.keys():
	if w in last_half_combination:
		cost = first_half_combination[w] + last_half_combination[w]
		if w == 0:
			cost = min(first_half_combination[w], last_half_combination[w])
		if ans == -1 or cost < ans:
			ans = cost
	elif w == 0:
		cost = first_half_combination[w]
		if cost < ans:
			ans = cost
print(ans)

Submission status: Wrong Answer

Solution code:
N,Ma,Mb = map(int,input().split())
first_half = []
last_half = []
for i in range(N):
	a,b,c = map(int,input().split())
	if i < N // 2:
		first_half.append((a,b,c))
	else:
		last_half.append((a,b,c))

first_half_combination = {}
last_half_combination = {}
def make_all_combination(i,a,b,c,d_list,flag):
	if i == len(d_list):
		if c == 0:
			return

		w = Ma * b - Mb * a
		if flag:
			if w not in first_half_combination or c < first_half_combination[w]:
				first_half_combination[w] = c
		else:
			if -w not in last_half_combination or c < last_half_combination[-w]:
				last_half_combination[-w] = c
		return
	make_all_combination(i+1,a,b,c,d_list,flag)
	make_all_combination(i+1,a+d_list[i][0],b+d_list[i][1],c+d_list[i][2],d_list,flag)

def search(w, lst):
	left,right = 0, len(lst)
	while left + 1 != right:
		p = (left + right) // 2
		if lst[p][0] < w:
			left = p
		else:
			right = p
	return left
make_all_combination(0,0,0,0,first_half,True)
make_all_combination(0,0,0,0,last_half,False)

ans = -1
for w in first_half_combination.keys():
	if w in last_half_combination:
		cost = first_half_combination[w] + last_half_combination[w]
		if w == 0:
			cost = min(first_half_combination[w], last_half_combination[w])
		if ans == -1 or cost < ans:
			ans = cost
	elif w == 0:
		cost = first_half_combination[w]
		if cost < ans:
			ans = cost
print(ans)

Submission status: Wrong Answer

Solution code:
N,Ma,Mb = map(int,input().split())
first_half = []
last_half = []
for i in range(N):
	a,b,c = map(int,input().split())
	if i < N // 2:
		first_half.append((a,b,c))
	else:
		last_half.append((a,b,c))

first_half_combination = {}
last_half_combination = {}
def make_all_combination(i,a,b,c,d_list,flag):
	if i == len(d_list):
		w = Ma * b - Mb * a
		if flag:
			if w not in first_half_combination or c < first_half_combination[w]:
				if c > 0:
					first_half_combination[w] = c
		else:
			if -w not in last_half_combination or c < last_half_combination[-w]:
				if c > 0:
					last_half_combination[-w] = c
		return
	make_all_combination(i+1,a,b,c,d_list,flag)
	make_all_combination(i+1,a+d_list[i][0],b+d_list[i][1],c+d_list[i][2],d_list,flag)

def search(w, lst):
	left,right = 0, len(lst)
	while left + 1 != right:
		p = (left + right) // 2
		if lst[p][0] < w:
			left = p
		else:
			right = p
	return left
make_all_combination(0,0,0,0,first_half,True)
make_all_combination(0,0,0,0,last_half,False)

ans = -1
for w in first_half_combination.keys():
	c = first_half_combination[w]
	if w in last_half_combination:
		cost = c + last_half_combination[w]
		if w == 0:
			cost = min(c, last_half_combination[w])
		if ans == -1 or cost < ans:
			ans = cost

print(ans)

Submission status: Wrong Answer

Solution code:
N,Ma,Mb = map(int,input().split())
first_half = []
last_half = []
for i in range(N):
	a,b,c = map(int,input().split())
	if i < N // 2:
		first_half.append((a,b,c))
	else:
		last_half.append((a,b,c))

first_half_combination = {}
last_half_combination = {}
def make_all_combination(i,a,b,c,d_list,flag):
	if i == len(d_list):
		w = Ma * b - Mb * a
		if flag:
			if w not in last_half_combination or c < last_half_combination[w]:
				if c > 0:
					first_half_combination[w] = c
		else:
			if -w not in last_half_combination or c < last_half_combination[-w]:
				if c > 0:
					last_half_combination[-w] = c
		return
	make_all_combination(i+1,a,b,c,d_list,flag)
	make_all_combination(i+1,a+d_list[i][0],b+d_list[i][1],c+d_list[i][2],d_list,flag)

def search(w, lst):
	left,right = 0, len(lst)
	while left + 1 != right:
		p = (left + right) // 2
		if lst[p][0] < w:
			left = p
		else:
			right = p
	return left
make_all_combination(0,0,0,0,first_half,True)
make_all_combination(0,0,0,0,last_half,False)

ans = -1
for w in first_half_combination.keys():
	c = first_half_combination[w]
	if w in last_half_combination:
		cost = c + last_half_combination[w]
		if w == 0:
			cost = min(c, last_half_combination[w])
		if ans == -1 or cost < ans:
			ans = cost

print(ans)

Submission status: Wrong Answer

Solution code:
N,Ma,Mb = map(int,input().split())
first_half = []
last_half = []
for i in range(N):
	a,b,c = map(int,input().split())
	if i < N // 2:
		first_half.append((a,b,c))
	else:
		last_half.append((a,b,c))

first_half_combination = []
last_half_combination = {}
def make_all_combination(i,a,b,c,d_list,flag):
	if i == len(d_list):
		w = Ma * b - Mb * a
		if flag:
			first_half_combination.append((w, c))
		else:
			if -w not in last_half_combination or c < last_half_combination[-w]:
				if c > 0:
					last_half_combination[-w] = c
		return
	make_all_combination(i+1,a,b,c,d_list,flag)
	make_all_combination(i+1,a+d_list[i][0],b+d_list[i][1],c+d_list[i][2],d_list,flag)

def search(w, lst):
	left,right = 0, len(lst)
	while left + 1 != right:
		p = (left + right) // 2
		if lst[p][0] < w:
			left = p
		else:
			right = p
	return left
make_all_combination(0,0,0,0,first_half,True)
make_all_combination(0,0,0,0,last_half,False)

ans = -1
for (w,c) in first_half_combination:
	if w in last_half_combination:
		cost = c + last_half_combination[w]
		if w == 0:
			if c > 0:
				cost = min(cost, c)
		if ans == -1 or cost < ans:
			ans = cost

print(ans)

Submission status: Wrong Answer

Solution code:
N,Ma,Mb = map(int,input().split())
first_half = []
last_half = []
for i in range(N):
	a,b,c = map(int,input().split())
	if i < N // 2:
		first_half.append((a,b,c))
	else:
		last_half.append((a,b,c))

first_half_combination = []
last_half_combination = []
def make_all_combination(i,a,b,c,d_list,c_list):
	if i == len(d_list):
		c_list.append((Ma * b - Mb * a, c))
		return
	make_all_combination(i+1,a,b,c,d_list,c_list)
	make_all_combination(i+1,a+d_list[i][0],b+d_list[i][1],c+d_list[i][2],d_list,c_list)

def search(w, lst):
	left,right = 0, len(lst)
	while left + 1 != right:
		p = (left + right) // 2
		if lst[p][0] < w:
			left = p
		else:
			right = p
	return left
make_all_combination(0,0,0,0,first_half,first_half_combination)
make_all_combination(0,0,0,0,last_half,last_half_combination)
first_half_combination.sort()
last_half_combination.sort()

ans_list = []
for (w,c) in first_half_combination:
	n = search(-w, last_half_combination)
	if last_half_combination[n][0] == -w:
		ans_list.append(c + last_half_combination[n][1])
	if n+1 < len(last_half_combination) and last_half_combination[n+1][0] == -w:
		ans_list.append(c + last_half_combination[n+1][1])

if len(ans_list) > 0:
	print(min(ans_list))
else:
	print(-1)

Submission status: Wrong Answer

Solution code:
N,Ma,Mb = map(int,input().split())
first_half = []
last_half = []
for i in range(N):
	a,b,c = map(int,input().split())
	if i < N // 2:
		first_half.append((a,b,c))
	else:
		last_half.append((a,b,c))

first_half_combination = []
last_half_combination = []
def make_all_combination(i,a,b,c,d_list,c_list):
	if i == len(d_list):
		c_list.append((Ma * b - Mb * a, c))
		return
	make_all_combination(i+1,a,b,c,d_list,c_list)
	make_all_combination(i+1,a+d_list[i][0],b+d_list[i][1],c+d_list[i][2],d_list,c_list)

def search(w, lst):
	left,right = 0, len(lst)
	while left + 1 != right:
		p = (left + right) // 2
		if lst[p][0] < w:
			left = p
		else:
			right = p
	return left
make_all_combination(0,0,0,0,first_half,first_half_combination)
make_all_combination(0,0,0,0,last_half,last_half_combination)
first_half_combination.sort()
last_half_combination.sort()

ans_list = []
for (w,c) in first_half_combination:
	n = search(-w, last_half_combination)
	if last_half_combination[n][0] == -w:
		ans_list.append(c + last_half_combination[n][1])

if len(ans_list) > 0:
	print(min(ans_list))
else:
	print(-1)

Submission status: Wrong Answer

Solution code:
N,MA,MB=map(int,input().split())
list1=[]
for i in range(N):
    a,b,c=map(int,input().split())
    list1.append([a,b,c])
#print(list1)
ans=1000
for j in range(1,N):
    for k in range(j):
        if (list1[j][0]+list1[k][0])*(MB)==(list1[j][1]+list1[k][1])*(MA):
            if list1[j][2]+list1[k][2]<ans:
                ans=list1[j][2]+list1[k][2]
if ans==1000:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
f = lambda: list(map(int,input().split()))
n,ma,mb = f()
A,B,C = [],[],[]
for _ in range(n):
    a,b,c = f()
    A.append(a)
    B.append(b)
    C.append(c)

sa,sb = sum(A), sum(B)
dp = [[[float('inf') for _ in range(sb+1)] for _ in range(sa+1)] for _ in range(n)]

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

ans = float('inf')
for i in range(sa):
    for j in range(sb):
        if i*ma == j*mb != 0:
            ans = min(ans, dp[n-1][i][j])
if ans == float('inf'):
    ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Aug 26 13:55:52 2018

@author: maezawa
"""

n, ma, mb = list(map(int, input().split()))
a = []
b = []
c = []
for i in range(n):
    ai, bi, ci = list(map(int, input().split()))
    a.append(ai)
    b.append(bi)
    c.append(ci)
    
x = [mb*a[i]-ma*b[i] for i in range(n)]


dp = [[[0]*2 for _ in range(-100,101)] for _ in range(n+1)] 

for k in range(-100,100):
    dp[0][k][0] = 10**8
    dp[0][k][1] = 10**8
    
dp[0][0][0] = 0
dp[0][0][1] = 10**8

for i in range(1,n+1):
    for k in range(-100,100):
            dp[i][k][0] = dp[i-1][k][0]
            dp[i][k][1] = min(dp[i-1][k][1],
              dp[i-1][k-x[i-1]][0]+c[i-1],
              dp[i-1][k-x[i-1]][1]+c[i-1])

ans = dp[n][0][1]
if ans == 10**8:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N,Ma,Mb=map(int,input().split())
a=[0 for i in range(N)]
b=[0 for i in range(N)]
c=[0 for i in range(N)]
M=401
dp=[[[10**4 for cb in range(M)]for ca in range(M)]for i in range(N+1)]
for i in range(N):
    a[i],b[i],c[i]=map(int,input().split())
dp[0][0][0]=0
for i in range(N):
    for ca in range(M):
        for cb in range(M):
            if dp[i][ca][cb]==10**4:
                continue
            dp[i+1][ca][cb]=min([dp[i+1][ca][cb],dp[i][ca][cb]])
            dp[i+1][ca+a[i]][cb+b[i]]=min([dp[i+1][ca+a[i]][cb+b[i]],dp[i][ca][cb]+c[i]])
ans=10**4
for i in range(1,M):
    for j in range(1,M):
        if i*Mb==j*Ma:
            if dp[N-1][i][j]<ans:
                ans=dp[N][i][j]
if ans!=10**4:
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N,Ma,Mb=map(int,input().split())
a=[0 for i in range(N)]
b=[0 for i in range(N)]
c=[0 for i in range(N)]
M=401
dp=[[[10**4 for cb in range(M)]for ca in range(M)]for i in range(N+1)]
for i in range(N):
    a[i],b[i],c[i]=map(int,input().split())
dp[0][0][0]=0
for i in range(N):
    for ca in range(M):
        for cb in range(M):
            if ca+a[i]<M and cb+b[i]<M:
                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]])
            dp[i+1][ca][cb]=min([dp[i][ca][cb],dp[i+1][ca][cb]])
ans=10**4
for i in range(1,M):
    for j in range(1,M):
        if i*Mb==j*Ma:
            if dp[N-1][i][j]<ans:
                ans=dp[N-1][i][j]
if ans!=10**4:
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N,Ma,Mb=map(int,input().split())
a=[0 for i in range(N)]
b=[0 for i in range(N)]
c=[0 for i in range(N)]
M=401
dp=[[[10**4 for cb in range(M)]for ca in range(M)]for i in range(N+1)]
for i in range(N):
    a[i],b[i],c[i]=map(int,input().split())
dp[0][0][0]=0
for i in range(N):
    for ca in range(M):
        for cb in range(M):
            if ca+a[i]<M and cb+b[i]<M:
                dp[i+1][ca][cb]=min([dp[i][ca][cb],dp[i+1][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**4
for i in range(1,M):
    for j in range(1,M):
        if i*Mb==j*Ma:
            if dp[N-1][i][j]<ans:
                ans=dp[N-1][i][j]
if ans!=10**4:
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N,Ma,Mb=map(int,input().split())
a=[0 for i in range(N)]
b=[0 for i in range(N)]
c=[0 for i in range(N)]
M=401
dp=[[[10**4 for cb in range(M)]for ca in range(M)]for i in range(N)]
for i in range(N):
    a[i],b[i],c[i]=map(int,input().split())

for i in range(N):
    for ca in range(M):
        for cb in range(M):
            if i==0:
                if ca==a[0] and cb==b[0]:
                    dp[i][ca][cb]=c[0]
            else:
                if dp[i-1][ca][cb]!=10**4:
                    dp[i][ca][cb]=min([dp[i-1][ca][cb],dp[i][ca][cb]])
                    dp[i][ca+a[i]][cb+b[i]]=min([c[i]+dp[i-1][ca][cb],dp[i][ca+a[i]][cb+b[i]]])
ans=10**4
for i in range(M):
    for j in range(M):
        if i*Mb==j*Ma:
            if dp[N-1][i][j]<ans:
                ans=dp[N-1][i][j]
if ans!=10**4:
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
n,ma,mb = map(int,input().split())
x = []
amax = 0
bmax = 0
for i in range(n):
    x1,y1,z1=[int(i) for i in input().split()]
    x.append([x1,y1,z1])
    amax += x1
    bmax += y1
inf = float("inf")
dp = [[inf] * (amax+1) for i in range(bmax+1)]
dp[0][0] = 0
aru = {(0,0)}
for i in x:
  a = i[0]
  b = i[1]
  c = i[2]
  aru2 = list(aru)
  for j in aru2:
    dp[j[1]+b][j[0]+a] = min(dp[j[1]+b][j[0]+a],dp[j[1]][j[0]]+c)
    aru.add((j[0]+a,j[1]+b))
ans = inf
mam = ma+0
mbm = mb + 0
while (amax >= ma and bmax >= mb):
  suu = dp[mb][ma]
  ans = min(ans,suu)
  mb += mbm
  ma += mam
if ans == inf:
  print(-1)
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, MA, MB, ABC):
    dp = defaultdict(lambda: defaultdict(
        lambda: defaultdict(lambda: sys.maxsize)))
    AB_MAX = N*10
    dp[0][0][0] = 0
    for i, (a, b, c) in enumerate(ABC):
        for j in range(AB_MAX):
            for k in range(AB_MAX):
                if not (i in dp[i] and j in dp[i] and k in dp[i][j]):
                    continue
                dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k])
                dp[i+1][j+a][k+b] = min(dp[i+1][j+a][k+b], dp[i][j][k] + c)

    ans = sys.maxsize
    for a in dp[N]:
        if a == 0:
            continue
        for b in dp[N][a]:
            if a*MB == b*MA:
                ans = min(ans, dp[N][a][b])

    return ans if ans != sys.maxsize else -1


def main():
    N, MA, MB = read_int_n()
    ABC = [read_int_n() for _ in range(N)]

    print(slv(N, MA, MB, ABC))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

N,Ma,Mb = map(int, sdin().split())
ABC = []
for i in range(N):
    ABC.append(tuple(map(int, sdin().split())))

sum_a = [0]
sum_b = [0]
for a,b,c in ABC:
    sum_a.append(sum_a[-1]+a)
    sum_b.append(sum_b[-1]+b)
    
INF = float("inf")
MAX_W = 400
dp = [[[INF for _ in range(sum_b[-1]+1)] for _ in range(sum_a[-1]+1)] for _ in range(N+1)]
for i in range(N+1):
    dp[i][0][0] = 0

for i, (ai, bi, ci) in enumerate(ABC):
    for a in range(sum_a[i+1]+1):
        for b in range(sum_b[i+1]+1):
            if a-ai>=0 and b-bi>=0:
                dp[i+1][a][b] = min(dp[i][a][b], dp[i][a-ai][b-bi]+ci)
     
k = 1           
ans = INF
while (k*Ma<=sum_a[-1] and k*Mb<=sum_b[-1]):
    ans = min(ans, dp[N][k*Ma][k*Mb])
    k += 1
    
if ans == INF:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

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

N,Ma,Mb = map(int, sdin().split())
ABC = []
for i in range(N):
    ABC.append(tuple(map(int, sdin().split())))

INF = float("inf")
MAX_W = 400
dp = [[INF for _ in range(MAX_W+1)] for _ in range(MAX_W+1)]
dp[0][0] = 0

for a in range(MAX_W+1):
    for b in range(MAX_W+1):
        for ai, bi, ci in ABC:
            if a-ai>=0 and b-bi>=0:
                dp[a][b] = min(dp[a][b], dp[a-ai][b-bi]+ci)
     
k = 1           
ans = INF
while (k*Ma<=MAX_W and k*Mb<=MAX_W):
    ans = min(ans, dp[k*Ma][k*Mb])
    k += 1
    
if ans == INF:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

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

N,Ma,Mb = map(int, sdin().split())
ABC = []
for i in range(N):
    ABC.append(tuple(map(int, sdin().split())))

INF = float("inf")
MAX_W = 400
dp = [[INF for _ in range(MAX_W+1)] for _ in range(MAX_W+1)]
dp[0][0] = 0

for ai, bi, ci in ABC:
    for a in range(MAX_W+1):
        for b in range(MAX_W+1):
            if a-ai>=0 and b-bi>=0:
                dp[a][b] = min(dp[a][b], dp[a-ai][b-bi]+ci)
     
k = 1           
ans = INF
while (k*Ma<=MAX_W and k*Mb<=MAX_W):
    ans = min(ans, dp[k*Ma][k*Mb])
    k += 1
    
if ans == INF:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

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

for i in range(N):
    tmp = X[i]
    for j in range(i,N):
        if i != j:
            if (X[i][0]+X[j][0])*Mb == (X[i][1]+X[j][1])*Ma:
                y = min(y, X[i][2]+X[j][2])
            elif (tmp[0]+X[j][0])*Mb == (tmp[1]+X[j][1])*Ma:
                y = min(y, tmp[2]+X[j][2])
            else:
                tmp[0] += X[j][0]
                tmp[1] += X[j][1]
                tmp[2] += X[j][2]
                
    if tmp[0]*Mb == tmp[1]*Ma:
        y = min(y, tmp[2])
print(-1 if y>4000 else y)

Submission status: Wrong Answer

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

for i in range(N):
    tmp = X[i]
    for j in range(i,N):
        if i != j:
            if (tmp[0]+X[j][0])*Mb == (tmp[1]+X[j][1])*Ma:
                y = min(y,X[i][2]+X[j][2])
            else:
                tmp[0] += X[j][0]
                tmp[1] += X[j][1]
                tmp[2] += X[j][2]
                
    if tmp[0]*Mb == tmp[1]*Ma:
        y = min(y, tmp[2])
print(-1 if y>4000 else y)

Submission status: Wrong Answer

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

for i in range(N):
    tmp = X[i]
    for j in range(N):
        if i != j:
            if (tmp[0]+X[j][0])*Mb == (tmp[1]+X[j][1])*Ma:
                y = min(y,X[i][2]+X[j][2])
            else:
                tmp[0] += X[j][0]
                tmp[1] += X[j][1]
                tmp[2] += X[j][2]
                
    if tmp[0]*Mb == tmp[1]*Ma:
        y = min(y, tmp[2])
print(-1 if y>4000 else y)

Submission status: Wrong Answer

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

for i in range(N):
    tmp = X[i]
    for j in range(N):
        if i != j and (tmp[0]+X[j][0])*Mb == (tmp[1]+X[j][1])*Ma and y>tmp[2]:
            tmp[0] += X[j][0]
            tmp[1] += X[j][1]
            tmp[2] += X[j][2]
            y = min(y,tmp[2])
print(-1 if y>4000 else y)

Submission status: Wrong Answer

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

for i in range(N):
    tmp = X[i]
    for j in range(i,N-1):
        if (tmp[0]+X[j][0])*Mb == (tmp[1]+X[j][1])*Ma and y>tmp[2]:
            tmp[0] += X[j][0]
            tmp[1] += X[j][1]
            tmp[2] += X[j][2]
            y = min(y,tmp[2])
            
    if tmp[0]*Mb == tmp[1]*Ma:
        y = min(y, tmp[2])
print(-1 if y>4000 else y)

Submission status: Wrong Answer

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

for i in range(N):
    tmp = X[i]
    for j in range(i,N-1):
        if (tmp[0]+X[j][0])*Mb == (tmp[1]+X[j][1])*Ma:
            tmp[0] += X[j][0]
            tmp[1] += X[j][1]
            tmp[2] += X[j][2]
            
    if tmp[0]*Mb == tmp[1]*Ma:
        y = min(y, tmp[2])
print(-1 if y>4000 else y)