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())
dp=[[10**9 for j in range(400)]for i in range(400)]
dp[0][0]=0 
for i in range(n):
    a,b,c=map(int,input().split())
    for x in range(400):
        for y in range(400):
            if x<a or y<b:
                continue 
            dp[x][y]=min(dp[x][y],dp[x-a][y-b]+c)
mini=10**9 
for i in range(1,39):
    mini=min(mini,dp[ma*i][mb*i])
if mini>=10**9:
    print(-1)
else:
    print(mini)

Submission status: Wrong Answer

Solution code:
n,ma,mb=map(int,input().split())
dp=[[10**9 for j in range(500)]for i in range(500)]
dp[0][0]=0 
for i in range(n):
    a,b,c=map(int,input().split())
    for x in range(500):
        for y in range(500):
            if x<a or y<b:
                continue 
            dp[x][y]=min(dp[x][y],dp[x-a][y-b]+c)
mini=10**9 
for i in range(1,49):
    mini=min(mini,dp[ma*i][mb*i])
if mini>=10**9:
    print(-1)
else:
    print(mini)

Submission status: Wrong Answer

Solution code:

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

lis = [[float("inf")] * 401 for i in range(401)]
lis[0][0] = 0

for x in range(N):

    a,b,c = map(int,input().split())

    for i in range(401):

        for j in range(401):

            if lis[i][j] != float("inf"):

                if i + a < 400 and j + b < 400:

                    lis[i+a][j+b] = min( lis[i][j] + c , lis[i+a][j+b] )

ans = float("inf")

for i in range(400):

    i += 1

    for j in range(400):

        j += 1

        if i * Mb == j * Ma:

            ans = min(ans , lis[i][j])

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

Submission status: Wrong Answer

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

def main():
    N, Ma, Mb = map(int, input().split())

    INF = int(1e5)
    dp = [[INF] * 4200 for i in range(4200)]

    dp[0][0] = 0
    xy = set()
    xy.add((0, 0))
    for i in range(N):
        a, b, c = map(int, input().split())
        add_xy = set()
        for x, y in xy:
            if dp[x][y] + c < dp[x + a][y + b]:
                dp[x+a][y+b] = dp[x][y] + c
                add_xy.add((x+a, y+b))
        xy |= add_xy
    ans = INF
    x, y = Ma, Mb
    while x < 4200 and y < 4200:
        ans = min(ans, dp[x][y])
        x += Ma
        y += Mb
    
    if ans != INF:
        print(ans)
    else:
        print(-1)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n, ma, mb = map(int, input().split())
ls = [list(map(int, input().split())) for _ in range(n)]
MAX_N = 10 * n + 1
dp = [[[float('inf')] * MAX_N for _ in range(MAX_N)] for _ in range(n + 1)]
dp[0][0][0] = 0

for k, (a, b, c) in enumerate(ls):
    for i in range(MAX_N):
        for j in range(MAX_N):
            if i + a < MAX_N and j + b < MAX_N:
                dp[k + 1][i + a][j + b] = min(dp[k][i][j] + c,
                                              dp[k + 1][i + a][j + b],
                                              dp[k][i + a][j + b])
p = min([
    dp[n][ma * i][mb * i] for i in range(MAX_N)
    if ma * i < MAX_N and mb * i < MAX_N
])
if p < float('inf'):
    print(p)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = map(int, input().split())
ABC = [tuple(map(int, input().split())) for _ in range(N)]

INF, W = 10 ** 10, N * 10 * max(Ma, Mb)
dp = [[INF] * (W * 2 + 1) for _ in range(N + 1)]

for i, (a, b, c) in enumerate(ABC):
    diff = a * Mb - b * Ma
    dp[i + 1][diff + W] = min(dp[i][diff + W], c)
    for d in range(-W, W + 1):
        if not dp[i][d] == INF:
            dp[i + 1][diff + d] = min(dp[i][diff + d], c + dp[i][d])

print("-1" if dp[N][W] == INF else dp[N][W])

Submission status: Wrong Answer

Solution code:
n, ma, mb = map(int,input().split())
A = [list(map(int,input().split())) for i in range(n)]

s = 10

