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())
A, B, C = [], [], []
for i in range(N):
    ai, bi, ci = map(int, input().split())
    A.append(ai)
    B.append(bi)
    C.append(ci)

INF = float('inf')
dp = [[[INF] * 401 for i in range(401)] for j in range(N + 1)]
dp[0][0][0] = 0

for i in range(1, N + 1):
    for a in range(1, 401):
        for b in range(1, 401):
            if A[i - 1] <= a and B[i - 1] <= b:
                dp[i][a][b] = min(dp[i - 1][a][b], dp[i - 1][a - A[i - 1]][b - B[i - 1]] + C[i - 1])
            else:
                dp[i][a][b] = min(dp[i][a][b], dp[i - 1][a][b])
ans = INF
for i in range(1, 401):
    target_a = Ma * i
    target_b = Mb * i
    if target_a > 400 or target_b > 400:
        break
    ans = min(ans, dp[N][target_a][target_b])

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

Submission status: Wrong Answer

Solution code:
import random
import bisect
import math

nn, ma, mb = [int(i) for i in input().split()]
a =  [0 for i in range(nn)]
b =  [0 for i in range(nn)]
c =  [0 for i in range(nn)]
for i in range(nn):
     a[i], b[i], c[i] = [int(i) for i in input().split()]

option = 0     # 0:PSO 1:CFA 2:CFArank
max_or_min = 0 # 0:minimize 1:maxmize
dimension = nn
iter=1

N = 50 #粒子の数
T = 1200 #世代数(ループの回数)
maximum=1
minimum=0


#--------粒　子　群　最　適　化------------------------------

#評価関数
def criterion(x):

    z = sum([c[i] * x[i] for i in range(dimension)])
    if mb * sum([a[i] * x[i] for i in range(dimension)]) - ma * sum([b[i] * x[i] for i in range(dimension)]) != 0 or sum(x) == 0:
        z = 1000000
    return z

#粒子の位置の更新を行う関数
def update_position(x, v, x_min, x_max):
#    new_x = [x_min[i] if x[i]+ v[i] < x_min[i] else x_max[i] if x[i]+ v[i] > x_max[i] else x[i]+ v[i] for i in range(dimension)]
    new_x = [1 if random.uniform(0, 1)< 1/(1+math.e**(-v[i])) else 0 for i in range(dimension)]
    return new_x

#粒子の速度の更新を行う関数
def update_velocity(x, v, p, g, w, ro_max=1.0, c1=2.00, c2=0.75):
    #パラメーターroはランダムに与える
    phi=c1+c2
    K=2/abs(2-phi-(phi*phi-4*phi)**0.5)
    
    #粒子速度の更新を行う
    if option!=0:
        new_v = [K * ( w * v[i] + c1 * random.uniform(0, ro_max) * (p[i] - x[i]) + c2 * random.uniform(0, ro_max) * (g[i] - x[i]) ) for i in range(dimension)]
    else:
        new_v = [w * v[i] + c1 * random.uniform(0, ro_max) * (p[i] - x[i]) + c2 * random.uniform(0, ro_max) * (g[i] - x[i]) for i in range(dimension)]

    return new_v

def main():
    w = 1.0
    w_best, w_worst = 1.25, 0.25
    x_min = [minimum for i in range(dimension)]
    x_max = [maximum for i in range(dimension)]
    
    #粒子位置, 速度, パーソナルベスト, グローバルベストの初期化を行う
    ps = [[random.randint(x_min[j], x_max[j]) for j in range(dimension)] for i in range(N)]
    vs = [[0.0 for j in range(dimension)] for i in range(N)]

    personal_best_positions = ps[:] 
    personal_best_scores = [criterion(p) for p in ps]

    if max_or_min == 1:
        best_particle = personal_best_scores.index(max(personal_best_scores))
    elif max_or_min == 0:
        best_particle = personal_best_scores.index(min(personal_best_scores))
    global_best_position = personal_best_positions[best_particle][:]

    for t in range(T):
        for n in range(N):
            x = ps[n][:]
            v = vs[n][:]
            p = personal_best_positions[n][:]

            if option>=2:
                best_list = sorted(personal_best_positions)
                mu = bisect.bisect_left( best_list, p ) + 1
                w = w_best - mu * (w_best - w_worst) / (N - 1)


            #粒子の位置の更新を行う
            new_x = update_position(x, v, x_min, x_max)
            ps[n] = new_x[:]

            #粒子の速度の更新を行う
            new_v = update_velocity(new_x, v, p, global_best_position, w)
            vs[n] = new_v[:]

            #評価値を求め, パーソナルベストの更新を行う
            score = criterion(new_x)

            if max_or_min == 1:
                if score > personal_best_scores[n]:
                    personal_best_scores[n] = score
                    personal_best_positions[n] = new_x[:]
            elif max_or_min == 0:
                if score < personal_best_scores[n]:
                    personal_best_scores[n] = score
                    personal_best_positions[n] = new_x[:]

        #グローバルベストの更新を行う
        if max_or_min == 1:
            best_particle = personal_best_scores.index(max(personal_best_scores))
            global_best_position = personal_best_positions[best_particle][:]

        elif max_or_min == 0:
            best_particle = personal_best_scores.index(min(personal_best_scores))
            global_best_position = personal_best_positions[best_particle][:]
        f.write(str(max(personal_best_scores))+"\n")
    #最適解
    if max_or_min == 1:
        return max(personal_best_scores)
    elif max_or_min == 0:
        #print(global_best_position)
        return min(personal_best_scores)

