Problem:


Score : 400 points

Problem Statement

Dolphin is planning to generate a small amount of a certain chemical substance C.

In order to generate the substance C, he must prepare a solution which is a mixture of two substances A and B in the ratio of M_a:M_b.

He does not have any stock of chemicals, however, so he will purchase some chemicals at a local pharmacy.

The pharmacy sells N kinds of chemicals. For each kind of chemical, there is exactly one package of that chemical in stock.

The package of chemical i contains a_i grams of the substance A and b_i grams of the substance B, and is sold for c_i yen (the currency of Japan).

Dolphin will purchase some of these packages. For some reason, he must use all contents of the purchased packages to generate the substance C.

Find the minimum amount of money required to generate the substance C.

If it is not possible to generate the substance C by purchasing any combination of packages at the pharmacy, report that fact.  

Constraints

1≦N≦40 

1≦a_i,b_i≦10 

1≦c_i≦100 

1≦M_a,M_b≦10 

gcd(M_a,M_b)=1

a_i, b_i, c_i, M_a and M_b are integers.

Input

The input is given from Standard Input in the following format:

N M_a M_b  
a_1 b_1 c_1  
a_2 b_2 c_2
:  
a_N b_N c_N  

Output

Print the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C, print -1 instead.

Sample Input 1

3 1 1
1 2 1
2 1 2
3 3 10

Sample Output 1

3

The amount of money spent will be minimized by purchasing the packages of chemicals 1 and 2.

In this case, the mixture of the purchased chemicals will contain 3 grams of the substance A and 3 grams of the substance B, which are in the desired ratio: 3:3=1:1.

The total price of these packages is 3 yen.  

Sample Input 2

1 1 10
10 10 10

Sample Output 2

-1

The ratio 1:10 of the two substances A and B cannot be satisfied by purchasing any combination of the packages. Thus, the output should be -1.



Sample input:
3 1 1
1 2 1
2 1 2
3 3 10


Sample output:
3

Submission status: Wrong Answer

Solution code:
n,MA,MB=map(int,input().split())
INF=10**10
A=[]
B=[]
C=[]
for i in range(n):
  a,b,c=map(int,input().split())
  if MA*b-MB*a>0:
    A.append([MA*b-MB*a,c])
  elif MA*b-MB*a<0:
    B.append([MB*a-MA*b,c])
  else:
    C.append(c)
dp1=[[INF]*10010 for i in range(len(A)+1)]
for i in range(1,len(A)+1):
  for j in range(1,10010):
    if j<A[i-1][0]:
      dp1[i][j]=dp1[i-1][j]
    elif j==A[i-1][0]:
      dp1[i][j]=min(A[i-1][1],dp1[i-1][j])
    else:
      dp1[i][j]=min(dp1[i-1][j],dp1[i][j-A[i-1][0]]+A[i-1][1])
dp2=[[INF]*10010 for i in range(len(B)+1)]
for i in range(1,len(B)+1):
  for j in range(1,10010):
    if j<B[i-1][0]:
      dp2[i][j]=dp2[i-1][j]
    elif j==B[i-1][0]:
      dp2[i][j]=min(B[i-1][1],dp2[i-1][j])
    else:
      dp2[i][j]=min(dp2[i-1][j],dp2[i][j-B[i-1][0]]+B[i-1][1])
ans=INF
for i in range(10):
  a=dp1[-1][i]+dp2[-1][i]
  if a<ans:
    ans=a
for i in range(len(C)):
  if c<ans:
    ans=c
if ans<INF:
  print(ans)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
N,A,B = map(int,input().split())
wariai = A/B
cheep = 101
a,b,c = list(),list(),list()
for i in range(N):
  x,y,z = map(int,input().split())
  a.append(x)
  b.append(y)
  c.append(z)
  if wariai == x/y:
    if cheep > z:
      cheep = z
