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())
abc=[list(map(int,input().split())) for _ in range(n)]
import numpy as np
dp=np.full((401,401),4001)
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=4001
while max(ma_,mb_)<401:
  ans=min(ans,dp[ma_,mb_])
  ma_+=ma
  mb_+=mb
print(ans if ans<4001 else -1)

Submission status: Wrong Answer

Solution code:
def twodevider(n):
  while n% 2 == 0:
    n/= 2
  if(n == 1):
    return True
  else:
    return False

N, Ma, Mb = map(int, input().split())
data = []

for i in range(N):
  data.append(tuple(map(int, input().split())))
  
memory = set()
prices = set()
count = 0

for i in range(2** N- 1):
  flag = 0
  count+= 1
  strbinc = str(bin(count))[2:].zfill(N)
  total_a = 0
  total_b = 0
  total_price = 0
  for j in range(N):
    total_a+= int(strbinc[j])* data[j][0]
    total_b+= int(strbinc[j])* data[j][1]
    total_price+= int(strbinc[j])* data[j][2]
  if(total_a* Mb == total_b* Ma):
    for item in memory:
      if(twodevider(i- item)):
        flag = 1
        break
    if(flag == 1):
      continue
    memory.add(i)
    prices.add(total_price)
    
try:
  print(min(prices))
except:
  print(-1)

Submission status: Wrong Answer

Solution code:
#!python3

LI = lambda: list(map(int, input().split()))

# input
N, Ma, Mb = LI()
ABC = [LI() for _ in range(N)]

INF = 10 ** 5


def main():
    w = [[INF] * 401 for _ in range(401)]
    w[0][0] = 0
    for a, b, c in ABC:
        l = w[:]
        for j in range(a, 401):
            for k in range(b, 401):
                l[j][k] = min(w[j][k], w[j - a][k - b] + c)
        w = l

    ans = INF
    n = 400 // max(Ma, Mb) + 1
    for i in range(1, n):
        ans = min(ans, w[Ma * i][Mb * i])
    ans = -1 if ans == INF else ans
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!python3

LI = lambda: list(map(int, input().split()))

# input
N, Ma, Mb = LI()
ABC = [LI() for _ in range(N)]

INF = 10 ** 5


def main():
    w = [[INF] * 401 for _ in range(401)]
    w[0][0] = 0
    for a, b, c in ABC:
        l = w[:]
        for j in range(a, 401):
            for k in range(b, 401):
                l[j][k] = min(l[j][k], w[j - a][k - b] + c)
        w = l

    ans = INF
    n = 400 // max(Ma, Mb) + 1
    for i in range(1, n):
        ans = min(ans, w[Ma * i][Mb * i])
    ans = -1 if ans == INF else ans
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!python3

LI = lambda: list(map(int, input().split()))

# input
N, Ma, Mb = LI()
ABC = [LI() for _ in range(N)]

INF = 10 ** 5


def main():
    w = [[INF] * 401 for _ in range(401)]
    w[0][0] = 0
    for i in range(1, N + 1):
        l = w.copy()
        a, b, c = ABC[i - 1]
        for j in range(a, 401):
            for k in range(b, 401):
                l[j][k] = min(l[j][k], w[j - a][k - b] + c)
        w = l

    ans = INF
    n = 400 // max(Ma, Mb) + 1
    for i in range(1, n):
        ans = min(ans, w[Ma * i][Mb * i])
    ans = -1 if ans == INF else ans
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]
DP = [0]*(N+1)
for i in range(N+1):
    DP[i] = [[float("inf")]*(10*N+1) for i in range(10*N+1)]
for i in range(N+1):
    DP[i][0][0] = 0
for i in range(N):
    for j in range(1, 10*N+1):
        for k in range(1,10*N+1):
            if DP[i+1][j][k] > DP[i][j-A[i][0]][k-A[i][1]]+A[i][2] and j-A[i][0] >= 0 and k-A[i][1] >= 0:
               DP[i+1][j][k] = min(DP[i][j-A[i][0]][k-A[i][1]]+A[i][2], DP[i][j][k])
            else:
                DP[i+1][j][k] = DP[i][j][k]
