Buggy python submission (verdict: Time Limit Exceeded):
import sys


n,a,b=[int(x) for x in str(input()).split()]
A=[]
for i in range(n):
  A.append([int(x) for x in sys.stdin.readline().split()])



#print(A)
def f(ma,mb,cost,index):

  newa=ma+A[index][0]
  newb=mb+A[index][1]
  newc=cost+A[index][2]
  
  #print(index,newa,newb,newc)
  if newa*b==a*newb:
    return newc
  elif index==n-1:
    return 100000000000000000000
  else:
    ith=f(newa,newb,newc,index+1)
    return min(ith,f(ma,mb,cost,index+1))



if f(0,0,0,0)<100000000000000000000:
  print(f(0,0,0,0))
else:
  print(-1)


Fixed submission:
N,Ma,Mb=[int(x) for x in input().split()]
A=[]
B=[]
C=[]
for i in range(N):
	a,b,c=[int(x) for x in input().split()]
	A.append(a)
	B.append(b)
	C.append(c)


ANS=[[[-1 for k in range(401)] for j in range(401)] for i in range(41)]

INF=1000000000000000000000

def f(ind,wa,wb):
	global Ma
	global Mb
	global N
	if ANS[ind][wa][wb]==-1:
		if ind==0:
			if (wa+A[0])*Mb==(wb+B[0])*Ma:
				ANS[ind][wa][wb] = C[0]
			elif wa*Mb==wb*Ma and (wa!=0 and wb!=0):
				ANS[ind][wa][wb] = 0
			else:
				ANS[ind][wa][wb] = INF
		else:
			ANS[ind][wa][wb] = min(f(ind-1,wa,wb),f(ind-1,wa+A[ind],wb+B[ind])+C[ind])
	return ANS[ind][wa][wb]

ans=f(N-1,0,0)

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

Buggy python submission (verdict: Time Limit Exceeded):
n,a,b=[int(x) for x in str(input()).split()]
A=[]
for i in range(n):
  A.append([int(x) for x in str(input()).split()])



#print(A)
def f(ma,mb,cost,index):

  newa=ma+A[index][0]
  newb=mb+A[index][1]
  newc=cost+A[index][2]
  
  #print(index,newa,newb,newc)
  if newa*b==a*newb:
    return newc
  elif index==n-1:
    return 100000000000000000000
  else:
    ith=f(newa,newb,newc,index+1)
    return min(ith,f(ma,mb,cost,index+1))



if f(0,0,0,0)<100000000000000000000:
  print(f(0,0,0,0))
else:
  print(-1)


Fixed submission:
N,Ma,Mb=[int(x) for x in input().split()]
A=[]
B=[]
C=[]
for i in range(N):
	a,b,c=[int(x) for x in input().split()]
	A.append(a)
	B.append(b)
	C.append(c)


ANS=[[[-1 for k in range(401)] for j in range(401)] for i in range(41)]

INF=1000000000000000000000

def f(ind,wa,wb):
	global Ma
	global Mb
	global N
	if ANS[ind][wa][wb]==-1:
		if ind==0:
			if (wa+A[0])*Mb==(wb+B[0])*Ma:
				ANS[ind][wa][wb] = C[0]
			elif wa*Mb==wb*Ma and (wa!=0 and wb!=0):
				ANS[ind][wa][wb] = 0
			else:
				ANS[ind][wa][wb] = INF
		else:
			ANS[ind][wa][wb] = min(f(ind-1,wa,wb),f(ind-1,wa+A[ind],wb+B[ind])+C[ind])
	return ANS[ind][wa][wb]

ans=f(N-1,0,0)

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

Buggy python submission (verdict: Runtime Error):
n,a,b=[int(x) for x in str(input()).split()]
A=[]
for i in range(n):
  A.append([int(x) for x in str(input()).split()])
 
ans=[]
for i in range(10):
  y=[]
  for j in range(10):
    x=[]
    for k in range(n):
      x.append(-1)
    y.append(x)
  ans.append(y)

#print(ans)
#print(A)
def f(ma,mb,cost,index):

  if ans[ma][mb][index]==-1:
    newa=ma+A[index][0]
    newb=mb+A[index][1]
    newc=cost+A[index][2]
    
    #print(index,newa,newb,newc)
    if newa*b==a*newb:
      ans[ma][mb][index]=newc
    elif index==n-1:
      ans[ma][mb][index]=100000000000000000000
    else:
      ith=f(newa,newb,newc,index+1)
      ans[ma][mb][index]= min(ith,f(ma,mb,cost,index+1))
  return ans[ma][mb][index]
  
 
if f(0,0,0,0)<100000000000000000000:
  print(f(0,0,0,0))
else:
  print(-1)

Fixed submission:
N,Ma,Mb=[int(x) for x in input().split()]
A=[]
B=[]
C=[]
for i in range(N):
	a,b,c=[int(x) for x in input().split()]
	A.append(a)
	B.append(b)
	C.append(c)


ANS=[[[-1 for k in range(401)] for j in range(401)] for i in range(41)]

INF=1000000000000000000000

def f(ind,wa,wb):
	global Ma
	global Mb
	global N
	if ANS[ind][wa][wb]==-1:
		if ind==0:
			if (wa+A[0])*Mb==(wb+B[0])*Ma:
				ANS[ind][wa][wb] = C[0]
			elif wa*Mb==wb*Ma and (wa!=0 and wb!=0):
				ANS[ind][wa][wb] = 0
			else:
				ANS[ind][wa][wb] = INF
		else:
			ANS[ind][wa][wb] = min(f(ind-1,wa,wb),f(ind-1,wa+A[ind],wb+B[ind])+C[ind])
	return ANS[ind][wa][wb]

ans=f(N-1,0,0)

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

Buggy python submission (verdict: Time Limit Exceeded):
n,a,b=[int(x) for x in str(input()).split()]
A=[]
for i in range(n):
  A.append([int(x) for x in str(input()).split()])



#print(A)
def f(ma,mb,cost,index):

  newa=ma+A[index][0]
  newb=mb+A[index][1]
  newc=cost+A[index][2]
  
  #print(index,newa,newb,newc)
  if newa*b==a*newb:
    return newc
  elif index==n-1:
    return 100000000000000000000
  else:
    ith=f(newa,newb,newc,index+1)
    return min(ith,f(ma,mb,cost,index+1))



if f(0,0,0,0)<100000000000000000000:
  print(f(0,0,0,0))
else:
  print(-1)

Fixed submission:
N,Ma,Mb=[int(x) for x in input().split()]
A=[]
B=[]
C=[]
for i in range(N):
	a,b,c=[int(x) for x in input().split()]
	A.append(a)
	B.append(b)
	C.append(c)


ANS=[[[-1 for k in range(401)] for j in range(401)] for i in range(41)]

INF=1000000000000000000000

def f(ind,wa,wb):
	global Ma
	global Mb
	global N
	if ANS[ind][wa][wb]==-1:
		if ind==0:
			if (wa+A[0])*Mb==(wb+B[0])*Ma:
				ANS[ind][wa][wb] = C[0]
			elif wa*Mb==wb*Ma and (wa!=0 and wb!=0):
				ANS[ind][wa][wb] = 0
			else:
				ANS[ind][wa][wb] = INF
		else:
			ANS[ind][wa][wb] = min(f(ind-1,wa,wb),f(ind-1,wa+A[ind],wb+B[ind])+C[ind])
	return ANS[ind][wa][wb]

ans=f(N-1,0,0)

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