DP = [[0] * (s+1) for i in range(s+1)]
C = [[float("inf")] * (s+1) for i in range(s+1)]
DP[0][0] = 1
C[0][0] = 0

for k in range(n):
    for i in range(s, -1, -1):
        for j in range(s, -1, -1):
            x = i-A[k][0]
            y = j-A[k][1]
            # print(x,y,i,j)
            if x >= 0 and y >= 0 and DP[x][y] == 1 and C[x][y] + A[k][2] < C[i][j]:
                C[i][j] = C[x][y] + A[k][2]
                DP[i][j] = 1
            # print(DP)

ans = float("inf")
ma0 = ma
mb0 = mb
while ma <= s and mb <= s:
    if DP[ma][mb] == 1:
        ans = min(ans, C[ma][mb])
    ma += ma0
    mb += mb0

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

Submission status: Wrong Answer

Solution code:
n, ma, mb = map(int,input().split())
A = [list(map(int,input().split())) for i in range(n)]

s = 400

DP = [[0] * (s+1) for i in range(s+1)]
C = [[float("inf")] * (s+1) for i in range(s+1)]
DP[0][0] = 1
C[0][0] = 0

for k in range(n):
    for i in range(s, -1, -1):
        for j in range(s, -1, -1):
            x = i-A[k][0]
            y = j-A[k][1]
            # print(x,y,i,j)
            if x >= 0 and y >= 0 and DP[x][y] == 1 and C[x][y] + A[k][2] < C[i][j]:
                C[i][j] = C[x][y] + A[k][2]
                DP[i][j] = 1
            # print(DP)

ans = float("inf")
ma0 = ma
mb0 = mb
while ma <= s and mb <= s:
    if DP[ma][mb] == 1:
        ans = min(ans, C[ma][mb])
    ma += ma0
    mb += ma0

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

Submission status: Wrong Answer

Solution code:
n, ma, mb = map(int,input().split())
A = [list(map(int,input().split())) for i in range(n)]

s = 10

DP = [[0] * (s+1) for i in range(s+1)]
C = [[float("inf")] * (s+1) for i in range(s+1)]
DP[0][0] = 1
C[0][0] = 0

for k in range(n):
    for i in range(s, -1, -1):
        for j in range(s, -1, -1):
            x = i-A[k][0]
            y = j-A[k][1]
            # print(x,y,i,j)
            if x >= 0 and y >= 0 and DP[x][y] == 1 and C[x][y] + A[k][2] < C[i][j]:
                C[i][j] = C[x][y] + A[k][2]
                DP[i][j] = 1
            # print(DP)

ans = float("inf")
ma0 = ma
mb0 = mb
while ma <= s and mb <= s:
    if DP[ma][mb] == 1:
        ans = min(ans, C[ma][mb])
    ma += ma0
    mb += ma0

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

Submission status: Wrong Answer

Solution code:
import itertools

(n, ma, mb) = map(int, input().split())
materials = [list(map(int, input().split())) for _ in range(n)]

minimum = float("inf")
found = False
for i in range(1, n):
    for comb in list(itertools.combinations(materials, i)):
        count = 0
        sum_a = 0
        sum_b = 0
        for (a, b, c) in comb:
            sum_a += a
            sum_b += b
            count += c
        if 0 == sum_a % ma and 0 == sum_b % mb and sum_a == sum_b:
            minimum = min(minimum, count)
            found = True
print(minimum if found else "-1")

Submission status: Wrong Answer

Solution code:
inf = float("inf")
N, Ma, Mb = map(int, input().split())
ABC = []
for _ in range(N):
    a, b, c = map(int, input().split())
    ABC.append((a, b, c))
dp = [[inf] * 420 for _ in range(420)]
dp[0][0] = 0
for a, b, c in ABC:
    for i in range(410):
        for j in range(410):
            if dp[i][j] < inf:
                dp[i+a][j+b] = min(dp[i+a][j+b], dp[i][j] + c)
ans = inf
n = 1
while max(Ma * n, Mb * n) < 420:
    ans = min(ans, dp[Ma * n][Mb * n])
    n += 1
print(ans if ans < inf else -1)

Submission status: Wrong Answer