kouho = set()
for i in range(1, 10*N+1):
    if Ma*i < 10*N+1 and Mb*i < 10*N+1 and DP[N][Ma*i][Mb*i] != float("inf"):
        kouho.add(DP[N][Ma*i][Mb*i])
    else:
        break
if len(kouho) == 0:
    print(-1)
else:
    print(min(kouho))

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]
DP = [0]*(N+1)
for i in range(N+1):
    DP[i] = [[float("inf")]*(10*N+1) for i in range(10*N+1)]
for i in range(N+1):
    DP[i][0][0] = 0
for i in range(N):
    for j in range(1, 10*N+1):
        for k in range(1,10*N+1):
            if DP[i+1][j][k] > DP[i][j-A[i][0]][k-A[i][1]]+A[i][2] and j-A[i][0] >= 0 and k-A[i][1] >= 0:
               DP[i+1][j][k] = min(DP[i][j-A[i][0]][k-A[i][1]]+A[i][2], DP[i][j][k])
            else:
                DP[i+1][j][k] = DP[i][j][k]
kouho = set()
for i in range(1, 10*N+1):
    if Ma*i < 10*N+1 and Mb*i < 10*N+1:
        kouho.add(DP[N][Ma*i][Mb*i])
    else:
        break
if len(kouho) == 0:
    print(-1)
else:
    print(min(kouho))

Submission status: Wrong Answer

