Buggy python submission (verdict: Time Limit Exceeded):
# 入力
N = int(input())
gA, sA, bA = map(int, input().split())
gB, sB, bB = map(int, input().split())

A = (gA, sA, bA)
B = (gB, sB, bB)


# 各貴金属について、買う／売る可能性のある取引所は一意に定まることを用いて
# 全探索により解を求める
def f(n, r):
    R = len(r)
    return (
        n if R == 0 else
        n + (r[-1][1] - r[-1][0]) * (n // r[-1][0]) if R == 1 else
        max(
            n + (r[0][1] - r[0][0]) * x +
            (r[-1][1] - r[-1][0]) * ((n - r[0][0] * x) // r[-1][0])
            for x in range(n // r[0][0] + 1)
        ) if R == 2 else
        max(
            n + (r[0][1] - r[0][0]) * x + (r[1][1] - r[1][0]) * y +
            (r[-1][1] - r[-1][0]) * ((n - r[0][0] * x - r[1][0] * y) // r[-1][0])
            for x in range(n // r[0][0] + 1)
            for y in range((n - x * r[0][0]) // r[1][0] + 1)
        )
    )


ans = f(
    f(
        N,
        [
            (a, b)
            for a, b in zip(A, B)
            if a < b
        ]
    ),
    [
        (b, a)
        for b, a in zip(B, A)
        if a > b
    ]
)

# 出力
print(ans)


Fixed submission:
N = int(input())
gA, sA, bA = map(int, input().split())
gB, sB, bB = map(int, input().split())

ans = 0

if gB > gA and sB > sA and bB > bA:
    for g in range(N // gA + 1):
        n = N - g * gA
        for s in range(n // sA + 1):
            m = n - s * sA
            b = m // bA
            m = m % bA
            ans = max(ans, g * gB + s * sB + b * bB + m)
elif gB > gA and sB > sA and bB <= bA:
    for g in range(N // gA + 1):
        n = N - g * gA
        for s in range(n // sA + 1):
            m = n - s * sA
            m = g * gB + s * sB + m
            b = m // bB
            m = m % bB
            ans = max(ans, b * bA + m)
elif gB > gA and sB <= sA and bB > bA:
    for g in range(N // gA + 1):
        n = N - g * gA
        for b in range(n // bA + 1):
            m = n - b * bA
            m = g * gB + b * bB + m
            s = m // sB
            m = m % sB
            ans = max(ans, s * sA + m)
elif gB <= gA and sB > sA and bB > bA:
    for s in range(N // sA + 1):
        n = N - s * sA
        for b in range(n // bA + 1):
            m = n - b * bA
            m = s * sB + b * bB + m
            g = m // gB
            m = m % gB
            ans = max(ans, g * gA + m)
elif gB > gA and sB <= sA and bB <= bA:
    g = N // gA
    n = N % gA
    n = g * gB + n
    for s in range(n // sB + 1):
        m = n - s * sB
        b = m // bB
        m = m % bB
        ans = max(ans, s * sA + b * bA + m)
elif gB <= gA and sB > sA and bB <= bA:
    s = N // sA
    n = N % sA
    n = s * sB + n
    for g in range(n // gB + 1):
        m = n - g * gB
        b = m // bB
        m = m % bB
        ans = max(ans, g * gA + b * bA + m)
elif gB <= gA and sB <= sA and bB > bA:
    b = N // bA
    n = N % bA
    n = b * bB + n
    for s in range(n // sB + 1):
        m = n - s * sB
        g = m // gB
        m = m % gB
        ans = max(ans, g * gA + s * sA + m)
else:
    for g in range(N // gB + 1):
        n = N - g * gB
        for s in range(n // sB + 1):
            m = n - s * sB
            b = m // bB
            m = m % bB
            ans = max(ans, g * gA + s * sA + b * bA + m)

print(ans)