Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10 ** 8)
ni = lambda: int(sys.stdin.readline())
nm = lambda: map(int, sys.stdin.readline().split())

N = ni()
g1, s1, b1 = nm()
g2, s2, b2 = nm()

INF = 10 ** 15


def solve():
    x1 = [g1, s1, b1]
    x2 = [g2, s2, b2]

    # dp[i] := max future donguri when we use i donguri
    dp = [-INF] * (N + 1)
    dp[0] = N
    for i in range(3):
        t1 = x1[i]
        t2 = x2[i]
        if t1 >= t2:
            continue
        for j in range(t1, N + 1):
            if dp[j - t1] >= N:
                dp[j] = max(dp[j], dp[j - t1] - t1 + t2)

    don = max(dp)
    dp = [0] * (don + 1)
    dp[0] = don
    for i in range(3):
        t1 = x1[i]
        t2 = x2[i]
        if t1 <= t2:
            continue
        for j in range(t2, don + 1):
            if dp[j - t2] >= N:
                dp[j] = max(dp[j], dp[j - t2] - t2 + t1)
    return max(dp)


print(solve())


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 8)
ni = lambda: int(sys.stdin.readline())
nm = lambda: map(int, sys.stdin.readline().split())

N = ni()
g1, s1, b1 = nm()
g2, s2, b2 = nm()

INF = 10 ** 15


def solve():
    x1 = [g1, s1, b1]
    x2 = [g2, s2, b2]

    # dp[i] := max future donguri when we use i donguri
    dp = [-INF] * (N + 1)
    dp[0] = N
    for i in range(3):
        t1 = x1[i]
        t2 = x2[i]
        if t1 >= t2:
            continue
        for j in range(t1, N + 1):
            r = dp[j - t1] - t1
            if r >= 0 and dp[j] < r + t2:
                dp[j] = r + t2

    don = max(dp)
    dp = [0] * (don + 1)
    dp[0] = don
    for i in range(3):
        t1 = x1[i]
        t2 = x2[i]
        if t1 <= t2:
            continue
        for j in range(t2, don + 1):
            r = dp[j - t2] - t2
            if r >= 0 and dp[j] < r + t1:
                dp[j] = r + t1
    return max(dp)


print(solve())

Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10 ** 8)
ni = lambda: int(sys.stdin.readline())
nm = lambda: map(int, sys.stdin.readline().split())

N = ni()
g1, s1, b1 = nm()
g2, s2, b2 = nm()


