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:
dp_n = 10
n, ma, mb = map(int, input().split())
dp = [[float('inf')]*dp_n for _ in range(dp_n)]
dp[0][0] = 0
for i in range(n):
    a, b, c = map(int, input().split())
    updated = [[False]*dp_n for _ in range(dp_n)]
    for j in range(dp_n):
        for i in range(dp_n):
            if dp[i][j] != float('inf'):
                try:
                    if not updated[i][j]:
                        dp[i+a][j+b] = min(dp[i+a][j+b],dp[i][j]+c)
                        updated[i+a][j+b] = True
                except:
                    pass
res = float('inf')
for j in range(1,dp_n):
    for i in range(1,dp_n):
        if i/ma*mb == j or mb/j*i == ma or ma/mb == i/b:
            res = min(res,dp[i][j])
print(res if res != float('inf') else -1)

Submission status: Wrong Answer

Solution code:
dp_n = 500
n, ma, mb = map(int, input().split())
dp = [[float('inf')]*dp_n for _ in range(dp_n)]
dp[0][0] = 0
for i in range(n):
    a, b, c = map(int, input().split())
    updated = [[False]*dp_n for _ in range(dp_n)]
    for j in range(dp_n):
        for i in range(dp_n):
            if dp[i][j] != float('inf'):
                try:
                    if not updated[i][j]:
                        dp[i+a][j+b] = min(dp[i+a][j+b],dp[i][j]+c)
                        updated[i+a][j+b] = True
                except:
                    pass
res = float('inf')
for j in range(1,dp_n):
    for i in range(1,dp_n):
        if i/ma*mb == j or mb/j*i == ma:
            res = min(res,dp[i][j])
print(res if res != float('inf') else -1)

Submission status: Wrong Answer

Solution code:
dp_n = 410
n, ma, mb = map(int, input().split())
dp = [[float('inf')]*dp_n for _ in range(dp_n)]
dp[0][0] = 0
for i in range(n):
    a, b, c = map(int, input().split())
    updated = [[False]*dp_n for _ in range(dp_n)]
    for j in range(dp_n):
        for i in range(dp_n):
            if dp[i][j] != float('inf'):
                try:
                    if not updated[i][j]:
                        dp[i+a][j+b] = min(dp[i+a][j+b],dp[i][j]+c)
                        updated[i+a][j+b] = True
                except:
                    pass
res = float('inf')
for j in range(1,dp_n):
    for i in range(1,dp_n):
        if i/ma*mb == j or mb/j*i == ma:
            res = min(res,dp[i][j])
print(res if res != float('inf') else -1)

Submission status: Wrong Answer

Solution code:
n, ma, mb = map(int, input().split())
dp = [[float('inf')]*410 for _ in range(410)]
dp[0][0] = 0
for i in range(n):
    a, b, c = map(int, input().split())
    updated = [[False]*410 for _ in range(410)]
    for j in range(410):
        for i in range(410):
            if dp[i][j] != float('inf'):
                try:
                    if updated[i+a][j+b] == False:
                        dp[i+a][j+b] = min(dp[i+a][j+b],dp[i][j]+c)
                        updated[i+a][j+b] = True
                except:
                    pass
res = float('inf')
for j in range(1,410):
    for i in range(1,410):
        if i/ma*mb == j or mb/j*i == ma:
            res = min(res,dp[i][j])
print(res if res != float('inf') else -1)

Submission status: Wrong Answer

Solution code:
n, ma, mb = map(int, input().split())
dp = [[float('inf')]*410 for _ in range(410)]
dp[0][0] = 0
for i in range(n):
    a, b, c = map(int, input().split())
    updated = [[False]*410 for _ in range(410)]
    for j in range(410):
        for i in range(410):
            if dp[i][j] != float('inf'):
                try:
                    if not updated[i+a][j+b]:
                        dp[i+a][j+b] = min(dp[i+a][j+b],dp[i][j]+c)
                        updated[i+a][j+b] = 1
                except:
                    pass
res = float('inf')
for j in range(1,410):
    for i in range(1,410):
        if i/ma*mb == j or mb/j*i == ma:
            res = min(res,dp[i][j])
print(res if res != float('inf') else -1)

Submission status: Wrong Answer