for i in range(N):
  if cheep > c[i]:
    for n in range(N):
      if n == i:
        continue
      if cheep > c[i] + c[n]:
        for m in range(N):
          if m == i or m == n:
            continue
          if cheep > c[i] + c[n] + c[m]:
            if wariai == (a[i]+a[n]+a[m])/(b[i]+b[n]+a[m]):
              cheep = c[i]+c[n]+c[m]
        if wariai == (a[i]+a[n])/(b[i]+b[n]):
          cheep = c[i]+c[n]
    if wariai == a[i]/b[i]:
      cheep = c[i]
if cheep == 101:
  print(-1)
else:
  print(cheep)

Submission status: Wrong Answer

Solution code:
INF = 10**18

N, Ma, Mb = map(int, input().split())
sol = [tuple(map(int, input().split())) for _ in range(N)]

if N % 2 == 1:
    sol.append((0, 0, 0))
    N += 1

M = N // 2

sol1 = sol[:M]
sol2 = sol[M:]

S1 = []
S2 = []

for i in range(2**M):
    A1, B1, C1 = 0, 0, 0
    A2, B2, C2 = 0, 0, 0
    for j in range(M):
        if (i >> j) & 1:
            a1, b1, c1 = sol1[j]
            a2, b2, c2 = sol2[j]
            A1 += a1
            B1 += b1
            C1 += c1
            A2 += a2
            B2 += b2
            C2 += c2
    S1.append((A1, B1, C1))
    S2.append((A2, B2, C2))

D = dict()
S2.sort(key=lambda x: x[2], reverse=True)

for i in range(2**M):
    a, b, c = S2[i]
    x = Mb * a - Ma * b
    if not x in D or c > 0:
        D[x] = c

res = INF

for i in range(2**M):
    a, b, c = S1[i]
    x = Ma * b - Mb * a
    if x in D and D[x] + c > 0:
        res = min(res, D[x] + c)

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

Submission status: Wrong Answer

Solution code:
INF = 10**18

N, Ma, Mb = map(int, input().split())
sol = [tuple(map(int, input().split())) for _ in range(N)]

if N % 2 == 1:
    sol.append((0, 0, 0))
    N += 1

M = N // 2

sol1 = sol[:M]
sol2 = sol[M:]

S1 = []
S2 = []

for i in range(2**M):
    A1, B1, C1 = 0, 0, 0
    A2, B2, C2 = 0, 0, 0
    for j in range(M):
        if (i >> j) & 1:
            a1, b1, c1 = sol1[j]
            a2, b2, c2 = sol2[j]
            A1 += a1
            B1 += b1
            C1 += c1
            A2 += a2
            B2 += b2
            C2 += c2
    S1.append((A1, B1, C1))
    S2.append((A2, B2, C2))

D = dict()
S2.sort(key=lambda x: x[2], reverse=True)

for i in range(2**M):
    a, b, c = S2[i]
    x = Mb * a - Ma * b
    D[x] = c

res = INF

for i in range(2**M):
    a, b, c = S1[i]
    x = Ma * b - Mb * a
    if x in D and D[x] + c > 0:
        res = min(res, D[x] + c)

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

Submission status: Wrong Answer

Solution code:
n,ma,mb=map(int,input().split())
ABC=[list(map(int,input().split())) for _ in range(n)]
infi=10**15
dp=[[[infi]*(401) for _ in range(401)]for _ in range(41)]
dp[0][0][0]=0
for i in range(n):
    a,b,c=ABC[i]
    for j in range(401):
        for k in range(401):
            ni=i+1
            nj=j+a
            nk=k+b
            if nj>400:
                continue
            if nk>400:
                continue
            dp[ni][nj][nk]=min(dp[i][j][k]+c, dp[i][nj][nk], dp[ni][nj][nk])

ans=infi
for i in range(1,401):
    if ma*i>400:
        break
    if mb*i>400:
        break    
    ans=min(ans, dp[n][ma*i][mb*i])