Solution code:
inf = float("inf")
N, Ma, Mb = map(int, input().split())
ABC = []
for _ in range(N):
    a, b, c = map(int, input().split())
    ABC.append((a, b, c))
dp = [[inf] * 420 for _ in range(420)]
dp[0][0] = 0
for a, b, c in ABC:
    for i in range(410):
        for j in range(410):
            if dp[i][j] < inf:
                dp[i+a][j+b] = min(dp[i+a][j+b], dp[i][j] + c)
ans = inf
n = 1
while max(Ma * n, Mb * n) < 420:
    print(Ma * n, Mb * n)
    ans = min(ans, dp[Ma * n][Mb * n])
    n += 1
print(ans if ans < inf else -1)

Submission status: Wrong Answer

Solution code:
inf = float("inf")
N, Ma, Mb = map(int, input().split())
ABC = []
for _ in range(N):
    a, b, c = map(int, input().split())
    ABC.append((a, b, c))
dp = [[inf] * 420 for _ in range(420)]
dp[0][0] = 0
for a, b, c in ABC:
    for i in range(410):
        for j in range(410):
            if dp[i][j] < inf:
                dp[i+a][j+b] = min(dp[i+a][j+b], dp[i][j] + c)
ans = min([dp[Ma*n][Mb*n] for n in range(1, 41)])
print(ans if ans < inf else -1)

Submission status: Wrong Answer

Solution code:
n,ma,mb=map(int,input().split())
ab=[[[10**6]*401 for _ in range(401)] for _ in range(n+1)]
ab[0][0][0]=0

for p in range(1,n+1):
    a,b,c=map(int,input().split())
    for i in range(400):
        for j in range(400):
            if i+a<=400 and j+b<=400:
                ab[p][i+a][j+b]=min(ab[p][i+a][j+b],ab[p-1][i][j]+c)

ans=10**6-1
for i in range(401):
    for j in range(401):
        if i!=0 and j!=0:
            if i*mb==j*ma:
                ans=min(ans,ab[n][i][j])

if ans==10**6-1:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
n,ma,mb=map(int,input().split())
ab=[[[10**6]*401 for _ in range(401)] for _ in range(n+1)]
ab[0][0][0]=0

for p in range(1,n+1):
    a,b,c=map(int,input().split())
    for i in range(400):
        for j in range(400):
            if i+a<=400 and j+b<=400:
                ab[p][i+a][j+b]=min(ab[p][i+a][j+b],ab[p-1][i][j]+c)

ans=10**6-1
for p in range(1,n+1):
    for i in range(401):
        for j in range(401):
            if i!=0 and j!=0:
                if i*mb==j*ma:
                    ans=min(ans,ab[p][i][j])

if ans==10**6-1:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
def main():

    N, Ma, Mb = map(int, input().split())
    drug = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        drug.append([a, b, c])

    cur = dict()
    cur[(0, 0)] = 0
    for i in range(N):
        temp = cur.copy()
        for p in cur:
            a, b = p[0]+drug[i][0], p[1]+drug[i][1]

            if (a, b) not in temp:
                temp[(a, b)] = cur[p] + drug[i][2]
            else:
                temp[(a, b)] = min(temp[(a, b)], cur[p]+drug[i][2])
        cur = temp

    # print(cur)
    min_c = float('inf')
    for p in cur:
        if p != (0, 0) and p[0]*Mb == p[1]*Ma:
            min_c = min(min_c, cur[p])
    if min_c == float('inf'):
        return -1
    else:
        return min_c

Submission status: Wrong Answer

Solution code:
n,ma,mb=map(int,input().split())
abc=[]
for i in range(n):
    abc.append([int(j) for j in input().split()])

dp=[[10**18 for i in range(500)]for j in range(500)]
dp[0][0]=0

q=[[0,0]]
for a,b,c in abc:
    l=[]
    for i,j in q:
        l.append([i,j])
        l.append([i+a,j+b])
        dp[a+i][b+j]=min(dp[a+i][b+j],dp[i][j]+c)
    q=l

ans=1145141919
i=1
while ma*i<500 and mb*i<500:
    ans=min(ans,dp[ma*i][mb*i])
    i+=1