Solution code:
n, ma, mb = map(int, input().split())
dp = [[float('inf')]*410 for _ in range(410)]
dp[0][0] = 0
for i in range(n):
    a, b, c = map(int, input().split())
    updated = [[False]*410 for _ in range(410)]
    for j in range(410):
        for i in range(410):
            if dp[i][j] != float('inf'):
                try:
                    if not updated[i+a][j+b]:
                        dp[i+a][j+b] = min(dp[i+a][j+b],dp[i][j]+c)
                        updated[i+a][j+b] = 1
                except:
                    pass
res = float('inf')
for j in range(1,410):
    for i in range(1,410):
        if i/ma*mb == j:
            res = min(res,dp[i][j])
print(res if res != float('inf') else -1)

Submission status: Wrong Answer

Solution code:
n, ma, mb = map(int, input().split())
dp = [[float('inf')]*410 for _ in range(410)]
dp[0][0] = 0
for i in range(n):
    a, b, c = map(int, input().split())
    updated = [[False]*410 for _ in range(410)]
    for j in range(410):
        for i in range(410):
            if dp[i][j] != float('inf'):
                try:
                    if not updated[i+a][j+b]:
                        dp[i+a][j+b] = min(dp[i+a][j+b],dp[i][j]+c)
                        updated[i+a][j+b] = 1
                except:
                    pass
res = float('inf')
for j in range(1,410):
    for i in range(1,410):
        if ma/i*j == mb and dp[i][j] != float('inf'):
            res = min(res,dp[i][j])
print(res if res != float('inf') else -1)

Submission status: Wrong Answer

Solution code:
n, ma, mb = map(int, input().split())
dp = [[float('inf')]*500 for _ in range(500)]
dp[0][0] = 0
for i in range(n):
    a, b, c = map(int, input().split())
    updated = {}
    for j in range(500):
        for i in range(500):
            if dp[i][j] != float('inf'):
                try:
                    if str(i+a)+":"+str(j+b) not in updated:
                        dp[i+a][j+b] = min(dp[i+a][j+b],dp[i][j]+c)
                        updated[str(i+a)+":"+str(j+b)] = 1
                except:
                    pass
res = float('inf')
for j in range(1,500):
    for i in range(1,500):
        if ma/i*j == mb and dp[i][j] != float('inf'):
            res = min(res,dp[i][j])
print(res if res != float('inf') else -1)

Submission status: Wrong Answer

Solution code:
n, ma, mb = map(int, input().split())
dp = [[float('inf')]*500 for _ in range(500)]
dp[0][0] = 0
for i in range(n):
    a, b, c = map(int, input().split())
    for j in range(500):
        for i in range(500):
            if dp[i][j] != float('inf'):
                try:
                    dp[i+a][j+b] = min(dp[i+a][j+b],dp[i][j]+c)
                except:
                    pass
res = float('inf')
for j in range(1,500):
    for i in range(1,500):
        if ma/i*j == mb and dp[i][j] != float('inf'):
            res = min(res,dp[i][j])
print(res if res != float('inf') else -1)

Submission status: Wrong Answer

Solution code:
n, ma, mb = map(int, input().split())
dp = [[float('inf')]*500 for _ in range(500)]
dp[0][0] = 0
for i in range(n):
    a, b, c = map(int, input().split())
    for j in range(500):
        for i in range(500):
            if dp[i][j] != -1:
                try:
                    dp[i+a][j+b] = min(dp[i+a][j+b],dp[i][j]+c)
                except:
                    pass
res = float('inf')
for j in range(1,500):
    for i in range(1,500):
        if ma/i*j == mb and dp[i][j] != float('inf'):
            res = min(res,dp[i][j])
print(res if res != float('inf') else -1)

Submission status: Wrong Answer

Solution code:
import math


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


def read_ints():
    return list(map(int, input().strip().split(' ')))


def read_into(*A):
    for arr, a in zip(A, read_ints()):
        arr.append(a)


