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,raw_input().split())
src = []
sum_a = sum_b = 0
for i in range(N):
  a,b,c = map(int,raw_input().split())
  src.append((a,b,c))
  sum_a += a
  sum_b += b

INF = 10**9
dp = [[[INF for b in range(sum_b+1)] for a in range(sum_a+1)] for i in range(N+1)]
dp[0][0][0] = 0

for i in range(N):
  ai,bi,ci = src[i]
  for a in range(sum_a+1 - ai):
    for b in range(sum_b+1 - bi):
      dp[i+1][a+ai][b+bi] = min(dp[i][a+ai][b+bi], dp[i][a][b] + ci)

ans = INF
for i in range(1,N):
  n = 1
  while True:
    a,b = n*MA,n*MB
    if a > sum_a or b > sum_b: break
    ans = min(ans, dp[i][a][b])
    n += 1

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

Submission status: Wrong Answer

Solution code:
N,MA,MB = map(int,raw_input().split())
src = []
sum_a = sum_b = 0
for i in range(N):
  a,b,c = map(int,raw_input().split())
  src.append((a,b,c))
  sum_a += a
  sum_b += b

INF = 10**9
dp = [[[INF for b in range(sum_b+1)] for a in range(sum_a+1)] for i in range(N+1)]
dp[0][0][0] = 0

ans = INF
for i in range(N):
  ai,bi,ci = src[i]
  for a in range(sum_a+1 - ai):
    for b in range(sum_b+1 - bi):
      dp[i+1][a+ai][b+bi] = min(dp[i][a+ai][b+bi], dp[i][a][b] + ci)
      if (a+ai)*MB == (b+bi)*MA:
        ans = min(ans, dp[i+1][a+ai][b+bi])

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

Submission status: Wrong Answer

Solution code:
INF = float('inf')

def solve():
    N, Ma, Mb = map(int, input().split())
    l = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        l.append((a, b, c))
 
    dp = [[[INF] * 401 for _ in range(401)] for _ in range(N + 1)]
    dp[0][0][0] = 0
    
    for i in range(N):
        for ca in range(401):
            for cb in range(401):
                if dp[i][ca][cb] != INF:
                    dp[i + 1][ca][cb] = min(dp[i + 1][ca][cb], dp[i][ca][cb])
                    dp[i + 1][ca + l[i][0]][cb + l[i][1]] = min(dp[i + 1][ca + l[i][0]][cb + l[i][1]], dp[i][ca][cb] + l[i][2])
 
    ans = INF
    for i in range(N):
        for ca in range(1, 401):
            for cb in range(1, 401):
                if dp[i][ca][cb] != INF:
                    if Ma * cb == Mb * ca:
                        ans = min(ans, dp[i][ca][cb])
 
    print(-1 if ans == INF else ans)
 
 
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
INF = float('inf')