print(ans if ans!=infi else -1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
n,ma,mb=map(int,input().split())
ABC=[list(map(int,input().split())) for _ in range(n)]
infi=10**15
dp=[[[infi]*(201) for _ in range(201)]for _ in range(41)]
dp[0][0][0]=0
for i in range(n):
    a,b,c=ABC[i]
    for j in range(201):
        for k in range(201):
            ni=i+1
            nj=j+a
            nk=k+b
            if nj>100:
                continue
            if nk>100:
                continue
            dp[ni][nj][nk]=min(dp[i][j][k]+c, dp[i][nj][nk], dp[ni][nj][nk])

ans=infi
for i in range(1,201):
    if ma*i>100:
        break
    if mb*i>100:
        break    
    ans=min(ans, dp[n][ma*i][mb*i])
print(ans if ans!=infi else -1)

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.buffer.readline
n,ma,mb=map(int,input().split())
ABC=[list(map(int,input().split())) for _ in range(n)]
infi=10**15
dp=[[[infi]*(101) for _ in range(101)]for _ in range(41)]
dp[0][0][0]=0
for i in range(n):
    a,b,c=ABC[i]
    for j in range(101):
        for k in range(101):
            ni=i+1
            nj=j+a
            nk=k+b
            if nj>100:
                continue
            if nk>100:
                continue
            dp[ni][nj][nk]=min(dp[i][j][k]+c, dp[i][nj][nk], dp[ni][nj][nk])

ans=infi
for i in range(1,101):
    if ma*i>100:
        break
    if mb*i>100:
        break    
    ans=min(ans, dp[n][ma*i][mb*i])
print(ans if ans!=infi else -1)

Submission status: Wrong Answer

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7
from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline


#N=int(input())

N,Ma,Mb=map(int,input().split())
ABC=[list(map(int,input().split())) for i in range(N)]
idle_max=10**9
dp=[[[idle_max for b in range(400)] for a in range(400)] for i in range(N)]
used=set()
a,b,c=ABC[0]
dp[0][a][b]=c

for n in range(1,N):
    a,b,c=ABC[n]
    dp[n][a][b]=min(dp[n-1][a][b],c)
    for x in range(400):
        for y in range(400):
            if x-a>=0 and y-b>=0 and dp[n-1][x-a][y-b]!=idle_max:
                dp[n][x][y]=min(dp[n-1][x-a][y-b]+c,dp[n-1][x][y])
            else:
                dp[n][x][y]=dp[n-1][x][y]
value=idle_max
for x in range(1,400):
    for y in range(1,400):
        z=gcd(x,y)
        a=x//z;b=y//z
        if Ma==a and Mb==b:
            value=min(dp[-1][x][y],value)

if value!=idle_max:
    print(value)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7
from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline


#N=int(input())

N,Ma,Mb=map(int,input().split())
ABC=[list(map(int,input().split())) for i in range(N)]
idle_max=10**9
dp=[[[idle_max for b in range(400)] for a in range(400)] for i in range(N)]
used=set()
a,b,c=ABC[0]
dp[0][a][b]=c

for n in range(1,N):
    a,b,c=ABC[n]
    dp[n][a][b]=min(dp[n-1][a][b],c)
    for x in range(400):
        for y in range(400):
            if x-a>=0 and y-b>=0:
                dp[n][x][y]=min(dp[n-1][x-a][y-b]+c,dp[n-1][x][y])
            else:
                dp[n][x][y]=dp[n-1][x][y]
value=idle_max
for x in range(1,400):
    for y in range(1,400):
        z=gcd(x,y)
        a=x//z;b=y//z
        if Ma==a and Mb==b:
            value=min(dp[-1][x][y],value)

if value!=idle_max:
    print(value)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7
from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline


#N=int(input())

N,Ma,Mb=map(int,input().split())
ABC=[list(map(int,input().split())) for i in range(N)]
idle_max=10**9
dp=[[idle_max for m in range(403*400)] for i in range(N)]
used=set()
a,b,c=ABC[0]
dp[0][a*401+b]=c
for n in range(1,N):
    a,b,c=ABC[n]
    for i in range(len(dp[0])):
        if i-(a*401+b)>=0 and dp[n-1][i-(a*401+b)]!=idle_max:
            dp[n][i]=min(dp[n-1][i-(a*401+b)]+c,dp[n-1][i])
        else:
            dp[n][i]=dp[n-1][i]
    dp[n][a*401+b]=min(dp[n-1][i],c)

value=idle_max
for i in range(len(dp[0])):
    if dp[-1][i]:
        A=i//401;B=i%401
        if A and B:
            Z=gcd(A,B)
            A//=Z;B//=Z
            if Ma==A and Mb==B:
                value=min(value,dp[-1][i])
if value!=idle_max:
    print(value)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7
from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline


#N=int(input())

N,Ma,Mb=map(int,input().split())
ABC=[list(map(int,input().split())) for i in range(N)]
idle_max=10**9
dp=[[idle_max for m in range(403*400)] for i in range(N)]
used=set()
a,b,c=ABC[0]
dp[0][a*401+b]=c
for n in range(1,N):
    a,b,c=ABC[n]
    for i in range(len(dp[0])):
        if i-(a*401+b)>=0 and dp[n-1][i-(a*401+b)]!=idle_max:
            dp[n][i]=min(dp[n-1][i-(a*401+b)]+c,dp[n-1][i])
        else:
            dp[n][i]=dp[n-1][i]
    dp[n][a*401+b]=c

value=idle_max
for i in range(len(dp[0])):
    if dp[-1][i]:
        A=i//401;B=i%401
        if A and B:
            Z=gcd(A,B)
            A//=Z;B//=Z
            if Ma==A and Mb==B:
                value=min(value,dp[-1][i])
if value!=idle_max:
    print(value)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7
from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline


#N=int(input())

N,Ma,Mb=map(int,input().split())
ABC=[list(map(int,input().split())) for i in range(N)]
idle_max=10**9
dp=[[idle_max for m in range(403*400)] for i in range(N)]
used=set()
a,b,c=ABC[0]
dp[0][a*401+b]=c
for n in range(1,N):
    a,b,c=ABC[n]
    for i in range(len(dp[0])):
        if i-(a*401+b)>=0 and dp[n-1][i-(a*401+b)]!=idle_max:
            dp[n][i]=min(dp[n-1][i-(a*401+b)]+c,dp[n-1][i])
        else:
            dp[n][i]=dp[n-1][i]

value=idle_max
for i in range(len(dp[0])):
    if dp[-1][i]:
        A=i//401;B=i%401
        if A and B:
            Z=gcd(A,B)
            A//=Z;B//=Z
            if Ma==A and Mb==B:
                value=min(value,dp[-1][i])
if value!=idle_max:
    print(value)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7
from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline


#N=int(input())

N,Ma,Mb=map(int,input().split())
ABC=[list(map(int,input().split())) for i in range(N)]
idle_max=10**9
dp=[[idle_max for m in range(403*400)] for i in range(N)]
used=set()
a,b,c=ABC[0]
dp[0][a*401+b]=c
for n in range(1,N):
    a,b,c=ABC[n]
    for i in range(len(dp[0])):
        if i-(a*401+b)>=0 and dp[n-1][i-(a*401+b)]:
            dp[n][i]=min(dp[n-1][i-(a*401+b)]+c,dp[n-1][i])
        else:
            dp[n][i]=dp[n-1][i]

value=idle_max
for i in range(len(dp[0])):
    if dp[-1][i]:
        A=i//401;B=i%401
        if A and B:
            Z=gcd(A,B)
            A//=Z;B//=Z
            if Ma==A and Mb==B:
                value=min(value,dp[-1][i])
if value!=idle_max:
    print(value)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
#import decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7
from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys
#input = sys.stdin.readline


#N=int(input())

N,Ma,Mb=map(int,input().split())
ABC=[list(map(int,input().split())) for i in range(N)]
idle_max=10**9
dp=[[idle_max for m in range(401*400)] for i in range(N)]
used=set()
a,b,c=ABC[0]
dp[0][a*401+b]=c
for n in range(1,N):
    a,b,c=ABC[n]
    for i in range(len(dp[0])):
        if i-(a*401+b)>=0 and dp[n-1][i-(a*401+b)]:
            dp[n][i]=min(dp[n-1][i-(a*401+b)]+c,dp[n-1][i])
        else:
            dp[n][i]=dp[n-1][i]

value=idle_max
for i in range(len(dp[0])):
    if dp[-1][i]:
        A=i//401;B=i%401
        if A and B:
            Z=gcd(A,B)
            A//=Z;B//=Z
            if Ma==A and Mb==B:
                value=min(value,dp[-1][i])
if value!=idle_max:
    print(value)
else:
    print(-1)

Submission status: Wrong Answer

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

a = [0]*n
b = [0]*n
c = [0]*n
min_cost = 1000000000

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

for i in range(2**n):
    total_a = 0
    total_b = 0
    total_cost = 0
    for j in range(n):
        if((i >> j) & 1):
            total_a += a[j]
            total_b += b[j]
            total_cost += c[j]
    if (total_cost != 0 and total_a//ma == total_b//mb):
        min_cost = min(min_cost,total_cost)
    
if (min_cost < 1000000000):
    print(min_cost)
else:
    print('-1')

Submission status: Wrong Answer

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,Ma,Mb=MI()
    a=[0]*N
    b=[0]*N
    c=[0]*N
    for i in range(N):
        a[i],b[i],c[i]=MI()

        
    
    inf=10**5
    #dp[i][j][k]はi番目の商品まででaがjg,bがkgをとるときの最低金額
    dp=[[[inf]*(N*10+1) for _ in range(N*10+1)] for i in range(N+1)]
    dp[0][0][0]=0
    
    ans=inf
    for i in range(N):
        for j in range(N*10+1-a[i]):
            for k in range(N*10+1-b[i]):
                if dp[i][j][k]!=inf:
                    dp[i+1][j][k]=dp[i][j][k]
                    dp[i+1][j+a[i]][k+b[i]]=min(dp[i+1][j+a[i]][k+b[i]],dp[i][j][k]+c[i])
                    if (j+a[i])*Mb==(k+b[i])*Ma:
                        ans=min(ans,dp[i+1][j+a[i]][k+b[i]])
                    
    if ans==inf:
        print(-1)
    else:
        print(ans)
    
    
    

main()

Submission status: Wrong Answer

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,Ma,Mb=MI()
    a=[0]*N
    b=[0]*N
    c=[0]*N
    cab=[[0,0,0] for _ in range(N)]
    for i in range(N):
        cab[i][1],cab[i][2],cab[i][0]=MI()
    cab.sort()
    for i in range(N):
        a[i]=cab[i][1]
        b[i]=cab[i][2]
        c[i]=cab[i][0]
        
    
    inf=10**5
    #dp[i][j][k]はi番目の商品まででaがjg,bがkgをとるときの最低金額
    dp=[[[inf]*(N*10+1) for _ in range(N*10+1)] for i in range(N+1)]
    dp[0][0][0]=0
    
    ans=inf
    for i in range(N):
        for j in range(N*10+1-a[i]):
            for k in range(N*10+1-b[i]):
                if dp[i][j][k]!=inf:
                    dp[i+1][j+a[i]][k+b[i]]=min(dp[i+1][j+a[i]][k+b[i]],dp[i][j][k]+c[i])
                    if (j+a[i])*Mb==(k+b[i])*Ma:
                        ans=min(ans,dp[i+1][j+a[i]][k+b[i]])
                    
    if ans==inf:
        print(-1)
    else:
        print(ans)
    
    
    

main()

Submission status: Wrong Answer

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,Ma,Mb=MI()
    a=[0]*N
    b=[0]*N
    c=[0]*N
    for i in range(N):
        a[i],b[i],c[i]=MI()
    
    inf=10**5
    #dp[i][j][k]はi番目の商品まででaがjg,bがkgをとるときの最低金額
    dp=[[[inf]*(N*10+1) for _ in range(N*10+1)] for i in range(N+1)]
    dp[0][0][0]=0
    
    ans=inf
    for i in range(N):
        for j in range(N*10+1-a[i]):
            for k in range(N*10+1-b[i]):
                if dp[i][j][k]!=inf:
                    dp[i+1][j+a[i]][k+b[i]]=min(dp[i+1][j+a[i]][k+b[i]],dp[i][j][k]+c[i])
                    if (j+a[i])*Mb==(k+b[i])*Ma:
                        ans=min(ans,dp[i+1][j+a[i]][k+b[i]])
                    
    if ans==inf:
        print(-1)
    else:
        print(ans)
    
    
    

main()

Submission status: Wrong Answer

Solution code:

N, Ma, Mb = map(int,input().split())
rate = Ma / Mb
a = [0] * N
b = [0] * N
c = [0] * N
MA = 0
MB = 0
MC = 0
for i in range(N):
    a[i], b[i], c[i] = map(int, input().split())
    MA += a[i]
    MB += b[i]
    MC += c[i]

dp = [[[MC for i in range(MB+1)] for j in range(MA+1)] for k in range(N+1)]
dp[0][0][0] = 0

for it_N in range(N):
    for it_MA in range(MA+1):
        for it_MB in range(MB+1):
            if it_MA >= a[it_N] and it_MB >= b[it_N]:
                if dp[it_N][it_MA-a[it_N]][it_MB-b[it_N]] != MC:
                    dp[it_N+1][it_MA][it_MB] = min(dp[it_N][it_MA-a[it_N]][it_MB-b[it_N]] + c[it_N], dp[it_N][it_MA][it_MB])
            else:
                dp[it_N+1][it_MA][it_MB]=dp[it_N][it_MA][it_MB]


ans = -1
for a in range(1, MA+1):
    for b in range(1, MB+1):
        if a/b == rate and dp[N][a][b] != MC:
            if ans == -1:
                ans = dp[N][a][b]
            else:
                ans = min(dp[N][a][b], ans)
print(ans)

Submission status: Wrong Answer

Solution code:

N, Ma, Mb = map(int,input().split())
rate = Ma / Mb
a = [0] * N
b = [0] * N
c = [0] * N
MA = 0
MB = 0
for i in range(N):
    a[i], b[i], c[i] = map(int, input().split())
    MA += a[i]
    MB += b[i]

dp = [[[0 for i in range(MB+1)] for j in range(MA+1)] for k in range(N+1)]

for it_N in range(N):
    for it_MA in range(MA+1):
        for it_MB in range(MB+1):
            if it_MA > a[it_N] and it_MB > b[it_N]:
                if dp[it_N][it_MA-a[it_N]][it_MB-b[it_N]] != 0:
                    dp[it_N+1][it_MA][it_MB] = min(dp[it_N][it_MA-a[it_N]][it_MB-b[it_N]] + c[it_N], dp[it_N][it_MA][it_MB])
                else:
                    dp[it_N+1][a[it_N]][b[it_N]] = c[it_N]

#中身確認
ans = -1
for a in range(1, MA+1):
    for b in range(1, MB+1):
        if a/b == rate and dp[N][a][b] != 0:
            if ans == -1:
                ans = dp[N][a][b]
            else:
                ans = min([dp[N][a][b], ans])
print(ans)

Submission status: Wrong Answer

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

dp = [[[10**10 for _ in range(430)] for _ in range(430)] for _ in range(45)]

dp[0][0][0] = 0

for i in range(n):
    a=l[i][0]
    b=l[i][1]
    c=l[i][2]
    for j in range(410):
        for k in range(410):
            dp[i+1][j][k] = min(dp[i+1][j][k] ,dp[i][j][k])
            dp[i+1][j+a][k+b] =min(dp[i+1][j+a][k+b], dp[i][j][k] +c)

ans=10**10
for i in range(n):
    for j in range(1,410):
        for k in range(1,410):
            if ma*k == mb*j:
                if dp[i][j][k] < ans:
                    ans=dp[i][j][k]
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)]