def solve():
    """
    dp[400][400] = 160.000*10 = 1.600.000

    dp[x+a[i]][y+b[i]] = min(dp[x+a[i]][y+b[i]], c[i]+dp[x][y])
    dp[0][0] = 0
    """
    n, ma, mb = read_ints()
    A, B, C = [], [], []
    for _ in range(n):
        read_into(A, B, C)
    dp_upper_A, dp_upper_B = max(A)*n+1, max(B)*n+1
    dp = [
        [[math.inf]*dp_upper_B for _ in range(dp_upper_A)] for _ in range(n+1)
    ]
    dp[0][0][0] = 0
    for i in range(n):
        for x in range(dp_upper_A):
            for y in range(dp_upper_B):
                if x+A[i] < dp_upper_A and y+B[i] < dp_upper_B:
                    dp[i+1][x+A[i]][y+B[i]] = min(dp[i+1][x+A[i]][y+B[i]], dp[i][x+A[i]][y+B[i]], C[i]+dp[i][x][y])
    i = 1
    min_cost = math.inf
    while i*ma < dp_upper_A and i*mb < dp_upper_B:
        min_cost = min(min_cost, dp[n][i*ma][i*mb])
        i += 1
    return -1 if min_cost == math.inf else min_cost


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

Submission status: Wrong Answer

Solution code:
import math


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


def read_ints():
    return list(map(int, input().strip().split(' ')))


def read_into(*A):
    for arr, a in zip(A, read_ints()):
        arr.append(a)


def solve():
    """
    dp[400][400] = 160.000*10 = 1.600.000

    dp[x+a[i]][y+b[i]] = min(dp[x+a[i]][y+b[i]], c[i]+dp[x][y])
    dp[0][0] = 0
    """
    n, ma, mb = read_ints()
    A, B, C = [], [], []
    for _ in range(n):
        read_into(A, B, C)
    dp_upper_A, dp_upper_B = max(A)*n+1, max(B)*n+1
    dp = [
        [[math.inf]*dp_upper_B for _ in range(dp_upper_A)] for _ in range(n+1)
    ]
    dp[0][0][0] = 0
    for i in range(n):
        for x in range(dp_upper_A):
            for y in range(dp_upper_B):
                if x+A[i] < dp_upper_A and y+B[i] < dp_upper_B:
                    dp[i+1][x+A[i]][y+B[i]] = min(dp[i+1][x+A[i]][y+B[i]], C[i]+dp[i][x][y])
    i = 1
    min_cost = math.inf
    while i*ma < dp_upper_A and i*mb < dp_upper_B:
        min_cost = min(min_cost, dp[n][i*ma][i*mb])
        i += 1
    return -1 if min_cost == math.inf else min_cost


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

Submission status: Wrong Answer

Solution code:
import math


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


def read_ints():
    return list(map(int, input().strip().split(' ')))


def read_into(*A):
    for arr, a in zip(A, read_ints()):
        arr.append(a)


def solve():
    """
    dp[400][400] = 160.000*10 = 1.600.000

    dp[x+a[i]][y+b[i]] = min(dp[x+a[i]][y+b[i]], c[i]+dp[x][y])
    dp[0][0] = 0
    """
    n, ma, mb = read_ints()
    A, B, C = [], [], []
    for _ in range(n):
        read_into(A, B, C)
    dp_upper_A, dp_upper_B = max(A)*n+1, max(B)*n+1
    dp = [
        [[math.inf]*dp_upper_B for _ in range(dp_upper_A)] for _ in range(n+1)
    ]
    dp[0][0][0] = 0
    for i in range(n):
        for x in range(dp_upper_A):
            for y in range(dp_upper_B):
                if x+A[i] < dp_upper_A and y+B[i] < dp_upper_B:
                    dp[i+1][x+A[i]][y+B[i]] = min(dp[i][x+A[i]][y+B[i]], C[i]+dp[i][x][y])
    i = 1
    min_cost = math.inf
    while i*ma < dp_upper_A and i*mb < dp_upper_B:
        min_cost = min(min_cost, dp[n][i*ma][i*mb])
        i += 1
    return -1 if min_cost == math.inf else min_cost


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

Submission status: Wrong Answer

Solution code:
import math


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


def read_ints():
    return list(map(int, input().strip().split(' ')))


def read_into(*A):
    for arr, a in zip(A, read_ints()):
        arr.append(a)