#--------------------------------------------------------------
with open('test_'+ str(option)+".txt", 'w') as f:
    
  if max_or_min == 1:
    best=-float("inf")
    for i in range(iter):
        best=max(best, main())

  elif max_or_min == 0:
    best=float("inf")
    for i in range(iter):
        best=min(best, main())
if best == 1000000:
    print(-1)
else:
    print(best)

Submission status: Wrong Answer

Solution code:
import random
import bisect
import math

nn, ma, mb = [int(i) for i in input().split()]
a =  [0 for i in range(nn)]
b =  [0 for i in range(nn)]
c =  [0 for i in range(nn)]
for i in range(nn):
     a[i], b[i], c[i] = [int(i) for i in input().split()]

option = 0     # 0:PSO 1:CFA 2:CFArank
max_or_min = 0 # 0:minimize 1:maxmize
dimension = nn
iter=1

N = 50 #粒子の数
T = 1000 #世代数(ループの回数)
maximum=1
minimum=0


#--------粒　子　群　最　適　化------------------------------

#評価関数
def criterion(x):

    z = sum([c[i] * x[i] for i in range(dimension)])
    if mb * sum([a[i] * x[i] for i in range(dimension)]) - ma * sum([b[i] * x[i] for i in range(dimension)]) != 0 or sum(x) == 0:
        z = 1000000
    return z

#粒子の位置の更新を行う関数
def update_position(x, v, x_min, x_max):
#    new_x = [x_min[i] if x[i]+ v[i] < x_min[i] else x_max[i] if x[i]+ v[i] > x_max[i] else x[i]+ v[i] for i in range(dimension)]
    new_x = [1 if random.uniform(0, 1)< 1/(1+math.e**(-v[i])) else 0 for i in range(dimension)]
    return new_x

#粒子の速度の更新を行う関数
def update_velocity(x, v, p, g, w, ro_max=1.0, c1=2.00, c2=0.75):
    #パラメーターroはランダムに与える
    phi=c1+c2
    K=2/abs(2-phi-(phi*phi-4*phi)**0.5)
    
    #粒子速度の更新を行う
    if option!=0:
        new_v = [K * ( w * v[i] + c1 * random.uniform(0, ro_max) * (p[i] - x[i]) + c2 * random.uniform(0, ro_max) * (g[i] - x[i]) ) for i in range(dimension)]
    else:
        new_v = [w * v[i] + c1 * random.uniform(0, ro_max) * (p[i] - x[i]) + c2 * random.uniform(0, ro_max) * (g[i] - x[i]) for i in range(dimension)]

    return new_v