INF = 40 * 100 + 1
ab_max = 10 * n

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

for i, (a, b, c) in enumerate(abc, 1):
    for j in range(ab_max + 1):
        for k in range(ab_max + 1):
            dp[i][j][k] = dp[i-1][j][k]
            if j - a >= 0 and k - b >= 0:
                if dp[i-1][j-a][k-b] != INF:
                    dp[i][j][k] = min(dp[i][j][k], dp[i-1][j-a][k-b] + c)


ans = INF
for j in range(1, ab_max + 1):
    for k in range(1, ab_max + 1):
        if j * mb == k * ma:
            ans = min(ans, dp[n][j][k])

if ans == INF:
    ans = -1

print(ans)
print(*dp[n], sep="\n")

Submission status: Wrong Answer

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

INF = float("inf")
ab_max = 10 * n

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

for i, (a, b, c) in enumerate(abc, 1):
    for j in range(1, ab_max + 1):
        for k in range(1, ab_max + 1):
            dp[i][j][k] = dp[i-1][j][k]
            if j - a >= 0 and k - b >= 0:
                if dp[i-1][j-a][k-b] != INF:
                    dp[i][j][k] = min(dp[i][j][k], dp[i-1][j-a][k-b] + c)


ans = INF
for j in range(1, ab_max + 1):
    for k in range(1, ab_max + 1):
        if j * mb == k * ma:
            ans = min(ans, dp[n][j][k])

