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):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
gsb_A = LIST()
gsb_B = LIST()
gsb_delta = {}






dp = [0] * (N+1)
for i in range(N+1):
	if i-gsb_A[0] >= 0:
		dp[i] = max(dp[i], dp[i-gsb_A[0]]+gsb_B[0])
	if i-gsb_A[1] >= 0:
		dp[i] = max(dp[i], dp[i-gsb_A[1]]+gsb_B[1])
	if i-gsb_A[2] >= 0:
		dp[i] = max(dp[i], dp[i-gsb_A[2]]+gsb_B[2])
	if i >= 1 and i-gsb_A[0] < 0 and i-gsb_A[1] < 0 and i-gsb_A[2] < 0:
		dp[i] = dp[i-1]+1


M = max(N, dp[N])
dp = [0] * (M+1)
for i in range(M+1):
	if i-gsb_B[0] >= 0:
		dp[i] = max(dp[i], dp[i-gsb_B[0]]+gsb_A[0])
	if i-gsb_B[1] >= 0:
		dp[i] = max(dp[i], dp[i-gsb_B[1]]+gsb_A[1])
	if i-gsb_B[2] >= 0:
		dp[i] = max(dp[i], dp[i-gsb_B[2]]+gsb_A[2])
	if i >= 1 and i-gsb_B[0] < 0 and i-gsb_B[1] < 0 and i-gsb_B[2] < 0:
		dp[i] = dp[i-1]+1

print(max(M, dp[M]))

Solution (Python):
n = int(input())
w1 = list(map(int,input().split()))
w2 = list(map(int,input().split()))
v1 = [0,0,0]
for i in range(3):
    v1[i] = w2[i] - w1[i]
dp1 = [0] * (n+1)
for i in range(n):
    p = 0
    q = 0
    r = 0
    if i+1 - w1[0] >= 0:
        p = dp1[i+1 - w1[0]] + v1[0]
    if i+1 - w1[1] >= 0:
        q = dp1[i+1 - w1[1]] + v1[1]
    if i+1 - w1[2] >= 0:
        r = dp1[i+1 - w1[2]] + v1[2]
    dp1[i+1] = max(p,q,r,dp1[i])
m = dp1[-1] + n

v2 = [0,0,0]
for i in range(3):
    v2[i] = w1[i] - w2[i]
dp2 = [0] * (m+1)
for i in range(m):
    p = 0
    q = 0
    r = 0
    if i+1 - w2[0] >= 0:
        p = dp2[i+1 - w2[0]] + v2[0]
    if i+1 - w2[1] >= 0:
        q = dp2[i+1 - w2[1]] + v2[1]
    if i+1 - w2[2] >= 0:
        r = dp2[i+1 - w2[2]] + v2[2]
    dp2[i+1] = max(p,q,r,dp2[i])
print(m + dp2[-1])

Solution (Python):
def main():
    n = int(input())
    w1 = list(map(int,input().split()))
    w2 = list(map(int,input().split()))
    v1 = [0,0,0]
    for i in range(3):
        v1[i] = w2[i] - w1[i]
    dp1 = [0] * (n+1)
    for i in range(n):
        p = 0
        q = 0
        r = 0
        if i+1 - w1[0] >= 0:
            p = dp1[i+1 - w1[0]] + v1[0]
        if i+1 - w1[1] >= 0:
            q = dp1[i+1 - w1[1]] + v1[1]
        if i+1 - w1[2] >= 0:
            r = dp1[i+1 - w1[2]] + v1[2]
        dp1[i+1] = max(p,q,r,dp1[i])
    m = dp1[-1] + n

    v2 = [0,0,0]
    for i in range(3):
        v2[i] = w1[i] - w2[i]
    dp2 = [0] * (m+1)
    for i in range(m):
        p = 0
        q = 0
        r = 0
        if i+1 - w2[0] >= 0:
            p = dp2[i+1 - w2[0]] + v2[0]
        if i+1 - w2[1] >= 0:
            q = dp2[i+1 - w2[1]] + v2[1]
        if i+1 - w2[2] >= 0:
            r = dp2[i+1 - w2[2]] + v2[2]
        dp2[i+1] = max(p,q,r,dp2[i])
    print(m + dp2[-1])
    
