Buggy python submission (verdict: Time Limit Exceeded):
def sol():
    N, Ma, Mb = map(int, input().split())
    items = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        items.append((a, b, c))

    dp = [[set([()]) for _ in range(Ma)] for _ in range(N + 1)]

    for i, (a, _, _) in enumerate(items):
        for mod in range(Ma):
            for prev in dp[i][mod]:
                dp[i + 1][mod].add(prev)
            for prev in dp[i][(mod - a) % Ma]:
                dp[i + 1][mod].add(tuple(list(prev) + [i]))

    ans = float('inf')
    for addList in dp[N][0]:
        sumA = 0
        sumB = 0
        cost = 0
        for i in addList:
            sumA += items[i][0]
            sumB += items[i][1]
            cost += items[i][2]
        if sumA * sumB > 0 and sumA * Mb == sumB * Ma:
            ans = min(ans, cost)

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

sol()

Fixed submission:
def sol():
    N, Ma, Mb = map(int, input().split())

    A = []
    B = []
    cost = []
    sumA = 0
    sumB = 0

    for _ in range(N):
        a, b, c = map(int, input().split())
        A.append(a)
        B.append(b)
        cost.append(c)
        sumA += a
        sumB += b

    dp = [[[float('inf') for _ in range(sumB * 2 + 1)] for _ in range(sumA * 2 + 1)] for _ in range(N + 1)]
    dp[0][0][0] = 0

    for index in range(N):
        for sA in range(sumA + 1):
            for sB in range(sumB + 1):
                dp[index + 1][sA][sB] = min(
                    dp[index + 1][sA][sB],
                    dp[index][sA][sB]
                )
                dp[index + 1][sA + A[index]][sB + B[index]] = min(
                    dp[index + 1][sA + A[index]][sB + B[index]],
                    dp[index][sA][sB] + cost[index]
                )

    ans = float('inf')
    for sA in range(1, sumA):
        for sB in range(1, sumB):
            if sA * Mb == sB * Ma:
                ans = min(ans, dp[N][sA][sB])

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

sol()

Buggy python submission (verdict: Time Limit Exceeded):
def sol():
    N, Ma, Mb = map(int, input().split())
    items = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        items.append((a, b, c))

    dp = [[set([()]) for _ in range(Ma)] for _ in range(N + 1)]

    for i, (a, _, _) in enumerate(items):
        for mod in range(Ma):
            for prev in dp[i][mod]:
                dp[i + 1][mod].add(prev)
            for prev in dp[i][(mod - a) % Ma]:
                dp[i + 1][mod].add(tuple(list(prev) + [i]))

    maList = filter(lambda A : len(A) > 0, dp[N][0])

    ans = float('inf')
    for addList in maList:
        sumA = 0
        sumB = 0
        cost = 0
        for i in addList:
            sumA += items[i][0]
            sumB += items[i][1]
            cost += items[i][2]
        if sumA * Mb == sumB * Ma:
            ans = min(ans, cost)

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

sol()

Fixed submission:
def sol():
    N, Ma, Mb = map(int, input().split())

    A = []
    B = []
    cost = []
    sumA = 0
    sumB = 0

    for _ in range(N):
        a, b, c = map(int, input().split())
        A.append(a)
        B.append(b)
        cost.append(c)
        sumA += a
        sumB += b

    dp = [[[float('inf') for _ in range(sumB * 2 + 1)] for _ in range(sumA * 2 + 1)] for _ in range(N + 1)]
    dp[0][0][0] = 0

    for index in range(N):
        for sA in range(sumA + 1):
            for sB in range(sumB + 1):
                dp[index + 1][sA][sB] = min(
                    dp[index + 1][sA][sB],
                    dp[index][sA][sB]
                )
                dp[index + 1][sA + A[index]][sB + B[index]] = min(
                    dp[index + 1][sA + A[index]][sB + B[index]],
                    dp[index][sA][sB] + cost[index]
                )

    ans = float('inf')
    for sA in range(1, sumA):
        for sB in range(1, sumB):
            if sA * Mb == sB * Ma:
                ans = min(ans, dp[N][sA][sB])

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