def main():
    w = 1.0
    w_best, w_worst = 1.25, 0.25
    x_min = [minimum for i in range(dimension)]
    x_max = [maximum for i in range(dimension)]
    
    #粒子位置, 速度, パーソナルベスト, グローバルベストの初期化を行う
    ps = [[random.randint(x_min[j], x_max[j]) for j in range(dimension)] for i in range(N)]
    vs = [[0.0 for j in range(dimension)] for i in range(N)]

    personal_best_positions = ps[:] 
    personal_best_scores = [criterion(p) for p in ps]

    if max_or_min == 1:
        best_particle = personal_best_scores.index(max(personal_best_scores))
    elif max_or_min == 0:
        best_particle = personal_best_scores.index(min(personal_best_scores))
    global_best_position = personal_best_positions[best_particle][:]

    for t in range(T):
        for n in range(N):
            x = ps[n][:]
            v = vs[n][:]
            p = personal_best_positions[n][:]

            if option>=2:
                best_list = sorted(personal_best_positions)
                mu = bisect.bisect_left( best_list, p ) + 1
                w = w_best - mu * (w_best - w_worst) / (N - 1)


            #粒子の位置の更新を行う
            new_x = update_position(x, v, x_min, x_max)
            ps[n] = new_x[:]

            #粒子の速度の更新を行う
            new_v = update_velocity(new_x, v, p, global_best_position, w)
            vs[n] = new_v[:]

            #評価値を求め, パーソナルベストの更新を行う
            score = criterion(new_x)

            if max_or_min == 1:
                if score > personal_best_scores[n]:
                    personal_best_scores[n] = score
                    personal_best_positions[n] = new_x[:]
            elif max_or_min == 0:
                if score < personal_best_scores[n]:
                    personal_best_scores[n] = score
                    personal_best_positions[n] = new_x[:]

        #グローバルベストの更新を行う
        if max_or_min == 1:
            best_particle = personal_best_scores.index(max(personal_best_scores))
            global_best_position = personal_best_positions[best_particle][:]

        elif max_or_min == 0:
            best_particle = personal_best_scores.index(min(personal_best_scores))
            global_best_position = personal_best_positions[best_particle][:]
        f.write(str(max(personal_best_scores))+"\n")
    #最適解
    if max_or_min == 1:
        return max(personal_best_scores)
    elif max_or_min == 0:
        #print(global_best_position)
        return min(personal_best_scores)

#--------------------------------------------------------------
with open('test_'+ str(option)+".txt", 'w') as f:
    
  if max_or_min == 1:
    best=-float("inf")
    for i in range(iter):
        best=max(best, main())

  elif max_or_min == 0:
    best=float("inf")
    for i in range(iter):
        best=min(best, main())
if best == 1000000:
    print(-1)
else:
    print(best)

Submission status: Wrong Answer

Solution code:
N,MA,MB=map(int,input().split())

A=[0]
B=[0]
C=[0]

for i in range(N):
    a,b,c=map(int,input().split())
    A.append(a)
    B.append(b)
    C.append(c)
    
dp=[[[0,0,0]for j in range(N*max(C)+1)]for i in range(N+1)]
ans=1000000
for i in range(1,N+1):
    for j in range(1,N*max(C)+1):
        if j-C[i]<0:
            dp[i][j][0]=dp[i-1][j][0]
            dp[i][j][1]=dp[i-1][j][1]
            dp[i][j][2]=dp[i-1][j][2]
        else:
            dp[i][j][2]=max(dp[i-1][j][2],dp[i-1][j-C[i]][2]+C[i])
            if dp[i][j][2]==dp[i-1][j][2]:
                dp[i][j][0]=dp[i-1][j][0]
                dp[i][j][1]=dp[i-1][j][1]
            else:
                dp[i][j][0]=dp[i-1][j-C[i]][0]+A[i]
                dp[i][j][1]=dp[i-1][j-C[i]][1]+B[i]
                if dp[i][j][0]*MB==dp[i][j][1]*MA:
                    if ans>dp[i][j][2]:
                        ans=dp[i][j][2]
if ans==1000000:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = [int(_) for _ in input().split()]
ABC = [[int(_) for _ in input().split()] for _ in range(N)]
dp = {}
dp[ABC[0][0] + 1000 * ABC[0][1]] = ABC[0][2]
ans = 5000
for i in range(N - 1):
    dp_old = dp.copy()
    for ab in dp_old.keys():
        a = ab % 1000
        b = ab // 1000
        if a * Mb == b * Ma:
            ans = min(ans, dp_old[ab])
        dp[a + ABC[i + 1][0] + 1000 * (b + ABC[i + 1][1])] = min(
            dp_old[ab] + ABC[i + 1][2],
            dp_old.get(a + ABC[i + 1][0] + 1000 * (b + ABC[i + 1][1]), 5000))