def solve():
    """
    dp[400][400] = 160.000*10 = 1.600.000

    dp[x+a[i]][y+b[i]] = min(dp[x+a[i]][y+b[i]], c[i]+dp[x][y])
    dp[0][0] = 0
    """
    n, ma, mb = read_ints()
    A, B, C = [], [], []
    for _ in range(n):
        read_into(A, B, C)
    dp_upper_A, dp_upper_B = max(A)*n+1, max(B)*n+1
    dp = [
        [math.inf]*dp_upper_B for _ in range(dp_upper_A)
    ]
    used = [
        [-1]*dp_upper_B for _ in range(dp_upper_A)
    ]
    dp[0][0] = 0
    for i in range(n):
        for x in range(dp_upper_A):
            for y in range(dp_upper_B):
                if used[x][y] != i and x+A[i] < dp_upper_A and y+B[i] < dp_upper_B:
                    dp[x+A[i]][y+B[i]] = min(dp[x+A[i]][y+B[i]], C[i]+dp[x][y])
                    used[x][y] = i
    i = 1
    min_cost = math.inf
    while i*ma < dp_upper_A and i*mb < dp_upper_B:
        min_cost = min(min_cost, dp[i*ma][i*mb])
        i += 1
    return -1 if min_cost == math.inf else min_cost


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

Submission status: Wrong Answer

Solution code:
import math


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


def read_ints():
    return list(map(int, input().strip().split(' ')))


def read_into(*A):
    for arr, a in zip(A, read_ints()):
        arr.append(a)


def solve():
    """
    dp[400][400] = 160.000*10 = 1.600.000

    dp[x+a[i]][y+b[i]] = min(dp[x+a[i]][y+b[i]], c[i]+dp[x][y])
    dp[0][0] = 0
    """
    n, ma, mb = read_ints()
    A, B, C = [], [], []
    for _ in range(n):
        read_into(A, B, C)
    dp_upper_A, dp_upper_B = max(A)*n+1, max(B)*n+1
    dp = [
        [math.inf]*dp_upper_B for _ in range(dp_upper_A)
    ]
    dp[0][0] = 0
    for x in range(dp_upper_A):
        for y in range(dp_upper_B):
            for i in range(n):
                if x+A[i] < dp_upper_A and y+B[i] < dp_upper_B:
                    dp[x+A[i]][y+B[i]] = min(dp[x+A[i]][y+B[i]], C[i]+dp[x][y])
    i = 1
    min_cost = math.inf
    while i*ma < dp_upper_A and i*mb < dp_upper_B:
        min_cost = min(min_cost, dp[i*ma][i*mb])
        i += 1
    return -1 if min_cost == math.inf else min_cost


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

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")

n, ma, mb = list(map(int, input().split()))
abc = [None]*n
for i in range(n):
    a,b,c = tuple(map(int, input().split()))
    abc[i] = (a*mb - b*ma, c)
abc.sort()
def halfcomb(l):
    n = len(l)
    n1 = n//2
    n2 = (n+1)//2
    l1 = []
    l2 = []
    for b in range(1<<n2):
        v11, v12 = 0, 0
        v21, v22 = 0, 0
        for i in range(n2):
            if b&1<<i:
                item = l[n1+i]
                v21+=item[0]
                v22+=item[1]
                if b<(1<<n1) and i<n1:
                    item = l[i]
                    v11+=item[0]
                    v12+=item[1]
        if b<(1<<n1):
            l1.append((v11,v12))
        l2.append((v21,v22))
    return l1, l2
l1, l2 = halfcomb(abc)
l1.sort()
l2.sort()
from bisect import bisect_left as bl
ans = float("inf")
i = len(l2)-1
for v,c in l1:
    while i>=0 and l2[i][0]>=-v:
        i -= 1
    if i+1<len(l2) and l2[i+1][0]==-v and (c>0 or l2[i+1][1]>0):
        ans = min(ans, c+l2[i+1][1])
    if i<0:
        break
    
    
# for v,c in l1:
#     ind = bl(l2, (-v,-1))
#     if ind<len(l2) and l2[ind][0]==-v and not (c==0 and l2[ind][1]==0):
#         ans = min(ans, c + l2[ind][1])
if ans == float("inf"):
    ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n, ma, mb = list(map(int, input().split()))
abc = [None]*n
for i in range(n):
    a,b,c = tuple(map(int, input().split()))
    abc[i] = (a*mb - b*ma, c)
abc.sort()
def halfcomb(l):
    n = len(l)
    n1 = n//2
    n2 = (n+1)//2
    l1 = []
    l2 = []
    for b in range(1<<n2):
        v11, v12 = 0, 0
        v21, v22 = 0, 0
        for i in range(n2):
            if b&1<<i:
                item = l[n1+i]
                v21+=item[0]
                v22+=item[1]
                if b<(1<<n1) and i<n1:
                    item = l[i]
                    v11+=item[0]
                    v12+=item[1]
        if b<(1<<n1):
            l1.append((v11,v12))
        l2.append((v21,v22))
    return l1, l2
