Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
N,Ma,Mb=map(int,raw_input().split())
A=[]
B=[]
C=[]
a_sum=0
b_sum=0
for i in range(N):
	a,b,c=map(int,raw_input().split())
	A.append(a)
	B.append(b)
	C.append(c)
	a_sum+=a
	b_sum+=b

dp=defaultdict(lambda: float("inf"))
dp[(0,0,0)]=0


for k in range(1,N+1):
	for x in range(a_sum+1):
		for y in range(b_sum+1):
			dp[(k,x,y)]=min( dp[(k,x,y)], dp[(k-1,x,y)] )
			if x-A[k-1]>=0 and y-B[k-1]>=0:
				dp[(k,x,y)]=min( dp[(k-1,x,y)], dp[(k-1, x-A[k-1], y-B[k-1])]+C[k-1] )


ans=float("inf")
for x in range(1,a_sum+1):
	for y in range(1,b_sum+1):
		if x*Mb==y*Ma:
			ans=min(ans,dp[(N, x, y)])

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

Fixed submission:
from collections import defaultdict
N,Ma,Mb=map(int,raw_input().split())

dp=defaultdict(lambda: float("inf"))
dp[(0,0)]=0

for i in range(N):
	a,b,c=map(int,raw_input().split())
	for j in dp.items():
		x=j[0][0]
		y=j[0][1]
		z=j[1]
		na, nb, nc = x+a, y+b, z+c 
		dp[(na, nb)]=min( dp[(na, nb)], nc)


ans=float("inf")
for i in dp.items():
	x=i[0][0]
	y=i[0][1]
	z=i[1]
	if x==0 and y==0:
		continue
	if x*Mb==y*Ma:
		ans=min(ans,z)

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

Buggy python submission (verdict: Time Limit Exceeded):
N,Ma,Mb=map(int,raw_input().split())
A=[]
B=[]
C=[]
a_sum=0
b_sum=0
for i in range(N):
	a,b,c=map(int,raw_input().split())
	A.append(a)
	B.append(b)
	C.append(c)
	a_sum+=a
	b_sum+=b

dp=[ [ [ float("inf") for y in range(b_sum+1) ] for x in range(a_sum+1)] for k in range(N+1) ]
dp[0][0][0]=0


for k in range(1,N+1):
	for x in range(a_sum+1):
		for y in range(b_sum+1):
			dp[k][x][y]=min( dp[k][x][y], dp[k-1][x][y] )
			if x-A[k-1]>=0 and y-B[k-1]>=0:
				dp[k][x][y]=min( dp[k-1][x][y], dp[k-1][x-A[k-1]][y-B[k-1]]+C[k-1] )


ans=float("inf")
for x in range(1,a_sum+1):
	for y in range(1,b_sum+1):
		if x*Mb==y*Ma:
			ans=min(ans,dp[N][x][y])

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

Fixed submission:
from collections import defaultdict
N,Ma,Mb=map(int,raw_input().split())

dp=defaultdict(lambda: float("inf"))
dp[(0,0)]=0

for i in range(N):
	a,b,c=map(int,raw_input().split())
	for j in dp.items():
		x=j[0][0]
		y=j[0][1]
		z=j[1]
		na, nb, nc = x+a, y+b, z+c 
		dp[(na, nb)]=min( dp[(na, nb)], nc)


ans=float("inf")
for i in dp.items():
	x=i[0][0]
	y=i[0][1]
	z=i[1]
	if x==0 and y==0:
		continue
	if x*Mb==y*Ma:
		ans=min(ans,z)

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

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
N,Ma,Mb=map(int,raw_input().split())
item=[]
i_sum=0
for i in range(N):
	a,b,c=map(int,raw_input().split())
	i_sum+=c
	item.append( (a,b,c) )

cost=defaultdict(lambda: None)

for i in item:
	x=i[0]
	y=i[1]
	w=i[2]
	if len(cost)==0:
		cost[0]=[(0,0)]
		cost[w]=[(x,y)]
	else:
		for j in cost.items():
			tw=j[0]
			for k in j[1]:
				tx=k[0]
				ty=k[1]
				if cost[tw+w] is None:
					cost[tw+w]=[(x+tx,y+ty)]
				else:
					cost[tw+w]+=[(x+tx,y+ty)]


for i in cost.items():
	w=i[0]
	for x,y in i[1]:
		if x*Mb==y*Ma and x*Mb!=0:
			print w
			quit()
else:
	print -1
			


Fixed submission:
from collections import defaultdict
N,Ma,Mb=map(int,raw_input().split())

dp=defaultdict(lambda: float("inf"))
dp[(0,0)]=0

for i in range(N):
	a,b,c=map(int,raw_input().split())
	for j in dp.items():
		x=j[0][0]
		y=j[0][1]
		z=j[1]
		na, nb, nc = x+a, y+b, z+c 
		dp[(na, nb)]=min( dp[(na, nb)], nc)


ans=float("inf")
for i in dp.items():
	x=i[0][0]
	y=i[0][1]
	z=i[1]
	if x==0 and y==0:
		continue
	if x*Mb==y*Ma:
		ans=min(ans,z)

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