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 = 10000
maxa = 0
maxb = 0
maxc = 0
medi = []
for i in range(n):
    a, b, c = map(int,input().split())
    maxa = max(maxa, a)
    maxb = max(maxb, b)
    maxc = max(maxc, c)
    medi.append([a, b, c])
dp = [[[INF for k in range(maxb+1)] for j in range(maxa+1)] for i in range(n+1)]
dp[0][0][0] = 0
for i in range(n):
    for x in range(maxa+1):
        for y in range(maxb+1):
            if dp[i][x][y] == INF:
                continue
            dp[i + 1][x][y] = min(dp[i + 1][x][y], dp[i][x][y])
            a, b, c = medi[i]
            if x + a <= maxa and y + b <= maxb:
                dp[i + 1][x + a][y + b] = min(dp[i][x][y] + c, dp[i + 1][x + a][y + b])

t = 1
ok = False
while t*ma <= maxa and t*mb <= maxb:
    if dp[n][t*ma][t*mb] < INF:
        print(dp[n][t*ma][t*mb])
        ok = True
        break
    else:
        t += 1
    
if not ok:
    print(-1)

Submission status: Wrong Answer

Solution code:
n, ma, mb = map(int,input().split())
INF = 10000
medi = [list(map(int,input().split())) for _ in range(n)]
dp = [[[INF for maxb in range(500)] for maxa in range(500)] for i in range(n+1)]
dp[0][0][0] = 0
for i in range(n):
    for x in range(500):
        for y in range(500):
            if dp[i][x][y] == INF:
                continue
            dp[i + 1][x][y] = min(dp[i + 1][x][y], dp[i][x][y])
            a, b, c = medi[i]
            if x + a < 500 and y + b < 500:
                dp[i + 1][x + a][y + b] = min(dp[i][x][y] + c, dp[i + 1][x + a][y + b])

t = 1
ok = False
while t*ma < 500 and t*mb < 500:
    if dp[n][t*ma][t*mb] < INF:
        print(dp[n][t*ma][t*mb])
        ok = True
        break
    else:
        t += 1
    
if not ok:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 8)

N, Ma, Mb = map(int, input().split())
A = [[int(i) for i in input().split()] for i in range(N)]
memo = {i : {} for i in range(N)}

def dfs(i, a, b):
    if i == N:
        return float('inf')
    elif b > 0 and a / b == Ma / Mb:
        return 0
    elif (a, b) in memo[i]:
        return memo[i][(a, b)]
    else:
        ans = min(dfs(i + 1, a, b), dfs(i + 1, a + A[i][0], b + A[i][1]) + A[i][2])
    memo[i][(a, b)] = ans
    return ans

if dfs(0, 0, 0) == float('inf'):
    print(-1)
else:
    print(dfs(0, 0, 0))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 8)

N, Ma, Mb = map(int, input().split())
A = [[int(i) for i in input().split()] for i in range(N)]

def dfs(i, a, b):
    if i == N:
        return float('inf')
    elif b > 0 and a / b == Ma / Mb:
        return 0
    else:
        ans = min(dfs(i + 1, a, b), dfs(i + 1, a + A[i][0], b + A[i][1]) + A[i][2])
    return ans

if dfs(0, 0, 0) == float('inf'):
    print(-1)
else:
    print(dfs(0, 0, 0))

Submission status: Wrong Answer

Solution code:
#D問題
NM=40
ABM=10
MM=400

N,M1,M2=map(int,input().split())
A=[]
B=[]
C=[]
for i in range(N):
    a,b,c=map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(c)
cmax=max(C)
CM=N*cmax+10
dp=[[[CM for broop in range(401)] for aroop in range(401)] for nroop in range(N+1)]
dp[0][0][0]=0
for i in range(N):
    for cb in range(401):
        for ca in range(401):
            if dp[i][ca][cb]==CM:
                continue
            dp[i+1][ca][cb]=min(dp[i+1][ca][cb],dp[i][ca][cb])
            if ca+A[i]<401 and cb+B[i]<401:
                dp[i+1][ca+A[i]][cb+B[i]]=min(dp[i+1][ca+A[i]][cb+B[i]],dp[i][ca][cb]+C[i])

