Problem:
 Score : 600 points    Problem Statement The squirrel Chokudai has N acorns.
One day, he decides to do some trades in multiple precious metal exchanges to make more acorns.  His plan is as follows:   Get out of the nest with N acorns in his hands.  Go to Exchange A and do some trades.  Go to Exchange B and do some trades.  Go to Exchange A and do some trades.  Go back to the nest.   In Exchange X  (X = A, B) , he can perform the following operations any integer number of times (possibly zero) in any order:   Lose g_{X} acorns and gain 1 gram of gold.  Gain g_{X} acorns and lose 1 gram of gold.  Lose s_{X} acorns and gain 1 gram of silver.  Gain s_{X} acorns and lose 1 gram of silver.  Lose b_{X} acorns and gain 1 gram of bronze.  Gain b_{X} acorns and lose 1 gram of bronze.   Naturally, he cannot perform an operation that would leave him with a negative amount of acorns, gold, silver, or bronze.  What is the maximum number of acorns that he can bring to the nest?
Note that gold, silver, or bronze brought to the nest would be worthless because he is just a squirrel.      Constraints  1 \leq N \leq 5000  1 \leq g_{X} \leq 5000  1 \leq s_{X} \leq 5000  1 \leq b_{X} \leq 5000  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  g_A  s_A  b_A  g_B  s_B  b_B       Output Print the maximum number of acorns that Chokudai can bring to the nest.        Sample Input 1 23
1 1 1
2 1 1      Sample Output 1 46  He can bring 46 acorns to the nest, as follows:   In Exchange A , trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 }  In Exchange B , trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 }  In Exchange A , trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 }   He cannot have 47 or more acorns, so the answer is 46 .

Solution (Python):
from functools import reduce


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
        reduce(
            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)
            ),
            n
        ) if R == 2 else
        reduce(
            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 - r[0][0] * x) // r[1][0] + 1)
            ),
            n
        )
    )


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)