for ab in dp.keys():
    a = ab % 1000
    b = ab // 1000
    if a * Mb == b * Ma:
        ans = min(ans, dp[ab])
if ans == 5000:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = [int(_) for _ in input().split()]
ABC = [[int(_) for _ in input().split()] for _ in range(N)]
dp = {}
dp[ABC[0][0] + 1000 * ABC[0][1]] = ABC[0][2]
ans = 5000
for i in range(N - 1):
    dp_old = dp.copy()
    for ab in dp_old.keys():
        a = ab % 1000
        b = ab // 1000
        if a * Mb == b * Ma:
            ans = min(ans, dp_old[ab])
        dp[a + ABC[i + 1][0] + 1000 * (b + ABC[i + 1][1])] = min(
            dp_old[ab] + ABC[i + 1][2],
            dp_old.get(a + ABC[i + 1][0] + 1000 * (b + ABC[i + 1][1]), 5000))
    print(dp)
for ab in dp.keys():
    a = ab % 1000
    b = ab // 1000
    if a * Mb == b * Ma:
        ans = min(ans, dp[ab])
if ans == 5000:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = [int(_) for _ in input().split()]
ABC = [[int(_) for _ in input().split()] for _ in range(N)]
dp = {}
dp[0] = {}
dp[0][ABC[0][0] + 1000 * ABC[0][1]] = ABC[0][2]
ans = 5000
for i in range(N - 1):
    for ab in dp[i].keys():
        a = ab % 1000
        b = ab // 1000
        if a * Mb == b * Ma:
            ans = min(ans, dp[i][ab])
        dp[i + 1] = dp.get(i + 1, {})
        dp[i + 1][a + ABC[i + 1][0] + 1000 * (b + ABC[i + 1][1])] = min(
            dp[i][ab] + ABC[i + 1][2], dp[i].get(
                a + ABC[i + 1][0] + 1000 * (b + ABC[i + 1][1]), 5000))
for ab in dp[N - 1].keys():
    a = ab % 1000
    b = ab // 1000
    if a * Mb == b * Ma:
        ans = min(ans, dp[N - 1][ab])
if ans == 5000:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = [int(_) for _ in input().split()]
ABC = [[int(_) for _ in input().split()] for _ in range(N)]
dp = {}
dp[0] = {}
dp[0][ABC[0][0] + 1000 * ABC[0][1]] = ABC[0][2]
ans = 5000
for i in range(N - 1):
    for ab in dp[i].keys():
        a = ab % 1000
        b = ab // 1000
        if a * Mb == b * Ma:
            ans = min(ans, dp[i][ab])
        dp[i + 1] = dp.get(i + 1, {})
        dp[i + 1][a + ABC[i + 1][0] + 1000 * (b + ABC[i + 1][1])] = min(
            dp[i][ab] + ABC[i + 1][2], dp[i].get(
                a + ABC[i + 1][0] + 1000 * (b + ABC[i + 1][1]), 0))
for ab in dp[N - 1].keys():
    a = ab % 1000
    b = ab // 1000
    if a * Mb == b * Ma:
        ans = min(ans, dp[N - 1][ab])
if ans == 5000:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = [int(_) for _ in input().split()]
ABC = [[int(_) for _ in input().split()] for _ in range(N)]
dp = {}
dp[0] = {}
dp[0][ABC[0][0] + 1000 * ABC[0][1]] = ABC[0][2]
ans = 5000
for i in range(N - 1):
    for ab in dp[i].keys():
        a = ab % 1000
        b = ab // 1000
        if a * Mb == b * Ma:
            ans = min(ans, dp[i][ab])
        dp[i + 1] = dp.get(i + 1, {})
        dp[i + 1][a + ABC[i + 1][0] + 1000 * (b + ABC[i + 1][1])] = max(
            dp[i][ab] + ABC[i + 1][2], dp[i].get(
                a + ABC[i + 1][0] + 1000 * (b + ABC[i + 1][1]), 0))
for ab in dp[N - 1].keys():
    a = ab % 1000
    b = ab // 1000
    if a * Mb == b * Ma:
        ans = min(ans, dp[N - 1][ab])
if ans == 5000:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
n, ma, mb = map(int, input().split())
d = []
for i in range(n):#h:高さ
    d.append([int(m) for m in input().split()])

def gcd(a,b):
    if b == 0:
        return a
    return gcd(b, a % b)