if ans == INF:
    ans = -1

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)]

INF = 40 * 100 + 1
ab_max = 10 * n

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

for i, (a, b, c) in enumerate(abc, 1):
    for j in range(1, ab_max + 1):
        for k in range(1, ab_max + 1):
            dp[i][j][k] = dp[i-1][j][k]
            if j - a >= 0 and k - b >= 0:
                if dp[i-1][j-a][k-b] != INF:
                    dp[i][j][k] = min(dp[i][j][k], dp[i-1][j-a][k-b] + c)


ans = INF
for j in range(1, ab_max + 1):
    for k in range(1, ab_max + 1):
        if j * mb == k * ma:
            ans = min(ans, dp[n][j][k])

if ans == INF:
    ans = -1

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)]

INF = float("inf")

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

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

ans = INF
for j in range(1, 10 * n + 1):
    for k in range(1, 10 * n + 1):
        if j * mb == k * ma:
            ans = min(ans, dp[j][k])

if ans == INF:
    ans = -1

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)]

INF = 100 * 40 + 1

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

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

ans = INF
for j in range(1, 10 * n + 1):
    for k in range(1, 10 * n + 1):
        if j * mb == k * ma:
            ans = min(ans, dp[j][k])

if ans == INF:
    ans = -1

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)]

INF = 100 * 40 + 1

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(1, n + 1):
    a, b, c = abc[i-1]
    for j in range(1, 10 * n + 1):
        for k in range(1, 10 * n + 1):
            if j - a >= 0 and k - b >= 0:
                dp[i][j][k] = min(dp[i][j][k], dp[i-1][j][k], dp[i-1][j-a][k-b] + c)
            else:
                dp[i][j][k] = min(dp[i][j][k], dp[i-1][j][k])