Solution (Python):
from functools import reduce


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)
    if R == 0:
        return n
    elif R == 1:
        return n + (r[-1][1] - r[-1][0]) * (n // r[-1][0])
    elif R == 2:
        return reduce(
            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)
            ),
            n
        )
    else:
        res = 0
        for x in range(n // r[0][0] + 1):
            m = n - r[0][0] * x
            for y in range(m // r[1][0] + 1):
                res = max(
                    res,
                    n + (r[0][1] - r[0][0]) * x + (r[1][1] - r[1][0]) * y +
                    (r[-1][1] - r[-1][0]) * ((m - r[1][0] * y) // r[-1][0])
                )
        return res


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)

Solution (Python):

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)
    if R == 0:
        return n
    elif R == 1:
        return n + (r[-1][1] - r[-1][0]) * (n // r[-1][0])
    elif R == 2:
        res = 0
        for x in range(n // r[0][0] + 1):
            res = max(
                res,
                n + (r[0][1] - r[0][0]) * x +
                (r[-1][1] - r[-1][0]) * ((n - r[0][0] * x) // r[-1][0])
            )
        return res
    else:
        res = 0
        for x in range(n // r[0][0] + 1):
            m = n - r[0][0] * x
            for y in range(m // r[1][0] + 1):
                res = max(
                    res,
                    n + (r[0][1] - r[0][0]) * x + (r[1][1] - r[1][0]) * y +
                    (r[-1][1] - r[-1][0]) * ((m - r[1][0] * y) // r[-1][0])
                )
        return res


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)

Solution (Python):

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)
    if R == 0:
        return n
    elif R == 1:
        return n + (r[-1][1] - r[-1][0]) * (n // r[-1][0])
    elif R == 2:
        dr0 = r[0][1] - r[0][0]
        dr1 = r[-1][1] - r[-1][0]
        res = 0
        for x in range(n // r[0][0] + 1):
            res = max(res, dr0 * x + dr1 * ((n - r[0][0] * x) // r[-1][0]))
        return n + res
    else:
        dr0 = r[0][1] - r[0][0]
        dr1 = r[1][1] - r[1][0]
        dr2 = r[-1][1] - r[-1][0]
        res = 0
        for x in range(n // r[0][0] + 1):
            m = n - r[0][0] * x
            for y in range(m // r[1][0] + 1):
                res = max(
                    res,
                    dr0 * x + dr1 * y + dr2 * ((m - r[1][0] * y) // r[-1][0])
                )
        return n + res


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)

Solution (Python):
def change(N, A, B):
    ans = N
    dp = list(range(N+1))
    for i in range(3):
        w = A[i]
        v = B[i]
        for j in range(N+1):
            if w <= j:
                dp[j] = max(dp[j], dp[j-w] + v)
    return dp[N]

def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    N1 = change(N, A, B)
    N2 = change(N1, B, A)
    print(N2)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]


def count(n, a, b):
    a0, a1, a2 = a
    b0, b1, b2 = b
    dp = [0] * (n + 1)
    for i in range(n + 1):
        c0 = dp[i - a0] + b0 if i - a0 >= 0 else 0
        c1 = dp[i - a1] + b1 if i - a1 >= 0 else 0
        c2 = dp[i - a2] + b2 if i - a2 >= 0 else 0
        dp[i] = max(i, c0, c1, c2)
    return dp[n]


print(count(count(N, A, B), B, A))

Solution (Python):
N = int(input())
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]


def count(n, a, b):
    a0, a1, a2 = a
    b0, b1, b2 = b
    dp = [0] * (n + 1)
    for i in range(n + 1):
        c0 = dp[i - a0] + b0 if i - a0 >= 0 else 0
        c1 = dp[i - a1] + b1 if i - a1 >= 0 else 0
        c2 = dp[i - a2] + b2 if i - a2 >= 0 else 0
        dp[i] = max(i, c0, c1, c2)
    return dp[n]


print(count(count(N, A, B), B, A))

Solution (Python):



N = int( input())
A = list( map( int, input().split()))
B = list( map( int ,input().split()))
dpOne = [0]*(N+1)
for i in range(3):
    a, b = A[i], B[i]
    for j in range(N-a+1):
        if dpOne[j+a] < dpOne[j]+b:
            dpOne[j+a] = dpOne[j]+b
donguri = N
for i in range(N+1):
    if donguri < N-i+dpOne[i]:
        donguri = N - i +dpOne[i]
K = donguri
dpTwo = [0]*(K+1)
for i in range(3):
    a, b  = A[i], B[i]
    for j in range(K-b+1):
        if dpTwo[j+b] < dpTwo[j]+a:
            dpTwo[j+b] = dpTwo[j]+a
ans = K
for i in range(K+1):
    if ans < K - i + dpTwo[i]:
        ans = K-i+dpTwo[i]
print( ans)

Solution (Python):


def main():
    N = int( input())
    A = list( map( int, input().split()))
    B = list( map( int ,input().split()))
    dpOne = [0]*(N+1)
    for i in range(3):
        a, b = A[i], B[i]
        for j in range(N-a+1):
            if dpOne[j+a] < dpOne[j]+b:
                dpOne[j+a] = dpOne[j]+b
    donguri = N
    for i in range(N+1):
        if donguri < N-i+dpOne[i]:
            donguri = N - i +dpOne[i]
    K = donguri
    dpTwo = [0]*(K+1)
    for i in range(3):
        a, b  = A[i], B[i]
        for j in range(K-b+1):
            if dpTwo[j+b] < dpTwo[j]+a:
                dpTwo[j+b] = dpTwo[j]+a
    ans = K
    for i in range(K+1):
        if ans < K - i + dpTwo[i]:
            ans = K-i+dpTwo[i]
    print( ans)
if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
def main():
    N = int( input())
    A = list( map( int, input().split()))
    B = list( map( int ,input().split()))
    dpOne = [0]*(N+1)
    for i in range(3):
        a, b = A[i], B[i]
        for j in range(N-a+1):
            if dpOne[j+a] < dpOne[j]+b:
                dpOne[j+a] = dpOne[j]+b
    donguri = N
    for i in range(N+1):
        if donguri < N-i+dpOne[i]:
            donguri = N - i +dpOne[i]
    K = donguri
    dpTwo = [0]*(K+1)
    for i in range(3):
        a, b  = A[i], B[i]
        for j in range(K-b+1):
            if dpTwo[j+b] < dpTwo[j]+a:
                dpTwo[j+b] = dpTwo[j]+a
    ans = K
    for i in range(K+1):
        if ans < K - i + dpTwo[i]:
            ans = K-i+dpTwo[i]
    print( ans)
if __name__ == '__main__':
    main()

Solution (Python):
def napzak(w,v,limit):
    dp = [0]*(limit+1)
    wv = [(weight,value) for weight,value in zip(w,v) if value>weight]
    if wv == []:
        return limit
    for i in range(1,limit+1):
        for j,value in wv:
            if (i-j)>=0 and dp[i]<(dp[i-j]+value-j):
                dp[i] = dp[i-j]+value-j
    return dp[limit]+limit

n = int(input())
wa = list(map(int,input().split()))
wb = list(map(int,input().split()))

m = napzak(wa,wb,n)
ans = napzak(wb,wa,m)

print(ans)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)

def main():
    n = I()
    a = LI()
    b = LI()

    def f(c,n):
        c0 = c[0]
        if len(c) == 1:
            return n % c0[0] + n // c0[0] * c0[1]

        r = n
        for i in range(0,n+1,c0[0]):
            t = i // c0[0] * c0[1] + f(c[1:], n-i)
            if r < t:
                r = t
        return r

    c = sorted([(ai,bi) for ai,bi in zip(a,b) if ai < bi], reverse=True)
    t = n
    if len(c) > 0:
        t = f(c,n)

    d = sorted([(ai,bi) for ai,bi in zip(b,a) if ai < bi], reverse=True)
    r = t
    if len(d) > 0:
        r = f(d,t)

    return r


print(main())

Solution (Python):
N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
def solve(N, P):
    V = []; W = []
    for w, v in P:
        if w < v:
            V.append(v); W.append(w)
    if len(V) == 1:
        v = V[0]; w = W[0]
        k = N // w
        return N - k*w + k*v
    if len(V) == 2:
        w1, w2 = W
        M = w1 * w2
        dp = [i for i in range(M+1)]
        for v, w in zip(V, W):
            if v <= w:
                continue
            for k in range(w, M+1):
                dp[k] = max(dp[k], dp[k-w] + v)
        for i in range(M):
            dp[i+1] = max(dp[i+1], dp[i])
        ans = 0
        for i in range(M):
            k, r = divmod(N, (i+1))
            ans = max(ans, dp[i+1] * k + dp[r])
        return ans
    dp = [i for i in range(N+1)]
    for v, w in zip(V, W):
        if v <= w:
            continue
        for k in range(w, N+1):
            dp[k] = max(dp[k], dp[k-w] + v)
    return max(dp)

A = solve(N, [[ga, gb], [sa, sb], [ba, bb]])
B = solve(A, [[gb, ga], [sb, sa], [bb, ba]])
print(B)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
def nap(N,goods):
    dp=[-1*float('inf')]*(N+1)
    dp[0]=0
    for i in range(N):
        for w,v in goods:
            if i+w<=N:
                dp[i+w]=max(dp[i+w],dp[i]+v)
    return max([dp[i]+N-i for i in range(N+1)])
goods1=[(a,b) for a,b in zip(A,B) if a<b]
goods2=[(b,a) for a,b in zip(A,B) if a>b]
if len(goods1)>0:
    N=nap(N,[(a,b) for a,b in zip(A,B)])
if len(goods2)==0:
    print(N)
elif len(goods2)==1:
    print(N%goods2[0][0]+(N//goods2[0][0])*goods2[0][1])
elif len(goods2)==3:
    print(nap(N,goods2))
else:
    a,b,c,d=goods2[0][0],goods2[0][1],goods2[1][0],goods2[1][1]
    if (b-a)/a<(d-c)/c:
        a,b,c,d=c,d,a,b
    elif (b-a)/a==(d-c)/c and a>c:
        a,b,c,d=c,d,a,b
    bord=(N//a)*b+((N%a)//c)*d+(N%a)%c
    x=N//a
    ans=bord
    for i in range(1,1+x):
        now=(x-i)*b+((N-(x-i)*a)//c)*d+(N-(x-i)*a)%c
        if now+d<ans:
            break
        else:
            ans=max(ans,now)
    print(ans)

Solution (Python):
N = int(input())
GA, SA, BA = map(int, input().split())
GB, SB, BB = map(int, input().split())

def napsac(x, g1, s1, b1, g2, s2, b2):
    dp = [0 for _ in range(x+1)]
    dp[0] = 0

    def case(i, m1, m2):
        return 0 if i - m1 < 0 else dp[i-m1] + m2

    for i in range(1,x+1):
        dp[i] = max(i, case(i, g1, g2), case(i, s1, s2), case(i, b1, b2))

    return dp[-1]

print(napsac(napsac(N, GA, SA, BA, GB, SB, BB), GB, SB, BB, GA, SA, BA))

Solution (Python):
n = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())

dp1 = [0] * (n + 1)
if n > 5001:
    for i in range(1, 5001):
        feat = [i]
        if i - ga >= 0:
            feat.append(dp1[i-ga]+gb)
        if i - sa >= 0:
            feat.append(dp1[i-sa]+sb)
        if i - ba >= 0:
            feat.append(dp1[i-ba]+bb)
        dp[i] = max(feat)
    for i in range(5001, n+1):
        dp1[i] = max(i, dp1[i-ga]+gb, dp1[i-sa]+sb, dp1[i-ba]+bb)
else:
    for i in range(1, n+1):
        feat = [i]
        if i - ga >= 0:
            feat.append(dp1[i-ga]+gb)
        if i - sa >= 0:
            feat.append(dp1[i-sa]+sb)
        if i - ba >= 0:
            feat.append(dp1[i-ba]+bb)
        dp1[i] = max(feat)

B = dp1[-1]
dp2 = [0] * (B + 1)
if B > 5001:
    for i in range(1, 5001):
        feat = [i]
        if i - gb >= 0:
            feat.append(dp2[i-gb]+ga)
        if i - sb >= 0:
            feat.append(dp2[i-sb]+sa)
        if i - bb >= 0:
            feat.append(dp2[i-bb]+ba)
        dp2[i] = max(feat)

    for i in range(5001, B+1):
        dp2[i] = max(i, dp2[i-gb]+ga, dp2[i-sb]+sa, dp2[i-bb]+ba)
else:
    for i in range(1, B+1):
        feat = [i]
        if i - gb >= 0:
            feat.append(dp2[i-gb]+ga)
        if i - sb >= 0:
            feat.append(dp2[i-sb]+sa)
        if i - bb >= 0:
            feat.append(dp2[i-bb]+ba)
        dp2[i] = max(feat)

print(dp2[-1])

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

def donguri(n, a, b):
    dp = [0]*(n+1)
    for w in range(n+1):
        x0 = dp[w-a[0]] + b[0] if (w-a[0]>=0) else w
        x1 = dp[w-a[1]] + b[1] if (w-a[1]>=0) else w
        x2 = dp[w-a[2]] + b[2] if (w-a[2]>=0) else w
        dp[w] = max(w, x0, x1, x2)
    return dp[-1]

print(donguri(donguri(N,A,B),B,A))

Solution (Python):
N = int(input())						
A = list( map(int, input().split()) )	
B = list( map(int, input().split()) )	


def F(N,A,B):
	dp=[0]*(N+1)
	for n in range(N+1):
		v=n
		for i in range(3):
			if n>=A[i]:
				x = dp[n-A[i]]+B[i]
				if x>v:
					v = x
		dp[n]=v

		
		
		
		

		
	
	return dp[N]

ans = F(F(N,A,B),B,A)
print( ans )

Solution (Python):
def main():
    N = int(input())
    ga, sa, ba = map(int, input().split())
    gb, sb, bb = map(int, input().split())

    
    
    
    

    dp1 = list(range(N + 1))  
    dp1[0] = 0
    for i in range(N + 1):
        if i >= ga:
            dp1[i] = max(dp1[i], dp1[i - ga] + gb)
        if i >= sa:
            dp1[i] = max(dp1[i], dp1[i - sa] + sb)
        if i >= ba:
            dp1[i] = max(dp1[i], dp1[i - ba] + bb)

    N = dp1[N]
    dp2 = list(range(N + 1))  
    for i in range(N + 1):
        if i >= gb:
            dp2[i] = max(dp2[i], dp2[i - gb] + ga)
        if i >= sb:
            dp2[i] = max(dp2[i], dp2[i - sb] + sa)
        if i >= bb:
            dp2[i] = max(dp2[i], dp2[i - bb] + ba)
    print(dp2[N])


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N = int(input())
    a = tuple(map(int, input().split()))
    b = tuple(map(int, input().split()))

    
    dp = list(range(N + 1))
    mas = tuple(zip(a, b))
    for i in range(N + 1):
        for to_metal, to_acorn in mas:
            if i >= to_metal:
                dp[i] = max(dp[i], dp[i - to_metal] + to_acorn)
    N = dp[N]

    
    dp = list(range(N + 1))
    mas = tuple(zip(b, a))
    for i in range(N + 1):
        for to_metal, to_acorn in mas:
            if i >= to_metal:
                dp[i] = max(dp[i], dp[i - to_metal] + to_acorn)
    print(dp[N])


if __name__ == '__main__':
    main()

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub
import copy

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, GSB):
    def f(N, gsb):
        dp0 = [0] * (N+1)
        dp1 = [0] * (N+1)
        for i in range(3):
            v = gsb[1][i] - gsb[0][i]
            a = gsb[0][i]
            if v > 0:
                for w in range(N+1):
                    if w >= a:
                        dp1[w] = max(dp0[w], dp1[w-a] + v)
            else:
                dp1 = dp0
            dp0 = dp1
            dp1 = copy.copy(dp0)
        return dp0[N] + N
    tmp = f(N, GSB)
    
    GSB[0], GSB[1] = GSB[1], GSB[0]
    return f(tmp, GSB)


def main():
    N = read_int()
    GSB = [read_int_n() for _ in range(2)]
    print(slv(N, GSB))


if __name__ == '__main__':
    main()

Solution (Python):
def calc(n, to_metal, to_acorn):
    mas = tuple(zip(to_metal, to_acorn))
    dp = list(range(n + 1))
    for i in range(n + 1):
        for m, a in mas:
            if i >= m:
                dp[i] = max(dp[i], dp[i - m] + a)
    return dp[n]


def main():
    N = int(input())
    a = tuple(map(int, input().split()))
    b = tuple(map(int, input().split()))

    N = calc(N, a, b)  
    N = calc(N, b, a)  
    print(N)


if __name__ == '__main__':
    main()

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, GSB):
    def f(N, gsb):
        dp = [[0] * (N+1) for _ in range(4)]
        for i in range(3):
            v = gsb[1][i] - gsb[0][i]
            a = gsb[0][i]
            if v > 0:
                for w in range(N+1):
                    dp[i+1][w] = dp[i][w]
                    if w >= a:
                        dp[i+1][w] = max(dp[i][w], dp[i+1][w-a] + v)
            else:
                dp[i+1] = dp[i]
        return dp[3][N] + N
    tmp = f(N, GSB)
    
    GSB[0], GSB[1] = GSB[1], GSB[0]
    return f(tmp, GSB)


def main():
    N = read_int()
    GSB = [read_int_n() for _ in range(2)]
    print(slv(N, GSB))


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
A = [int(item) for item in input().split()]
B = [int(item) for item in input().split()]
 
dp = [0] * (n+1)
AB = [] 
for a, b in zip(A, B):
    if b > a:
        AB.append((a, b))
for i in range(n+1):
    for a, b in AB:
        if i - a >= 0:
            y = dp[i-a] + b - a
            if y > dp[i]:
                dp[i] = y
n += dp[n]
dp = [0] * (n+1)
BA = []
for b, a in zip(B, A):
    if a > b:
        BA.append((b, a))
for i in range(n+1):
    for b, a in BA:
        if i - b >= 0:
            y = dp[i-b] + a - b
            if y > dp[i]:
                dp[i] = y 
print(n + dp[n])

Solution (Python):
N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())






dp1 = list(range(N + 1))  
dp1[0] = 0
for i in range(N + 1):
    if i >= ga:
        dp1[i] = max(dp1[i], dp1[i - ga] + gb)
    if i >= sa:
        dp1[i] = max(dp1[i], dp1[i - sa] + sb)
    if i >= ba:
        dp1[i] = max(dp1[i], dp1[i - ba] + bb)

N = dp1[N]
dp2 = list(range(N + 1))  
for i in range(N + 1):
    if i >= gb:
        dp2[i] = max(dp2[i], dp2[i - gb] + ga)
    if i >= sb:
        dp2[i] = max(dp2[i], dp2[i - sb] + sa)
    if i >= bb:
        dp2[i] = max(dp2[i], dp2[i - bb] + ba)
print(dp2[N])

Solution (Python):
def main():
    N=int(input())
    A=list(map(int,input().split()))
    B=list(map(int,input().split()))
    dp=[N]*(N+1)
    
    C=[B[i]-A[i] for i in range(3)]
    for i in range(N):
        dp[i+1]=max(dp[i+1],dp[i])
        for j in range(3):
            if i+1-A[j]>=0:
                dp[i+1]=max(dp[i+1],dp[i+1-A[j]]+C[j])
    M=dp[N]
    dp=[0]*(M+1)
    C=[A[i]-B[i] for i in range(3) if A[i]-B[i]>=0]
    C=[]
    b=[]
    a=[]
    for i in range(3):
        if A[i]-B[i]>=0:
            C.append(A[i]-B[i])
            b.append(B[i])
            a.append(A[i])
    B=b
    A=a
    for i in range(M):
        dp[i+1]=max(dp[i+1],dp[i])
        for j in range(len(C)):
            if i+1-B[j]>=0:
                dp[i+1]=max(dp[i+1],dp[i+1-B[j]]+C[j])
    print(M+dp[M])
if __name__=='__main__':
  main()

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
dp=[N]*(N+1)

C=[B[i]-A[i] for i in range(3)]
for i in range(N):
    dp[i+1]=max(dp[i+1],dp[i])
    for j in range(3):
        if i+1-A[j]>=0:
            dp[i+1]=max(dp[i+1],dp[i+1-A[j]]+C[j])
M=dp[N]
dp=[0]*(M+1)
C=[A[i]-B[i] for i in range(3) if A[i]-B[i]>=0]
C=[]
b=[]
a=[]
for i in range(3):
    if A[i]-B[i]>=0:
        C.append(A[i]-B[i])
        b.append(B[i])
        a.append(A[i])
B=b
A=a
for i in range(M):
    dp[i+1]=max(dp[i+1],dp[i])
    for j in range(len(C)):
        if i+1-B[j]>=0:
            dp[i+1]=max(dp[i+1],dp[i+1-B[j]]+C[j])
print(M+dp[M])

Solution (Python):
import sys
if sys.version[0] == '2':
    range, input = xrange, raw_input
N = int(input())

A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]

dp = [0] * (N+1)
for a, b in ((a, b) for a, b in zip(A, B) if a < b):
    for w in range(a, N+1):
        dp[w] = max(dp[w], dp[w-a] + b-a)

M = max(dp) + N
dp = [0] * (M+1)
for a, b in ((a, b) for a, b in zip(A, B) if a > b):
    for w in range(b, M+1):
        dp[w] = max(dp[w], dp[w-b] + a-b)

print(max(dp) + M)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ea=[]
eb=[]
for i in range(3):
    if(a[i]<b[i]):
        ea.append((a[i],b[i]))
    elif(a[i]>b[i]):
        eb.append((b[i],a[i]))
ea.sort(reverse=True)
eb.sort(reverse=True)
def main(ea,n):
    if(len(ea)==1):
        fa=n%ea[0][0]+n//ea[0][0]*ea[0][1]
    elif(len(ea)==2):
        ga,gb=ea[0]
        ba,bb=ea[1]
        fa=n
        for i in range(n+1):
            if(ga*i>n):
                break
            k=n-ga*i
            tmp=i*gb+k%ba+(k//ba)*bb
            fa=max(tmp,fa)
    elif(len(ea)==3):
        ga,gb=ea[0]
        sa,sb=ea[1]
        ba,bb=ea[2]
        fa=n
        if(ga==sa):
            return main([ea[0],ea[2]],n)
        if(sa==ba):
            return main([ea[0],ea[1]],n)
        for i in range(n+1):
            if(ga*i>n):
                break
            for j in range(n+1):
                if(ga*i+sa*j>n):
                    break
                k=n-ga*i-sa*j
                tmp=i*gb+j*sb+k%ba+k//ba*bb
                fa=max(tmp,fa)
    else:
        fa=n
    return fa
n=main(ea,n)
n=main(eb,n)
print(n)

Solution (Python):


import sys
from sys import stdout
from bisect import bisect_left as binl
from copy import copy, deepcopy
from collections import defaultdict
import math


mod = 1


def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return tuple(map(int, input_tuple))


def intina():
    return [int(i) for i in input().split()]


def intinl(count):
    return [intin() for _ in range(count)]


def modadd(x, y):
    global mod
    return (x + y) % mod


def modmlt(x, y):
    global mod
    return (x * y) % mod


def mod_inv(x):
    
    global mod
    return pow(x, mod - 2, mod)


def gcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x


def combination(x, y):
    assert(x >= y)

    ret = math.factorial(x)
    ret = ret // (math.factorial(x - y) * math.factorial(y))

    return ret


def get_divisors(x):
    retlist = []
    for i in range(1, int(x**0.5) + 3):
        if x % i == 0:
            retlist.append(i)
            retlist.append(x // i)
    return retlist


def get_factors(x):
    retlist = []
    for i in range(2, int(x**0.5) + 3):
        while x % i == 0:
            retlist.append(i)
            x = x // i
    retlist.append(x)
    return retlist


def make_linklist(xylist):
    linklist = {}
    for a, b in xylist:
        linklist.setdefault(a, [])
        linklist.setdefault(b, [])
        linklist[a].append(b)
        linklist[b].append(a)
    return linklist


def calc_longest_distance(linklist, v=1):
    distance_list = {}
    distance_count = 0
    distance = 0
    vlist_previous = []
    vlist = [v]
    nodecount = len(linklist)

    while distance_count < nodecount:
        vlist_next = []
        for v in vlist:
            distance_list[v] = distance
            distance_count += 1
            vlist_next.extend(linklist[v])
        distance += 1
        vlist_to_del = vlist_previous
        vlist_previous = vlist
        vlist = list(set(vlist_next) - set(vlist_to_del))

    max_distance = -1
    max_v = None
    for v, distance in distance_list.items():
        if distance > max_distance:
            max_distance = distance
            max_v = v

    return (max_distance, max_v)


def calc_tree_diameter(linklist, v=1):
    _, u = calc_longest_distance(linklist, v)
    distance, _ = calc_longest_distance(linklist, u)
    return distance


class UnionFind:

    def __init__(self, n):
        self.parent = [i for i in range(n)]

    def root(self, i):
        if self.parent[i] == i:
            return i
        self.parent[i] = self.root(self.parent[i])
        return self.parent[i]

    def unite(self, i, j):
        rooti = self.root(i)
        rootj = self.root(j)
        if rooti == rootj:
            return
        if rooti < rootj:
            self.parent[rootj] = rooti
        else:
            self.parent[rooti] = rootj

    def same(self, i, j):
        return self.root(i) == self.root(j)





wvalue = []
cache = {}


def f(n, wvaluelen):
    global cache, wvalue
    if (n, wvaluelen) in cache:
        return cache[(n, wvaluelen)]
    if wvaluelen == 0:
        return 0
    if wvaluelen == 1:
        w, value = wvalue[0]
        ret = value * (n // w)
        cache[(n, wvaluelen)] = ret
        return ret

    ret = -1
    w, value = wvalue[wvaluelen-1]
    for i in range(n // w + 1):
        ret = max(ret, i * value + f(n - i * w, wvaluelen - 1))

    cache[(n, wvaluelen)] = ret
    return ret


def main():
    global cache, wvalue
    sys.setrecursionlimit(10000)

    n = intin()
    gsb_a = intina()
    gsb_b = intina()

    ga = gsb_a[0]
    sa = gsb_a[1]
    ba = gsb_a[2]

    gb = gsb_b[0]
    sb = gsb_b[1]
    bb = gsb_b[2]

    
    if gb - ga > 0:
        wvalue.append((ga, gb - ga))
    if sb - sa > 0:
        wvalue.append((sa, sb - sa))
    if bb - ba > 0:
        wvalue.append((ba, bb - ba))

    value = f(n, len(wvalue))
    cache = {}
    wvalue = []

    
    n += value

    
    if ga - gb > 0:
        wvalue.append((gb, ga - gb))
    if sa - sb > 0:
        wvalue.append((sb, sa - sb))
    if ba - bb > 0:
        wvalue.append((bb, ba - bb))

    value = f(n, len(wvalue))
    cache = {}
    wvalue = []

    
    n += value

    print(n)


if __name__ == '__main__':
    main()