#dptable,dp[n][a][b][c](a = dp[n][0])
"""dp = []
for i in range(401):
    for j in range(401):
        dp.append([i,j])"""

cost = []
for i in range(401):
    cost.append([])
for i in range(401):
    for j in range(401):
        cost[i].append([])
for i in range(401):
    for j in range(401):
        cost[i][j] = float("INF")
cost[0][0] = 0
pos = []
pos.append([0,0])
pos2 = set()
pos2.add((0,0))
for i in range(n):
    for v in range(2):
        for w in range(len(pos)):
            p = pos[w][0] + v * d[i][0]
            q = pos[w][1] + v * d[i][1]
            if (p, q) not in pos2:
                pos.append([p, q])
                pos2.add((p, q))
            if cost[p][q] > cost[pos[w][0]][pos[w][1]] + d[i][2]:
                cost[p][q] = cost[pos[w][0]][pos[w][1]] + d[i][2]

realcost = []
for i in range(401):
    for j in range(401):
        if gcd(i, j) != 0:
            if i // gcd(i, j) == ma and j // gcd(i, j) == mb and cost[i][j] != float("INF"):
                realcost.append(cost[i][j])

if len(realcost) > 0:
    print(min(realcost))
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
n, ma, mb = map(int, input().split())
d = []
for i in range(n):#h:高さ
    d.append([int(m) for m in input().split()])

def gcd(a,b):
    if b == 0:
        return a
    return gcd(b, a % b)

#dptable,dp[n][a][b][c](a = dp[n][0])
"""dp = []
for i in range(401):
    for j in range(401):
        dp.append([i,j])"""

cost = []
for k in range(160801):
    cost.append(float("INF"))
cost[0] = 0
pos = []
pos.append([0,0])
pos2 = set()
pos2.add((0,0))
for i in range(n):
    for v in range(2):
        for w in range(len(pos)):
            p = pos[w][0] + v * d[i][0]
            q = pos[w][1] + v * d[i][1]
            if (p, q) not in pos2:
                pos.append([p, q])
                pos2.add((p, q))
            if cost[p * 400 + q] > cost[pos[w][0] * 400 + pos[w][1]] + d[i][2]:
                cost[p * 400 + q] = cost[pos[w][0] * 400 + pos[w][1]] + d[i][2]

realcost = []
for i in range(401):
    for j in range(401):
        if gcd(i, j) != 0:
            if i // gcd(i, j) == ma and j // gcd(i, j) == mb and cost[i * 400 + j] != float("INF"):
                realcost.append(cost[i * 400 + j])

if len(realcost) > 0:
    print(min(realcost))
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, Ma, Mb = map(int, input().split())

dp = np.ones((N+1, 401, 401)) * float("inf")
dp[0][0][0] = 0



for i in range(N):
  a,b,c = map(int, input().split())
  for j in range(401):
    for k in range(401):
      if j-a < 0 or k-b < 0:
        dp[i+1][j][k] = dp[i][j][k]
      else:
        dp[i+1][j][k] = min(dp[i][j][k], dp[i][j-a][k-b] + c)

        
ans = float("inf")
        
a = Ma
b = Mb
while(a < 401 and b < 401):
  ans = min(ans, dp[N][a][b])
  a += Ma
  b += Mb
  
if ans == float("inf"):
  print(-1)
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
n, ma, mb = map(int, input().split())
d = []
for i in range(n):#h:高さ
    d.append([int(m) for m in input().split()])

def gcd(a,b):
    if b == 0:
        return a
    return gcd(b, a % b)

#dptable,dp[n][a][b][c](a = dp[n][0])
"""dp = []
for i in range(401):
    for j in range(401):
        dp.append([i,j])"""

cost = []
for k in range(160801):
    cost.append(float("INF"))
cost[0] = 0
pos = []
pos.append([0,0])
pos2 = set()
pos2.add((0,0))
for i in range(n):
    for v in range(2):
        for w in range(len(pos)):
            p = pos[w][0] + v * d[i][0]
            q = pos[w][1] + v * d[i][1]
            if (p, q) not in pos2:
                pos.append([p, q])
                pos2.add((p, q))
            if cost[p * 400 + q] > cost[pos[w][0] * 400 + pos[w][1]] + d[i][2]:
                cost[p * 400 + q] = cost[pos[w][0] * 400 + pos[w][1]] + d[i][2]