if ans==1145141919:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
n,ma,mb=map(int,input().split())
abc=[]
for i in range(n):
    abc.append([int(j) for j in input().split()])

dp=[[10**18 for i in range(401)]for j in range(401)]
dp[0][0]=0

q=[[0,0]]
for a,b,c in abc:
    l=[]
    for i,j in q:
        l.append([i,j])
        l.append([i+a,j+b])
        dp[a+i][b+j]=min(dp[a+i][b+j],dp[i][j]+c)
    q=l

ans=1145141919
i=1
while ma*i<401 and mb*i<401:
    ans=min(ans,dp[ma*i][mb*i])
    i+=1
if ans==1145141919:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
n,ma,mb=map(int,input().split())
abc=[]
for i in range(n):
    abc.append([int(j) for j in input().split()])

dp=[[10**18 for i in range(401)]for j in range(401)]
dp[0][0]=0
from collections import deque
q=deque([(0,0)])
for a,b,c in abc:
    l=[]
    while q:
        i,j=q.popleft()
        l.append([i,j])
        l.append([i+a,j+b])
        dp[a+i][b+j]=min(dp[a+i][b+j],dp[i][j]+c)
    q=deque(l)

ans=1145141919
i=1
while ma*i<401 and mb*i<401:
    ans=min(ans,dp[ma*i][mb*i])
    i+=1
if ans==1145141919:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
n,ma,mb=map(int,input().split())
abc=[]
for i in range(n):
    abc.append([int(j) for j in input().split()])

dp=[[10**100 for i in range(401)]for j in range(401)]
dp[0][0]=0
from collections import deque
q=deque([(0,0)])
p=0
for a,b,c in abc:
    l=[]
    while q:
        i,j=q.popleft()
        l.append([i,j])
        l.append([i+a,j+b])
        dp[a+i][b+j]=min(dp[a+i][b+j],dp[i][j]+c)
    q=deque(l)

ans=1145141919
i=1
while ma*i<401 and mb*i<401:
    ans=min(ans,dp[ma*i][mb*i])
    i+=1
if ans==1145141919:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
INF = float('inf')
n, ma, mb = map(int, input().split())
cmax = n * 10
t = [[INF] * (cmax + 1) for _ in range(cmax + 1)]
for _ in range(n):
  a, b, c = map(int, input().split())
  for aa in range(cmax, 0, -1):
    for bb in range(cmax, 0, -1):
      if t[aa][bb] == INF:
        continue
      t[a + aa][b + bb] = min(t[a + aa][b + bb], t[aa][bb] + c)
  t[a][b] = min(t[a][b], c)
result = INF
for a in range(1, cmax):
  for b in range(1, cmax):
    if a * mb == b * ma:
      t[a][b] = min(t[a][b], result)
if result == INF:
  result = -1
print(result)

Submission status: Wrong Answer

Solution code:
import sys
inf = float('inf')
n, ma, mb = map(int, input().split())
t = [[inf] * 401 for _ in range(401)]
t[0][0] = 0
for _ in range(n):
  a, b, c = map(int, input().split())
  for aa in range(400, -1, -1):
    for bb in range(400, -1, -1):
      if t[aa][bb] == inf:
        continue
      if t[a + aa][b + bb] > c + t[aa][bb]:
        t[a + aa][b + bb] = c + t[aa][bb]
result = inf
for a in range(400, -1, -1):
  for b in range(400, -1, -1):
    if a * mb == b * ma and t[a][b] < result:
      result = t[a][b]
if result == 0:
  print(-1)
else:
  print(result)

Submission status: Wrong Answer

Solution code:
import sys
inf = float('inf')
n, ma, mb = map(int, input().split())
t = [[inf] * 401 for _ in range(401)]
t[0][0] = 0
for _ in range(n):
  a, b, c = map(int, input().split())
  for aa in range(400, 0, -1):
    for bb in range(400, 0, -1):
      if t[aa][bb] == inf:
        continue
      if t[a + aa][b + bb] > c + t[aa][bb]:
        t[a + aa][b + bb] = c + t[aa][bb]
result = inf
for a in range(400, 0, -1):
  for b in range(400, 0, -1):
    if a * mb == b * ma and t[a][b] < result:
      result = t[a][b]