ans=CM
for cb in range(400):
    for ca in range(400):
        CB=cb+1
        CA=ca+1
        if CA*M2 == CB*M1:
            ans=min(ans,dp[N-1][CA][CB])

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

Submission status: Wrong Answer

Solution code:
#D問題
NM=40
ABM=10
MM=400

N,M1,M2=map(int,input().split())
A=[]
B=[]
C=[]
for i in range(N):
    a,b,c=map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(c)
cmax=max(C)
CM=N*cmax+10
dp=[[[CM for broop in range(401)] for aroop in range(401)] for nroop in range(N+1)]
dp[0][0][0]=0
for i in range(N):
    for cb in range(401):
        for ca in range(401):
            if dp[i][ca][cb]==CM:
                continue
            dp[i+1][ca][cb]=min(dp[i+1][ca][cb],dp[i][ca][cb])
            if ca+A[i]<401 and cb+B[i]<401:
                dp[i+1][ca+A[i]][cb+B[i]]=min(dp[i+1][ca+A[i]][cb+B[i]],dp[i][ca][cb]+C[i])

ans=CM
for cb in range(400):
    for ca in range(400):
        CB=cb+1
        CA=ca+1
        if CA*M2 == CB*M1:
            if dp[N-1][CA][CB] == 0:
                pass
            else:
                ans=min(ans,dp[N-1][CA][CB])

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

Submission status: Wrong Answer

Solution code:
#D問題
NM=40
ABM=10
MM=400

N,M1,M2=map(int,input().split())
A=[]
B=[]
C=[]
for i in range(N):
    a,b,c=map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(c)
cmax=max(C)
CM=N*cmax+1
dp=[[[CM for broop in range(401)] for aroop in range(401)] for nroop in range(N+1)]
dp[0][0][0]=0
for i in range(N):
    for cb in range(401):
        for ca in range(401):
            if dp[i][ca][cb]==CM:
                continue
            dp[i+1][ca][cb]=min(dp[i+1][ca][cb],dp[i][ca][cb])
            if ca+A[i]<401 and cb+B[i]<401: 
                dp[i+1][ca+A[i]][cb+B[i]]=min(dp[i+1][ca+A[i]][cb+B[i]],dp[i][ca][cb]+C[i])

ans=CM
for cb in range(400):
    CB=cb+1
    for ca in range(400):
        CA=ca+1
        if CA*M2 == CB*M1:
            if dp[N-1][CA][CB] == 0:
                pass
            else:
                ans=min(ans,dp[N-1][CA][CB])

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

Submission status: Wrong Answer

Solution code:
#D問題
NM=40
ABM=10
MM=400

N,M1,M2=map(int,input().split())
A=[]
B=[]
C=[]
for i in range(N):
    a,b,c=map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(c)
cmax=max(C)
CM=N*max(C)+1
dp=[[[CM for broop in range(401)] for aroop in range(401)] for nroop in range(NM+1)]
dp[0][0][0]=0
for i in range(N):
    for cb in range(401):
        for ca in range(401):
            if dp[i][ca][cb]==CM:
                continue
            dp[i+1][ca][cb]=min(dp[i+1][ca][cb],dp[i][ca][cb])
            if ca+A[i]<401 and cb+B[i]<401: 
                dp[i+1][ca+A[i]][cb+B[i]]=min(dp[i+1][ca+A[i]][cb+B[i]],dp[i][ca][cb]+C[i])

ans=CM
for cb in range(400):
    CB=cb+1
    for ca in range(400):
        CA=ca+1
        if CA*M2 == CB*M1:
            if dp[N-1][CA][CB] == 0:
                pass
            else:
                ans=min(ans,dp[N-1][CA][CB])

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

Submission status: Wrong Answer

Solution code:
#D問題
NM=40
ABM=10
MM=400