ans = INF
for j in range(1, 10 * n + 1):
    for k in range(1, 10 * n + 1):
        if j * mb == k * ma:
            ans = min(ans, dp[n][j][k])

if ans == INF:
    ans = -1

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)]

INF = 100 * 40 + 1

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(1, n + 1):
    a, b, c = abc[i-1]
    for j in range(1, 10 * n + 1):
        for k in range(1, 10 * n + 1):
            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)
            else:
                dp[i][j][k] = dp[i-1][j][k]

ans = INF
for j in range(1, 10 * n + 1):
    for k in range(1, 10 * n + 1):
        if j * mb == k * ma:
            ans = min(ans, dp[n][j][k])

if ans == INF:
    ans = -1

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)]

INF = 100 * 40 + 1

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(1, n + 1):
    a, b, c = abc[i-1]
    for j in range(1, 10 * n + 1):
        for k in range(1, 10 * n + 1):
            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)
            else:
                dp[i][j][k] = dp[i-1][j][k]

ans = INF
for j in range(1, 10 * n + 1):
    for k in range(1, 10 * n + 1):
        if j * mb == k * ma:
            ans = min(ans, dp[n][j][k])

if ans == INF:
    ans = -1

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)]

INF = 100 * 40 + 1

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(1, n + 1):
    a, b, c = abc[i-1]
    for j in range(1, 10 * n + 1):
        for k in range(1, 10 * n + 1):
            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)
            else:
                dp[i][j][k] = dp[i-1][j][k]