def solve():
    N, Ma, Mb = map(int, input().split())
    l = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        l.append((a, b, c))
 
    dp = [[[INF] * 401 for _ in range(401)] for _ in range(N + 1)]
    dp[0][0][0] = 0
    for i in range(N):
        for a in range(401):
            for b in range(401):
                if dp[i][a][b] != INF:
                    dp[i + 1][a][b] = min(dp[i + 1][a][b], dp[i][a][b])
                    dp[i + 1][a + l[i][0]][b + l[i][1]] = min(dp[i + 1][a + l[i][0]][b + l[i][1]], dp[i][a][b] + l[i][2])
 
    ans = INF
    for i in range(N):
        for a in range(1, 401):
            for b in range(1, 401):
                if dp[i][a][b] != INF:
                    if Ma * b == Mb * a:
                        ans = min(ans, dp[i][a][b])
 
    print(-1 if ans == INF else ans)
 
 
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
def solve():
    
    INF = 10**10
    
    N, Ma, Mb = map(int, input().split())
    
    l = []
    for _ in range(N):
        a,b,c = map(int, input().split())
        l.append((a,b,c))
        
    #dp table: dp[i][ca][cb] 
    dp = [[[INF] * 401 for _ in range(401)] for _ in range(N+1)]
    dp[0][0][0] = 0
    
    for i in range(N):
        for ca in range(401):
            for cb in range(401):
                if dp[i][ca][cb] != INF:
                    dp[i+1][ca][cb] = min(dp[i][ca][cb], dp[i+1][ca][cb]) #薬品iを加えない場合
                    dp[i+1][ca+l[i][0]][cb+l[i][1]] = min(dp[i+1][ca+l[i][0]][cb+l[i][1]], dp[i][ca][cb] + l[i][2]) #薬品iを加える場合
                    
    ans = INF
    for i in range(N):
        for ca in range(1, 401):
            for cb in range(1, 401):
                if dp[i][ca][cb] != INF:
                    if ca * Mb == cb * Ma:
                        ans = min(ans, dp[i][ca][cb])
    
    print(-1 if ans == INF else ans)

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
def solve():
    
    INF = float('inf')
    
    N, Ma, Mb = map(int, input().split())
    
    l = []
    for _ in range(N):
        a,b,c = map(int, input().split())
        l.append((a,b,c))
        
    #dp table: dp[i][ca][cb] 
    dp = [[[INF] * 401 for _ in range(401)] for _ in range(N+1)]
    dp[0][0][0] = 0
    
    for i in range(N):
        for ca in range(401):
            for cb in range(401):
                if dp[i][ca][cb] != INF:
                    dp[i+1][ca][cb] = min(dp[i][ca][cb], dp[i+1][ca][cb]) #薬品iを加えない場合
                    dp[i+1][ca+l[i][0]][cb+l[i][1]] = min(dp[i+1][ca+l[i][0]][cb+l[i][1]], dp[i][ca][cb] + l[i][2]) #薬品iを加える場合
                    
    ans = INF
    for i in range(N):
        for ca in range(1, 401):
            for cb in range(1, 401):
                if dp[i][ca][cb] != INF:
                    if ca * Mb == cb * Ma:
                        ans = min(ans, dp[i][ca][cb])
    
    print(-1 if ans == INF else ans)

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
def solve():
    
    INF = float('inf')
    
    N, Ma, Mb = map(int, input().split())
    
    l = []
    for _ in range(N):
        a,b,c = map(int, input().split())
        l.append((a,b,c))
        
    #dp table: dp[i][ca][cb] 
    dp = [[[INF] * 401 for _ in range(401)] for _ in range(N+1)]
    dp[0][0][0] = 0
    
    for i in range(N):
        for ca in range(401):
            for cb in range(401):
                if dp[i][ca][cb] != INF:
                    dp[i+1][ca][cb] = min(dp[i][ca][cb], dp[i+1][ca][cb]) #薬品iを加えない場合
                    dp[i+1][ca+l[i][0]][cb+l[i][1]] = min(dp[i+1][ca+l[i][0]][cb+l[i][1]], dp[i][ca][cb] + l[i][2]) #薬品iを加える場合
                    
    ans = INF
    for i in range(N):
        for ca in range(401):
            for cb in range(401):
                if dp[i][ca][cb] != INF:
                    if ca * Mb == cb * Ma:
                        ans = min(ans, dp[i][ca][cb])
    
    print(-1 if ans == INF else ans)

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
INF = float('inf')

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

l = []
for _ in range(N):
    a,b,c = map(int, input().split())
    l.append((a,b,c))
    
#dp table: dp[i][ca][cb] 
dp = [[[INF] * 401 for _ in range(401)] for _ in range(N+1)]
dp[0][0][0] = 0

for i in range(N):
    for ca in range(401):
        for cb in range(401):
            if dp[i][a][b] != INF:
                dp[i+1][a][b] = min(dp[i][a][b], dp[i+1][a][b]) #薬品iを加えない場合
                dp[i+1][a+l[i][0]][b+l[i][1]] = min(dp[i+1][a+l[i][0]][b+l[i][1]], dp[i][a][b] + l[i][2]) #薬品iを加える場合
                