l1, l2 = halfcomb(abc)
l1.sort()
l2.sort()
from bisect import bisect_left as bl
ans = float("inf")
for v,c in l1:
    ind = bl(l2, (-v,-1))
    if ind<len(l2) and l2[ind][0]==-v and not (c==0 and l2[ind][1]==0):
        ans = min(ans, c + l2[ind][1])
if ans == float("inf"):
    ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n, ma, mb = list(map(int, input().split()))
abc = [None]*n
for i in range(n):
    a,b,c = tuple(map(int, input().split()))
    abc[i] = (a*mb - b*ma, c)
abc.sort()
def halfcomb(l):
    n = len(l)
    n1 = n//2
    n2 = (n+1)//2
    l1 = []
    l2 = []
    for b in range(1<<n2):
        val1 = (0,0)
        val2 = (0,0)
        for i in range(n2):
            if b&1<<i:
                item = l[n1+i]
                val2 = (val2[0]+item[0], val2[1]+item[1])
                if b<(1<<n1) and i<n1:
                    item = l[i]
                    val1 = (val1[0]+item[0], val1[1]+item[1])
        if b<(1<<n1):
            l1.append(val1)
        l2.append(val2)
    return l1, l2
l1, l2 = halfcomb(abc)
l1.sort()
l2.sort()
from bisect import bisect_left as bl
ans = float("inf")
for v,c in l1:
    ind = bl(l2, (-v,-1))
    if ind<len(l2) and l2[ind][0]==-v and not (c==0 and l2[ind][1]==0):
        ans = min(ans, c + l2[ind][1])
if ans == float("inf"):
    ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
from collections import deque,defaultdict

inp=lambda v,w:v[0]*w[0]+v[1]*w[1]
isprop = lambda a,b: a[0]*b[1]==a[1]*b[0]
cwsum = lambda l:[sum([x[0] for x in l]), sum([x[1] for x in l])]
chmin = lambda a,b:(a,b)[b<a]
# cwsum = lambda l:np.sum(l,axis=0)
            
def main():
    N,Ma,Mb=map(int,input().split())
    ch=dict()
    infty=10**9
    for _ in range(N):
        a,b,c=map(int,input().split())
        ch[(a,b)]=c
    p=[(-1)*Mb,Ma]
    cand=[ch[v] for v in iter(ch) if inp(p,v)==0]
    if cand:
        m1=min(cand)
    else:
        m1=infty
    pch={inp(p,v):ch[v] for v in iter(ch) if inp(p,v)!=0} #p方向への射影
    kpch=list(pch.keys())
    n=len(kpch)
    if n==0:
        print(m1)
        return
    X=n*max([abs(x) for x in kpch])
    dp=[[infty]*(2*X+1) for _ in range(n)]
    for i in range(n):
        for j in range(2*X+1):
            if i==0 and j==X+kpch[0]:
                dp[i][j]=pch[kpch[0]]
            elif i>0 and j>=kpch[i] and j<=2*X+kpch[i]:
                dp[i][j]=chmin(dp[i-1][j],dp[i-1][j-kpch[i]]+pch[kpch[i]])
            elif i>0:
                dp[i][j]=dp[i-1][j]
    # print(ch)
    # print(kpch)
    # print(pch)
    # print(dp)
    # print(m1y)
    if dp[n-1][X]==infty:
        if m1<infty:
            print(m1)
        else:
            print(-1)
    else:
        print(min(m1,dp[n-1][X]))

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, A, B = map(int, input().split())
chem = []
for i in range(N):
    a, b, c = map(int, input().split())
    chem.append((a, b, c))

MAXX = N*10 + 1
MAXMONEY = 100*MAXX
money = [[MAXMONEY]*MAXX for _ in range(MAXX)]
ans = MAXMONEY

money[0][0] = 0
for i in range(N):
    a, b, cc = chem[i]
    for c1 in range(MAXX):
        for c2 in range(MAXX):
            if money[c1][c2] < MAXMONEY:
                newm = money[c1][c2] + cc
                if (c1 + a < MAXX) and (c2+b < MAXX) and (money[c1+a][c2+b] > newm):
                    money[c1 + a][c2 + b] = newm

