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: Runtime Error

Solution code:
from itertools import groupby, accumulate, product, permutations, combinations
def solve():
  ans = float('inf')
  N, A, B = map(int, input().split())
  mae = [list(map(int, input().split())) for _ in range((N+1)//2)]
  usiro = [list(map(int, input().split())) for _ in range(N-(N+1)//2)]
  if N==1:
    a,b,c = mae[0]
    if a*B==b*A:
      return c
    return -1
  mae_lis = [[float('inf')]*201 for _ in range(201)]
  for p in product([0,1],repeat=N//2):
    a,b,c = 0,0,0
    for i in range(N//2):
      if p[i]==1:
        a += mae[i][0];b += mae[i][1];c += mae[i][2]
      mae_lis[a][b] = min(mae_lis[a][b],c)
  usiro_lis = [[float('inf')]*201 for _ in range(201)]
  for p in product([0,1],repeat=N-N//2):
    a,b,c = 0,0,0
    for i in range(N//2):
      if p[i]==1:
        a += usiro[i][0];b += usiro[i][1];c += usiro[i][2]
      usiro_lis[a][b] = min(usiro_lis[a][b],c)
  for i in range(1,201):
    for j in range(1,201):
      c = mae_lis[i][j]
      if c==float('inf'):
        continue
      for k in range(1,201):
        if (i+k)*B%A!=0:
          continue
        l = (i+k)*B//A - j
        if l<=0:
          continue
        ans = min(ans,c+usiro_lis[k][l])
  return ans if ans<float('inf') else -1
print(solve())

Submission status: Runtime Error

Solution code:
from itertools import groupby, accumulate, product, permutations, combinations
def solve():
  ans = float('inf')
  N, A, B = map(int, input().split())
  mae = [list(map(int, input().split())) for _ in range(N//2)]
  usiro = [list(map(int, input().split())) for _ in range(N-N//2)]
  mae_lis = [[float('inf')]*201 for _ in range(201)]
  for p in product([0,1],repeat=N//2):
    a,b,c = 0,0,0
    for i in range(N//2):
      if p[i]==1:
        a += mae[i][0];b += mae[i][1];c += mae[i][2]
      mae_lis[a][b] = min(mae_lis[a][b],c)
  usiro_lis = [[float('inf')]*201 for _ in range(201)]
  for p in product([0,1],repeat=N-N//2):
    a,b,c = 0,0,0
    for i in range(N//2):
      if p[i]==1:
        a += usiro[i][0];b += usiro[i][1];c += usiro[i][2]
      usiro_lis[a][b] = min(usiro_lis[a][b],c)
  for i in range(1,201):
    for j in range(1,201):
      c = mae_lis[i][j]
      if c==float('inf'):
        continue
      for k in range(1,201):
        if (i+k)*B%A!=0:
          continue
        l = (i+k)*B//A - j
        if l<=0:
          continue
        ans = min(ans,c+usiro_lis[k][l])
  return ans if ans<float('inf') else -1
print(solve())

Submission status: Runtime Error

Solution code:
N, ma, mb = map(int, input().split())
a = [0]*N
b = [0]*N
c = [0]*N
NMAX = 10
ABMAX = 10
INF = 100000
for i in range(N):
    a[i], b[i], c[i] = map(int, input().split())
dp = [[[INF]*(NMAX*ABMAX+1)for _ in range(NMAX*ABMAX+1)]for _ in range(N+1)]
dp[0][0][0] = 0
for i in range(N):
    for ca in range(NMAX*ABMAX+1):
        for cb in range(NMAX*ABMAX+1):
            if dp[i][ca][cb] == INF:
                continue
            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 = INF
for ca in range(1, NMAX*ABMAX+1):
    for cb in range(1, NMAX*ABMAX+1):
        if ca * mb == cb * ma:
            ans = min(ans, dp[N][ca][cb])
print(ans if ans != INF else -1)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
from collections import deque,defaultdict

inp=lambda v,w:v[0]*w[0]+v[1]*w[1]
isprop = lambda a,b: a[0]*b[1]==a[1]*b[0]
cwsum = lambda l:[sum([x[0] for x in l]), sum([x[1] for x in l])]
chmin = lambda a,b:(a,b)[b<a]
# cwsum = lambda l:np.sum(l,axis=0)
            
def main():
    N,Ma,Mb=map(int,input().split())
    ch=dict()
    for _ in range(N):
        a,b,c=map(int,input().split())
        ch[(a,b)]=c
    p=[(-1)*Mb,Ma]
    pch={inp(p,v):ch[v] for v in iter(ch)} #p方向への射影
    kpch=list(pch.keys())
    infty=10**9
    X=N*200
    dp=[[infty]*(2*X+1) for _ in range(N)]
    for i in range(N):
        for j in range(2*X+1):
            if i==0 and j==X+kpch[0]:
                dp[i][j]=pch[kpch[0]]
            elif i>0 and j>=kpch[i] and j<=2*X+kpch[i]:
                dp[i][j]=chmin(dp[i-1][j],dp[i-1][j-kpch[i]]+pch[kpch[i]])
            elif i>0:
                dp[i][j]=dp[i-1][j]
    #  print(kpch)
    #  print(pch)
    #  print(dp)
    if dp[N-1][X]==infty:
        print(-1)
    else:
        print(dp[N-1][X])

if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
from collections import deque,defaultdict

inp=lambda v,w:v[0]*w[0]+v[1]*w[1]
isprop = lambda a,b: a[0]*b[1]==a[1]*b[0]
cwsum = lambda l:[sum([x[0] for x in l]), sum([x[1] for x in l])]
chmin = lambda a,b:(a,b)[b<a]
# cwsum = lambda l:np.sum(l,axis=0)
            
def main():
    N,Ma,Mb=map(int,input().split())
    ch=dict()
    for _ in range(N):
        a,b,c=map(int,input().split())
        ch[(a,b)]=c
    p=[(-1)*Mb,Ma]
    pch={inp(p,v):ch[v] for v in iter(ch)} #p方向への射影
    kpch=list(pch.keys())
    infty=10**9
    X=N*100
    dp=[[infty]*(2*X+1) for _ in range(N)]
    for i in range(N):
        for j in range(2*X+1):
            if i==0 and j==X+kpch[0]:
                dp[i][j]=pch[kpch[0]]
            elif i>0 and j>=kpch[i] and j<=2*X+kpch[i]:
                dp[i][j]=chmin(dp[i-1][j],dp[i-1][j-kpch[i]]+pch[kpch[i]])
            elif i>0:
                dp[i][j]=dp[i-1][j]
    #  print(kpch)
    #  print(pch)
    #  print(dp)
    if dp[N-1][X]==infty:
        print(-1)
    else:
        print(dp[N-1][X])

if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
from collections import deque,defaultdict

inp=lambda v,w:v[0]*w[0]+v[1]*w[1]
isprop = lambda a,b: a[0]*b[1]==a[1]*b[0]
cwsum = lambda l:[sum([x[0] for x in l]), sum([x[1] for x in l])]
chmin = lambda a,b:(a,b)[b<a]
# cwsum = lambda l:np.sum(l,axis=0)
            
def main():
    N,Ma,Mb=map(int,input().split())
    ch=dict()
    for _ in range(N):
        a,b,c=map(int,input().split())
        ch[(a,b)]=c
    p=[(-1)*Mb,Ma]
    pch={inp(p,v):ch[v] for v in iter(ch)} #p方向への射影
    kpch=list(pch.keys())
    infty=30
    X=N*4
    dp=[[infty]*(2*X+1) for _ in range(N)]
    for i in range(N):
        for j in range(2*X+1):
            if i==0 and j==X+kpch[0]:
                dp[i][j]=pch[kpch[0]]
            elif i>0 and j>=kpch[i] and j<=2*X+kpch[i]:
                dp[i][j]=chmin(dp[i-1][j],dp[i-1][j-kpch[i]]+pch[kpch[i]])
            elif i>0:
                dp[i][j]=dp[i-1][j]
    #  print(kpch)
    #  print(pch)
    #  print(dp)
    if dp[N-1][X]==infty:
        print(-1)
    else:
        print(dp[N-1][X])

if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
import more_itertools
import math
data = []
part = []
cost = math.inf
n = input()
n = n.split()
N = int(n[0])
Ma = int(n[1])
Mb = int(n[2])
for i in range(N):
    part.append(i)
    tmp = input()
    tmp = tmp.split()
    tmp = list(map(int, tmp))
    data.append(tmp)
part = more_itertools.powerset(part)
for i in part:
    a = 0
    b = 0
    c = 0
    for j in i:
        a += data[j][0]
        b += data[j][1]
        c += data[j][2]
    if (Ma*b == Mb*a) and cost > c and (c > 0):
        cost = c
if cost == math.inf:
    print('-1')
else:
    print(cost)

Submission status: Runtime Error

Solution code:
k, a, b = map(int, input().split()) 
item = [list(map(int, input().split())) for i in range(k)]
n = len(item)
cost_list = []
for i in range(2 ** n):
    bag = []
    for j in range(n):  # このループが一番のポイント
        if ((i >> j) & 1):  # 順に右にシフトさせ最下位bitのチェックを行う
            bag.append(item[j])  # フラグが立っていたら bag に果物を詰める

    drag_a = 0
    drag_b = 0
    cost = 0
    for var in bag:
        drag_a += var[0]
        drag_b += var[1]
        cost += var[2]
    if drag_a*b == drag_b*a:
        cost_list.append(cost)
cost_list.remove(0)
print(min(cost_list))

Submission status: Runtime Error

Solution code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#include<math.h>
#include<limits.h>
#define BIG 1000007
int min(int a,int b){
    if(a<b) return a;
    else return b;
}
int main(){
    int N,Ma,Mb;
    scanf("%d %d %d",&N,&Ma,&Mb);
    int a[42],b[42],c[42];
    for(int i=0;i<N;i++)
        scanf("%d %d %d",a+i,b+i,c+i);
    int DP[42][402][402];
    memset(DP,BIG,sizeof(DP));
    DP[0][a[0]][b[0]] = c[0];
    for(int i=1;i<N;i++){
        for(int j=0;j<402;j++){
            for(int k=0;k<402;k++){
                if(j-a[i]<0 || k-b[i]<0)
                    DP[i][j][k] = DP[i-1][j][k];
                else
                     DP[i][j][k]= min(DP[i-1][j][k],DP[i-1][j-a[i]][k-b[i]]+c[i]);

            }
        }
    }
    int ans = BIG;
    for(int i=1;i*Ma<402 && i*Mb<402;i++){
        ans = min(ans,DP[N-1][i*Ma][i*Mb]);
    }
    if(ans == BIG) printf("-1");
    else printf("%d",ans);
}

Submission status: Runtime Error

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

path = [[] for i in range(N)]

for _ in range(M):
	a,b = map(int,input().split())
	path[a-1].append(b-1)
	path[b-1].append(a-1)
vis = [0 for i in range(N)]
cnt = 0

def dfs(now,depth):
	global cnt
	if depth == N: cnt+=1
	for new in path[now]:
		if vis[new] == 0:
			vis[new] = 1
			dfs(new,depth+1)
			vis[new] = 0

vis[0] = 1
dfs(0,1)
print(cnt)

Submission status: Runtime Error

Solution code:
#!python3

LI = lambda: list(map(int, input().split()))

# input
N, Ma, Mb = LI()
ABC = [LI() for _ in range(N)]

INF = 10 ** 5


def main():
    w = [[INF] * 401 for _ in range(401)]
    w[0][0] = 0
    for a, b, c in ABC:
        l = w.copy()
        for j in range(a, 401):
            for k in range(b, 401):
                l[j][k] = min(l[j][k], w[j - a][k - b] + c)
        w = l

    ans = INF
    n = 400 // max(Ma, Mb) + 1
    for i in range(1, n):
        ans = min(ans, w[Ma * i][Mb * i])
    ans = -1 if ans == INF else ans
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
import itertools
import numpy as np
input = sys.stdin.readline
 
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return np.array(list(map(int, input().split())))


def main():
    n, ma, mb =  MI()
    num = ma / mb
    buy = [i for i in range(n)]
    mylist = [LI() for i in range(n)]
    result = []
    
    for j in range(1,len(buy)+1):
        for conb in itertools.combinations(buy, j):
            tempa = 0
            tempb = 0
            price = 0
            for i in conb:
                tempa += mylist[i][0]
                tempb += mylist[i][1]
                price += mylist[i][2]
            if tempa/tempb == num:
                result.append(price)
                
    if len(result) == 0:
        print(-1)
    else:
        print(min(result))


main()

Submission status: Runtime Error

Solution code:
import sys
import itertools
import numpy as np
input = sys.stdin.readline
 
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return np.array(list(map(int, input().split())))


def main():
    n, ma, mb =  MI()
    num = ma / mb
    buy = [i for i in range(n)]
    mylist = [LI() for i in range(n)]
    result = []
    
    for j in range(1,len(buy)+1):
        for conb in itertools.combinations(buy, j):
            tempa = 0
            tempb = 0
            price = 0
            for i in conb:
                tempa += mylist[i][0]
                tempb += mylist[i][1]
                price += mylist[i][2]
            if tempa/tempb == num:
                result.append(price)
                
    if len(result) == 0:
        print(-1)
    else:
        print(min(result))


main()

Submission status: Runtime Error

Solution code:
import sys
import itertools
import numpy as np
np_array = np.array
input = sys.stdin.readline
 
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return np_array(map(int, input().split()))


def main():
    n, ma, mb =  MI()
    num = ma / mb
    buy = [i for i in range(n)]
    mylist = [LI() for i in range(n)]
    result = []
    
    for j in range(1,len(buy)+1):
        for conb in itertools.combinations(buy, j):
            tempa = 0
            tempb = 0
            price = 0
            for i in conb:
                tempa += mylist[i][0]
                tempb += mylist[i][1]
                price += mylist[i][2]
            if tempa/tempb == num:
                result.append(price)
                
    if len(result) == 0:
        print(-1)
    else:
        print(min(result))


main()

Submission status: Runtime Error

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    INF = 10**5
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    n,ma,mb = LI()
    med = [LI() for _ in range(n)]
    h1 = []
    h2 = []

    for i in range(2 ** (n//2)):
        bit = 1
        ta = 0
        tb = 0
        tc = 0
        for j in range(n//2):
            if i & bit:
                a,b,c = med[j]
                ta += a
                tb += b
                tc += c
            bit <<= 1
        dif = ta * mb - tb * ma
        h1.append((dif,tc))

    for i in range(2 ** (n - n//2)):
        bit = 1
        ta = 0
        tb = 0
        tc = 0
        for j in range(n - n//2):
            if i & bit:
                a,b,c = med[j+n//2]
                ta += a
                tb += b
                tc += c
            bit <<= 1
        dif = tb * ma - ta * mb
        h2.append((dif,tc))

    h1.sort()
    h2.sort()
    i = 0
    ans = INF
    for h,c in h1:
        while h2[i][0] < h:
            i += 1
            if i >= len(h2): break
        else:
            while h2[i][0] == h:
                if h != 0 and h2[i][0] != 0:
                    ans = min(ans,c+h2[i][1])
                i += 1
            continue
        break
    if ans < INF:
        print(ans)
    else:
        print(-1)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    INF = 10**5
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    n,ma,mb = LI()
    med = [LI() for _ in range(n)]
    h1 = []
    h2 = []

    for i in range(2 ** (n//2)):
        b = 1
        ta = 0
        tb = 0
        tc = 0
        for j in range(n//2):
            if i & b:
                a,b,c = med[j]
                ta += a
                tb += b
                tc += c
            b <<= 1
        dif = ta * mb - tb * ma
        h1.append((dif,tc))

    for i in range(2 ** (n - n//2)):
        b = 1
        ta = 0
        tb = 0
        tc = 0
        for j in range(n - n//2):
            if i & b:
                a,b,c = med[j+n//2]
                ta += a
                tb += b
                tc += c
            b <<= 1
        dif = tb * ma - ta * mb
        h2.append((dif,tc))

    h1.sort()
    h2.sort()
    i = 0
    ans = INF
    for h,c in h1:
        while h2[i][0] < h:
            i += 1
            if i >= len(h2): break
        if h == h2[i][0] and (h != 0 and h2[i][0] != 0):
            ans = min(ans,c+h2[i][1])
        else:
            continue
        break
    if ans < INF:
        print(ans)
    else:
        print(-1)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    INF = 10**5
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    n,ma,mb = LI()
    med = [LI() for _ in range(n)]
    h1 = []
    h2 = []

    for i in range(2 ** (n//2)):
        b = 1
        ta = 0
        tb = 0
        tc = 0
        for j in range(n//2):
            if i & b:
                a,b,c = med[j]
                ta += a
                tb += b
                tc += c
            b <<= 1
        dif = ta * mb - tb * ma
        h1.append((dif,tc))

    for i in range(2 ** (n - n//2)):
        b = 1
        ta = 0
        tb = 0
        tc = 0
        for j in range(n//2,n):
            if i & b:
                a,b,c = med[j]
                ta += a
                tb += b
                tc += c
            b <<= 1
        dif = tb * ma - ta * mb
        h2.append((dif,tc))

    h1.sort()
    h2.sort()
    i = 0
    ans = INF
    for h,c in h1:
        while h2[i][0] < h:
            i += 1
            if i >= len(h2): break
        if h == h2[i][0] and (h != 0 and h2[i][0] != 0):
            ans = min(ans,c+h2[i][1])
        else:
            continue
        break
    if ans < INF:
        print(ans)
    else:
        print(-1)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    INF = 10**18
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    n,ma,mb = LI()
    med = [LI() for _ in range(n)]
    h1 = []
    h2 = []

    for i in range(2 ** (n//2)):
        b = 1
        ta = 0
        tb = 0
        tc = 0
        for j in range(n//2):
            if i & b:
                a,b,c = med[j]
                ta += a
                tb += b
                tc += c
            b <<= 1
        dif = ta * mb - tb * ma
        h1.append((dif,tc))

    for i in range(2 ** (n//2)):
        b = 1
        ta = 0
        tb = 0
        tc = 0
        for j in range(n//2,n):
            if i & b:
                a,b,c = med[j]
                ta += a
                tb += b
                tc += c
            b <<= 1
        dif = tb * ma - ta * mb
        h2.append((dif,tc))

    h1.sort()
    h2.sort()
    i = 0
    ans = INF
    for h,c in h1:
        while h2[i][0] < h:
            i += 1
            if i >= len(h2): break
        if h == h2[i][0] and (h != 0 and h2[i][0] != 0):
            ans = min(ans,c+h2[i][1])
    if ans < INF:
        print(ans)
    else:
        print(-1)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n,ma,mb = map(int,input().split())
L = []
sa = 0
sb = 0
for i in range(n):
    a,b,c = map(int,input().split())
    sa += a
    sb += b
    L.append([a,b,c])
dp = [[[10000 for i in range(sa+1)] for i in range(sb+1)] for i in range(n+1)]
dp[0][0][0] = 0
for i in range(n):
    a = L[i][0]
    b = L[i][1]
    c = L[i][2]
    for j in range(sa+1):
        for k in range(sb+1):
            if j-a >= 0 and k-b >= 0:
                dp[i+1][j][k] = min(dp[i][j-a][k-b]+c,dp[i+1][j][k],dp[i][j][k])
            else:
                dp[i+1][j][k] = min(dp[i+1][j][k],dp[i][j][k])
k = max(ma,mb)
t = max(sa,sb)
cur = 1
ans = 10000
while k*cur <= t:
    ans = min(ans,dp[n][ma*cur][mb*cur])
    cur += 1
if ans == 10000:
    print(-1)
else:
    print(ans)

Submission status: Runtime Error

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

for k in range(N):
    a[k],b[k],c[k]=map(int,input().split())
Sa=sum(a)
Sb=sum(b)
dp =[[5000 for k in range(Sb+1)]for k in range(Sa+1)]
dp[0][0]=0
for k in range(N):
    for j in range(Sa-a[k]+1):
        for l in range(Sb-b[k]+1):
            dp[j+a[k]][l+b[k]]=min(dp[j][l]+c[k], dp[j+a[k]][l+b[k]])

ans=[5000]
for k in range(1, max(sum(a)//Mb,sum(b)//Ma)+1):
    ans.append(dp[Mb*k][Ma*k])
if min(ans)==5000:
    print(-1)
else:
    print(min(ans))

Submission status: Runtime Error

Solution code:
n,ma,mb = map(int,input().split())
L = []
for i in range(n):
    a,b,c = map(int,input().split())
    L.append([a,b,c])
dp = [[10**5]*401 for i in range(401)]
make = [[0,0],[L[0][0],L[0][1]]]
dp[L[0][0]][L[0][1]] = L[0][2]
dp[0][0] = 0
for i in range(1,n):
    a = L[i][0]
    b = L[i][1]
    c = L[i][2]
    for j in range(len(make)):
        t = make[j][0]
        u = make[j][1]
        dp[t+a][u+b] = min(dp[t][u]+c,dp[t+a][u+b])
        make.append([t+a, u+b])
    make = list(set(make))
if ma <= mb:
    cnt = 1
    ans = 10**5
    while mb*cnt <= 400:
        ans = min(ans, dp[ma*cnt][mb*cnt])
        cnt += 1
    if ans == 10**5:
        print(-1)
    else:
        print(ans)
else:
    cnt = 1
    ans = 10**5
    while ma*cnt <= 400:
        ans = min(ans, dp[ma*cnt][mb*cnt])
        cnt += 1
    if ans == 10**5:
        print(-1)
    else:
        print(ans)

Submission status: Runtime Error

Solution code:
from math import gcd

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

dp=[[[float("inf") for i in range(0,401)] for j in range(0,401)] for k in range(0,N)]
dp=[[float("inf") for i in range(0,401)] for j in range(0,401)]

for i in range(0,401):
    for j in range(0,401):
        if not (i==0 and j==0):
            g=gcd(i,j)
            I=i//g
            J=j//g
            if I==ma and J==mb:
                dp[i][j]=0
            else:
                g=gcd(i+med[0][0],j+med[0][1])
                I=(i+med[0][0])//g
                J=(j+med[0][1])//g
                if I==ma and J==mb:
                    dp[i][j]=med[0][2]

for i in range(1,N):
    a,b,c=med[i]
    for j in range(0,401-a):
        for k in range(0,401-b):
            dp[j][k]=min(dp[j][k],c+dp[j+a][k+b])

if dp[0][0]==float("inf"):
    print(-1)
else:
    print(dp[0][0])

Submission status: Runtime Error

Solution code:
def main():
    from collections import defaultdict

    inf = 100 * 40 + 1

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

    dp = [inf] * (400 * N * 2 + 10)
    # dp[x]:=minimum price

    # (b-a)(Mb+Ma)-(b+a)(Mb-Ma)=0を目指す
    for _ in range(N):
        a, b, p = map(int, input().split())
        x = (b - a) * (Mb + Ma) - (b + a) * (Mb - Ma)
        for key in range(400 * N * 2 + 10):
            if dp[key] == inf: continue
            nkey = key + x
            dp[nkey] = min(dp[nkey], dp[key] + p)
        dp[x] = min(dp[x], p)

    if dp[0] == inf:
        print(-1)
    else:
        print(dp[0])


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

Submission status: Runtime Error

Solution code:
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]

n,ma,mb=map(int,input().split())
abc=[list(map(int,input().split())) for i in range(n)]
# dp=[[[float('inf')]*(400+1) for i in range(400+1)] for j in range(n+1)]
dp=[]
tmp=[]
for j in range(401):
    tmp2=[]
    for k in range(401):
        tmp2.append(float('inf'))
    tmp.append(tmp2)

for i in range(n+1):
    dp.append(tmp[:])
dp[0][0][0]=0
for i in range(n):
    a,b,c=abc[i]
    for j in range(400):
        for k in range(400):
            if dp[i][j][k]!=float('inf'):
                dp[i+1][j+a][k+b]=min(dp[i+1][j+a][k+b],dp[i][j][k]+c)
                dp[i+1][j][k]=min(dp[i+1][j][k],dp[i][j][k])
ans=float('inf')
now=[ma,mb]
while 1:
    a,b=now
    ans=min(ans,dp[n][a][b])
    if a+ma>400 or b+mb>400:
        break
    now=[a+ma,b+mb]
if ans==float('inf'):
    print(-1)
else:
    print(ans)

Submission status: Runtime Error

Solution code:
N,Ma,Mb= (int(x) for x in input().split())
a = [0]*N
b = [0]*N
c = [0]*N
# use = [0]*N
ans = 40*100+1
for i in range (N):
    a[i],b[i],c[i] = (int(x) for x in input().split())
# 0ならつかわない1なら使う
A = 0
B = 0
C = 0
def search(N,Ma,Mb,a,b,c,A,B,C):
    if (B!= 0 and A/B == Ma/Mb):
            return C
    if(N==1):
#         use[N-1] = 0
#         if (B!= 0 and A/B == Ma/Mb):
#             return C
        use[N-1] = 1
        A += a[N-1]
        B += b[N-1]
        C += c[N-1]
        if (B!= 0 and A/B == Ma/Mb):
             return C
        else: C = ans
        return C
    else:
#         use[N-1] = 0
        c0 = search(N-1,Ma,Mb,a,b,c,A,B,C)
#         use[N-1] = 1
        A += a[N-1]
        B += b[N-1]
        C += c[N-1]
        c1 = search(N-1,Ma,Mb,a,b,c,A,B,C)
    return min(c0,c1)

Ans = search(N,Ma,Mb,a,b,c,A,B,C)
if(Ans == ans):
    print("-1")
else: print(Ans)

Submission status: Runtime Error

Solution code:
N,Ma,Mb= (int(x) for x in input().split())
a = [0]*N
b = [0]*N
c = [0]*N
# use = [0]*N
ans = 40*100+1
for i in range (N):
    a[i],b[i],c[i] = (int(x) for x in input().split())
# 0ならつかわない1なら使う
A = 0
B = 0
C = 0
def search(N,Ma,Mb,a,b,c,A,B,C):
    if (B!= 0 and A/B == Ma/Mb):
            return C
    if(N==1):
#         use[N-1] = 0
#         if (B!= 0 and A/B == Ma/Mb):
#             return C
        use[N-1] = 1
        A += a[N-1]
        B += b[N-1]
        C += c[N-1]
        if (B!= 0 and A/B == Ma/Mb):
             return C
        else: C = ans
        return C
    else:
#         use[N-1] = 0
        c0 = search(N-1,Ma,Mb,a,b,c,A,B,C)
#         use[N-1] = 1
        A += a[N-1]
        B += b[N-1]
        C += c[N-1]
        c1 = search(N-1,Ma,Mb,a,b,c,A,B,C)
    return min(c0,c1)

Ans = search(N,Ma,Mb,a,b,c,use,A,B,C)
if(Ans == ans):
    print("-1")
else: print(Ans)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
import math

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

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

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


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

Submission status: Runtime Error

Solution code:
import math

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

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

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


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

Submission status: Runtime Error

Solution code:
import math

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

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

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

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