Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache, reduce

INF = float("inf")
sys.setrecursionlimit(10 ** 7)

# 4近傍（右, 下, 左, 上）
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]


def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W


def main():
    N, Ma, Mb = map(int, input().split())
    dp = [[[INF] * 401 for j in range(401)] for i in range(N + 1)]
    dp[0][0][0] = 0
    a_b_c = [map(int, input().split()) for _ in range(N)]

    for i, (a, b, c) in enumerate(a_b_c):
        for j in range(len(dp[0])):
            for k in range(len(dp[0][0])):
                # not use
                dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k])

                # use
                if j + a < len(dp[0]) and k + b < len(dp[0][0]):
                    dp[i + 1][j + a][k + b] = min(dp[i + 1][j + a][k + b], dp[i][j][k] + c)

    ans = INF
    for i in count(1):
        a, b = Ma * i, Mb * i
        if a >= len(dp[0]) or b >= len(dp[0][0]):
            break
        ans = min(ans, dp[-1][a][b])
    if ans != INF:
        print(ans)
    else:
        print(-1)

if __name__ == '__main__':
    main()


Fixed submission:
from collections import defaultdict
from fractions import Fraction

INF = int(1e14)


def main():
    N, Ma, Mb = map(int, input().split())
    l = []
    for _ in range(N):
        l.append(tuple(map(int, input().split())))

    ans = INF
    dp = [[[INF] * 401 for i in range(401)] for j in range(N + 1)]
    dp[0][0][0] = 0

    max_a, max_b = 1, 1
    for i in range(N):

        for a in range(max_a, -1, -1):
            for b in range(max_b, -1, -1):
                if dp[i][a][b] != INF:
                    dp[i + 1][a + l[i][0]][b + l[i][1]] = min(dp[i + 1][a + l[i][0]][b + l[i][1]], dp[i][a][b] + l[i][2])
                    dp[i + 1][a][b] = min(dp[i + 1][a][b], dp[i][a][b])

                    max_a = max(max_a, a + l[i][0])
                    max_b = max(max_b, b + l[i][1])

    for a in range(1, len(dp[0])):
        for b in range(1, len(dp[1])):
            if a * Mb == b * Ma:
                ans = min(ans, dp[N][a][b])

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

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
from fractions import Fraction

memo = {}
gl_ans = 10 ** 10

def dfs(i, a, b, c):
    global gl_ans
    if a != 0 and b != 0 and Fraction(a, b) == Fraction(Ma, Mb):
        gl_ans = min(gl_ans, c)
        return c

    if i == N:
        return 10 ** 10

    if (i, a, b, c) in memo:
        return memo[(i, a, b)]

    if c > gl_ans:
        return 10 ** 10

    ans = dfs(i + 1, a + l[i][0], b + l[i][1], c + l[i][2])
    ans = min(ans, dfs(i + 1, a, b, c))

    memo[(i, a, b, c)] = ans

    return ans


if __name__ == '__main__':

    N, Ma, Mb = map(int, input().split())
    l = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        l.append((a, b, c))

    s = dfs(0, 0, 0, 0)
    if s >= 10 ** 10:
        print(-1)
    else:
        print(s)
        

Fixed submission:
from collections import defaultdict
from fractions import Fraction

INF = int(1e14)


def main():
    N, Ma, Mb = map(int, input().split())
    l = []
    for _ in range(N):
        l.append(tuple(map(int, input().split())))

    ans = INF
    dp = [[[INF] * 401 for i in range(401)] for j in range(N + 1)]
    dp[0][0][0] = 0

    max_a, max_b = 1, 1
    for i in range(N):

        for a in range(max_a, -1, -1):
            for b in range(max_b, -1, -1):
                if dp[i][a][b] != INF:
                    dp[i + 1][a + l[i][0]][b + l[i][1]] = min(dp[i + 1][a + l[i][0]][b + l[i][1]], dp[i][a][b] + l[i][2])
                    dp[i + 1][a][b] = min(dp[i + 1][a][b], dp[i][a][b])

                    max_a = max(max_a, a + l[i][0])
                    max_b = max(max_b, b + l[i][1])

    for a in range(1, len(dp[0])):
        for b in range(1, len(dp[1])):
            if a * Mb == b * Ma:
                ans = min(ans, dp[N][a][b])

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

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
from fractions import Fraction

memo = {}

def dfs(i, a, b):
    if a != 0 and b != 0 and Fraction(a, b) == Fraction(Ma, Mb):
        return 0
    if i == N:
        return 10 ** 10

    if (i, a, b) in memo:
        return memo[(i, a, b)]

    ans = dfs(i + 1, a + l[i][0], b + l[i][1]) + l[i][2]
    ans = min(ans, dfs(i + 1, a, b))

    memo[(i, a, b)] = ans
    return ans


if __name__ == '__main__':

    N, Ma, Mb = map(int, input().split())
    l = []
    for _ in range(N):
        a, b, c = map(int, input().split())
        l.append((a, b, c))

    s = dfs(0, 0, 0)
    if s >= 10 ** 10:
        print(-1)
    else:
        print(s)


Fixed submission:
from collections import defaultdict
from fractions import Fraction

INF = int(1e14)


def main():
    N, Ma, Mb = map(int, input().split())
    l = []
    for _ in range(N):
        l.append(tuple(map(int, input().split())))

    ans = INF
    dp = [[[INF] * 401 for i in range(401)] for j in range(N + 1)]
    dp[0][0][0] = 0

    max_a, max_b = 1, 1
    for i in range(N):

        for a in range(max_a, -1, -1):
            for b in range(max_b, -1, -1):
                if dp[i][a][b] != INF:
                    dp[i + 1][a + l[i][0]][b + l[i][1]] = min(dp[i + 1][a + l[i][0]][b + l[i][1]], dp[i][a][b] + l[i][2])
                    dp[i + 1][a][b] = min(dp[i + 1][a][b], dp[i][a][b])

                    max_a = max(max_a, a + l[i][0])
                    max_b = max(max_b, b + l[i][1])

    for a in range(1, len(dp[0])):
        for b in range(1, len(dp[1])):
            if a * Mb == b * Ma:
                ans = min(ans, dp[N][a][b])

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

if __name__ == '__main__':
    main()