ans = INF
for i in range(N):
    for ca in range(401):
        for cb in range(401):
            if dp[i][a][b] != INF:
                if a * Mb == b * Ma:
                    ans = min(ans, dp[i][a][b])

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

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = [int(i) for i in input().split()]
a, b, c = [], [], []
for i in range(N):
    a_, b_, c_ = [int(i) for i in input().split()]
    a.append(a_)
    b.append(b_)
    c.append(c_)
cost = [[float('inf')] * 401 for _ in range(401)]


def solve():
    max_a, max_b = 1, 1
    cost[0][0] = 0
    for num in range(N):
        for ca in range(max_a,-1,-1):
            for cb in range(max_b,-1,-1):
                cost[ca + a[num]][cb + b[num]] = min(cost[ca][cb] + c[num], cost[ca + a[num]][cb + b[num]])
                cost[a[num]][b[num]] = min(cost[a[num]][b[num]], c[num])
        max_a += a[num]
        max_b += b[num]


solve()
ans = float('inf')
for i in range(1, 401):
    for n in range(1, 401):
        if i * Mb is n * Ma:
            ans = min(ans, cost[i][n])
if ans > 100000:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = [int(i) for i in input().split()]
a, b, c = [], [], []
for i in range(N):
    a_, b_, c_ = [int(i) for i in input().split()]
    a.append(a_)
    b.append(b_)
    c.append(c_)
cost = [[float('inf')] * 401 for _ in range(401)]


def solve():
    max_a, max_b = 1, 1
    cost[0][0] = 0
    for num in range(N):
        for ca in range(max_a+1):
            for cb in range(max_b+1):
                cost[ca + a[num]][cb + b[num]] = min(cost[ca][cb] + c[num], cost[ca + a[num]][cb + b[num]])
                cost[a[num]][b[num]] = min(cost[a[num]][b[num]],c[num])
        max_a += a[num]
        max_b += b[num]


solve()
ans = float('inf')
for i in range(1, 401):
    for n in range(1, 401):
        if i * Mb is n * Ma:
            ans = min(ans, cost[i][n])
if ans > 100000:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N, Ma, Mb = [int(i) for i in input().split()]
medic = [[0] * 3] * N
for i in range(N):
    medic[i] = [int(i) for i in input().split()]
cost = [[float('inf')] * 401 for _ in range(401)]


def solve(a, b, num, c):
    if num is N:
        return 0
    cost[a][b] = min(cost[a][b], c)
    solve(a + medic[num][0], b + medic[num][1], num + 1, c + medic[num][2])
    solve(a, b, num + 1, c)
    return 0