if result == 0:
  print(-1)
else:
  print(result)

Submission status: Wrong Answer

Solution code:
import sys
inf = float('inf')
n, ma, mb = map(int, input().split())
t = [[inf] * 401 for _ in range(401)]
t[0][0] = 0
for _ in range(n):
  a, b, c = map(int, input().split())
  for aa in range(400, 0, -1):
    for bb in range(400, 0, -1):
      if t[aa][bb] == inf:
        continue
      if t[a + aa][b + bb] > c + t[aa][bb]:
        t[a + aa][b + bb] = c + t[aa][bb]
result = inf
for a in range(400, 0, -1):
  for b in range(400, 0, -1):
    if a * mb == b * ma and t[a][b] < result:
      result = t[a][b]
if result == inf:
  print(-1)
else:
  print(result)

Submission status: Wrong Answer

Solution code:
import sys
n, ma, mb = map(int, input().split())
t = [[float('inf')] * 401 for _ in range(401)]
for _ in range(n):
  a, b, c = map(int, input().split())
  if t[a][b] > c:
    t[a][b] = c
  for aa in range(400, 0, -1):
    for bb in range(400, 0, -1):
      if t[aa][bb] == float('inf'):
        continue
      if t[a + aa][b + bb] > c + t[aa][bb]:
        t[a + aa][b + bb] = c + t[aa][bb]
result = float('inf')
for a in range(400, 0, -1):
  for b in range(400, 0, -1):
    if a * mb == b * ma and t[a][b] < result:
      result = t[a][b]
if result == float('inf'):
  print(-1)
else:
  print(result)

Submission status: Wrong Answer

Solution code:
import sys
n, ma, mb = map(int, input().split())
t = [[[] for _ in range(100 * n + 1)] for _ in range(n + 1)]
for i in range(n):
  a, b, c = map(int, input().split())
  t[i + 1][c].append((a, b))
  for j in range(1, 100 * n + 1):
    for aa, bb in t[i][j]:
      t[i + 1][j].append((aa, bb))
      t[i + 1][c + j].append((a + aa, b + bb))
for i in range(1, 100 * n + 1):
  for a, b in t[n][i]:
    if a % ma == 0 and b % mb == 0:
      print(i)
      sys.exit()
print(-1)

Submission status: Wrong Answer

Solution code:
n,a,b=[int(x) for x in str(input()).split()]
A=[]
for i in range(n):
  A.append([int(x) for x in str(input()).split()])
 
ans=[]
for i in range(500):
  y=[]
  for j in range(500):
    x=[]
    for k in range(n):
      x.append(-1)
    y.append(x)
  ans.append(y)

#print(ans)
#print(A)
def f(ma,mb,cost,index):

  if ans[ma][mb][index]==-1:
    newa=ma+A[index][0]
    newb=mb+A[index][1]
    newc=cost+A[index][2]
    
    #print(index,newa,newb,newc)
    if newa*b==a*newb:
      ans[ma][mb][index]=newc
    elif index==n-1:
      ans[ma][mb][index]=100000000000000000000
    else:
      ith=f(newa,newb,newc,index+1)
      ans[ma][mb][index]= min(ith,f(ma,mb,cost,index+1))
  return ans[ma][mb][index]
  
 
if f(0,0,0,0)<100000000000:
  print(f(0,0,0,0))
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
n,a,b=[int(x) for x in str(input()).split()]
A=[]
for i in range(n):
  A.append([int(x) for x in str(input()).split()])
 
ans=[]
for i in range(500):
  y=[]
  for j in range(500):
    x=[]
    for k in range(n):
      x.append(-1)
    y.append(x)
  ans.append(y)

#print(ans)
#print(A)
def f(ma,mb,cost,index):

  if ans[ma][mb][index]==-1:
    newa=ma+A[index][0]
    newb=mb+A[index][1]
    newc=cost+A[index][2]
    
    #print(index,newa,newb,newc)
    if newa*b==a*newb:
      ans[ma][mb][index]=newc
    elif index==n-1:
      ans[ma][mb][index]=100000000000000000000
    else:
      ith=f(newa,newb,newc,index+1)
      ans[ma][mb][index]= min(ith,f(ma,mb,cost,index+1))
  return ans[ma][mb][index]
  
 