def solve():
    # Start
    don = N
    # A
    don_max = N
    for kg in range(don // g1 + 1):
        don1 = don - kg * g1
        temp1 = kg * g2
        for ks in range(don1 // s1 + 1):
            don2 = don1 - ks * s1
            temp2 = ks * s2
            if b2 > b1:
                kb = don2 // b1
                don3 = don2 - kb * b1
                temp3 = kb * b2
            else:
                kb = 0
                don3 = don2
                temp3 = 0
            if don_max < don3 + temp1 + temp2 + temp3:
                don_max = don3 + temp1 + temp2 + temp3
                # print(f"1> don_max={don_max} g:{kg} s:{ks} b:{kb}")

    don = don_max
    for kg in range(don // g2 + 1):
        don1 = don - kg * g2
        temp1 = kg * g1
        for ks in range(don1 // s2 + 1):
            don2 = don1 - ks * s2
            temp2 = ks * s1
            if b1 > b2:
                kb = don2 // b2
                don3 = don2 - kb * b2
                temp3 = kb * b1
            else:
                kb = 0
                don3 = don2
                temp3 = 0
            if don_max < don3 + temp1 + temp2 + temp3:
                don_max = don3 + temp1 + temp2 + temp3
                # print(f"2> don_max={don_max} g:{kg} s:{ks} b:{kb}")
    return don_max


print(solve())


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 8)
ni = lambda: int(sys.stdin.readline())
nm = lambda: map(int, sys.stdin.readline().split())

N = ni()
g1, s1, b1 = nm()
g2, s2, b2 = nm()

INF = 10 ** 15


def solve():
    x1 = [g1, s1, b1]
    x2 = [g2, s2, b2]

    # dp[i] := max future donguri when we use i donguri
    dp = [-INF] * (N + 1)
    dp[0] = N
    for i in range(3):
        t1 = x1[i]
        t2 = x2[i]
        if t1 >= t2:
            continue
        for j in range(t1, N + 1):
            r = dp[j - t1] - t1
            if r >= 0 and dp[j] < r + t2:
                dp[j] = r + t2

    don = max(dp)
    dp = [0] * (don + 1)
    dp[0] = don
    for i in range(3):
        t1 = x1[i]
        t2 = x2[i]
        if t1 <= t2:
            continue
        for j in range(t2, don + 1):
            r = dp[j - t2] - t2
            if r >= 0 and dp[j] < r + t1:
                dp[j] = r + t1
    return max(dp)


print(solve())

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

sys.setrecursionlimit(10 ** 8)
ni = lambda: int(sys.stdin.readline())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
ns = lambda: sys.stdin.readline().rstrip()

N = ni()
g1, s1, b1 = nm()
g2, s2, b2 = nm()


def solve():
    gr, sr, br = (g2, g1), (s2, s1), (b2, b1)
    rp = sorted([(Fraction(n, d), n, d) for (n, d) in [gr, sr, br] if n > d])
    rn = sorted([(Fraction(n, d), n, d) for (n, d) in [gr, sr, br] if n < d])

    # Start
    don = N
    # A
    don_max = N
    for kg in range(don // g1 + 1):
        don1 = don - kg * g1
        temp1 = kg * g2
        for ks in range(don1 // s1 + 1):
            don2 = don1 - ks * s1
            temp2 = ks * s2
            if b2 > b1:
                kb = don2 // b1
                don3 = don2 - kb * b1
                temp3 = kb * b2
            else:
                kb = 0
                don3 = don2
                temp3 = 0
            if don_max < don3 + temp1 + temp2 + temp3:
                don_max = don3 + temp1 + temp2 + temp3
                # print(f"1> don_max={don_max} g:{kg} s:{ks} b:{kb}")

    don = don_max
    for kg in range(don // g2 + 1):
        don1 = don - kg * g2
        temp1 = kg * g1
        for ks in range(don1 // s2 + 1):
            don2 = don1 - ks * s2
            temp2 = ks * s1
            if b1 > b2:
                kb = don2 // b2
                don3 = don2 - kb * b2
                temp3 = kb * b1
            else:
                kb = 0
                don3 = don2
                temp3 = 0
            if don_max < don3 + temp1 + temp2 + temp3:
                don_max = don3 + temp1 + temp2 + temp3
                # print(f"2> don_max={don_max} g:{kg} s:{ks} b:{kb}")
    return don_max


print(solve())


Fixed submission:
import sys

sys.setrecursionlimit(10 ** 8)
ni = lambda: int(sys.stdin.readline())
nm = lambda: map(int, sys.stdin.readline().split())

N = ni()
g1, s1, b1 = nm()
g2, s2, b2 = nm()

INF = 10 ** 15


def solve():
    x1 = [g1, s1, b1]
    x2 = [g2, s2, b2]

    # dp[i] := max future donguri when we use i donguri
    dp = [-INF] * (N + 1)
    dp[0] = N
    for i in range(3):
        t1 = x1[i]
        t2 = x2[i]
        if t1 >= t2:
            continue
        for j in range(t1, N + 1):
            r = dp[j - t1] - t1
            if r >= 0 and dp[j] < r + t2:
                dp[j] = r + t2

    don = max(dp)
    dp = [0] * (don + 1)
    dp[0] = don
    for i in range(3):
        t1 = x1[i]
        t2 = x2[i]
        if t1 <= t2:
            continue
        for j in range(t2, don + 1):
            r = dp[j - t2] - t2
            if r >= 0 and dp[j] < r + t1:
                dp[j] = r + t1
    return max(dp)


print(solve())