N,M1,M2=map(int,input().split())
A=[]
B=[]
C=[]
for i in range(N):
    a,b,c=map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(c)
cmax=max(C)
CM=N*max(C)+1
dp=[[[CM for broop in range(401)] for aroop in range(401)] for nroop in range(NM+1)]
dp[0][0][0]=0
for i in range(N):
    for cb in range(401):
        for ca in range(401):
            if dp[i][ca][cb]==CM:
                continue
            dp[i+1][ca][cb]=min(dp[i+1][ca][cb],dp[i][ca][cb])
            if ca+A[i]<401 and cb+B[i]<401: 
                dp[i+1][ca+A[i]][cb+B[i]]=min(dp[i+1][ca+A[i]][cb+B[i]],dp[i][ca][cb]+C[i])

ans=CM
for cb in range(400):
    CB=cb+1
    for ca in range(400):
        CA=ca+1
        if ca*M2 == cb*M1:
            if dp[N-1][CA][CB] == 0:
                pass
            else:
                ans=min(ans,dp[N-1][CA][CB])

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

Submission status: Wrong Answer

Solution code:
#D問題
NM=40
ABM=10
MM=400

N,M1,M2=map(int,input().split())
A=[]
B=[]
C=[]
for i in range(N):
    a,b,c=map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(c)
cmax=max(C)
CM=N*max(C)+1
dp=[[[CM for broop in range(401)] for aroop in range(401)] for nroop in range(NM+1)]
dp[0][0][0]=0
for i in range(N):
    for cb in range(401):
        for ca in range(401):
            if dp[i][ca][cb]==CM:
                continue
            dp[i+1][ca][cb]=min(dp[i+1][ca][cb],dp[i][ca][cb])
            if ca+A[i]<401 and cb+B[i]<401: 
                dp[i+1][ca+A[i]][cb+B[i]]=min(dp[i+1][ca+A[i]][cb+B[i]],dp[i][ca][cb]+C[i])

ans=CM
for cb in range(400):
    CB=cb+1
    for ca in range(400):
        CA=ca+1
        if ca*M2 == cb*M1:
            ans=min(ans,dp[N-1][CA][CB])

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

Submission status: Wrong Answer

Solution code:
#D問題
NM=40
ABM=10
MM=400

N,M1,M2=map(int,input().split())
A=[]
B=[]
C=[]
for i in range(N):
    a,b,c=map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(c)
cmax=max(C)
CM=N*max(C)+1
dp=[[[CM for broop in range(401)] for aroop in range(401)] for nroop in range(NM+1)]
dp[0][0][0]=0
for i in range(N-1):
    for cb in range(401):
        for ca in range(401):
            if dp[i][ca][cb]==CM:
                continue
            dp[i+1][ca][cb]=min(dp[i+1][ca][cb],dp[i][ca][cb])
            if ca+A[i]<401 and cb+B[i]<401: 
                dp[i+1][ca+A[i]][cb+B[i]]=min(dp[i+1][ca+A[i]][cb+B[i]],dp[i][ca][cb]+C[i])

ans=CM
for cb in range(400):
    CB=cb+1
    for ca in range(400):
        CA=ca+1
        if ca*M2 == cb*M1:
            ans=min(ans,dp[N][CA][CB])

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

Submission status: Wrong Answer

Solution code:
#D問題
NM=40
ABM=10
MM=400

N,M1,M2=map(int,input().split())
A=[]
B=[]
C=[]
for i in range(N):
    a,b,c=map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(c)
cmax=max(C)
CM=N*max(C)+1
dp=[[[CM for broop in range(401)] for aroop in range(401)] for nroop in range(NM+1)]
dp[0][0][0]=0
for i in range(N):
    for cb in range(401):
        for ca in range(401):
            if dp[i][ca][cb]==CM:
                continue
            dp[i+1][ca][cb]=min(dp[i+1][ca][cb],dp[i][ca][cb])
            if ca+A[i]<401 and cb+B[i]<401: 
                dp[i+1][ca+A[i]][cb+B[i]]=min(dp[i+1][ca+A[i]][cb+B[i]],dp[i][ca][cb]+C[i])