ans = INF
for j in range(1, 10 * n + 1):
    for k in range(1, 10 * n + 1):
        if j * mb == k * ma:
            ans = min(ans, dp[n][j][k])

if ans == INF:
    ans = -1

print(ans)
print(*dp, sep="\n")

Submission status: Wrong Answer

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

INF = 100 * 40 + 1

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(1, n + 1):
    a, b, c = abc[i-1]
    for j in range(1, 10 * n + 1):
        for k in range(1, 10 * n + 1):
            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 j in range(1, 10 * n + 1):
    for k in range(1, 10 * n + 1):
        if j * mb == k * ma:
            ans = min(ans, dp[n][j][k])

if ans == INF:
    ans = -1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
import numpy as np

def main():
    N,A,B = map(int,input().split())
    INF = 10**10
    dp = np.full((N+1,10*N+2,10*N+2),INF)
    dp[0][0][0] = 0
    
    for i in range(N):
        a,b,c = map(int,input().split())
        dp[i+1][a:,b:] = np.minimum(dp[i][a:,b:],dp[i][:10*N+2-a,:10*N+2-b]+c)
        dp[i+1] = np.minimum(dp[i],dp[i+1])
    
    ans = INF
    x,y = A,B
    while x <= 10*N+1 and y <= 10*N+1:
        ans = min(ans,dp[N][x][y])
        x += A
        y += B
        
    print(ans if ans != INF else -1)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