rr = int(min((MAXX+0.9*A)//A, (MAXX+0.9*B)//B))
for bai in range(1, rr):
    if money[A*bai][B*bai] < ans:
        ans = money[A*bai][B*bai]

if ans >= MAXMONEY: ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
N, A, B = map(int, input().split())
chem = []
for i in range(N):
    a, b, c = map(int, input().split())
    chem.append((a, b, c))

MAXX = 40*10 + 1
MAXMONEY = 100*MAXX
money = [[MAXMONEY]*MAXX for _ in range(MAXX)]
ans = MAXMONEY

money[0][0] = 0
for i in range(N):
    a, b, c = chem[i]
    for c1 in range(MAXX):
        for c2 in range(MAXX):
            if money[c1][c2] < MAXMONEY:
                newm = money[c1][c2] + c
                if (c1 + a < MAXX) and (c2+b < MAXX) and (money[c1+a][c2+b] > newm):
                    money[c1 + a][c2 + b] = newm

rr = int(min((MAXX+0.9*A)//A, (MAXX+0.9*B)//B))
for bai in range(1, rr):
    if money[A*bai][B*bai] < ans:
        ans = money[A*bai][B*bai]

if ans >= MAXMONEY: ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
N, A, B = map(int, input().split())
chem = []
for i in range(N):
    a, b, c = map(int, input().split())
    chem.append((a, b, c))

MAXX = 40*10 + 1
MAXMONEY = 100*(N+1)
money = [[MAXMONEY]*MAXX for _ in range(MAXX)]
ans = MAXMONEY

money[0][0] = 0
for i in range(N):
    a, b, c = chem[i]
    for c1 in range(MAXX):
        for c2 in range(MAXX):
            if money[c1][c2] < MAXMONEY:
                newm = money[c1][c2] + c
                if (c1 + a < MAXX) and (c2+b < MAXX) and (money[c1+a][c2+b] > newm):
                    money[c1 + a][c2 + b] = newm

rr = int(min((MAXX+0.9*A)//A, (MAXX+0.9*B)//B))
for bai in range(1, rr):
    if money[A*bai][B*bai] < ans:
        ans = money[A*bai][B*bai]

if ans >= MAXMONEY: ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
N, A, B = map(int, input().split())
chem = []
for i in range(N):
    a, b, c = map(int, input().split())
    chem.append((a, b, c))

MAXX = 40*10 + 1
MAXMONEY = 100*(N+1)
money = [[MAXMONEY]*MAXX for _ in range(MAXX)]
ans = MAXMONEY

money[0][0] = 0
for i in range(N):
    a, b, c = chem[i]
    for c1 in range(MAXX):
        for c2 in range(MAXX):
            if money[c1][c2] < MAXMONEY:
                newm = money[c1][c2] + c
                if (c1 + a < MAXX) and (c2+b < MAXX) and (money[c1+a][c2+b] > newm):
                    money[c1 + a][c2 + b] = newm

rr = int(min((MAXX+0.5*A)//A, (MAXX+0.5*B)//B))
for bai in range(1, rr):
    if money[A*bai][B*bai] < ans:
        ans = money[A*bai][B*bai]

if ans >= MAXMONEY: ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
N, A, B = map(int, input().split())
chem = []
for i in range(N):
    a, b, c = map(int, input().split())
    chem.append((a, b, c))

MAXX = 40*10 + 1
MAXMONEY = 100*N+10
money = [[MAXMONEY]*MAXX for _ in range(MAXX)]
ans = MAXMONEY

money[0][0] = 0
for i in range(N):
    a, b, c = chem[i]
    for c1 in range(MAXX):
        for c2 in range(MAXX):
            if money[c1][c2] < MAXMONEY:
                newm = money[c1][c2] + c
                if (c1 + a < MAXX) and (c2+b < MAXX) and (money[c1+a][c2+b] > newm):
                    money[c1 + a][c2 + b] = newm
                    if (c1 + a)*B == (c2+b)*A and ans > newm:
                        ans = newm
if ans >= MAXMONEY: ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
N,MA,MB = map(int, input().split())
A = []
for _ in range(N):
  a,b,c = map(int, input().split())
  A.append([a,b,c])
  
# dp[i][ca][cb]
# i番目までのitemでaがca gram, bがcb gram 変えるときの最小コスト
# i:1-indexed. so max(i) = N
# maxca,maxcb = N*10
# max_cost(inf) > N *100
# N <= 40

gram_max = 410
cost_inf = 10000
dp = [[[ cost_inf for _ in range(gram_max)] for _ in range(gram_max)] for _ in range(N+1)]

dp[0][0][0] = 0
for i in range(1,N+1):
  a,b,c = A[i-1]
  for ca in range(0,gram_max):
    for cb in range(0,gram_max):
      dp[i][ca][cb] = min(dp[i][ca][cb], dp[i-1][ca][cb]) # なにもしない
      if ca-a >= 0 and cb-b >= 0:
        dp[i][ca][cb] = min(dp[i][ca][cb], dp[i-1][ca-a][cb-b] + c) 
        
ans = 10000
for ca in range(1,gram_max):
  for cb in range(1,gram_max):
    # ca:cb = MA:MB
    if MA*ca == MB*cb:
      ans = min(ans, dp[N][ca][cb])
      
print(ans if ans < 5000 else -1)

Submission status: Wrong Answer

Solution code:
N,MA,MB = map(int, input().split())
A = []
for _ in range(N):
  a,b,c = map(int, input().split())
  A.append([a,b,c])
  
# dp[i][ca][cb]
# i番目までのitemでaがca gram, bがcb gram 変えるときの最小コスト
# i:1-indexed. so max(i) = N
# maxca,maxcb = N*10
# max_cost(inf) > N *100
# N <= 40

gram_max = 410
cost_inf = 10000
dp = [[[ cost_inf for _ in range(gram_max)] for _ in range(gram_max)] for _ in range(N+1)]

dp[0][0][0] = 0
for i in range(1,N+1):
  a,b,c = A[i-1]
  for ca in range(0,10*i+1):
    for cb in range(0,10*i+1):
      dp[i][ca][cb] = min(dp[i][ca][cb], dp[i-1][ca][cb]) # なにもしない
      if ca-a >= 0 and cb-b >= 0:
        dp[i][ca][cb] = min(dp[i][ca][cb], dp[i-1][ca-a][cb-b] + c) 
        
ans = 10000
for ca in range(1,gram_max):
  for cb in range(1,gram_max):
    # ca:cb = MA:MB
    if MA*ca == MB*cb:
      ans = min(ans, dp[N][ca][cb])
      
print(ans if ans < 5000 else -1)

Submission status: Wrong Answer

Solution code:
n,ma,mb= map(int,input().split())
dl=[]
d=abs(mb-ma)
al=[]
for _ in range(n):
    a,b,c= map(int,input().split())
    if d>=0:
        dl.append((b-a,c))
        al.append(a)
    else:
        dl.append((a-b,c))
        al.append(b)
gmin=[999999999999999]
def dfs(id,diff,cost,suma):
    if diff%d==0 and diff//d!=0 and suma%ma==0 and suma//ma!=0 and cost!=0:
        gmin[0]=min(gmin[0],cost)
        return
    if id==n:return
    dfs(id+1,diff,cost,suma)
    if cost+dl[id][1]>gmin[0]:
        return
    dfs(id+1,diff+dl[id][0],cost+dl[id][1],suma+al[id])

def dfszero(id,diff,cost):
    if diff==0 and cost!=0:
        gmin[0]=min(gmin[0],cost)
        return
    if id==n:return
    dfszero(id+1,diff,cost)
    if cost+dl[id][1]>gmin[0]:return
    dfszero(id+1,diff+dl[id][0],cost+dl[id][1])

if d==0:dfszero(0,0,0)
else:dfs(0,0,0,0)
if gmin[0]==999999999999999:
    print(-1)
else:print(gmin[0])

Submission status: Wrong Answer

Solution code:
n,ma,mb= map(int,input().split())
dl=[]
d=abs(mb-ma)
al=[]
for _ in range(n):
    a,b,c= map(int,input().split())
    if d>=0:
        dl.append((b-a,c))
        al.append(a)
    else:
        dl.append((a-b,c))
        al.append(b)
gmin=[999999999999999]
def dfs(id,diff,cost,suma):
    if diff%d==0 and diff//d!=0 and suma%ma==0 and suma//ma!=0 and cost!=0:
        gmin[0]=min(gmin[0],cost)
        return
    if id==n:return
    dfs(id+1,diff,cost,suma)
    if cost+dl[id][1]>gmin[0]:
        return
    dfs(id+1,diff+dl[id][0],cost+dl[id][1],suma+al[id])

def dfszero(id,diff,cost):
    if diff==0 and cost!=0:
        gmin[0]=min(gmin[0],cost)
        return
    if id==n:return
    dfszero(id+1,diff,cost)
    if cost+dl[id][1]>gmin[0]:return
    dfszero(id+1,diff+dl[id][0],cost+dl[id][1])

if d==0:dfszero(0,0,0)
else:dfs(0,0,0,0)
if gmin[0]==999999999999999:
    print(0)
else:print(gmin[0])

Submission status: Wrong Answer

Solution code:
n,ma,mb= map(int,input().split())
dl=[]
d=abs(mb-ma)
al=[]
for _ in range(n):
    a,b,c= map(int,input().split())
    if d>=0:
        dl.append((b-a,c))
        al.append(a)
    else:
        dl.append((a-b,c))
        al.append(b)
gmin=[999999999999999]
def dfs(id,diff,cost,suma):
    if diff%d==0 and diff//d!=0 and suma%ma==0 and suma//ma!=0 and cost!=0:
        gmin[0]=min(gmin[0],cost)
        return
    if id==n:return
    dfs(id+1,diff,cost,suma)
    if cost+dl[id][1]>gmin[0]:
        return
    dfs(id+1,diff+dl[id][0],cost+dl[id][1],suma+al[id])

def dfszero(id,diff,cost):
    if diff==0 and cost!=0:
        gmin[0]=min(gmin[0],cost)
        return
    if id==n:return
    dfszero(id+1,diff,cost)
    if cost+dl[id][1]>gmin[0]:return
    dfszero(id+1,diff+dl[id][0],cost+dl[id][1])

if d==0:dfszero(0,0,0)
else:dfs(0,0,0,0)
print(gmin[0])

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, x, y = map(int, readline().split())
    mat = []
    for _ in range(n):
        a, b, c = map(int, readline().split())
        mat.append((a, b, c))

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

    for i in range(1, n + 1):
        a, b, c = mat[i - 1]
        for j in range(401):
            for k in range(401):
                if j - a >= 0 and k - b >= 0:
                    dp[i][j][k] = min(dp[i - 1][j][k], dp[i - 1][j - a][k - b] + c)

    ans = INF
    for i, j in zip(range(x, 401, x), range(y, 401, y)):
        ans = min(ans, dp[n][i][j])
    if ans == INF:
        print(-1)
    else:
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = map(int,input().split())
L = []
for _ in range(N):
    L.append( list(map(int,input().split())) )
dp = [[[10**10 for _ in range(601)] for _ in range(601)] for _ in range(N+10)]
dp[0][0][0] = 0

for l in range(N):
    for i in range(200):
        for j in range(200):
            dp[l+1][i][j] = min(dp[l+1][i][j],dp[l][i][j])
            dp[l+1][i+L[l][0]][j+L[l][1]] = min(dp[l+1][i+L[l][0]][j+L[l][1]],dp[l][i][j] + L[l][2])


ans = 10**10
for i in range(1,10000):
    try: ans = min(ans, dp[N+1][Ma*i][Mb*i])
    except: pass
if ans == 10**10: print(-1)
else: print(ans)

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = map(int,input().split())
L = []
for _ in range(N):
    L.append( list(map(int,input().split())) )
dp = [[[10**10 for _ in range(601)] for _ in range(601)] for _ in range(N+10)]
dp[0][0][0] = 0

for l in range(N):
    for i in range(500):
        for j in range(500):
            dp[l+1][i][j] = min(dp[l+1][i][j],dp[l][i][j])
            dp[l+1][i+L[l][0]][j+L[l][1]] = min(dp[l+1][i+L[l][0]][j+L[l][1]],dp[l][i][j] + L[l][2])


ans = 10**10
for l in range(N):
    for i in range(1,10000):
        try: ans = min(ans, dp[l][Ma*i][Mb*i])
        except: pass
if ans == 10**10: print(-1)
else: print(ans)

Submission status: Wrong Answer

Solution code:
n,ma,mb=map(int,input().split())
abc=[list(map(int,input().split())) for _ in range(n)]
import numpy as np
dp=np.full((1001,1001),5001)
dp[0,0]=0
for a,b,c in abc:
  dp[a:,b:]=np.minimum(dp[a:,b:],dp[:-a,:-b]+c)
ma_,mb_=ma,mb
ans=5001
while max(ma_,mb_)<1001:
  ans=min(ans,dp[ma_,mb_])
  ma_+=ma
  mb_+=mb
print(ans if ans<5001 else -1)