ans=CM
for cb in range(400):
    CB=cb+1
    for ca in range(400):
        CA=ca+1
        if ca*M2 == cb*M1:
            ans=min(ans,dp[N][CA][CB])

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

Submission status: Wrong Answer

Solution code:
#D問題
NM=40
ABM=10
MM=400

N,M1,M2=map(int,input().split())
A=[]
B=[]
C=[]
for i in range(N):
    a,b,c=map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(c)
cmax=max(C)
CM=N*cmax
dp=[[[CM for broop in range(401)] for aroop in range(401)] for nroop in range(NM+1)]
dp[0][0][0]=0
for i in range(N):
    for cb in range(401):
        for ca in range(401):
            if dp[i][ca][cb]==CM:
                continue
            dp[i+1][ca][cb]=min(dp[i+1][ca][cb],dp[i][ca][cb])
            if ca+A[i]<400 and cb+B[i]<400: 
                dp[i+1][ca+A[i]][cb+B[i]]=min(dp[i+1][ca+A[i]][cb+B[i]],dp[i][ca][cb]+C[i])

ans=CM
for cb in range(400):
    CB=cb+1
    for ca in range(400):
        CA=ca+1
        if ca*M2 == cb*M1:
            ans=min(ans,dp[N][CA][CB])

if ans == N*cmax:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N,A,B=map(int,input().split())

L=[]
for i in range(N):
    a,b,c=map(int,input().split())
    L.append([a,b,c])
#print(L)

dp=[[100000 for i in range(401)]for j in range(401)]
dp[0][0]=0

for n in range(N):
    for i in range(401):
        for j in range(401):
            if dp[400-i][400-j]<100000:
                if 400-i+L[n][0]<=400 and 400-j+L[n][1]<=400:
                    if dp[400-i+L[n][0]][400-j+L[n][1]]>dp[400-i][400-j]+L[n][2]:
                        dp[400-i+L[n][0]][400-j+L[n][1]]=dp[400-i][400-j]+L[n][2]
ans=100000
for i in range(1,10):
    for j in range(1,10):
        if i*B==j*A and ans>dp[i][j]:
            ans=dp[i][j]
if ans<100000:
    print(ans)
else:
    print(-1)
#print(dp)

Submission status: Wrong Answer

Solution code:
N,A,B=map(int,input().split())

L=[]
for i in range(N):
    a,b,c=map(int,input().split())
    L.append([a,b,c])
#print(L)

dp=[[100000 for i in range(401)]for j in range(401)]
dp[0][0]=0

for n in range(N):
    for i in range(401):
        for j in range(401):
            if dp[i][j]<100000:
                if i+L[n][0]<=400 and j+L[n][1]<=400:
                    if dp[i+L[n][0]][j+L[n][1]]>dp[i][j]+L[n][2]:
                        dp[i+L[n][0]][j+L[n][1]]=dp[i][j]+L[n][2]
ans=100000
for i in range(1,401):
    for j in range(1,401):
        if i*B==j*A and ans>dp[i][j]:
            ans=dp[i][j]
if ans<100000:
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
mod=1777777777
n,ma,mb=map(int,input().split())
d=[[mod for j in range(250)] for i in range(250)]
for i in range(n):
    a,b,c=map(int,input().split())
    d[a][b]=min(d[a][b],c)
    for j in range(1,250-a):
        for k in range(1,250-b):
            if d[j][k]!=mod:
                d[j+a][k+b]=min(d[j+a][k+b],d[j][k]+c)