sol()

Buggy python submission (verdict: Time Limit Exceeded):
def sol():
    N, Ma, Mb = map(int, input().split())
    items = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        items.append((a, b, c))

    dp = [[[[]] for _ in range(Ma)] for _ in range(N + 1)]

    for i, (a, _, _) in enumerate(items):
        for mod in range(Ma):
            for prev in dp[i][mod]:
                dp[i + 1][mod].append(prev)
            for prev in dp[i][(mod - a) % Ma]:
                dp[i + 1][mod].append(prev + [i])

    maList = filter(lambda A : len(A) > 0, dp[N][0])

    ans = float('inf')
    for addList in maList:
        sumA = 0
        sumB = 0
        cost = 0
        for i in addList:
            sumA += items[i][0]
            sumB += items[i][1]
            cost += items[i][2]
        if sumA * Mb == sumB * Ma:
            ans = min(ans, cost)

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

sol()

Fixed submission:
def sol():
    N, Ma, Mb = map(int, input().split())

    A = []
    B = []
    cost = []
    sumA = 0
    sumB = 0

    for _ in range(N):
        a, b, c = map(int, input().split())
        A.append(a)
        B.append(b)
        cost.append(c)
        sumA += a
        sumB += b

    dp = [[[float('inf') for _ in range(sumB * 2 + 1)] for _ in range(sumA * 2 + 1)] for _ in range(N + 1)]
    dp[0][0][0] = 0

    for index in range(N):
        for sA in range(sumA + 1):
            for sB in range(sumB + 1):
                dp[index + 1][sA][sB] = min(
                    dp[index + 1][sA][sB],
                    dp[index][sA][sB]
                )
                dp[index + 1][sA + A[index]][sB + B[index]] = min(
                    dp[index + 1][sA + A[index]][sB + B[index]],
                    dp[index][sA][sB] + cost[index]
                )

    ans = float('inf')
    for sA in range(1, sumA):
        for sB in range(1, sumB):
            if sA * Mb == sB * Ma:
                ans = min(ans, dp[N][sA][sB])

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

sol()

Buggy python submission (verdict: Time Limit Exceeded):
def sol():
    N, Ma, Mb = map(int, input().split())
    items = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        items.append((a, b, c))

    dp = [[[[]] for _ in range(Ma)] for _ in range(N + 1)]

    for i, (a, _, _) in enumerate(items):
        for mod in range(Ma):
            for prev in dp[i][mod]:
                dp[i + 1][mod].append(prev)
            for prev in dp[i][(mod - a) % Ma]:
                dp[i + 1][mod].append(prev + [i])

    maList = filter(lambda A : len(A) > 0, dp[N][0])

    ans = float('inf')
    for addList in maList:
        sumA = 0
        sumB = 0
        cost = 0
        for i in addList:
            sumA += items[i][0]
            sumB += items[i][1]
            cost += items[i][2]
        if sumA * Mb == sumB * Ma:
            ans = min(ans, cost)

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

sol()

Fixed submission:
def sol():
    N, Ma, Mb = map(int, input().split())

    A = []
    B = []
    cost = []
    sumA = 0
    sumB = 0

    for _ in range(N):
        a, b, c = map(int, input().split())
        A.append(a)
        B.append(b)
        cost.append(c)
        sumA += a
        sumB += b

    dp = [[[float('inf') for _ in range(sumB * 2 + 1)] for _ in range(sumA * 2 + 1)] for _ in range(N + 1)]
    dp[0][0][0] = 0

    for index in range(N):
        for sA in range(sumA + 1):
            for sB in range(sumB + 1):
                dp[index + 1][sA][sB] = min(
                    dp[index + 1][sA][sB],
                    dp[index][sA][sB]
                )
                dp[index + 1][sA + A[index]][sB + B[index]] = min(
                    dp[index + 1][sA + A[index]][sB + B[index]],
                    dp[index][sA][sB] + cost[index]
                )

    ans = float('inf')
    for sA in range(1, sumA):
        for sB in range(1, sumB):
            if sA * Mb == sB * Ma:
                ans = min(ans, dp[N][sA][sB])

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

sol()