realcost = []
for k in range(160801):
    #t =  gcd(k // 400, k - k // 400)
    if gcd(k // 400, k - (k // 400) * 400) != 0:
        if (k // 400) // gcd(k // 400, k - (k // 400) * 400) == ma and (k - (k // 400) * 400) // gcd(k // 400, k - (k // 400) * 400) == mb and cost[k] != float("INF"):
            realcost.append(cost[k])

if len(realcost) > 0:
    print(min(realcost))
else:
    print(-1)

Submission status: Wrong Answer

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

a, b, c = [], [], []

for i in range(n):
    aa,bb,cc = list(map(int,input().split()))
    a.append(aa)
    b.append(bb)
    c.append(cc)

dp = [[float("inf")]*(sum(b)+1) for i in range(sum(a)+1)]
dp[0][0] = 0
max_a = 0
max_b = 0
for i in range(n):
    max_a += a[i]
    max_b += b[i]
    for ai in range(a[i], max_a+1):
        for bi in range(b[i], max_b+1):
            dp[ai][bi] = min(dp[ai][bi], dp[ai-a[i]][bi-b[i]] + c[i])

ans = float("inf")
for i in range(1, min(max_a//ma, max_b//mb)):
    ans = min(ans, dp[i*ma][i*mb])
if ans == float("inf"):
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

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

dp= [[[float("inf")] * (10 * N + 11) for a in range(10 * N + 11) ] for i in range(N + 1)]
dp[0][0][0] = 0

for i in range(N):
    a, b, c = L[i]
    for ca in range(10 * N + 1):
        for cb in range(10 * N + 1):
            dp[i+1][ca][cb] = min(dp[i+1][ca][cb], dp[i][ca][cb])
            dp[i+1][ca + a][cb + b] = min(dp[i+1][ca + a][cb + b], dp[i][ca][cb] + c)

reslis = []
ra, rb = (Ma, Mb)
while ra <= 10 * N and rb <= 10 * N:
    reslis.append(dp[N][ra][rb])
    ra += Ma
    rb += Mb

reslis.sort()
res = reslis[0]
if res == float("inf"):
    print(-1)
else:
    print(res)

Submission status: Wrong Answer

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

dp= [[[float("inf") for b in range(10 * N + 10)] for a in range(10 * N + 10) ] for i in range(N + 1)]
dp[0][0][0] = 0

for i in range(N):
    a, b, c = L[i]
    for ca in range(10 * N):
        for cb in range(10 * N):
            dp[i+1][ca][cb] = min(dp[i+1][ca][cb], dp[i][ca][cb])
            dp[i+1][ca + a][cb + b] = min(dp[i+1][ca + a][cb + b], dp[i][ca][cb] + c)

reslis = []
ra, rb = (Ma, Mb)
while ra <= 10 * N and rb <= 10 * N:
    reslis.append(dp[N][ra][rb])
    ra += Ma
    rb += Mb

reslis.sort()
res = reslis[0]
if res == float("inf"):
    print(-1)
else:
    print(res)

Submission status: Wrong Answer

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

dp= [[[float("inf") for b in range(10 * N + 1)] for a in range(10 * N + 1) ] for i in range(N + 1)]
dp[0][0][0] = 0

for i in range(N):
    a, b, c = L[i]
    for ca in range(N):
        for cb in range(N):
            dp[i+1][ca][cb] = min(dp[i+1][ca][cb], dp[i][ca][cb])
            dp[i+1][ca + a][cb + b] = min(dp[i+1][ca + a][cb + b], dp[i][ca][cb] + c)

reslis = []
ra, rb = (Ma, Mb)
while ra <= 10 * N and rb <= 10 * N:
    reslis.append(dp[N][ra][rb])
    ra += Ma
    rb += Mb

reslis.sort()
res = reslis[0]
if res == float("inf"):
    print(-1)
else:
    print(res)

Submission status: Wrong Answer

Solution code:
N,Ma,Mb=map(int,input().split())
dp=[[[10**9]*401 for i in range(401)] for i in range(N)]
for i in range(N):
  a,b,c=map(int,input().split())
  if i==0:
    dp[0][a][b]=c
  else:
    for p in range(401):
      for q in range(401):
        if p==0 or q==0:
          dp[i][p][q]=0
        else:
          if p>=a and q>=b:
            dp[i][p][q]=min(dp[i-1][p][q],dp[i-1][p-a][q-b]+c)
          else:
            dp[i][p][q]=dp[i-1][p][q]
ans=10**9
for a in range(1,401):
  if Mb*a/Ma<=400 and Mb*a%Ma==0:
    ans=min(ans,dp[N-1][a][Mb*a//Ma])
if ans==10**9:
  print(-1)
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
N, Ma, Mb=map(int,input().strip().split(' '))
inf=10**8
a=[]
b=[]
c=[]
for i in range(N):
    at,bt,ct=map(int,input().strip().split(' '))
    a.append(at)
    b.append(bt)
    c.append(ct)
suma=sum(a)
sumb=sum(b)
dp=[[[inf]*(sumb+1) for i in range(suma+1)]for j in range(N+1)]
dp[0][a[0]][b[0]]=c[0]
for i in range(N):
    for j in range(suma+1):
        for k in range(sumb+1):
            if a[i]<=j and b[i]<=k:
                dp[i+1][j][k]=min(dp[i][j][k],dp[i][j-a[i]][k-b[i]]+c[i])
            else:
                dp[i+1][j][k]=dp[i][j][k]
            
m=min(suma//Ma,sumb//Mb)
ans=-1
for i in range(m):
    #if dp[N][i*Ma][i*Mb]!=inf and dp[N][i*Ma][i*Mb]!=0:
    if dp[N][i*Ma][i*Mb]!=inf:
        if ans==-1:
            ans=dp[N][i*Ma][i*Mb]
        else:
            ans=min(ans,dp[N][i*Ma][i*Mb])
        #break
print(ans)

Submission status: Wrong Answer

Solution code:
N, Ma, Mb=map(int,input().strip().split(' '))
inf=10**8
a=[]
b=[]
c=[]
for i in range(N):
    at,bt,ct=map(int,input().strip().split(' '))
    a.append(at)
    b.append(bt)
    c.append(ct)
suma=sum(a)
sumb=sum(b)
dp=[[[inf]*(sumb+1) for i in range(suma+1)]for j in range(N+1)]
dp[0][a[0]][b[0]]=c[0]
for i in range(N):
    for j in range(suma+1):
        for k in range(sumb+1):
            if a[i]<=j and b[i]<=k:
                dp[i+1][j][k]=min(dp[i][j][k],dp[i][j-a[i]][k-b[i]]+c[i])
            else:
                dp[i+1][j][k]=dp[i][j][k]
            
m=min(suma//Ma,sumb//Mb)
ans=-1
for i in range(m):
    #if dp[N][i*Ma][i*Mb]!=inf and dp[N][i*Ma][i*Mb]!=0:
    if dp[N][i*Ma][i*Mb]!=inf:
        ans=dp[N][i*Ma][i*Mb]
        break
print(ans)

Submission status: Wrong Answer

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

mix=set([(0,0)])
ans=10**9
cost={(0,0):0}
for i in range(N):
    a,b,c=list(map(int,input().split()))
    tmp=mix.copy()
    tmp2=cost.copy()
    for x in tmp:
        d=x[0]+a
        e=x[1]+b
        f=min(tmp2.get((d,e),10**9),cost[x]+c)
        cost[(d,e)]=f
        mix.add((d, e))
        if d*Mb==e*Ma and f<ans:
            ans=f
"""
print(cost)
print(mix)
"""
if ans==10**9:
    ans=-1
print(ans)

Submission status: Wrong Answer

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

mix=set([(0,0)])
ans=10**9
cost={(0,0):0}
for i in range(N):
    a,b,c=list(map(int,input().split()))
    tmp=mix.copy()
    for x in tmp:
        d=x[0]+a
        e=x[1]+b
        f=min(cost.get((d,e),10**9),cost[x]+c)
        cost[(d,e)]=f
        mix.add((d, e))
        if d*Mb==e*Ma and f<ans:
            ans=f
"""
print(cost)
print(mix)
"""
if ans==10**9:
    ans=-1
print(ans)

Submission status: Wrong Answer

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

mix=set([(0,0)])
ans=10**9
cost={(0,0):0}
for i in range(N):
    a,b,c=list(map(int,input().split()))
    tmp=mix.copy()
    for x in tmp:
        d=x[0]+a
        e=x[1]+b
        f=min(cost.get((d,e),10**9),cost[x]+c)
        cost[(d,e)]=f
        mix.add((d, e))
        if d*Mb==e*Ma and f<ans:
            ans=f
print(cost)
print(mix)
if ans==10**9:
    ans=-1
print(ans)

Submission status: Wrong Answer

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

mix=set([(0,0)])
rate=Ma/Mb
ans=10**9
cost={(0,0):0}
for i in range(N):
    a,b,c=list(map(int,input().split()))
    tmp=mix.copy()
    for x in tmp:
        d=x[0]+a
        e=x[1]+b
        f=min(cost.get((d,e),10**9),cost[x]+c)
        cost[(d,e)]=f
        mix.add((d, e))
        if d/Ma==e/Mb and f<ans:
            ans=f
if ans==10**9:
    ans=-1
print(ans)

Submission status: Wrong Answer

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

mix=set([(0,0)])
rate=Ma/Mb
ans=10**9
cost={(0,0):0}
for i in range(N):
    a,b,c=list(map(int,input().split()))
    tmp=mix.copy()
    for x in tmp:
        d=x[0]+a
        e=x[1]+b
        f=min(cost.get((d,e),10**9),cost[x]+c)
        cost[(d,e)]=f
        mix.add((d, e))
        if d/e==Ma/Mb and f<ans:
            ans=f
if ans==10**9:
    ans=-1
print(ans)

Submission status: Wrong Answer

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

inf = float('inf')

dp = [[inf for i in range(n*10)] for i in range(n*10)]

for i in range(n):
    for j in range(i*10,-1):
        for k in range(i*10,-1):
            if dp[j][k]!=inf:
                dp[j + abc[i][0]][k + abc[i][1]] =min(dp[j+abc[i][0]][k+abc[i][1]],dp[j][k]+abc[i][2])
    dp[abc[i][0] - 1][abc[i][1] - 1]= min(dp[abc[i][0] - 1][abc[i][1] - 1], abc[i][2])

ans = inf

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

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

Submission status: Wrong Answer

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

inf = float('inf')

dp = [[inf for i in range(n*10)] for i in range(n*10)]

for i in range(n):
    for j in range(i*10):
        for k in range(i*10):
            if dp[j][k]!=inf:
                dp[j + abc[i][0]][k + abc[i][1]] =min(dp[j+abc[i][0]][k+abc[i][1]],dp[j][k]+abc[i][2])
    dp[abc[i][0] - 1][abc[i][1] - 1]= min(dp[abc[i][0] - 1][abc[i][1] - 1], abc[i][2])

ans = inf

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

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

Submission status: Wrong Answer

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

A0 = [A[i][1] for i in range(N) if A[i][0] == 0]
A1 = [A[i] for i in range(N) if A[i][0] > 0]
A2 = [A[i] for i in range(N) if A[i][0] < 0]

def DP(A):
    dp = {0:0}
    for i in range(len(A)):
        keys = [key for key in dp]
        for j in keys:
            if A[i][0]+j not in dp:
                dp[A[i][0]+j] = A[i][1]+dp[j]
            else:
                dp[A[i][0]+j] = min(dp[A[i][0]+j],A[i][1]+dp[j])
    return dp

dA1 = DP(A1)
dA2 = DP(A2)
ans = float('inf')

for x in dA1:
    if x != 0 and -x in dA2:
        ans = min(dA1[x]+dA2[-x],ans)
for x in A0:
    ans = min(x,ans)

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

Submission status: Wrong Answer

Solution code:
def inpl(): return [int(i) for i in input().split()]

N, Ma, Mb = inpl()
abc = [inpl() for _ in range(N)]

maxc = sum(list(map(list, zip(*abc)))[2])+1
inf = maxc+1
pd = [[[inf for _ in range(maxc)] for _ in range(maxc)] for _ in range(N)]

pd[0][0][0] = 0
pd[0][abc[0][0]][abc[0][1]] = abc[0][2]
for i in range(1,N):
    for j in range(maxc):
        for k in range(maxc):
            pd[i][j][k] = \
            min(pd[i-1][j][k],pd[i-1][k-abc[i][0]][j-abc[i][1]] + abc[i][2])

ctr = inf
n = 1
while True:
    if maxc-1 < n*max(Ma,Mb):
        break
    ctr = min(ctr,pd[N-1][n*Ma][n*Mb])
    n += 1
if ctr == inf:
    print('-1')
else:
    print(ctr)