Solution code:
def main():
    n, ma, mb = map(int, input().split())
    ABC = []
    for i in range(n):
        a, b, c = map(int, input().split())
        ABC.append((a, b, c))

    INF = 10**18
    dp = [[INF]*405 for i in range(405)]
    dp[0][0] = 0
    for i in range(n):
        a, b, c = ABC[i]
        for j in range(405):
            for k in range(405):
                nj, nk = j+a, k+b
                if 1 <= nj <= 404 and 1 <= nk <= 404:
                    dp[nj][nk] = min(dp[nj][nk], dp[j][k]+c)

    import fractions
    ans = INF
    for i in range(1, 405):
        for j in range(1, 405):
            if ma*j == mb*i:
                ans = min(ans, dp[i][j])
    if ans == INF:
        print(-1)
    else:
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():

    n, ma, mb = map(int, input().split())
    ABC = []
    for i in range(n):
        a, b, c = map(int, input().split())
        ABC.append((a, b, c))

    INF = 10**18
    dp = [[INF]*405 for i in range(405)]
    dp[0][0] = 0
    for i in range(n):
        a, b, c = ABC[i]
        for j in range(405):
            for k in range(405):
                nj, nk = j+a, k+b
                if 1 <= nj <= 404 and 1 <= nk <= 404:
                    dp[nj][nk] = min(dp[nj][nk], dp[j][k]+c)

    import fractions
    ans = INF
    for i in range(1, 405):
        for j in range(1, 405):
            g = fractions.gcd(i, j)
            if i//g == ma and j//g == mb:
                ans = min(ans, dp[i][j])
    if ans == INF:
        print(-1)
    else:
        print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n,ma,mb = map(int,ipt().split())
    dp = [[10**18]*(n*10+1) for _ in [0]*(n*10+1)]
    dp[0][0] = 0
    for _ in range(n):
        a,b,c = map(int,ipt().split())
        for i in range(n*10+1-a):
            dpi = dp[i]
            dpni = dp[i+a]
            for j in range(n*10+1-b):
                if dpni[j+b] > dpi[j]+c:
                    dpni[j+b] = dpi[j]+c
    mi = 10**18
    for i in range(1,n*10//max(ma,mb)+1):
        m = dp[ma*i][mb*i]
        if m < mi:
            mi = m
    if mi == 10**18:
        print(-1)
    else:
        print(mi)
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    INF = 40001
    N, MA, MB = list(map(int, input().split(' ')))
    A, B, C = list(), list(), list()
    for _ in range(N):
        a, b, c = list(map(int, input().split(' ')))
        A.append(a)
        B.append(b)
        C.append(c)
    dp = [[INF for _ in range(8001)] for _ in range(N + 1)]
    dp[0][0] = 0
    for n in range(1, N + 1):
        a, b, c = A[n - 1], B[n - 1], C[n - 1]
        for v in range(-4000, 4001):
            if dp[n - 1][v] == 0 and v == 0:
                dp[n][v] = c + dp[n - 1][v - (MB * a - MA * b)]
            else:
                dp[n][v] = min([
                    dp[n - 1][v],
                    c + dp[n - 1][v - (MB * a - MA * b)]
                ])
    ans = dp[N][0]
    if ans >= INF:
        ans = -1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    INF = 10 ** 10
    N, MA, MB = list(map(int, input().split(' ')))
    A, B, C = list(), list(), list()
    for _ in range(N):
        a, b, c = list(map(int, input().split(' ')))
        A.append(a)
        B.append(b)
        C.append(c)
    dp = [[INF for _ in range(10001)] for _ in range(N + 1)]
    dp[0][0] = 0
    for n in range(1, N + 1):
        a, b, c = A[n - 1], B[n - 1], C[n - 1]
        for v in range(-5000, 5001):
            if dp[n - 1][v] == 0:
                dp[n][v] = c + dp[n - 1][v - (MB * a - MA * b)]
            else:
                dp[n][v] = min([
                    dp[n - 1][v],
                    c + dp[n - 1][v - (MB * a - MA * b)]
                ])
    ans = dp[N][0]
    if ans >= INF:
        ans = -1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    INF = 10 ** 10
    N, MA, MB = list(map(int, input().split(' ')))
    A, B, C = list(), list(), list()
    for _ in range(N):
        a, b, c = list(map(int, input().split(' ')))
        A.append(a)
        B.append(b)
        C.append(c)
    dp = [[INF for _ in range(8001)] for _ in range(N + 1)]
    dp[0][0] = 0
    for n in range(1, N + 1):
        a, b, c = A[n - 1], B[n - 1], C[n - 1]
        for v in range(-4000, 4001):
            if dp[n - 1][v] == 0:
                dp[n][v] = c + dp[n - 1][v - (MB * a - MA * b)]
            else:
                dp[n][v] = min([
                    dp[n - 1][v],
                    c + dp[n - 1][v - (MB * a - MA * b)]
                ])
    ans = dp[N][0]
    if ans >= INF:
        ans = -1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    INF = 40001
    N, MA, MB = list(map(int, input().split(' ')))
    A, B, C = list(), list(), list()
    for _ in range(N):
        a, b, c = list(map(int, input().split(' ')))
        A.append(a)
        B.append(b)
        C.append(c)
    dp = [[INF for _ in range(8001)] for _ in range(N + 1)]
    dp[0][0] = 0
    for n in range(1, N + 1):
        a, b, c = A[n - 1], B[n - 1], C[n - 1]
        for v in range(-4000, 4001):
            if dp[n - 1][v] == 0:
                dp[n][v] = c + dp[n - 1][v - (MB * a - MA * b)]
            else:
                dp[n][v] = min([
                    dp[n - 1][v],
                    c + dp[n - 1][v - (MB * a - MA * b)]
                ])
    ans = dp[N][0]
    if ans >= INF:
        ans = -1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    INF = 40001
    N, MA, MB = list(map(int, input().split(' ')))
    A, B, C = list(), list(), list()
    for _ in range(N):
        a, b, c = list(map(int, input().split(' ')))
        A.append(a)
        B.append(b)
        C.append(c)
    dp = [[INF for _ in range(8000)] for _ in range(N + 1)]
    dp[0][0] = 0
    for n in range(1, N + 1):
        a, b, c = A[n - 1], B[n - 1], C[n - 1]
        for v in range(-4000, 4001):
            if dp[n - 1][v] == 0:
                dp[n][v] = c + dp[n - 1][v - (MB * a - MA * b)]
            else:
                dp[n][v] = min([
                    dp[n - 1][v],
                    c + dp[n - 1][v - (MB * a - MA * b)]
                ])
    ans = dp[N][0]
    if ans >= INF:
        ans = -1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    INF = 10**5
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    n,ma,mb = LI()
    med = [LI() for _ in range(n)]
    h1 = [INF] * 100000

    for i in range(2 ** (n//2)):
        bit = 1
        ta = 0
        tb = 0
        tc = 0
        for j in range(n//2):
            if i & bit:
                a,b,c = med[j]
                ta += a
                tb += b
                tc += c
            bit <<= 1
        dif = ta * mb - tb * ma
        if h1[dif] > tc: h1[dif] = tc

    ans = INF

    for i in range(2 ** (n - n//2)):
        bit = 1
        ta = 0
        tb = 0
        tc = 0
        for j in range(n - n//2):
            if i & bit:
                a,b,c = med[j+n//2]
                ta += a
                tb += b
                tc += c
            bit <<= 1
        dif = tb * ma - ta * mb
        if h1[dif]+tc > 0:
            ans = min(ans,h1[dif]+tc)

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    INF = 10**5
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    n,ma,mb = LI()
    med = [LI() for _ in range(n)]
    h1 = [INF] * 80001

    for i in range(2 ** (n//2)):
        bit = 1
        ta = 0
        tb = 0
        tc = 0
        for j in range(n//2):
            if i & bit:
                a,b,c = med[j]
                ta += a
                tb += b
                tc += c
            bit <<= 1
        dif = ta * mb - tb * ma
        if h1[dif] > tc: h1[dif] = tc

    ans = INF

    for i in range(2 ** (n - n//2)):
        bit = 1
        ta = 0
        tb = 0
        tc = 0
        for j in range(n - n//2):
            if i & bit:
                a,b,c = med[j+n//2]
                ta += a
                tb += b
                tc += c
            bit <<= 1
        dif = tb * ma - ta * mb
        if h1[dif]+tc > 0:
            ans = min(ans,h1[dif]+tc)

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    INF = 10**5
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    n,ma,mb = LI()
    med = [LI() for _ in range(n)]
    h1 = []
    h2 = []

    for i in range(2 ** (n//2)):
        bit = 1
        ta = 0
        tb = 0
        tc = 0
        for j in range(n//2):
            if i & bit:
                a,b,c = med[j]
                ta += a
                tb += b
                tc += c
            bit <<= 1
        dif = ta * mb - tb * ma
        h1.append((dif,tc))

    for i in range(2 ** (n - n//2)):
        bit = 1
        ta = 0
        tb = 0
        tc = 0
        for j in range(n - n//2):
            if i & bit:
                a,b,c = med[j+n//2]
                ta += a
                tb += b
                tc += c
            bit <<= 1
        dif = tb * ma - ta * mb
        h2.append((dif,tc))

    h1.sort()
    h2.sort()
    i = 0
    ans = INF
    for h,c in h1:
        while h2[i][0] < h:
            i += 1
            if i >= len(h2): break
        else:
            while h2[i][0] == h:
                if c != 0 or h2[i][1] != 0:
                    ans = min(ans,c+h2[i][1])
                i += 1
                if i >= len(h2): break

        if i >= len(h2): break
    if ans < INF:
        print(ans)
    else:
        print(-1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    INF = 10**5
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    n,ma,mb = LI()
    med = [LI() for _ in range(n)]
    h1 = []
    h2 = []

    for i in range(2 ** (n//2)):
        bit = 1
        ta = 0
        tb = 0
        tc = 0
        for j in range(n//2):
            if i & bit:
                a,b,c = med[j]
                ta += a
                tb += b
                tc += c
            bit <<= 1
        dif = ta * mb - tb * ma
        h1.append((dif,tc))

    for i in range(2 ** (n - n//2)):
        bit = 1
        ta = 0
        tb = 0
        tc = 0
        for j in range(n - n//2):
            if i & bit:
                a,b,c = med[j+n//2]
                ta += a
                tb += b
                tc += c
            bit <<= 1
        dif = tb * ma - ta * mb
        h2.append((dif,tc))

    h1.sort()
    h2.sort()
    i = 0
    ans = INF
    for h,c in h1:
        while h2[i][0] < h:
            i += 1
            if i >= len(h2): break
        else:
            while h2[i][0] == h:
                if h != 0 and h2[i][0] != 0:
                    ans = min(ans,c+h2[i][1])
                i += 1
                if i >= len(h2): break
            
        if i >= len(h2): break
    if ans < INF:
        print(ans)
    else:
        print(-1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    INF = 10**5
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    n,ma,mb = LI()
    med = [LI() for _ in range(n)]
    h1 = []
    h2 = []

    for i in range(2 ** (n//2)):
        bit = 1
        ta = 0
        tb = 0
        tc = 0
        for j in range(n//2):
            if i & bit:
                a,b,c = med[j]
                ta += a
                tb += b
                tc += c
            bit <<= 1
        dif = ta * mb - tb * ma
        h1.append((dif,tc))

    for i in range(2 ** (n - n//2)):
        bit = 1
        ta = 0
        tb = 0
        tc = 0
        for j in range(n - n//2):
            if i & bit:
                a,b,c = med[j+n//2]
                ta += a
                tb += b
                tc += c
            bit <<= 1
        dif = tb * ma - ta * mb
        h2.append((dif,tc))

    h1.sort()
    h2.sort()
    i = 0
    ans = INF
    for h,c in h1:
        while h2[i][0] < h:
            i += 1
            if i >= len(h2): break
        else:
            if h == h2[i][0] and (h != 0 and h2[i][0] != 0):
                ans = min(ans,c+h2[i][1])
            continue
        break
    if ans < INF:
        print(ans)
    else:
        print(-1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def resolve():
    import sys
    input = sys.stdin.readline
    # 整数 1 つ
    # n = int(input())
    # 整数複数個
    n, ma, mb = map(int, input().split())
    # 整数 N 個 (改行区切り)
    # N = [int(input()) for i in range(N)]
    # 整数 N 個 (スペース区切り)
    # N = list(map(int, input().split()))
    # 整数 (縦 H 横 W の行列)
    A = [list(map(int, input().split())) for i in range(n)]

    Cost = [[5000] * 401 for _ in range(401)]
    Cost[0][0] = 0

    for i in range(n):
        for j in range(401):
            for k in range(401):
                if j+A[i][0] <= 400 and k+A[i][1] <= 400 and Cost[j][k] < 5000:
                    Cost[j+A[i][0]][k+A[i][1]] = min(Cost[j+A[i][0]][k+A[i][1]], (Cost[j][k] + A[i][2]))

    cmin = 5000
    for a in range(1, 401):
        for b in range(1, 401):
            if a*mb == b*ma:
                cmin = min(cmin, Cost[a][b])

    if cmin == 5000:
        print(-1)
    else:
        print(cmin)
#    print(Cost[3])

resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    import sys
    input = sys.stdin.readline
    # 整数 1 つ
    # n = int(input())
    # 整数複数個
    n, ma, mb = map(int, input().split())
    # 整数 N 個 (改行区切り)
    # N = [int(input()) for i in range(N)]
    # 整数 N 個 (スペース区切り)
    # N = list(map(int, input().split()))
    # 整数 (縦 H 横 W の行列)
    A = [list(map(int, input().split())) for i in range(n)]

    Cost = [[5000] * 401 for _ in range(401)]
    Cost[0][0] = 0

    for i in range(n):
        for j in range(401):
            for k in range(401):
                if j+A[i][0] <= 400 and k+A[i][1] <= 400 and (Cost[j][k] < 5000 or (j==0 and k==0)):
                    Cost[j+A[i][0]][k+A[i][1]] = min(Cost[j+A[i][0]][k+A[i][1]], (Cost[j][k] + A[i][2]))

    cmin = 5000
    for a in range(1, 401):
        for b in range(1, 401):
            if a/b == ma/mb:
                cmin = min(cmin, Cost[a][b])

    if cmin == 5000:
        print(-1)
    else:
        print(cmin)
#    print(Cost[3])

resolve()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n, ma, mb = map(int, input().split())
inf = 10000
med = [tuple(map(int, input().split())) for _ in range(n)]
dp = [[inf] * 401 for _ in range(401)]
dp[0][0] = 0
for i in range(401):
    for j in range(401):
        for k in range(n):
            a, b, c = med[k]
            if i - a >= 0 and j - b >= 0:
                dp[i][j] = min(dp[i][j], dp[i-a][j-b] + c)

ans = inf
for k in range(1, 401):
    if k * ma > 400 or k * mb > 400:
        break
    x, y = k * ma, k * mb
    ans = min(ans, dp[x][y])

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

Submission status: Wrong Answer

Solution code:
n, ma, mb = map(int, input().split())
inf = 10000
med = [tuple(map(int, input().split())) for _ in range(n)]
dp = [[inf] * 401 for _ in range(401)]
dp[0][0] = 0
for i in range(401):
    for j in range(401):
        for k in range(n):
            a, b, c = med[k]
            if i - a >= 0 and j - b >= 0:
                dp[i][j] = min(dp[i][j], dp[i-a][j-b] + c)

ans = inf
for k in range(1, 401):
    if k * ma > 400 or k * mb > 400:
        break
    x, y = k * ma, k * mb
    ans = min(ans, dp[x][y])

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

Submission status: Wrong Answer

Solution code:
import itertools

N,MA,MB = map(int,input().split())
arr = []

for i in range(N):
  l = list(map(int,input().split()))
  arr.append(l)
  
dp = [ [ [50000 for k in range(401)] for j in range(401)] for i in range(N+1) ]
dp[0][0][0] = 0
ans = 50000

for i,a,b in itertools.product(range(1,N+1),range(401),range(401)):
  A = arr[i-1][0]
  B = arr[i-1][1]
  C = arr[i-1][2]
  
  if A <= a and B <= b and dp[i-1][a-A][b-B] != 50000:
    dp[i][a][b] = min(dp[i-1][a][b],dp[i-1][a-A][b-B] + C)
  else:
    dp[i][a][b] = dp[i-1][a][b]

for a,b in itertools.product(range(401),range(401)):
  if a * MB == b ** MA and a != 0 and b != 0:
    ans = min(ans,dp[-1][a][b])
    
if ans == 50000:
  print(-1)
else:
  print(int(ans))

Submission status: Wrong Answer

Solution code:
import itertools

N,MA,MB = map(int,input().split())
arr = []

for i in range(N):
  l = list(map(int,input().split()))
  arr.append(l)
  
dp = [ [ [50000 for k in range(401)] for j in range(401)] for i in range(N+1) ]
dp[0][0][0] = 0
ans = 50000

for i,a,b in itertools.product(range(1,N+1),range(401),range(401)):
  A = arr[i-1][0]
  B = arr[i-1][1]
  C = arr[i-1][2]
  
  if A <= a and B <= b:
    dp[i][a][b] = min(dp[i-1][a][b],dp[i-1][a-A][b-B] + C)
  else:
    dp[i][a][b] = dp[i-1][a][b]

for a,b in itertools.product(range(401),range(401)):
  if a * MB == b ** MA and a != 0 and b != 0:
    ans = min(ans,dp[-1][a][b])
    
if ans == 50000:
  print(-1)
else:
  print(int(ans))

Submission status: Wrong Answer

Solution code:
import itertools

N,MA,MB = map(int,input().split())
arr = []

for i in range(N):
  l = list(map(int,input().split()))
  arr.append(l)
  
dp = [ [ [50000 for k in range(401)] for j in range(401)] for i in range(N+1) ]
dp[0][0][0] = 0

for i,a,b in itertools.product(range(1,N+1),range(401),range(401)):
  A = arr[i-1][0]
  B = arr[i-1][1]
  C = arr[i-1][2]
  
  if A <= a and B <= b:
    dp[i][a][b] = min(dp[i][a][b],dp[i-1][a-A][b-B] + C)
  else:
    dp[i][a][b] = dp[i-1][a][b]

ans = 50000
for i,a,b in itertools.product(range(N+1),range(401),range(401)):
  if a * MB == b ** MA and a != 0 and b != 0:
    ans = min(ans,dp[i][a][b])
if ans == 50000:
  print(-1)
else:
  print(int(ans))

Submission status: Wrong Answer

Solution code:
import itertools

N,MA,MB = map(int,input().split())
arr = []

for i in range(N):
  l = list(map(int,input().split()))
  arr.append(l)
  
dp = [ [ [50000 for k in range(401)] for j in range(401)] for i in range(N+1) ]
dp[0][0][0] = 0

for i,a,b in itertools.product(range(1,N+1),range(401),range(401)):
  A = arr[i-1][0]
  B = arr[i-1][1]
  C = arr[i-1][2]
  
  if A <= a and B <= b:
    dp[i][a][b] = min(dp[i][a-A][b-B] + C,dp[i-1][a][b])
  else:
    dp[i][a][b] = dp[i-1][a][b]

ans = 50000
for i,a,b in itertools.product(range(N+1),range(401),range(401)):
  if a * MB == b ** MA and a != 0 and b != 0:
    ans = min(ans,dp[i][a][b])
if ans == 50000:
  print(-1)
else:
  print(int(ans))

Submission status: Wrong Answer

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

import sys
# import time
# import math
# import numpy as np
# import scipy.sparse.csgraph as cs            # csgraph_from_dense(ndarray, null_value=inf), bellman_ford(G, return_predecessors=True), dijkstra, floyd_warshall
# import random                                # random, uniform, randint, randrange, shuffle, sample
# import string                                # ascii_lowercase, ascii_uppercase, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from datetime import date, datetime          # date.today(), date(year,month,day) => date obj; datetime.now(), datetime(year,month,day,hour,second,microsecond) => datetime obj; subtraction => timedelta obj
# from datetime.datetime import strptime       # strptime('2019/01/01 10:05:20', '%Y/%m/%d/ %H:%M:%S') returns datetime obj
# from datetime import timedelta               # td.days, td.seconds, td.microseconds, td.total_seconds(). abs function is also available.
# from copy import copy, deepcopy              # use deepcopy to copy multi-dimentional matrix without reference
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from fractions import gcd                    # for Python 3.4 (previous contest @AtCoder)



def main():
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79...e+308
    # inf = 2 ** 64 - 1             # (for fast JIT compile in PyPy) 1.84...e+19
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    
    
    n, ma, mb = mi()
    L = [lmi() for _ in range(n)]

    # dp[i+1][x][y] = i 番目までを使用して a を x[g], b を y[g] にする時のコスト
    dp = [[[inf]*(10*n+1) for _ in range(10*n+1)] for _ in range(n+1)]
    dp[0][0][0] = 0
    for i in range(n):
        for x in range(1,10*n+1):
            for y in range(1, 10*n+1):
                i_a, i_b, i_c = L[i]
                if x - i_a >= 0 and y - i_b >= 0:
                    dp[i+1][x][y] = min(dp[i][x][y], dp[i][x-i_a][y-i_b] + i_c)
                else:
                    dp[i+1][x][y] = dp[i][x][y]
    # for line in dp:
    #     for moo in line:
    #         print(moo)
    #     print('hi')
    
    ans = inf
    mul = (10 * n) // max(ma, mb)
    for i in range(1, mul):
        ans = min(ans, dp[n][i * ma][i * mb])
    
    print(ans) if ans != inf else print(-1)
    



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n, ma, mb = map(int, input().split())

INF = 10**10
dp = [[[INF for _ in range(400+5)] for _ in range(400+5)] for _ in range(n+5)]

for i in range(1, n+1):
    a, b, c = map(int, input().split())
    dp[i][a][b] = min(dp[i][a][b], c)
    for j in range(400+5-a):
        for k in range(400+5-b):
            dp[i][j+a][k+b] = min(dp[i][j+a][k+b], dp[i-1][j][k]+c)

ans = INF
for i in range(1, n+1):
    t = 0
    while max(ma, mb)*t <= 400:
        ans = min(ans, dp[i][ma*t][mb*t])
        t += 1

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