Buggy python submission (verdict: Time Limit Exceeded):
from itertools import product
from bisect import bisect_left
N,Ma,Mb = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(N)]
if N==1:
    if A[0][0]*Mb==A[0][1]*Ma:
        print(A[0][2])
    else:
        print(-1)
else:
    A1 = A[:N//2]  # N//2
    A2 = A[N//2:]  # N-N//2
    B1 = []
    for x in product((0,1),repeat=N//2):
        cnt1 = 0
        cnt2 = 0
        for i in range(N//2):
            cnt1 += x[i]*(Ma*A1[i][1]-Mb*A1[i][0])
            cnt2 += x[i]*A1[i][2]
        B1.append((cnt1,cnt2))
    B2 = []
    for x in product((0,1),repeat=N-N//2):
        cnt1 = 0
        cnt2 = 0
        for i in range(N-N//2):
            cnt1 += x[i]*(Ma*A2[i][1]-Mb*A2[i][0])
            cnt2 += x[i]*A2[i][2]
        B2.append((cnt1,cnt2))
    B1 = sorted(B1,key=lambda x:x[1])
    B1 = sorted(B1,key=lambda x:x[0])
    B2 = sorted(B2,key=lambda x:x[1])
    B2 = sorted(B2,key=lambda x:x[0])
    cmin = 10000
    for b1 in B1:
        b,c = b1[0],b1[1]
        if c==0:
            ind = bisect_left(B2,(-b,1))
        else:
            ind = bisect_left(B2,(-b,0))
        if ind<len(B2) and B2[ind][0]==-b:
            cmin = min(cmin,c+B2[ind][1])
    if cmin>=10000:
        print(-1)
    else:
        print(cmin)

Fixed submission:
INFTY = 10**4
N,Ma,Mb = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(N)]
dp = [[[INFTY for _ in range(401)] for _ in range(401)] for _ in range(N+1)]
dp[0][0][0] = 0
for i in range(1,N+1):
    for j in range(401):
        for k in range(401):
            dp[i][j][k] = dp[i-1][j][k]
            a = A[i-1][0]
            b = A[i-1][1]
            c = A[i-1][2]
            if j>=a and k>=b:
                dp[i][j][k] = min(dp[i][j][k],dp[i-1][j-a][k-b]+c)
cmin = INFTY
for j in range(1,401):
    for k in range(1,401):
        if j*Mb==k*Ma:
            cmin = min(cmin,dp[N][j][k])
if cmin>=INFTY:
    print(-1)
else:
    print(cmin)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import product
from bisect import bisect_left
N,Ma,Mb = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(N)]
if N==1:
    if A[0][0]*Mb==A[0][1]*Ma:
        print(A[0][2])
    else:
        print(-1)
else:
    A1 = A[:N//2]  # N//2
    A2 = A[N//2:]  # N-N//2
    B1 = []
    for x in product((0,1),repeat=N//2):
        cnt1 = 0
        cnt2 = 0
        for i in range(N//2):
            cnt1 += x[i]*(Ma*A1[i][1]-Mb*A1[i][0])
            cnt2 += x[i]*A1[i][2]
        B1.append((cnt1,cnt2))
    B2 = []
    for x in product((0,1),repeat=N-N//2):
        cnt1 = 0
        cnt2 = 0
        for i in range(N-N//2):
            cnt1 += x[i]*(Ma*A2[i][1]-Mb*A2[i][0])
            cnt2 += x[i]*A2[i][2]
        B2.append((cnt1,cnt2))
    B1 = sorted(B1,key=lambda x:x[1])
    B1 = sorted(B1,key=lambda x:x[0])
    B2 = sorted(B2,key=lambda x:x[1])
    B2 = sorted(B2,key=lambda x:x[0])
    cmin = 10000
    for b1 in B1:
        b,c = b1[0],b1[1]
        if c==0:
            ind = bisect_left(B2,(-b,1))
        else:
            ind = bisect_left(B2,(-b,0))
        if ind<len(B2) and B2[ind][0]==-b:
            cmin = min(cmin,c+B2[ind][1])
    if cmin>=10000:
        print(-1)
    else:
        print(cmin)

Fixed submission:
INFTY = 10**4
N,Ma,Mb = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(N)]
dp = [[[INFTY for _ in range(401)] for _ in range(401)] for _ in range(N+1)]
dp[0][0][0] = 0
for i in range(1,N+1):
    for j in range(401):
        for k in range(401):
            dp[i][j][k] = dp[i-1][j][k]
            a = A[i-1][0]
            b = A[i-1][1]
            c = A[i-1][2]
            if j>=a and k>=b:
                dp[i][j][k] = min(dp[i][j][k],dp[i-1][j-a][k-b]+c)
cmin = INFTY
for j in range(1,401):
    for k in range(1,401):
        if j*Mb==k*Ma:
            cmin = min(cmin,dp[N][j][k])
if cmin>=INFTY:
    print(-1)
else:
    print(cmin)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import product
from bisect import bisect_left
N,Ma,Mb = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(N)]
if N==1:
    if A[0][0]*Mb==A[0][1]*Ma:
        print(A[0][2])
    else:
        print(-1)
else:
    A1 = A[:N//2]  # N//2
    A2 = A[N//2:]  # N-N//2
    B1 = []
    for x in product((0,1),repeat=N//2):
        cnt1 = 0
        cnt2 = 0
        for i in range(N//2):
            if x[i]==1:
                cnt1 += Ma*A1[i][1]-Mb*A1[i][0]
                cnt2 += A1[i][2]
        B1.append((cnt1,cnt2))
    B2 = []
    for x in product((0,1),repeat=N-N//2):
        cnt1 = 0
        cnt2 = 0
        for i in range(N-N//2):
            if x[i]==1:
                cnt1 += Ma*A2[i][1]-Mb*A2[i][0]
                cnt2 += A2[i][2]
        B2.append((cnt1,cnt2))
    B1 = sorted(B1,key=lambda x:x[1])
    B1 = sorted(B1,key=lambda x:x[0])
    B2 = sorted(B2,key=lambda x:x[1])
    B2 = sorted(B2,key=lambda x:x[0])
    cmin = 10000
    for b1 in B1:
        b,c = b1[0],b1[1]
        if c==0:
            ind = bisect_left(B2,(-b,1))
        else:
            ind = bisect_left(B2,(-b,0))
        if ind<len(B2) and B2[ind][0]==-b:
            cmin = min(cmin,c+B2[ind][1])
    if cmin>=10000:
        print(-1)
    else:
        print(cmin)

Fixed submission:
INFTY = 10**4
N,Ma,Mb = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(N)]
dp = [[[INFTY for _ in range(401)] for _ in range(401)] for _ in range(N+1)]
dp[0][0][0] = 0
for i in range(1,N+1):
    for j in range(401):
        for k in range(401):
            dp[i][j][k] = dp[i-1][j][k]
            a = A[i-1][0]
            b = A[i-1][1]
            c = A[i-1][2]
            if j>=a and k>=b:
                dp[i][j][k] = min(dp[i][j][k],dp[i-1][j-a][k-b]+c)
cmin = INFTY
for j in range(1,401):
    for k in range(1,401):
        if j*Mb==k*Ma:
            cmin = min(cmin,dp[N][j][k])
if cmin>=INFTY:
    print(-1)
else:
    print(cmin)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import product
from bisect import bisect_left
N,Ma,Mb = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(N)]
if N==1:
    if A[0][0]*Mb==A[0][1]*Ma:
        print(A[0][2])
    else:
        print(-1)
else:
    A1 = A[:N//2]  # N//2
    A2 = A[N//2:]  # N-N//2
    B1 = []
    for x in product((0,1),repeat=N//2):
        cnt1 = 0
        cnt2 = 0
        for i in range(N//2):
            if x[i]==1:
                cnt1 += Ma*A1[i][1]-Mb*A1[i][0]
                cnt2 += A1[i][2]
        B1.append((cnt1,cnt2))
    B2 = []
    for x in product((0,1),repeat=N-N//2):
        cnt1 = 0
        cnt2 = 0
        for i in range(N-N//2):
            if x[i]==1:
                cnt1 += Ma*A2[i][1]-Mb*A2[i][0]
                cnt2 += A2[i][2]
        B2.append((cnt1,cnt2))
    B1 = sorted(B1,key=lambda x:x[1])
    B1 = sorted(B1,key=lambda x:x[0])
    B2 = sorted(B2,key=lambda x:x[1])
    B2 = sorted(B2,key=lambda x:x[0])
    cmin = 10000
    for b1 in B1:
        b,c = b1[0],b1[1]
        if c==0:
            ind = bisect_left(B2,(-b,1))
        else:
            ind = bisect_left(B2,(-b,0))
        if ind<len(B2) and B2[ind][0]==-b:
            cmin = min(cmin,c+B2[ind][1])
    if cmin>=10000:
        print(-1)
    else:
        print(cmin)

Fixed submission:
INFTY = 10**4
N,Ma,Mb = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(N)]
dp = [[[INFTY for _ in range(401)] for _ in range(401)] for _ in range(N+1)]
dp[0][0][0] = 0
for i in range(1,N+1):
    for j in range(401):
        for k in range(401):
            dp[i][j][k] = dp[i-1][j][k]
            a = A[i-1][0]
            b = A[i-1][1]
            c = A[i-1][2]
            if j>=a and k>=b:
                dp[i][j][k] = min(dp[i][j][k],dp[i-1][j-a][k-b]+c)
cmin = INFTY
for j in range(1,401):
    for k in range(1,401):
        if j*Mb==k*Ma:
            cmin = min(cmin,dp[N][j][k])
if cmin>=INFTY:
    print(-1)
else:
    print(cmin)

Buggy python submission (verdict: Runtime Error):
from itertools import combinations
from bisect import bisect_left
N,Ma,Mb = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(N)]
if N==1:
    if A[0][0]*Mb==A[0][1]*Ma:
        print(A[0][2])
    else:
        print(-1)
else:
    A1 = A[:N//2]  # N//2
    A2 = A[N//2:]  # N-N//2
    B1 = []
    for k in range(1,N//2+1):
        for x in combinations(range(N//2),k):
            cnt1 = 0
            cnt2 = 0
            for i in x:
                cnt1 += Ma*A1[i][1]-Mb*A1[i][0]
                cnt2 += A1[i][2]
            B1.append((cnt1,cnt2))
    B2 = []
    for k in range(1,N-N//2+1):
        for x in combinations(range(N-N//2),k):
            cnt1 = 0
            cnt2 = 0
            for i in x:
                cnt1 += Ma*A2[i][1]-Mb*A2[i][0]
                cnt2 += A2[i][2]
            B2.append((cnt1,cnt2))
    B1 = sorted(B1,key=lambda x:x[1])
    B1 = sorted(B1,key=lambda x:x[0])
    B2 = sorted(B2,key=lambda x:x[1])
    B2 = sorted(B2,key=lambda x:x[0])
    cmin = 10000
    for b1 in B1:
        b,c = b1[0],b1[1]
        ind = bisect_left(B2,(-b,0))
        if B2[ind][0]==-b:
            cmin = min(cmin,c+B2[ind][1])
    if cmin>=10000:
        print(-1)
    else:
        print(cmin)

Fixed submission:
INFTY = 10**4
N,Ma,Mb = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(N)]
dp = [[[INFTY for _ in range(401)] for _ in range(401)] for _ in range(N+1)]
dp[0][0][0] = 0
for i in range(1,N+1):
    for j in range(401):
        for k in range(401):
            dp[i][j][k] = dp[i-1][j][k]
            a = A[i-1][0]
            b = A[i-1][1]
            c = A[i-1][2]
            if j>=a and k>=b:
                dp[i][j][k] = min(dp[i][j][k],dp[i-1][j-a][k-b]+c)
cmin = INFTY
for j in range(1,401):
    for k in range(1,401):
        if j*Mb==k*Ma:
            cmin = min(cmin,dp[N][j][k])
if cmin>=INFTY:
    print(-1)
else:
    print(cmin)

Buggy python submission (verdict: Runtime Error):
from itertools import combinations
from bisect import bisect_left
N,Ma,Mb = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(N)]
A1 = A[:N//2]  # N//2
A2 = A[N//2:]  # N-N//2
B1 = []
for k in range(1,N//2+1):
    for x in combinations(range(N//2),k):
        cnt1 = 0
        cnt2 = 0
        for i in x:
            cnt1 += Ma*A1[i][1]-Mb*A1[i][0]
            cnt2 += A1[i][2]
        B1.append((cnt1,cnt2))
B2 = []
for k in range(1,N-N//2+1):
    for x in combinations(range(N-N//2),k):
        cnt1 = 0
        cnt2 = 0
        for i in x:
            cnt1 += Ma*A2[i][1]-Mb*A2[i][0]
            cnt2 += A2[i][2]
        B2.append((cnt1,cnt2))
B1 = sorted(B1,key=lambda x:x[1])
B1 = sorted(B1,key=lambda x:x[0])
B2 = sorted(B2,key=lambda x:x[1])
B2 = sorted(B2,key=lambda x:x[0])
cmin = 10000
for b1 in B1:
    b,c = b1[0],b1[1]
    ind = bisect_left(B2,(-b,0))
    if B2[ind][0]==-b:
        cmin = min(cmin,c+B2[ind][1])
if cmin>=10000:
    print(-1)
else:
    print(cmin)

Fixed submission:
INFTY = 10**4
N,Ma,Mb = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(N)]
dp = [[[INFTY for _ in range(401)] for _ in range(401)] for _ in range(N+1)]
dp[0][0][0] = 0
for i in range(1,N+1):
    for j in range(401):
        for k in range(401):
            dp[i][j][k] = dp[i-1][j][k]
            a = A[i-1][0]
            b = A[i-1][1]
            c = A[i-1][2]
            if j>=a and k>=b:
                dp[i][j][k] = min(dp[i][j][k],dp[i-1][j-a][k-b]+c)
cmin = INFTY
for j in range(1,401):
    for k in range(1,401):
        if j*Mb==k*Ma:
            cmin = min(cmin,dp[N][j][k])
if cmin>=INFTY:
    print(-1)
else:
    print(cmin)