if __name__ == "__main__":
    main()

Solution (Python):
def knapsack_123(values,weights,capacity):
    work = [0 for i in range(capacity+1)]
    for v,w in zip(values,weights):
        for c in range(w, capacity+1):
            work[c]=max(work[c],work[c-w]+v)
    return work[-1]
def sol(inp=input):
    n=int(inp())
    prices_a=list(map(int,inp().split()))+[1]
    prices_b=list(map(int,inp().split()))+[1]
    
    valA=knapsack_123(values=prices_b,weights=prices_a,capacity=n)
    valB=knapsack_123(values=prices_a,weights=prices_b,capacity=valA)
    return '{}'.format(valB)
print(sol())

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    n,k = LI()
    print(n%k)
    return


def B():
    n = I()
    po = LIR(n)
    if n == 1:
        print(1)
        quit()
    ans = float("inf")
    for i in range(n):
        for j in range(n):
            if i == j:continue
            x,y = po[i]
            s,t = po[j]
            p,q = s-x,t-y
            m = n
            for k in range(n):
                for l in range(n):
                    if (po[k][0]-po[l][0],po[k][1]-po[l][1]) == (p,q):m -= 1
            if m < ans:
                ans = m
    print(ans)
    return


def C():
    n = I()
    a = LI()
    a.sort()
    if a[0] >= 0:
        print(sum(a)-2*a[0])
        for i in range(1,n-1):
            print(a[0],a[i])
            a[0] -= a[i]
        print(a[n-1],a[0])
    else:
        if a[-1] < 0:
            print(2*a[-1]-sum(a))
            for i in range(n-1):
                print(a[-1],a[i])
                a[-1] -= a[i]
        else:
            m = bisect.bisect_left(a,0)
            p = n-m
            ans = -sum(a[:m])+sum(a[m:])
            print(ans)
            if p > m:
                l = a[-1]
                a = a[:-1]
                a = a[::-1]
                p -= 1
                p,m = m,p
                k = a[m]-a[m-1]
                print(a[m],a[m-1])
                for i in range(p-1):
                    print(a[m-i-2],k)
                    k = a[m-i-2]-k
                    print(a[m+i+1],k)
                    k = a[m+i+1]-k
                for i in range(m-p):
                    print(k,a[i])
                    k -= a[i]
                print(l,k)
            else:
                k = a[m]-a[m-1]
                print(a[m],a[m-1])
                for i in range(p-1):
                    print(a[m-i-2],k)
                    k = a[m-i-2]-k
                    print(a[m+i+1],k)
                    k = a[m+i+1]-k
                for i in range(m-p):
                    print(k,a[i])
                    k -= a[i]
    return


def D():
    n = I()
    a = LI()
    b = LI()
    dp = [0 for i in range(n+1)]
    for w in range(n+1):
        dp[w] = w
        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
        if x0 > x1:
            x1 = x0
        x2 = dp[w-a[2]]+b[2] if w-a[2] >= 0 else w
        if x1 > x2:
            x2 = x1
        if x2 > dp[w]:
            dp[w] = x2
    n = dp[-1]
    dp = [0 for i in range(n+1)]
    for w in range(n+1):
        dp[w] = w
        x0 = dp[w-b[0]]+a[0] if w-b[0] >= 0 else w
        x1 = dp[w-b[1]]+a[1] if w-b[1] >= 0 else w
        if x0 > x1:
            x1 = x0
        x2 = dp[w-b[2]]+a[2] if w-b[2] >= 0 else w
        if x1 > x2:
            x2 = x1
        if x2 > dp[w]:
            dp[w] = x2
    print(dp[n])
    return