def main():
    N,A,B = map(int,input().split())
    dp = [[10**10 for _ in range(10*N+1)] for _ in range(10*N+1)]
    dp[0][0] = 0
    
    for _ in range(N):
        a,b,c = map(int,input().split())
        for i in range(10*N+1-a):
            for j in range(10*N+1-b):
                dp[i+a][j+b] = min(dp[i+a][j+b],dp[i][j]+c)
    
    ans = 10**10
    for i in range((10*N+1)//max(A,B)-1):
        ans = min(ans,dp[A*(i+1)][B*(i+1)])
        
    if ans == 10**10:
        print(-1)
    else:
        print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np
n, ma, mb = map(int, input().split())
r = 10 * n + 6
inf = 10**5
dp = np.full((n+1, r, r), inf)
dp[0][0][0] = 0
for i in range(n):
    a, b, c = map(int, input().split())
    for j in range(r):
        for k in range(r):
            if j + a < r and k + b < r:
                dp[i+1][j+a][k+b] = min(
                    dp[i][j][k] + c, dp[i][j+a][k+b]
                )
            dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k])

ans = inf
x, y = 0, 0
for i in range(1, n+1):
    while True:
        x += ma
        y += mb
        if x < r and y < r:
            ans = min(ans, dp[i][x][y])
        else:
            break
if ans == inf:
    print(-1)
else:
    print(int(ans))