solve(medic[0][0], medic[0][1], 0, medic[0][2])
solve(0, 0, 0, float('inf'))
ans = float('inf')
for i in range(400 // max(Ma, Mb)):
    ans = min(ans, cost[Ma * i][Mb * i])
if ans > 1000:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import fractions

n, ma, mb = map(int,input().split())
a = [0] * (n + 1)
b = [0] * (n + 1)
c = [0] * (n + 1)
for i in range(1, n + 1):
    a[i], b[i], c[i] = map(int,input().split())

#iまでの薬品で、aがj(g),bがk(g)となるときの最小コスト
dp = [[[float('inf') for i in range(n + 1)] for j in range(401)] \
    for k in range(401)]

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

for i in range(1, n + 1):
    for j in range(401):
        for k in range(401):
            if dp[k][j][i - 1] == float('inf'):
                continue
            # i番目を買うか、買わないか
            if j < a[i] or k < b[i]:
                dp[k][j][i] = dp[k][j][i - 1]
            else:
                dp[k][j][i] = min(dp[k][j][i - 1], \
                    dp[k - b[i]][j - a[i]][i - 1] + c[i])

i = 1
ans = float('inf')
while True:
    if i * ma > 400 or i * mb > 400:
        break
    ans = min(ans, dp[i * mb][i * ma][n])
    i += 1

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

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict


INF = 10 ** 10 // 3
def main():
    N, Ma, Mb = map(int, input().split())
    l = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        l.append((a, b, c))

    dp = [[[INF] * 500 for _ in range(500)] for _ in range(N + 1)]
    dp[0][0][0] = 0
    for i in range(len(l)):
        for a in range(len(dp[0])):
            for b in range(len(dp[0][0])):
                if dp[i][a][b] != INF:
                    dp[i + 1][a][b] = min(dp[i + 1][a][b], dp[i][a][b])
                    dp[i + 1][a + l[i][0]][b + l[i][1]] = min(dp[i + 1][a + l[i][0]][b + l[i][1]], dp[i][a][b] + l[i][2])

    ans = INF
    for i in range(len(l)):
        for a in range(1, len(dp[0])):
            for b in range(1, len(dp[0][0])):
                if dp[i][a][b] != INF:
                    if Ma * b == Mb * a:
                        ans = min(ans, dp[i][a][b])

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import copy

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


N, Ma, Mb = map(int, input().split())
drugs = [0]*N

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

scores = {}
for (a, b, c) in drugs:
  tmp = gcd(a, b)
  a //= tmp
  b //= tmp

  if not (a, b) in scores.keys():
    scores[(a, b)] = c
  else:
    if c < scores[(a, b)]:
      scores[(a, b)] = c

  oldScores = copy.deepcopy(scores)
  for (olda, oldb) in list(oldScores.keys())[:]:
    na = olda + a
    nb = oldb + b
    tmp = gcd(na, nb)
    na //= tmp
    nb //= tmp
    if not (na, nb) in scores.keys():
      scores[(na, nb)] = oldScores[(olda, oldb)] + c
    else:
      if oldScores[(olda, oldb)] + c < scores[(na, nb)]:
        scores[(na, nb)] = oldScores[(olda, oldb)] + c

if not (Ma, Mb) in scores.keys():
  print(-1)
else:
  print(scores[(Ma, Mb)])

Submission status: Wrong Answer

Solution code:
import copy

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


N, Ma, Mb = map(int, input().split())
drugs = [0]*N

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

scores = {}
for (a, b, c) in drugs:
  tmp = gcd(a, b)
  a //= tmp
  b //= tmp

  if not (a, b) in scores:
    scores[(a, b)] = c
  else:
    if c < scores[(a, b)]:
      scores[(a, b)] = c

  oldScores = copy.deepcopy(scores)
  for (olda, oldb) in list(oldScores.keys())[:]:
    na = olda + a
    nb = oldb + b
    tmp = gcd(na, nb)
    na //= tmp
    nb //= tmp
    if not (na, nb) in scores:
      scores[(na, nb)] = oldScores[(olda, oldb)] + c
    else:
      if oldScores[(olda, oldb)] + c < scores[(na, nb)]:
        scores[(na, nb)] = oldScores[(olda, oldb)] + c

if not (Ma, Mb) in scores.keys():
  print(-1)
else:
  print(scores[(Ma, Mb)])

Submission status: Wrong Answer

Solution code:
def gcd(a, b):  # a >= b
  if b == 0:
    return a
  else:
    return gcd(b, a % b)


N, Ma, Mb = map(int, input().split())
drugs = [0]*N

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

scores = {}
for (a, b, c) in drugs:
  tmp = gcd(a, b)
  a //= tmp
  b //= tmp

  if not (a, b) in scores:
    scores[(a, b)] = c
  else:
    if c < scores[(a, b)]:
      scores[(a, b)] = c

  
  for (olda, oldb) in list(scores.keys())[:]:
    na = olda + a
    nb = oldb + b
    tmp = gcd(na, nb)
    na //= tmp
    nb //= tmp
    if not (na, nb) in scores:
      scores[(na, nb)] = scores[(olda, oldb)] + c
    else:
      if scores[(olda, oldb)] + c < scores[(na, nb)]:
        scores[(na, nb)] = scores[(olda, oldb)] + c

if not (Ma, Mb) in scores.keys():
  print(-1)
else:
  print(scores[(Ma, Mb)])

Submission status: Wrong Answer

Solution code:
N, M_a, M_b = map(int, raw_input().split())
v = [map(int, raw_input().split()) for i in range(N)]
def solve():
    dp = {(0, 0): 0}
    for i in range(N):
        a, b, c = v[i]
        for (A, B), C in dp.items():
            nA, nB, nC = A+a, B+b, C+c
            if not (nA, nB) in dp:
                dp[nA, nB] = nC
            else:
                dp[nA, nB] = min(dp[nA, nB], nC)
    for (a, b), c in sorted(dp.items(), key=lambda x: x[1]):
        if (a, b) == (0, 0):
            continue
        if a*M_b == b*M_a:
            print a, b, M_a, M_b
            return c
    return -1
print solve()

Submission status: Wrong Answer

Solution code:
N, M_a, M_b = map(int, raw_input().split())
v = [map(int, raw_input().split()) for i in range(N)]
def solve():
    dp = {(0, 0): 0}
    for i in range(N):
        a, b, c = v[i]
        for A, B in dp.keys():
            nA, nB, nC = A+a, B+b, dp[A, B]+c
            if not (nA, nB) in dp:
                dp[nA, nB] = nC
            else:
                dp[nA, nB] = min(dp[nA, nB], nC)
    for (a, b), c in sorted(dp.items(), key=lambda x: x[1]):
        if (a, b) == (0, 0):
            continue
        if a*M_b == b*M_a:
            return c
    return -1
print solve()

Submission status: Wrong Answer

Solution code:
N, M_a, M_b = map(int, raw_input().split())
v = [map(int, raw_input().split()) for i in range(N)]
def solve():
    dp = {(0, 0): 0}
    for i in range(N):
        a, b, c = v[i]
        for A, B in dp.keys():
            nA, nB, nC = A+a, B+b, dp[A, B]+c
            if not (nA, nB) in dp:
                dp[nA, nB] = nC
            else:
                dp[nA, nB] = min(dp[nA, nB], nC)
    for (a, b), c in sorted(dp.items(), key=lambda x: x[1]):
        if (a, b) == (0, 0):
            continue
        if a*M_b == b*M_a:
            return c
    return -1
print str(solve())

Submission status: Wrong Answer

Solution code:
import numpy as np

if 0:
    n,ma,mb = map(int, input().split(' '))
    abc = [list(map(int, input().split(' '))) for i in range(n)]
elif 1:
    n,ma,mb = 3,1,1
    abc = [[1,2,1],[2,1,2],[3,3,10]]
else:
    n,ma,mb = 1,1,10
    abc = [[10,10,10]]
    

INF = np.int32(10000000)
dp = np.ones((n+1,10*n+1,10*n+1),dtype=np.int32)*INF
dp[0,0,0] = 0

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

i = 1
ret = []
while True:
    try:
        ret.append(dp[-1,i*ma,i*mb])
        i+= 1
    except:
        break
    
ret = min(ret)
if ret == INF:
    ret = -1
print(ret)

Submission status: Wrong Answer

Solution code:
import numpy as np

if 1:
    n,ma,mb = map(int, input().split(' '))
    abc = [list(map(int, input().split(' '))) for i in range(n)]
elif 0:
    n,ma,mb = 3,1,1
    abc = [[1,2,1],[2,1,2],[3,3,10]]
else:
    n,ma,mb = 1,1,10
    abc = [[10,10,10]]
    

INF = np.int32(10000000)
dp = np.ones((n+1,10*n+1,10*n+1),dtype=np.int32)*INF
dp[0,0] = 0

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

i = 1
ret = []
while True:
    try:
        ret.append(dp[-1,i*ma,i*mb])
        i+= 1
    except:
        break
    
ret = min(ret)
if ret == INF:
    ret = -1
print(ret)

Submission status: Wrong Answer

Solution code:

import numpy as np

if 1:
    n,ma,mb = map(int, input().split(' '))
    abc = [list(map(int, input().split(' '))) for i in range(n)]
elif 0:
    n,ma,mb = 3,1,1
    abc = [[1,2,1],[2,1,2],[3,3,10]]
else:
    n,ma,mb = 1,1,10
    abc = [[10,10,10]]
    

INF = np.int32(10000000)
dp = np.ones((n+1,10*n+1,10*n+1),dtype=np.int32)*INF
dp[0,0] = 0

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

i = 1
ret = []
while True:
    try:
        ret.append(dp[-1,i*ma,i*mb])
        i+= 1
    except:
        break
    
ret = min(ret)
if ret == INF:
    ret = -1
print(ret)

Submission status: Wrong Answer

Solution code:

import numpy as np

if 1:
    n,ma,mb = map(int, input().split(' '))
    abc = [list(map(int, input().split(' '))) for i in range(n)]
elif 0:
    n,ma,mb = 3,1,1
    abc = [[1,2,1],[2,1,2],[3,3,10]]
else:
    n,ma,mb = 1,1,10
    abc = [[10,10,10]]
    

INF = np.int32(10000000)
dp = np.ones((10*n+1,10*n+1),dtype=np.int32)*INF
dp[0,0] = 0

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

i = 1
ret = []
while True:
    try:
        ret.append(dp[i*ma,i*mb])
        i+= 1
    except:
        break
    
ret = min(ret)
if ret == INF:
    ret = -1
print(ret)

Submission status: Wrong Answer

Solution code:

import numpy as np

if 1:
    n,ma,mb = map(int, input().split(' '))
    abc = [list(map(int, input().split(' '))) for i in range(n)]
elif 0:
    n,ma,mb = 3,1,1
    abc = [[1,2,1],[2,1,2],[3,3,10]]
else:
    n,ma,mb = 1,1,10
    abc = [[10,10,10]]
    

INF = np.int32(10000000)
dp = np.ones((10*n+1,10*n+1),dtype=np.int32)*INF
dp[0,0] = 0

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

if ma < mb:
    index = [(i*ma,i*mb) for i in range(1,int((10*n)/mb)+1)]
else:
    index = [(i*ma,i*mb) for i in range(1,int((10*n)/ma)+1)]

ret = min([dp[i,j] for i,j in index])
if ret == INF:
    ret = -1
print(ret)

Submission status: Wrong Answer

Solution code:
'''
Created on 2017/04/15

@author: admin
'''
import itertools
# import random
# for i in range(40):
#     print(random.randint(1, 10), random.randint(1, 10), random.randint(1, 100))

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

chemicals = [[int(i) for i in input().split()] for i in range(N)]

ans = False
cost = []
for i, j in itertools.combinations(chemicals, 2):
    if(i[0]*Mb == j[0]*Ma): 
#         print(i, j, "cost:", i[2]+j[2])
        cost.append(i[2]+j[2])
        ans = True;
# print(cost)

if(ans is not True): print(-1)
else: print(min(cost))

Submission status: Wrong Answer

Solution code:
import itertools
# import random
# for i in range(40):
#     print(random.randint(1, 10), random.randint(1, 10), random.randint(1, 100))

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

chemicals = [[int(i) for i in input().split()] for i in range(N)]

ans = False
cost = []
for i, j in itertools.combinations(chemicals, 2):
    if(i[0]*Mb == j[0]*Ma): 
        print(i, j, "cost:", i[2]+j[2])
        cost.append(i[2]+j[2])
        ans = True;
# print(cost)

if(ans is not True): print(-1)
else: print(min(cost))

Submission status: Wrong Answer

Solution code:
'''
Created on 2017/04/15

@author: admin
'''
import itertools
# import random
# for i in range(40):
#     print(random.randint(1, 10), random.randint(1, 10), random.randint(1, 100))

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

chemicals = [[int(i) for i in input().split()] for i in range(N)]
# print(chemicals)


ans = False
cost = []
for i, j in itertools.combinations(chemicals, 2):
    if(i[0]/j[0] == Ma/Mb): 
#         print(i, j, "cost:", i[2]+j[2])
        cost.append(i[2]+j[2])
        ans = True;
# print(cost)
if(ans is not True): print(-1)
else: print(min(cost))

Submission status: Wrong Answer

Solution code:
'''
Created on 2017/04/15

@author: admin
'''
import itertools
# import random
# for i in range(40):
#     print(random.randint(1, 10), random.randint(1, 10), random.randint(1, 100))

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

chemicals = [[int(i) for i in input().split()] for i in range(N)]
# print(chemicals)


ans = False
cost = []
for i, j in itertools.combinations(chemicals, 2):
    if(i[0] + j[0] == i[1] + j[1]): 
#         print(i, j, "cost:", i[2]+j[2])
        cost.append(i[2]+j[2])
        ans = True;
# print(cost)
if(ans is not True): print(-1)
else: print(min(cost))

Submission status: Wrong Answer

Solution code:
'''
Created on 2017/04/15

@author: admin
'''
import itertools
# import random

# for i in range(20):
#     print(random.randint(1, 10), random.randint(1, 10), random.randint(1, 100))

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

chemicals = [[int(i) for i in input().split()] for i in range(N)]
# print(chemicals)


ans = False
cost = []
for i, j in itertools.combinations(chemicals, 2):
    if(i[0] + j[0] == i[1] + j[1]): 
#         print(i, j, "cost:", i[2]+j[2])
        cost.append(i[2]+j[2])
        ans = True;
#     print(i, j)
print(cost)
if(ans is not True): print(-1)
else: print(min(cost))

Submission status: Wrong Answer

Solution code:
'''
Created on 2017/04/15

@author: admin
'''
import itertools

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

chemicals = [[int(i) for i in input().split()] for i in range(N)]
# print(chemicals)

ans = False
for i, j in itertools.combinations(chemicals, 2):
    if(i[0] + j[0] == i[1] + j[1]): 
#         print(i, j, "cost:", i[2]+j[2])
        print(i[2]+j[2])
        ans = True;
#     print(i, j)

if(ans is not True): print(-1)

Submission status: Wrong Answer

Solution code:
global N
global Nlist
global Ma
global Mb
global minicost
minicost = 10000000
N, Ma, Mb = map(int,input().split()) 
Nlist = []
for i in range(N):
    a,b,c = map(int,input().split())
    Nlist.append([a,b,c])
def dvector(Masum,Mbsum,cost,vector):
    print(vector)
    global Ma
    global Mb
    global Nlist
    global minicost
    if len(vector) == len(Nlist):
        return 0
    dvector(Masum,Mbsum,cost,vector+[0])
    Masum+= Nlist[len(vector)-1][0]
    Mbsum+= Nlist[len(vector)-1][1]
    cost += Nlist[len(vector)-1][2]
    if Mbsum > 0:
        if Masum/Mbsum == Ma/Mb:
            if minicost > cost:
                minicost = cost
    print(vector,cost,minicost)
    if cost < minicost:
        dvector(Masum,Mbsum,cost,vector+[1])
    return 0
    
dvector(0,0,0,[])
if minicost == 10000000:
    print("-1")
else:
    print(minicost)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

N, Ma, Mb = list(map(int, input().split()))
inlist = []
for i in range(N):
	inlist.append(list(map(int, input().split())))

inlist_a = inlist[0:(N//2)]
inlist_b = inlist[(N//2):N]

def getcomblist(inlist, isIncludeFirstZero):
	ret = []
	index = 0 if isIncludeFirstZero==True else 1
	for i in range(index, 2**len(inlist)):
		elm = [0 for i in range(len(inlist[0]))]
		for j in range(len(inlist)):
			for k in range(len(inlist[0])):
				elm[k] += inlist[j][k] * ((i >> j) & 0b1)
		ret.append(elm)
	return ret

comb_a = getcomblist(inlist_a, False)
comb_b = getcomblist(inlist_b, True)
comb_b.sort(key=lambda x:x[2], reverse=False)

min = float("inf")
for i in comb_a:
	for j in comb_b:
		if i[2] + j[2] > min:
			break
		if ((i[0]+j[0]) * Mb) == ((i[1]+j[1]) * Ma):
			min = i[2] + j[2]
			break

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
import itertools

N, Ma, Mb =[int(n) for n in input().split()] 
drugs = np.array([[int(n) for n in input().split()] for i in range(N)]).astype(np.int32)
a_max, b_max, c_max = drugs.T[0].max(), drugs.T[1].max(), drugs.T[2].max()
inf = c_max + 1
drugs = drugs.tolist()

dp = np.ones((N+1, N*a_max + 1, N*b_max + 1)).astype(np.int32) * inf
dp = dp.tolist()
dp[0][0][0] = 0

for i, ca, cb in itertools.product(range(N), range(N*a_max + 1), range(N*b_max + 1)):
    if dp[i][ca][cb] != inf:
        dp[i+1][ca][cb] = min(dp[i+1][ca][cb], dp[i][ca][cb])  
        dp[i+1][ca + drugs[i][0]][cb + drugs[i][1]] = min(dp[i+1][ca + drugs[i][0]][cb + drugs[i][1]], 
                                                          dp[i][ca][cb] + drugs[i][2])

ans = inf

for ca, cb in itertools.product(range(1, N*a_max + 1), range(1, N*b_max + 1)):
    if ca*Mb == cb*Ma:
        ans = min(ans, dp[N][ca][cb])
print(ans if ans != inf else -1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

N, Ma, Mb =[int(n) for n in input().split()] 
drugs = np.array([[int(n) for n in input().split()] for i in range(N)]).astype(np.int32)
a_max, b_max, c_max = drugs.T[0].max(), drugs.T[1].max(), drugs.T[2].max()
inf = c_max + 1
drugs = drugs.tolist()

dp = np.ones((N, N*a_max + 1, N*b_max + 1)).astype(np.int32) * inf
dp = dp.tolist()
dp[0][0][0] = 0

for i in range(N-1):
    for ca in range(N*a_max + 1):
        for cb in range(N*b_max + 1):
            if dp[i][ca][cb] != inf:
                dp[i+1][ca][cb] = min(dp[i+1][ca][cb], dp[i][ca][cb])  
                dp[i+1][ca + drugs[i][0]][cb + drugs[i][1]] = min(dp[i+1][ca + drugs[i][0]][cb + drugs[i][1]], 
                                                                  dp[i][ca][cb] + drugs[i][2])

ans = inf
for ca in range(1, N*a_max + 1):
    for cb in range(1, N*b_max + 1):
        if ca*Mb == cb*Ma:
            ans = min(ans, dp[-1][ca][cb])
print(ans if ans != inf else -1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

N, Ma, Mb =[int(n) for n in input().split()] 
drugs = np.array([[int(n) for n in input().split()] for i in range(N)]).astype(np.int32)
a_max, b_max, c_max = drugs.T[0].max(), drugs.T[1].max(), drugs.T[2].max()
inf = c_max + 1
drugs = drugs.tolist()

dp = np.ones((N, N*a_max + 1, N*b_max + 1)).astype(np.int32) * inf
dp = dp.tolist()
dp[0][0][0] = 0

for i in range(N-1):
    for ca in range(N*a_max + 1):
        for cb in range(N*b_max + 1):
            if dp[i][ca][cb] != inf: 
                dp[i+1][ca][cb] = min(dp[i+1][ca][cb], dp[i][ca][cb]) 
                dp[i+1][ca + drugs[i][0]][cb + drugs[i][1]] = min(dp[i+1][ca + drugs[i][0]][cb + drugs[i][1]], 
                                                                  dp[i][ca][cb] + drugs[i][2]) 

ans = inf
for ca in range(N*a_max + 1):
    for cb in range(N*b_max + 1):
        if ca*Mb == cb*Ma and ca!=0:
            ans = min(ans, dp[-1][ca][cb])
print(ans if ans != inf else -1)

Submission status: Wrong Answer

Solution code:

from collections import defaultdict

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

A,B,C = [0]*N,[0]*N,[0]*N

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

MAX = sum(C)+1

table = defaultdict(lambda: MAX)
table[(0,0)] = 0

for a,b,c in zip(A,B,C):
  for e in list(table.keys()):
    ne = (e[0]+a, e[1]+b)
    table[ne] = min(table[ne], table[e] + c)

max_iter = min(sum(A)//Ma, sum(B)//Mb) + 1

try:
  cost = min(table[(Ma*i,Mb*i)] for i in range(1,max_iter))
except ValueError:
  cost = MAX

if cost == MAX:
  print(-1)
else:
  print(cost)