if f(0,0,0,0)<100000000000000000000:
  print(f(0,0,0,0))
else:
  print(-1)

Submission status: Wrong Answer

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

INF = 10000000

dp = [[[INF for _ in range(10*N)] for _ in range(10*N)] for _ in range(N)]

for i, (a, b, c) in enumerate(ABC):
    a -= 1
    b -= 1
    if i == 0:
        dp[0][a][b] = c
        continue
    for ai in range(10*N):
        for bi in range(10*N):
            if a > ai or b > bi:
                dp[i][ai][bi] = dp[i-1][ai][bi]
            elif ai == a and bi == b:
                dp[i][ai][bi] = min(dp[i-1][ai][bi], c)
            else:
                dp[i][ai][bi] = min(dp[i-1][ai-a][bi-b]+c, dp[i-1][ai][bi])

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

Submission status: Wrong Answer

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

inf = 10 ** 10
a_sum = sum(a_list)
b_sum = sum(b_list)
dp = [[[inf for _ in range(b_sum + 1)] for _ in range(a_sum + 1)] for _ in range(N + 1)]
dp[0][0][0] = 0
for i in range(N):
    a, b, c = a_list[i], b_list[i], c_list[i]
    for j in range(a_sum + 1):
        for k in range(b_sum + 1):
            if j - a >= 0 and k - b >= 0:
                dp[i + 1][j][k] = min(dp[i][j - a][k - b] + c, dp[i][j][k])

x = 1
ans = inf
while Ma * x <= a_sum and Mb * x <= b_sum:
    ans = min(ans, dp[-1][Ma * x][Mb * x])
    x += 1

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

Submission status: Wrong Answer

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

    dp = [[set([()]) for _ in range(Ma)] for _ in range(N + 1)]

    for i, (a, _, _) in enumerate(items):
        for mod in range(Ma):
            for prev in dp[i][mod]:
                dp[i + 1][mod].add(prev)
            for prev in dp[i][(mod - a) % Ma]:
                dp[i + 1][mod].add(tuple(list(prev) + [i]))

    print('-1')
    return

    ans = float('inf')
    for addList in dp[N][0]:
        sumA = 0
        sumB = 0
        cost = 0
        for i in addList:
            sumA += items[i][0]
            sumB += items[i][1]
            cost += items[i][2]
        if sumA * sumB > 0 and sumA * Mb == sumB * Ma:
            ans = min(ans, cost)

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

sol()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a, b = LI()
    a -= 2
    b -= 2
    a %= 13
    b %= 13
    if a > b:
        print("Alice")
    elif a < b:
        print("Bob")
    else:
        print("Draw")
    return

#B
def B():
    n, m = LI()
    a = SR(n)
    b = SR(m)
    if a == b:
        print("Yes")
        return
    for y in range(n - m):
        for x in range(n - m):
            if a[y][x] == b[0][0]:
                flg = 0
                for yi in range(m):
                    for xi in range(m):
                        if b[yi][xi] == a[y + yi][x + xi]:
                            continue
                        flg = 1
                        break
                    if flg:
                        break
                if flg:
                    continue
                print("Yes")
                return
    print("No")
    return

#C
def C():
    n, m = LI()
    edg = [[] for i in range(n)]
    for _ in range(m):
        a, b = LI_()
        edg[a].append(b)
        edg[b].append(a)
    def dfs(i, c):
        res = 0
        c[i] = False
        if not (True in c):
            c[i] = True
            return 1
        for e in edg[i]:
            if c[e]:
                res += dfs(e, c)
        c[i] = True
        return res
    print(dfs(0, [True] * n))
    return

#D
def D():
    n, Ma, Mb = LI()
    abc = LIR(n)
    dp = [[inf] * (10 * n + 1) for i in range(10 * n + 1)]
    dp[0][0] = 0
    for a, b, c in abc:
        for i in range(a, 10 * n + 1):
            for j in range(b, 10 * n + 1):
                dp[i][j] = min(dp[i - a][j - b] + c, dp[i][j])
    ans = inf
    ma = Ma
    mb = Mb
    i = 1
    while 1:
        ans = min(ans, dp[ma * i][mb * i])
        i += 1
        if ma * i > 10 * n or mb * i > 10 * n:
            break
    if ans == inf:
        print(-1)
        return
    print(ans)
    return