ans=mod
for i in range(250):
    if i%ma==0 and mb*i//ma<250:
        ans=min(ans,d[i][mb*i//ma])
print([ans,-1][ans==mod])

Submission status: Wrong Answer

Solution code:
mod=1777777777
n,ma,mb=map(int,input().split())
d=[[mod for j in range(200)] for i in range(200)]
for i in range(n):
    a,b,c=map(int,input().split())
    d[a][b]=min(d[a][b],c)
    for j in range(1,200-a):
        for k in range(1,200-b):
            if d[j][k]!=mod:
                d[j+a][k+b]=min(d[j+a][k+b],d[j][k]+c)
ans=mod
for i in range(200):
    if i%ma==0 and mb*i//ma<200:
        ans=min(ans,d[i][mb*i//ma])
print([ans,-1][ans==mod])

Submission status: Wrong Answer

Solution code:
mod=1777777777
n,ma,mb=map(int,input().split())
d=[[mod for j in range(200)] for i in range(200)]
for i in range(n):
    a,b,c=map(int,input().split())
    x=a
    y=b
    d[x][y]=min(d[x][y],c)
    for j in range(200-a):
        for k in range(200-b):
            if d[j][k]!=mod:
                d[j+a][k+b]=min(d[j+a][k+b],d[j][k]+c)
ans=mod
for i in range(200):
    if i%ma==0 and mb*i//ma<200:
        ans=min(ans,d[i][mb*i//ma])

print([ans,-1][ans==mod])

Submission status: Wrong Answer

Solution code:
mod=1777777777
n,ma,mb=map(int,input().split())
d=[[mod for j in range(100)] for i in range(100)]
for i in range(n):
    a,b,c=map(int,input().split())
    x=a
    y=b
    d[x][y]=min(d[x][y],c)
    for j in range(100-a):
        for k in range(100-b):
            if d[j][k]!=mod:
                d[j+a][k+b]=min(d[j+a][k+b],d[j][k]+c)
ans=mod
for i in range(100):
    if i%ma==0 and mb*i//ma<100:
        ans=min(ans,d[i][mb*i//ma])

print([ans,-1][ans==mod])

Submission status: Wrong Answer

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

max_a = sum(list(zip(*info))[0]) + 1
max_b = sum(list(zip(*info))[1]) + 1

dp = [[[float("inf")]*(max_b) for i in range(max_a)] for i in range(n+1)]

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

for ni in range(1,len(info)+1):
  for ai in range(0,max_a):
    for bi in range(0,max_b):
      if ai-info[ni-1][0] < 0 or bi-info[ni-1][1] < 0:
        dp[ni][ai][bi] = dp[ni-1][ai][bi]
      else:
        dp[ni][ai][bi] = min(dp[ni-1][ai][bi],dp[ni][ai-info[ni-1][0]][bi-info[ni-1][1]]+info[ni-1][2])
    
ans = float("inf")
for ai in range(1,max_a):
  for bi in range(1,max_b):
    if ai*mb == bi*ma:
      ans = min(ans,dp[n][ai][bi])
#print(dp[n])
if ans == float("inf"):
  print(-1)
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
n,a,b=map(int,input().split())
d=[[0]*401 for i in range(401)]
h=[]
for i in range(n):
    s=[]
    c=d[:]
    p,q,r=map(int,input().split())
    if d[p][q]!=0:
        d[p][q]=min(r,c[p][q])
    else:
        s.append([p,q])
        d[p][q]=r
    for j in range(len(h)-1,-1,-1):
        if h[j][0]+p<401 and h[j][1]+q<401:
            if c[h[j][0]+p][h[j][1]+q]!=0:
                d[h[j][0]+p][h[j][1]+q]=min(r+c[h[j][0]][h[j][1]],d[h[j][0]+p][h[j][1]+q])
            else:
                s.append([h[j][0]+p,h[j][1]+q])
                d[h[j][0]+p][h[j][1]+q]=r+c[h[j][0]][h[j][1]]
    else:
        h+=s
else:
    c=float("inf")
    k=1
    while a*k<401 and b*k<401:
        if d[a*k][b*k]!=0:
            c=min(c,d[a*k][b*k])
        k+=1
    else:
        if c==float("inf"):
            print(-1)
        else:
            print(c)

Submission status: Wrong Answer

Solution code:
n,a,b=map(int,input().split())
d=[[0]*401 for i in range(401)]
h=[]
for i in range(n):
    s=[]
    p,q,r=map(int,input().split())
    if d[p][q]!=0:
        d[p][q]=min(r,d[p][q])
        r=min(r,d[p][q])
    else:
        s.append([p,q])
        d[p][q]=r
    for j in range(len(h)):
        if h[j][0]+p<401 and h[j][1]+q<401:
            if d[h[j][0]+p][h[j][1]+q]!=0:
                d[h[j][0]+p][h[j][1]+q]=min(r+d[h[j][0]][h[j][1]],d[h[j][0]+p][h[j][1]+q])
            else:
                s.append([h[j][0]+p,h[j][1]+q])
                d[h[j][0]+p][h[j][1]+q]=r+d[h[j][0]][h[j][1]]
    else:
        h+=s
else:
    c=float("inf")
    k=1
    while a*k<401 and b*k<401:
        if d[a*k][b*k]!=0:
            c=min(c,d[a*k][b*k])
        k+=1
    else:
        if c==float("inf"):
            print(-1)
        else:
            print(c)

Submission status: Wrong Answer

Solution code:
n,a,b=map(int,input().split())
d=[[0]*401 for i in range(401)]
h=[]
for i in range(n):
    s=[]
    p,q,r=map(int,input().split())
    if d[p][q]!=0:
        d[p][q]=min(r,d[p][q])
    else:
        s.append([p,q])
        d[p][q]=r
    for j in range(len(h)):
        if h[j][0]+p<401 and h[j][1]+q<401:
            if d[h[j][0]+p][h[j][1]+q]!=0:
                d[h[j][0]+p][h[j][1]+q]=min(r+d[h[j][0]][h[j][1]],d[h[j][0]+p][h[j][1]+q])
            else:
                s.append([h[j][0]+p,h[j][1]+q])
                d[h[j][0]+p][h[j][1]+q]=r+d[h[j][0]][h[j][1]]
    else:
        h+=s
else:
    c=float("inf")
    k=1
    while a*k<401 and b*k<401:
        if d[a*k][b*k]!=0:
            c=min(c,d[a*k][b*k])
        k+=1
    else:
        if c==float("inf"):
            print(-1)
        else:
            print(c)

Submission status: Wrong Answer

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

for i in range(401):
    for j in range(401):
        for x in L:
            dp[i+ x[0] ][j+ x[1] ] = min(dp[i+x[0]][j+x[1]],dp[i][j] + x[2])
ans = 10**10
for i in range(401):
    for j in range(401):
        if i *Mb == Ma *j and not (i == 0 and j ==0):
            ans = min (ans, dp[i][j])
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 = float("inf")

dp = [ [ [inf] * 401 for _ in range(401)] for _ in range(N+1)]
dp[0][0][0] = 0

for i in range(N):
  a,b,c = ABC[i]
  for ma in range(400):
    for mb in range(400):
      dp[i+1][ma][mb] = min(dp[i+1][ma][mb],
                            dp[i][ma][mb])
      if ma + a > 400: continue
      if mb + b > 400: continue
        
      dp[i+1][ma+a][mb+b] = \
      min(dp[i+1][ma+a][mb+b],
          dp[i][ma][mb]+c)
      
ans = inf
for i in range(1,40000):
  if Ma*i > 400 or Mb*i > 400: break
  ans = min(ans, dp[N][Ma*i][Mb*i])

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N, MA, MB = map(int, input().split())
    dp = [[[float("inf")] * (401) for _ in range(401)] for _ in range(2)]
    dp[0][0][0] = 0
    dp[1][0][0] = 0
    for i in range(N):
        a, b, c = map(int, input().split())
        prev = (i-1) % 2
        current = i % 2
        for j in range(401):
            for k in range(401):
                if j-a >= 0 and k-b >= 0:
                    dp[current][j][k] = min(dp[prev][j][k],
                                            dp[prev][j-a][k-b] + c)
                else:
                    dp[current][j][k] = dp[prev][j][k]
    ans = float("inf")
    for i in range(1, 401):
        a = MA * i
        b = MB * i
        if a <= 400 and b <= 400:
            ans = min(ans, dp[(N) % 2][a][b])
        else:
            continue
    if ans == float("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())
    dp = [[[float("inf")] * (401) for _ in range(401)] for _ in range(2)]
    dp[0][0][0] = 0
    dp[1][0][0] = 0
    for i in range(N):
        a, b, c = map(int, input().split())
        prev = (i-1) % 2
        current = i % 2
        for j in range(401):
            for k in range(401):
                try:
                    dp[current][j+a][k+b] = min(dp[prev][j+a][k+b],
                                                dp[prev][j][k] + c)
                except IndexError:
                    pass
    ans = float("inf")
    for i in range(1, 401):
        a = MA * i
        b = MB * i
        if a <= 400 and b <= 400:
            ans = min(ans, dp[(N+1) % 2][a][b])
        else:
            continue
    if ans == float("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())
    dp = [[[float("inf")] * (401) for _ in range(401)] for _ in range(2)]
    dp[0][0][0] = 0
    dp[1][0][0] = 0
    suma, sumb = 0, 0
    for i in range(N):
        a, b, c = map(int, input().split())
        prev = (i-1) % 2
        current = i % 2
        for j in range(suma+1):
            for k in range(sumb+1):
                try:
                    dp[current][j+a][k+b] = min(dp[prev][j+a][k+b],
                                                dp[prev][j][k] + c)
                except IndexError:
                    pass
        suma += a
        sumb += b
    ans = float("inf")
    for i in range(1, 401):
        a = MA * i
        b = MB * i
        if a <= 400 and b <= 400:
            ans = min(ans, dp[(N+1) % 2][a][b])
        else:
            continue
    if ans == float("inf"):
        print(-1)
    else:
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

dp=[[float("inf") for _ in range(11)]for _ in range(11)]
dp[0][0]=0

from collections import deque
def gcd(a,b):
    while b:
        a,b=b,a%b
    return a

for i in range(N):
    c=cost[i]
    for j in range(0,11):
        for z in range(0,11):
            a=j+c[0]
            b=z+c[1]
            k=gcd(a,b)
            a=a//k
            b=b//k
            if(a>10 or b>10):
                continue
            dp[a][b]=min(dp[a][b],dp[j][z]+c[2])


ans=10**10
for i in range(1,100):
    a=i*Ma
    b=i*Mb
    if(a>10 or b>10):
        break
    ans=min(ans,dp[a][b])
if(ans==10**10):
    print(-1)

else:
    print(ans)

Submission status: Wrong Answer

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

c = n * 100 + 1

dp = dict()
dp[0] = [[0,0]]
ans = 999999

for i in range(n):
    if abc[i][2] in dp.keys():
        dp[abc[i][2]] = dp[abc[i][2]] + [[abc[i][0], abc[i][1]]]
    else:
        dp[abc[i][2]] = [[abc[i][0], abc[i][1]]]
    temp = [] + list(dp.items())
    for k, v in temp:
        #print(k ,v)
        for a, b in v:
            if (a + abc[i][0]) * mb == (b + abc[i][1]) * ma:
                #print(i, a, b, v, ans, k, abc[i])
                ans = min(ans, k + abc[i][2])
            if k + abc[i][2] in dp.keys():
                dp[k + abc[i][2]] = dp[k + abc[i][2]] + [[a + abc[i][0], b + abc[i][1]]]
            else:
                dp[k + abc[i][2]] = [[a + abc[i][0], b + abc[i][1]]]

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

Submission status: Wrong Answer

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

c = n * 100 + 1

dp = [[[] for _ in range(c)] for _ in range(n + 1)]

#print(dp)
#print(dp[0])

dp[0][0].append([0,0])

ans = 999999

for i in range(n):
    for j in range(c):
        for a, b in dp[i][j]:
            if (abc[i][0] + a) * mb == (abc[i][1] + b) * ma:
                ans = min(ans, abc[i][2] + j)
            dp[i+1][j + abc[i][2]].append([abc[i][0] + a, abc[i][1] + b])

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

MAX = 40001


def main():
    N, Ma, Mb = [int(a) for a in input().split()]

    abc = [
        [int(a) for a in input().split()]
        for _ in range(N)
    ]

    dp = [
        [
            [MAX for b in range(10 * N + 1)]
            for a in range(10 * N + 1)
        ]
        for i in range(N)
    ]  # dp[i][a][b] = i番目までを使ってaグラムbグラムを作る最小コスト

    a, b, c = abc[0]
    dp[0][a][b] = c
    dp[0][0][0] = 0

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

    res = MAX
    l = dp[-1]
    for a in range(10 * N):
        aa = a * Mb
        if aa % Ma != 0:
            continue
        b = aa // Ma
        if b >= len(l[a]):
            continue
        c = l[a][b]
        res = min(res, c)

    if res == MAX:
        res = -1
    print(res)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

MAX = 40001


def main():
    N, Ma, Mb = [int(a) for a in input().split()]

    abc = [
        [int(a) for a in input().split()]
        for _ in range(N)
    ]

    dp = [
        [
            [MAX for b in range(10 * N + 1)]
            for a in range(10 * N + 1)
        ]
        for i in range(N)
    ]  # dp[i][a][b] = i番目までを使ってaグラムbグラムを作る最小コスト

    a, b, c = abc[0]
    dp[0][a][b] = c

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

    res = MAX
    l = dp[-1]
    for a in range(10 * N):
        aa = a * Mb
        if aa % Ma != 0:
            continue
        b = aa // Ma
        if b >= len(l[a]):
            continue
        c = l[a][b]
        res = min(res, c)

    if res == MAX:
        res = -1
    print(res)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict


def main():
    N, Ma, Mb = [int(a) for a in input().split()]

    abc = [
        [int(a) for a in input().split()]
        for _ in range(N)
    ]

    dp = [
        [
            [2 ** 32 for b in range(10 * N + 1)]
            for a in range(10 * N + 1)
        ]
        for i in range(N)
    ]  # dp[i][a][b] = i番目までを使ってaグラムbグラムを作る最小コスト

    a, b, c = abc[0]
    dp[0][a][b] = c

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

    res = 2 ** 32
    l = dp[-1]
    for a in range(10 * N):
        aa = a * Mb
        if aa % Ma != 0:
            continue
        b = aa // Ma
        if b > 10 * N:
            continue
        c = l[a][b]
        res = min(res, c)

    if res == 2 ** 32:
        res = -1
    print(res)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

A,B,C = [],[],[]
for i in range(N):
    a,b,c=map(int,input().split())
    A += [a]
    B += [b]
    C += [c]

dp = [[[10**18 for _ in range(401)] for _ in range(401)] for _ in range(N+1)]

dp[0][0][0]=0
for i in range(N):
    for ca in range(i * 10):
        for cb in range(i * 10):
            dp[i+1][ca][cb] = min(dp[i+1][ca][cb],dp[i][ca][cb])
            dp[i+1][ca+A[i]][cb+B[i]] = min(dp[i+1][ca+A[i]][cb+B[i]],dp[i][ca][cb]+C[i])

ans = 10**18
for ca in range(401):
    for cb in range(401):
        if not ca*Mb == cb*Ma or ca == 0:
            continue
        if ans > dp[N][ca][cb]:
            ans = dp[N][ca][cb]
        
if ans == 10**18:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

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

dp = [[10**18 for _ in range(401)] for _ in range(401)]

dp[0][0]=0
for i in range(N):
    a,b,c=map(int,input().split())
    for ca in range(361, -1, -1):
        for cb in range(361, -1, -1):
            dp[ca+a][cb+b] = min(dp[ca+a][cb+b],dp[ca][cb]+c)