Buggy python submission (verdict: Time Limit Exceeded):
inpl = lambda: list(map(int,input().split()))
N = int(input())
rate = []
rate.append(inpl())
rate.append(inpl())
AB = []
BA = []
for i in range(3):
    rA, rB = rate[0][i], rate[1][i]
    if rB > rA:
        AB.append((rA, rB))
    elif rA > rB:
        BA.append((rB, rA))

dpAB = [0]*(N+1)
q = [0]*(len(AB)+1)
for i in range(1,N+1):
    k = 1
    for cost, ret in AB:
        if i >= cost:
            q[k] = dpAB[i-cost] + ret
            k += 1
    dpAB[i]=max(dpAB[i-1]+1, max(q))

M = dpAB[N]
dpBA = [0]*(M+1)
q = [0]*(len(BA)+1)
for i in range(1,M+1):
    k = 1
    for cost, ret in BA:
        if i >= cost:
            q[k] = dpBA[i-cost] + ret
            k += 1
    dpBA[i] = max(dpBA[i-1]+1, max(q))

print(dpBA[M])

Fixed submission:
from fractions import gcd
from functools import reduce
inpl = lambda: list(map(int,input().split()))
def lcm(a,b):
    return a*b//gcd(a,b)
def lcm_list(numbers):
    return reduce(lcm, numbers)

N = int(input())
rate = []
rate.append(inpl())
rate.append(inpl())
AB = []
BA = []
for i in range(3):
    rA, rB = rate[0][i], rate[1][i]
    if rB > rA:
        AB.append((rA, rB))
    elif rA > rB:
        BA.append((rB, rA))
for l in [AB, BA]:
    l.sort(key=lambda x: -x[1]/x[0])

def buy_and_sell(N, cost_ret):
    if len(cost_ret) == 0:
        return N
    else:
        Nunit = lcm_list([ x[0] for x in cost_ret ])
        b0, s0 = cost_ret[0]
        q = N // b0
        n = max(0, q - 2*Nunit//b0 + 1)
        Ndp = N - n*b0
        dp = [0]*(Ndp+1)
        w = [0]*(len(cost_ret)+1)
        for i in range(1,Ndp+1):
            k = 1
            for cost, ret in cost_ret:
                if i >= cost:
                    w[k] = dp[i-cost] + ret
                    k += 1
            dp[i] = max(dp[i-1]+1, max(w))
        return s0*n + dp[-1]

M = buy_and_sell(N, AB)
L = buy_and_sell(M, BA)
print(L)

Buggy python submission (verdict: Time Limit Exceeded):
inpl = lambda: list(map(int,input().split()))
N = int(input())
rate = []
rate.append(inpl())
rate.append(inpl())
AB = []
BA = []
for i in range(3):
    rA, rB = rate[0][i], rate[1][i]
    if rB > rA:
        AB.append((rA, rB))
    elif rA > rB:
        BA.append((rB, rA))

dpAB = [0]
for i in range(1,N+1):
    q = [0]
    for cost, ret in AB:
        if i >= cost:
            q.append(dpAB[i-cost] + ret)
    dpAB.append(max(dpAB[i-1]+1, max(q)))

M = dpAB[N]
dpBA = [0]
for i in range(1,M+1):
    q = [0]
    for cost, ret in BA:
        if i >= cost:
            q.append(dpBA[i-cost] + ret)
    dpBA.append(max(dpBA[i-1]+1, max(q)))

print(dpBA[M])

Fixed submission:
from fractions import gcd
from functools import reduce
inpl = lambda: list(map(int,input().split()))
def lcm(a,b):
    return a*b//gcd(a,b)
def lcm_list(numbers):
    return reduce(lcm, numbers)

N = int(input())
rate = []
rate.append(inpl())
rate.append(inpl())
AB = []
BA = []
for i in range(3):
    rA, rB = rate[0][i], rate[1][i]
    if rB > rA:
        AB.append((rA, rB))
    elif rA > rB:
        BA.append((rB, rA))
for l in [AB, BA]:
    l.sort(key=lambda x: -x[1]/x[0])

def buy_and_sell(N, cost_ret):
    if len(cost_ret) == 0:
        return N
    else:
        Nunit = lcm_list([ x[0] for x in cost_ret ])
        b0, s0 = cost_ret[0]
        q = N // b0
        n = max(0, q - 2*Nunit//b0 + 1)
        Ndp = N - n*b0
        dp = [0]*(Ndp+1)
        w = [0]*(len(cost_ret)+1)
        for i in range(1,Ndp+1):
            k = 1
            for cost, ret in cost_ret:
                if i >= cost:
                    w[k] = dp[i-cost] + ret
                    k += 1
            dp[i] = max(dp[i-1]+1, max(w))
        return s0*n + dp[-1]

M = buy_and_sell(N, AB)
L = buy_and_sell(M, BA)
print(L)