Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
n,ma,mb = map(int, input().split())
abc = []
sa = 0
sb = 0
for _ in range(n):
    a,b,c = map(int, input().split())
    sa += a
    sb += b
    abc.append((a,b,c))
inf = 10**5
dp = [[[inf for _ in range(sb+1)] for _ in range(sa+1)] for _ in range(n+1)]
dp[0][0][0] = 0
for i in range(n):
    a,b,c = abc[i]
    for ai in range(sa+1):
        for bi in range(sb+1):
            dp[i+1][ai][bi] = min(dp[i+1][ai][bi], dp[i][ai][bi])
            if ai+a<sa+1 and bi+b<sb+1:
                dp[i+1][ai+a][bi+b] = min(dp[i+1][ai+a][bi+b], dp[i][ai][bi]+c)
# print(dp)

res = inf
for ai in range(1,sa+1):
    for bi in range(1,sb+1):
        if ma*bi==mb*ai:
            res = min(res, dp[n][ai][bi])
if res == inf:
    print(-1)
else:
    print(res)


Fixed submission:
# -*- coding: utf-8 -*-
inf = 10**10
n,ma,mb = map(int, input().split())

a = []
b = []
c = []
for _ in range(n):
    x,y,z = map(int, input().split())
    a.append(x)
    b.append(y)
    c.append(z)

memo = {}
def rec(i, maj, mbj):
    k = str(i)+"-"+str(maj)+"-"+str(mbj)
    if k in memo:
        return memo[k]
    # print(i, ":", maj, mbj, maj/ma, mbj/mb)
    ret = 0
    if i==n:
        if maj>0 and mbj>0 and maj/ma == mbj/mb:
            ret = 0
        else:
            ret = inf
    elif maj>0 and mbj>0 and maj/ma == mbj/mb:
        ret = 0
    else:
        r1 = rec(i+1, maj, mbj) # i番目を使わない場合
        # i番目を使う場合
        r2 = rec(i+1, maj+a[i], mbj+b[i]) + c[i]
        if r1>0 and r2==0:
            ret = r1
        elif r1==0 and r2>0:
            ret = r2
        else:
            ret = min(r1,r2)
    memo[k] = ret
    return ret

ret = rec(0, 0, 0)
if ret == inf:
    print(-1)
else:
    print(ret)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
n,ma,mb = map(int, input().split())
abc = []
for _ in range(n):
    a,b,c = map(int, input().split())
    abc.append((a,b,c))
inf = 10**5
dp = [[[inf for _ in range(n*10+1)] for _ in range(n*10+1)] for _ in range(n+1)]
dp[0][0][0] = 0
for i in range(n):
    a,b,c = abc[i]
    for ai in range(n*10+1):
        for bi in range(n*10+1):
            dp[i+1][ai][bi] = min(dp[i+1][ai][bi], dp[i][ai][bi])
            if ai+a<n*10+1 and bi+b<n*10+1:
                dp[i+1][ai+a][bi+b] = min(dp[i+1][ai+a][bi+b], dp[i][ai][bi]+c)
# print(dp)

res = inf
for ai in range(1,n*10+1):
    for bi in range(1,n*10+1):
        if ma*bi==mb*ai:
            res = min(res, dp[n][ai][bi])
if res == inf:
    print(-1)
else:
    print(res)


Fixed submission:
# -*- coding: utf-8 -*-
inf = 10**10
n,ma,mb = map(int, input().split())

a = []
b = []
c = []
for _ in range(n):
    x,y,z = map(int, input().split())
    a.append(x)
    b.append(y)
    c.append(z)

memo = {}
def rec(i, maj, mbj):
    k = str(i)+"-"+str(maj)+"-"+str(mbj)
    if k in memo:
        return memo[k]
    # print(i, ":", maj, mbj, maj/ma, mbj/mb)
    ret = 0
    if i==n:
        if maj>0 and mbj>0 and maj/ma == mbj/mb:
            ret = 0
        else:
            ret = inf
    elif maj>0 and mbj>0 and maj/ma == mbj/mb:
        ret = 0
    else:
        r1 = rec(i+1, maj, mbj) # i番目を使わない場合
        # i番目を使う場合
        r2 = rec(i+1, maj+a[i], mbj+b[i]) + c[i]
        if r1>0 and r2==0:
            ret = r1
        elif r1==0 and r2>0:
            ret = r2
        else:
            ret = min(r1,r2)
    memo[k] = ret
    return ret

ret = rec(0, 0, 0)
if ret == inf:
    print(-1)
else:
    print(ret)