#Solve
if __name__ == '__main__':
    D()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a, b = LI()
    a -= 2
    b -= 2
    a %= 13
    b %= 13
    if a > b:
        print("Alice")
    elif a < b:
        print("Bob")
    else:
        print("Draw")
    return

#B
def B():
    n, m = LI()
    a = SR(n)
    b = SR(m)
    if a == b:
        print("Yes")
        return
    for y in range(n - m):
        for x in range(n - m):
            if a[y][x] == b[0][0]:
                flg = 0
                for yi in range(m):
                    for xi in range(m):
                        if b[yi][xi] == a[y + yi][x + xi]:
                            continue
                        flg = 1
                        break
                    if flg:
                        break
                if flg:
                    continue
                print("Yes")
                return
    print("No")
    return

#C
def C():
    n, m = LI()
    edg = [[] for i in range(n)]
    for _ in range(m):
        a, b = LI_()
        edg[a].append(b)
        edg[b].append(a)
    def dfs(i, c):
        res = 0
        c[i] = False
        if not (True in c):
            c[i] = True
            return 1
        for e in edg[i]:
            if c[e]:
                res += dfs(e, c)
        c[i] = True
        return res
    print(dfs(0, [True] * n))
    return

#D
def D():
    n, ma, mb = LI()
    abc = LIR(n)
    dp = [[inf] * (10 * n + 1) for i in range(10 * n + 1)]
    dp[0][0] = 0
    for a, b, c in abc:
        for i in range(a, 10 * n + 1):
            for j in range(b, 10 * n + 1):
                dp[i][j] = min(dp[i - a][j - b] + c, dp[i][j])
    ans = inf
    for i in range(ma, 10 * n + 1, ma):
        if i // ma * mb <= 10 * n:
            ans = min(ans, dp[i][i // ma * mb])
            continue
        break
    if ans == inf:
        print(-1)
        return
    print(ans)
    return

#Solve
if __name__ == '__main__':
    D()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a, b = LI()
    a -= 2
    b -= 2
    a %= 13
    b %= 13
    if a > b:
        print("Alice")
    elif a < b:
        print("Bob")
    else:
        print("Draw")
    return

#B
def B():
    n, m = LI()
    a = SR(n)
    b = SR(m)
    if a == b:
        print("Yes")
        return
    for y in range(n - m):
        for x in range(n - m):
            if a[y][x] == b[0][0]:
                flg = 0
                for yi in range(m):
                    for xi in range(m):
                        if b[yi][xi] == a[y + yi][x + xi]:
                            continue
                        flg = 1
                        break
                    if flg:
                        break
                if flg:
                    continue
                print("Yes")
                return
    print("No")
    return

#C
def C():
    n, m = LI()
    edg = [[] for i in range(n)]
    for _ in range(m):
        a, b = LI_()
        edg[a].append(b)
        edg[b].append(a)
    def dfs(i, c):
        res = 0
        c[i] = False
        if not (True in c):
            c[i] = True
            return 1
        for e in edg[i]:
            if c[e]:
                res += dfs(e, c)
        c[i] = True
        return res
    print(dfs(0, [True] * n))
    return

#D
def D():
    n, ma, mb = LI()
    abc = LIR(n)
    dp = [[inf] * (40 * n + 1) for i in range(40 * n + 1)]
    dp[0][0] = 0
    for a, b, c in abc:
        for i in range(a, 40 * n + 1):
            for j in range(b, 40 * n + 1):
                dp[i][j] = min(dp[i - a][j - b] + c, dp[i][j])
    ans = inf
    for i in range(ma, 40 * n + 1, ma):
        if i // ma * mb <= 40 * n:
            ans = min(ans, dp[i][i // ma * mb])
            continue
        break
    if ans == inf:
        print(-1)
        return
    print(ans)
    return

#Solve
if __name__ == '__main__':
    D()