def E():
    n = I()

    return


def F():
    n = I()

    return


def G():
    n = I()

    return


def H():
    n = I()

    return


def I_():
    n = I()

    return


def J():
    n = I()

    return


if __name__ == "__main__":
    D()

Solution (Python):
import sys
input=sys.stdin.readline
n=int(input())
A=tuple(map(int,input().split()))
B=tuple(map(int,input().split()))

def exchange(n,A,B):
    R=[]
    for a,b in zip(A,B):
        if a<b:
            R.append((a,b))
    if len(R)==0:
        return n
    if len(R)==1:
        a,b=R[0]
        return n+n//a*(b-a)
    if len(R)==2:
        a0,b0=R[0]; a1,b1=R[1]
        ans=n
        s=n//a0
        for i in range(s+1):
            p=b0*i
            q=n-a0*i
            r=q//a1
            ans=max(ans,p+q+r*(b1-a1))
        return ans
    a0,b0=R[0]; a1,b1=R[1]; a2,b2=R[2]
    ans=n
    s=n//a0
    for i in range(s+1):
        p=n-a0*i
        t=p//a1
        for j in range(t+1):
            q=p-a1*j
            ans=max(ans,b0*i+b1*j+q+q//a2*(b2-a2))
    return ans

tmp=exchange(n,A,B)
print(exchange(tmp,B,A))

Solution (Python):
def solve(n, ga, sa, ba, gb, sb, bb):
    gd, sd, bd = gb - ga, sb - sa, bb - ba

    if bd <= 0:
        if sd <= 0:
            if gd <= 0:
                return n
            return n + n // ga * gd
        if gd <= 0:
            return n + n // sa * sd
        return max(n + g * gd + (n - g * ga) // sa * sd for g in range(n // ga + 1))
    if sd <= 0:
        if gd <= 0:
            return n + n // ba * bd
        return max(n + g * gd + (n - g * ga) // ba * bd for g in range(n // ga + 1))
    if gd <= 0:
        return max(n + s * sd + (n - s * sa) // ba * bd for s in range(n // sa + 1))
    (_, ga, gd), (_, sa, sd), (_, ba, bd) = \
        sorted([(gb / ga, -ga, gd), (sb / sa, -sa, sd), (bb / ba, -ba, bd)], reverse=True)
    ga, sa, ba = -ga, -sa, -ba
    sga = 0 if n // ga < 1000 else n // ga // 2
    return max(n + g * gd + s * sd + (n - g * ga - s * sa) // ba * bd
               for g in range(sga, n // ga + 1)
               for s in range((n - g * ga) // sa + 1))


n = int(input())
ga, sa, ba = list(map(int, input().split()))
gb, sb, bb = list(map(int, input().split()))
n2 = solve(n, ga, sa, ba, gb, sb, bb)
n3 = solve(n2, gb, sb, bb, ga, sa, ba)
print(n3)

Solution (Python):
import sys
range = xrange
input = raw_input
 
def main():
  n = int(input())
  A = [int(x) for x in input().split()]
  B = [int(x) for x in input().split()]
 
  DP = [0]*(n+1)
  DP[n] = n
  for j in range(3):
      sell = A[j]
      extra = B[j] - sell
      if extra > 0:
          for i in reversed(range(len(DP) - sell)):
              DP[i] = max(DP[i], DP[i + sell] + extra)
  n = max(DP)
  A,B = B,A
 
  DP = [0]*(n+1)
  DP[n] = n
  for j in range(3):
      sell = A[j]
      extra = B[j] - sell
      if extra > 0:
          for i in reversed(range(len(DP) - sell)):
              DP[i] = max(DP[i], DP[i + sell] + extra) 
  print(max(DP))
 
main()

Solution (Python):
import sys
range = xrange
input = raw_input
 
def main():
  n = int(input())
  A = [int(x) for x in input().split()]
  B = [int(x) for x in input().split()]
 
  DP = [0]*(n+1)
  DP[n] = n
  for j in range(3):
      sell = A[j]
      extra = B[j] - sell
      if extra > 0:
          for i in reversed(range(sell,len(DP))):
              DP[i - sell] = max(DP[i - sell], DP[i] + extra)
  n = max(DP)
  A,B = B,A
 
  DP = [0]*(n+1)
  DP[n] = n
  for j in range(3):
      sell = A[j]
      extra = B[j] - sell
      if extra > 0:
          for i in reversed(range(sell,len(DP))):
              DP[i - sell] = max(DP[i - sell], DP[i] + extra) 
  print(max(DP))
 
main()

Solution (Python):
import sys
range = xrange
input = raw_input
 
def main():
  n = int(input())
  A = [int(x) for x in input().split()]
  B = [int(x) for x in input().split()]

  DP = [0]*(n+1)
  DP[n] = n
  for j in range(3):
      sell = A[j]
      extra = B[j] - sell
      if extra > 0:
          for i in reversed(range(sell,len(DP))):
              if DP[i] + extra > DP[i - sell]:
                  DP[i-sell] = DP[i] + extra
  n = max(DP)
  A,B = B,A

  DP = [0]*(n+1)
  DP[n] = n
  for j in range(3):
      sell = A[j]
      extra = B[j] - sell
      if extra > 0:
          for i in reversed(range(sell,len(DP))):
              if DP[i] + extra > DP[i - sell]:
                  DP[i-sell] = DP[i] + extra

  print(max(DP))

main()

Solution (Python):
import sys
range = xrange
input = 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)
DP[n] = n
for j in range(3):
    sell = A[j]
    extra = B[j] - sell
    if extra > 0:
        for i in reversed(range(sell,len(DP))):
            if DP[i] + extra > DP[i - sell]:
                DP[i-sell] = DP[i] + extra
n = max(DP)
A,B = B,A
 
DP = [0]*(n+1)
DP[n] = n
for j in range(3):
    sell = A[j]
    extra = B[j] - sell
    if extra > 0:
        for i in reversed(range(sell,len(DP))):
            if DP[i] + extra > DP[i - sell]:
                DP[i-sell] = DP[i] + extra
 
print(max(DP))

Solution (Python):
def li():
    return list(map(int, input().split()))


if __name__ == "__main__":
    n = int(input())
    [ga, sa, ba] = li()
    [gb, sb, bb] = li()

    
    dp = [0] * (n + 1)

    for i in range(1, len(dp)):
        g_trade = dp[i - ga] + gb if i - ga >= 0 else dp[i - 1]
        s_trade = dp[i - sa] + sb if i - sa >= 0 else dp[i - 1]
        b_trade = dp[i - ba] + bb if i - ba >= 0 else dp[i - 1]
        dp[i] = max(dp[i - 1] + 1, g_trade, s_trade, b_trade)
    
    a_result = dp[-1]

    
    dp = [0] * (a_result + 1)

    for i in range(1, len(dp)):
        g_trade = dp[i - gb] + ga if i - gb >= 0 else dp[i - 1]
        s_trade = dp[i - sb] + sa if i - sb >= 0 else dp[i - 1]
        b_trade = dp[i - bb] + ba if i - bb >= 0 else dp[i - 1]
        dp[i] = max(dp[i - 1] + 1, g_trade, s_trade, b_trade)
    
    print(dp[-1])

Solution (Python):
def li():
    return list(map(int, input().split()))


if __name__ == "__main__":
    n = int(input())
    [ga, sa, ba] = li()
    [gb, sb, bb] = li()

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    dp = [0] * (n + 1)

    for i in range(1, len(dp)):
        g_trade = dp[i - ga] + gb if i - ga >= 0 else dp[i - 1]
        s_trade = dp[i - sa] + sb if i - sa >= 0 else dp[i - 1]
        b_trade = dp[i - ba] + bb if i - ba >= 0 else dp[i - 1]
        dp[i] = max(dp[i - 1] + 1, g_trade, s_trade, b_trade)
    
    a_result = dp[-1]

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    dp = [0] * (a_result + 1)

    for i in range(1, len(dp)):
        g_trade = dp[i - gb] + ga if i - gb >= 0 else dp[i - 1]
        s_trade = dp[i - sb] + sa if i - sb >= 0 else dp[i - 1]
        b_trade = dp[i - bb] + ba if i - bb >= 0 else dp[i - 1]
        dp[i] = max(dp[i - 1] + 1, g_trade, s_trade, b_trade)
    
    print(dp[-1])

Solution (Python):
from itertools import*
from math import*
from collections import*
from heapq import*
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf = float("inf")
mod = 10**9+7
from functools import reduce
import sys
sys.setrecursionlimit(10**7)


def f(N,A,B):
    dp = [0]*(N+1)
    for i in range(N+1):
        x = i
        for j in range(3):
            if i >= A[j]:
                y = dp[i-A[j]]+B[j]
                if x < y:
                    x = y
        dp[i] = x
    return dp[N]
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
print(f(f(N,A,B),B,A))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
dp = [0]*(n+1)
fir = []
sec = []
for i in range(3):
  if a[i]>b[i]:
    sec.append((b[i],a[i]))
  else:
    fir.append((a[i],b[i]))
for i in range(1,n+1):
  dp[i] = i
  for x,y in fir:
    if i-x >= 0:
      dp[i] = max(dp[i],dp[i-x]+y)
n = dp[n]
dp = [0]*(n+1)
for i in range(1,n+1):
  dp[i] = i
  for x,y in sec:
    if i-x >= 0:
      dp[i] = max(dp[i],dp[i-x]+y)
print(dp[n])

Solution (Python):
N = int(input())

ga, sa, ba = list(map(int, input().split()))
gb, sb, bb = list(map(int, input().split()))










vmax = 0
for g in range(0,N + 1, ga):
    for s in range(0,N + 1 - g, sa):
        v = (g // ga) * gb + (s // sa) * sb
        amari = N - g - s
        if amari < 0:
            break
        if ba < bb:
            v += amari // ba * bb
            amari %= ba
        v += amari

        vmax = max(vmax, v)

D = vmax
vmax = D

if ba < bb:
    tmp = sa,sb
    sa,sb = ba,bb
    ba,bb = tmp

for g in range(0,D + 1, gb):
    if ga < gb and g > 0:
        break
    for s in range(0, D + 1 - g, sb):
        if sa < sb and s > 0:
            break
        v = (g // gb) * ga + (s // sb) * sa
        amari = D - g - s
        if amari < 0:
            break
        if ba > bb:
            v += amari // bb * ba
            amari %= bb
        v += amari

        vmax = max(vmax, v)

print(vmax)

Solution (Python):
import sys

from sys import setrecursionlimit
setrecursionlimit(10**9)

def inpl(): return list(map(int, input().split()))
N = int(input())
GA = inpl()
GB = inpl()

typ = [GA[i] <= GB[i] for i in range(3)]

if sum(typ) == 3:
    ans = 0
    for g in range(N//GA[0] + 1):
        D = N - GA[0]*g
        for s in range(D//GA[1] + 1):
            b, D2 = divmod(D - GA[1]*s, GA[2])
            ans = max(ans, D2 + g*GB[0] + s*GB[1] + b*GB[2])

elif sum(typ) == 2:
    ans = 0
    x, y = [i for i in range(3) if typ[i]]
    z = [i for i in range(3) if not typ[i]][0]
    for a in range(N//GA[x] + 1):
        b, D = divmod(N - GA[x]*a, GA[y])
        c, D2 = divmod(D + GB[x]*a + GB[y]*b, GB[z])
        ans = max(ans, D2 + c*GA[z])

elif sum(typ) == 1:
    ans = 0
    x, y = [i for i in range(3) if not typ[i]]
    z = [i for i in range(3) if typ[i]][0]
    c, D = divmod(N, GA[z])
    D += GB[z]*c

    for a in range(D//GB[x] + 1):
        b, D2 = divmod(D-GB[x]*a, GB[y])
        ans = max(ans, D2 + GA[x]*a + GA[y]*b)


else:
    ans = 0
    for g in range(N//GB[0] + 1):
        D = N - GB[0]*g
        for s in range(D//GB[1] + 1):
            b, D2 = divmod(D - GB[1]*s, GB[2])
            ans = max(ans, D2 + g*GA[0] + s*GA[1] + b*GA[2])

print(ans)

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

Achange = []
Bchange = []

for i in range(3):
  if A[i] > B[i]:
    Bchange.append(i)
  elif A[i] < B[i]:
    Achange.append(i)




Dmax1 = N
if Achange:
  if len(Achange)==3:
    i = Achange[0]
    j = Achange[1]
    k = Achange[2]
    for x in range(N+1):
      for y in range(N+1):
        if A[i]*x+A[j]*y <= N:
          Dmax1 = max(Dmax1, B[i]*x + B[j]*y + (N-A[i]*x-A[j]*y)%A[k] + (N-A[i]*x-A[j]*y)//A[k] * B[k])
    
  if len(Achange)==2:
    i = Achange[0]
    j = Achange[1]
    for x in range(N+1):
      if A[i]*x <= N:
          Dmax1 = max(Dmax1, B[i]*x + (N-A[i]*x)%A[j] + (N-A[i]*x)//A[j] * B[j])
    
  if len(Achange)==1:
    i = Achange[0]
    Dmax1 = max(Dmax1, N%A[i] + (N//A[i] * B[i]))
    



Achange = Bchange
A,B=B,A
N = Dmax1


if Achange:
  if len(Achange)==3:
    i = Achange[0]
    j = Achange[1]
    k = Achange[2]
    for x in range(N+1):
      for y in range(N+1):
        if A[i]*x+A[j]*y <= N:
          Dmax1 = max(Dmax1, B[i]*x + B[j]*y + (N-A[i]*x-A[j]*y)%A[k] + (N-A[i]*x-A[j]*y)//A[k] * B[k])
    
  if len(Achange)==2:
    i = Achange[0]
    j = Achange[1]
    for x in range(N+1):
      if A[i]*x <= N:
          Dmax1 = max(Dmax1, B[i]*x + (N-A[i]*x)%A[j] + (N-A[i]*x)//A[j] * B[j])
    
  if len(Achange)==1:
    i = Achange[0]
    Dmax1 = max(Dmax1, N%A[i] + (N//A[i] * B[i]))
    
print(Dmax1)

Solution (Python):
import sys

from sys import setrecursionlimit
setrecursionlimit(10**9)

def inpl(): return list(map(int, input().split()))
N = int(input())
GA = inpl()
GB = inpl()

typ = [GA[i] <= GB[i] for i in range(3)]

if sum(typ) == 3:
    ans = 0
    for g in range(N//GA[0] + 1):
        D = N - GA[0]*g
        for s in range(D//GA[1] + 1):
            b, D2 = divmod(D - GA[1]*s, GA[2])
            ans = max(ans, D2 + g*GB[0] + s*GB[1] + b*GB[2])

elif sum(typ) == 2:
    ans = 0
    x, y = [i for i in range(3) if typ[i]]
    z = [i for i in range(3) if not typ[i]][0]
    for a in range(N//GA[x] + 1):
        b, D = divmod(N - GA[x]*a, GA[y])
        c, D2 = divmod(D + GB[x]*a + GB[y]*b, GB[z])
        ans = max(ans, D2 + c*GA[z])

elif sum(typ) == 1:
    ans = 0
    x, y = [i for i in range(3) if not typ[i]]
    z = [i for i in range(3) if typ[i]][0]
    c, D = divmod(N, GA[z])
    D += GB[z]*c

    for a in range(D//GB[x] + 1):
        b, D2 = divmod(D-GB[x]*a, GB[y])
        ans = max(ans, D2 + GA[x]*a + GA[y]*b)

    for b in range(D//GB[y] + 1):
        a, D2 = divmod(D-GB[y]*b, GB[x])
        ans = max(ans, D2 + GA[x]*a + GA[y]*b)

else:
    ans = 0
    for g in range(N//GB[0] + 1):
        D = N - GB[0]*g
        for s in range(D//GB[1] + 1):
            b, D2 = divmod(D - GB[1]*s, GB[2])
            ans = max(ans, D2 + g*GA[0] + s*GA[1] + b*GA[2])

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def YesNo(x): return 'Yes' if x else 'No'
def LI(): return [int(x) for x in sys.stdin.readline().split()]
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 II(): return int(sys.stdin.readline())
def SI(): return input()

def get(n, rates):
    rates.sort()
    r0, a0, b0 = rates[0]
    r1, a1, b1 = rates[1]
    r2, a2, b2 = rates[2]
    if r0 >= 1:
        return n
    if r1 >= 1 or a0 == a1 == a2 or r0 == r1 == r2:
        return (n % a0) + (n // a0) * b0
    if r2 >= 1 or r1 == r2 or a1 == a2 or r0 == r1 or a0 == a1 or a0 == a2:
        if r0 == r1 or a0 == a1:
            r1, a1, b1 = r2, a2, b2
        ret = n
        for m0 in range(n // a0 + 1):
            rem = n - m0 * a0
            d = rem % a1
            tmp = d + m0 * b0 + (rem // a1) * b1
            ret = max(ret, tmp)
        return ret
    ret = n
    for m0 in range(n // a0 + 1):
        d0 = m0 * b0
        rem = n - m0 * a0
        for m1 in range(rem // a1 + 1):
            n2 = rem - m1 * a1
            d = n2 % a2
            tmp = d + d0 + m1 * b1 + (n2 // a2) * b2
            ret = max(ret, tmp)
    return ret

def main():
    N = II()
    ga, sa, ba = LI()
    gb, sb, bb = LI()
    rates = [[ga / gb, ga, gb], [sa / sb, sa, sb], [ba / bb, ba, bb]]
    rates2 = [[gb / ga, gb, ga], [sb / sa, sb, sa], [bb / ba, bb, ba]]
    ans = N
    ans = get(ans, rates)
    ans = get(ans, rates2)
    return ans

print(main())

Solution (Python):
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)

Solution (Python):
N = int(input())
GA, SA, BA = map(int, input().split())
GB, SB, BB = map(int, input().split())
rate1 = [(a, b - a) for a, b in ((GA, GB), (SA, SB), (BA, BB)) if a < b]
rate2 = [(b, a - b) for a, b in ((GA, GB), (SA, SB), (BA, BB)) if a > b]


def visit(n, rate):
    if len(rate) == 1:
        m, d = rate[0]
        n += (n // m) * d
    elif len(rate) == 2:
        m1, d1 = rate[0]
        m2, d2 = rate[1]
        ans = 0
        for i in range(n // m1 + 1):
            j = (n - i * m1) // m2
            ans = max(ans, d1 * i + d2 * j)
        n += ans
    elif len(rate) == 3:
        m1, d1 = rate[0]
        m2, d2 = rate[1]
        m3, d3 = rate[2]
        ans = 0
        for i in range(n // m1 + 1):
            for j in range((n - i * m1) // m2 + 1):
                k = (n - i * m1 - j * m2) // m3
                ans = max(ans, d1 * i + d2 * j + d3 * k)
        n += ans
    return n


N = visit(N, rate1)
N = visit(N, rate2)
print(N)