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):
N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

cur = N
dp = list(range(cur + 1))
for i in range(cur + 1):
    if i >= As[0]:
        if dp[i] < dp[i - As[0]] + Bs[0]:
            dp[i] = dp[i - As[0]] + Bs[0]
    if i >= As[1]:
        if dp[i] < dp[i - As[1]] + Bs[1]:
            dp[i] = dp[i - As[1]] + Bs[1]
    if i >= As[2]:
        if dp[i] < dp[i - As[2]] + Bs[2]:
            dp[i] = dp[i - As[2]] + Bs[2]

cur = dp[cur]
dp = list(range(cur + 1))
As, Bs = Bs, As
for i in range(cur + 1):
    if i >= As[0]:
        if dp[i] < dp[i - As[0]] + Bs[0]:
            dp[i] = dp[i - As[0]] + Bs[0]
    if i >= As[1]:
        if dp[i] < dp[i - As[1]] + Bs[1]:
            dp[i] = dp[i - As[1]] + Bs[1]
    if i >= As[2]:
        if dp[i] < dp[i - As[2]] + Bs[2]:
            dp[i] = dp[i - As[2]] + Bs[2]


print(dp[cur])

Solution (Python):
def main(n,A,B):
  AB=[[a,b] for a,b in zip(A,B)]
  dp=[0]*(n+1)
  dp[0]=n
  
  for a,b in AB:
    
    ddp=[0]*(n+1)
    ddp[0]=n
    for i in range(n+1):
      ddp[i]=max(dp[i],ddp[i-a]+b-a if i-a>=0 else 0)
    dp=ddp
  n=max(dp)
  dp=[0]*(n+1)
  dp[0]=n
  for a,b in AB:
    if b>a:continue
    ddp=[0]*(n+1)
    ddp[0]=n
    for i in range(n+1):
      ddp[i]=max(dp[i],ddp[i-b]+a-b if i-b>=0 else 0)
    dp=ddp
  return max(dp)

n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
print(main(n,A,B))

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

t1 = []
t2 = []
if ga < gb:
    t1.append((ga,gb))
else:
    t2.append((gb,ga))
if sa < sb:
    t1.append((sa,sb))
else:
    t2.append((sb,sa))
if ba < bb:
    t1.append((ba,bb))
else:
    t2.append((bb,ba))

dp = [0] * (N+1)
N1 = N
for i in range(1,N+1):
    for c,r in t1:
        if c <= i:
            dp[i] = max(dp[i], dp[i-c] + r)
    N1 = max(N1, N-i+dp[i])

ans = N1
dp = [0] * (N1+1)
for i in range(1,N1+1):
    for c,r in t2:
        if c <= i:
            dp[i] = max(dp[i], dp[i-c] + r)
    ans = max(ans, N1-i+dp[i])

print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
dp = [_ for _ in range(N+1)]
for i in range(N+1):
  donguri = dp[i]
  if i >= A[0]:
    gold = dp[i-A[0]]+B[0]
  else:
    gold = 0
  if i >= A[1]:
    silver = dp[i-A[1]]+B[1]
  else:
    silver = 0
  if i >= A[2]:
    bronze = dp[i-A[2]]+B[2]
  else:
    bronze = 0
  dp[i] = max(donguri,gold,silver,bronze)
M = dp[N]

dp = [_ for _ in range(M+1)]
for i in range(M+1):
  donguri = dp[i]
  if i >= B[0]:
    gold = dp[i-B[0]]+A[0]
  else:
    gold = 0
  if i >= B[1]:
    silver = dp[i-B[1]]+A[1]
  else:
    silver = 0
  if i >= B[2]:
    bronze = dp[i-B[2]]+A[2]
  else:
    bronze = 0
  dp[i] = max(donguri,gold,silver,bronze)
ans = dp[M]
print(ans)

Solution (Python):


def main():
    N = int(input())
    gsba = list(map(int, input().split()))
    gsbb = list(map(int, input().split()))
    
    
    w = gsba
    v = [gsbb[i]-gsba[i] for i in range(3)]
    dp = [0]*(N+1)

    for i in range(3):
        for j in range(N+1):
            if j >= w[i]:
                dp[j] = max(dp[j],dp[j-w[i]]+v[i])

    M = dp[-1] + N
    w = gsbb
    v = [gsba[i]-gsbb[i] for i in range(3)]
    dp = [0]*(M+1)

    for i in range(3):
        for j in range(M+1):
            if j >= w[i]:
                dp[j] = max(dp[j],dp[j-w[i]]+v[i])

    print(dp[-1] + M)

if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
a = tuple(map(int, input().split()))
b = tuple(map(int, input().split()))
dp = [0] * (N + 1)
res = 0
for i in range(N, -1, -1):
  for j in range(3):
    if a[j] < b[j] and (i - a[j] >= 0): dp[i - a[j]] = max(dp[i - a[j]], dp[i] + b[j])
  res = max(res, i + dp[i])

d = []
for i in range(3):
  if a[i] > b[i]: d.append((b[i], a[i]))
if len(d) == 0: print(res)
elif len(d) == 1: print(res // d[0][0] * d[0][1] + res % d[0][0])
elif len(d) == 2:
  dp = [0] * (res + 1)
  rres = 0
  for i in range(res, -1, -1):
    for x, y in d:
      if i - x >= 0: dp[i - x] = max(dp[i - x], dp[i] + y)
    rres = max(rres, i + dp[i])
  print(rres)
else:
  dp = [0] * (res + 1)
  rres = 0
  for i in range(res, -1, -1):
    for j in range(3):
      if a[j] > b[j] and (i - b[j] >= 0): dp[i - b[j]] = max(dp[i - b[j]], dp[i] + a[j])
    rres = max(rres, i + dp[i])
  print(rres)

Solution (Python):
mod = 1000000007
eps = 10**-9
inf = 10**9


def main():
    import sys
    input = sys.stdin.readline

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

    dp = [[-inf] * (N+1) for _ in range(4)]
    dp[0][N] = N
    for i in range(3):
        for j in range(N, -1, -1):
            dp[i+1][j] = max(dp[i+1][j], dp[i][j])
            if j - A[i] >= 0:
                dp[i+1][j-A[i]] = max(dp[i+1][j-A[i]], dp[i+1][j] + B[i] - A[i])
    N = max(dp[-1])
    dp = [[-inf] * (N + 1) for _ in range(4)]
    dp[0][N] = N
    for i in range(3):
        for j in range(N, -1, -1):
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
            if j - B[i] >= 0:
                dp[i + 1][j - B[i]] = max(dp[i + 1][j - B[i]], dp[i+1][j] + A[i] - B[i])
    print(max(dp[-1]))


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import bisect
import math
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
from operator import itemgetter

def calc(n,p,q,r,s,t,u):
    dp=[-1]*(n+1)
    for i in range(n+1):
        dp[i]=i
    dp[0]=0
    for i in range(1,n+1):
        if i>=p and dp[i-p]>=0:
            if dp[i]<dp[i-p]+s:
                dp[i]=dp[i-p]+s
        if i>=q and dp[i-q]>=0:
            if dp[i]<dp[i-q]+t:
                dp[i]=dp[i-q]+t
        if i>=r and dp[i-r]>=0:
            if dp[i]<dp[i-r]+u:
                dp[i]=dp[i-r]+u
    
    return max(dp)

n=int(input())
p,q,r=map(int,readline().split())
s,t,u=map(int,readline().split())

res=calc(n,p,q,r,s,t,u)
ans=calc(res,s,t,u,p,q,r)
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

n = int(input())
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))
def f(n):
  dp = list(range(n+1))
  for a, b in zip(A, B):
    if b <= a:
      continue
    for i in range(n+1):
      if i-a >= 0:
        dp[i] = max(dp[i], dp[i-a]+b)
  return dp[n]
m = f(n)
A, B = B, A
ans = f(m)
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

n = int(input())
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))
def f(n):
  dp = list(range(n+1))
  for a, b in zip(A, B):
    if b <= a:
      continue
    for i in range(n+1):
      if i-a >= 0:
        dp[i] = max(dp[i], dp[i-a]+b)
  return max(dp)
m = f(n)
A, B = B, A
ans = f(m)
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

n = int(input())
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))
def f(n):
  dp = list(range(n+1))
  for a, b in zip(A, B):
    for i in range(n+1):
      if i-a >= 0:
        dp[i] = max(dp[i], dp[i-a]+b)
  return max(dp)
m = f(n)
A, B = B, A
ans = f(m)
print(ans)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
DP=[[0]*(N+1) for i in range(4)]
for i in range(3):
  for j in range(N+1):
    DP[i+1][j]=max(DP[i][j],DP[i+1][j])
    if j+A[i]<=N:
      DP[i][j+A[i]]=max(DP[i][j+A[i]],DP[i][j]+B[i])
for i in range(N+1):
  N=max(N,i+DP[3][-i-1])
DP=[[0]*(N+1) for i in range(4)]
for i in range(3):
  for j in range(N+1):
    DP[i+1][j]=max(DP[i][j],DP[i+1][j])
    if j+B[i]<=N:
      DP[i][j+B[i]]=max(DP[i][j+B[i]],DP[i][j]+A[i])
for i in range(N+1):
  N=max(N,i+DP[3][-i-1])
print(N)

Solution (Python):
import math

import queue
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n = int(ipt())
    ga,sa,ba = map(int,ipt().split())
    gb,sb,bb = map(int,ipt().split())

    dp = [0]*(5005)
    for i in range(n+1):
        dp[i] = dp[i-1]
        if i >= ga and dp[i] < (gb-ga)+dp[i-ga]:
            dp[i] = gb-ga+dp[i-ga]
        if i >= sa and dp[i] < (sb-sa)+dp[i-sa]:
            dp[i] = sb-sa+dp[i-sa]
        if i >= ba and dp[i] < (bb-ba)+dp[i-ba]:
            dp[i] = bb-ba+dp[i-ba]
    ni = n+dp[n]

    dp = [0]*max(5005,ni+5)
    for i in range(ni+1):
        dp[i] = dp[i-1]
        if i >= gb and dp[i] < (ga-gb)+dp[i-gb]:
            dp[i] = ga-gb+dp[i-gb]
        if i >= sb and dp[i] < (sa-sb)+dp[i-sb]:
            dp[i] = sa-sb+dp[i-sb]
        if i >= bb and dp[i] < (ba-bb)+dp[i-bb]:
            dp[i] = ba-bb+dp[i-bb]
    print(ni+dp[ni])

    return None

if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


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

def sub1(n, ga, gb):
    return n - (n//ga)*ga + (n//ga)*gb
def sub2(n, ga, gb, sa, sb):
    ans = -1
    for i in range((n//sa)+1):
        val = sub1(n - (i*sa), ga, gb) + (i*sb)
        ans = max(ans, val)
    return ans
def sub3(n, ga, gb, sa, sb, ba, bb):
    ans = -1
    for i in range((n//ba)+1):
        val = sub2(n - (i*ba), ga, gb, sa, sb) + (i*bb)
        ans = max(ans, val)
    return ans

arg1 = []
arg2 = []
for a,b in [(ga,gb), (sa,sb), (ba,bb)]:
    if a<b:
        arg1.extend([a,b])
    elif a>b:
        arg2.extend([b,a])
d = {
    0: lambda n: n,
    1: sub1,
    2: sub2,
    3: sub3
}
nn = d[len(arg1)//2](n, *arg1)
nnn = d[len(arg2)//2](nn, *arg2)
print(nnn)

Solution (Python):
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    A=LI()
    B=LI()
    
    dp=[0]*(N+1)
    
    
    v=[]
    for i in range(3):
        vv=B[i]-A[i]
        v.append(vv)
        
    for i in range(3):
        for j in range(A[i],N+1,1):
            dp[j]=max(dp[j-A[i]]+v[i],dp[j])
            
    N2=N
    for j in range(N+1):
        temp=N+dp[j]
        N2=max(N2,temp)

        
    
    dp2=[0]*(N2+1)
    for i in range(3):
        for j in range(B[i],N2+1,1):
            dp2[j]=max(dp2[j-B[i]]-v[i],dp2[j])
            
    ans=N2
    for j in range(N2+1):
        temp=N2+dp2[j]
        ans=max(ans,temp)
        
    print(ans)
            
  
            

main()

Solution (Python):

import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""
dp, 取引所Bで一度全てacornにする
取引所Aでの通貨の値段を重さとする
"""

N = ir()
A = lr()
B = lr()
dp = [x for x in range(N+1)]
for a, b in zip(A, B):
    for x in range(N):
        if x + a <= N:
            dp[x+a] = max(dp[x+a], dp[x] + b)

half_way = max(dp)
dp = [x for x in range(half_way+1)]
A, B = B, A
for a, b in zip(A, B):
    for x in range(half_way):
        if x + a <= half_way:
            dp[x+a] = max(dp[x+a], dp[x] + b)

answer = max(dp)
print(answer)

Solution (Python):
n = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
dp = [0] * (n + 1)
for i in range(n):
    if i + ga <= n:
        dp[i + ga] = max(dp[i + ga], dp[i] + gb)
    if i + sa <= n:
        dp[i + sa] = max(dp[i + sa], dp[i] + sb)
    if i + ba <= n:
        dp[i + ba] = max(dp[i + ba], dp[i] + bb)
ans = 0
for i in range(n + 1):
    ans = max(ans, dp[i] + n - i)
p = min(25000024, ans)
dp = [0] * (p + 1)
for i in range(p):
    if i + gb <= p:
        dp[i + gb] = max(dp[i + gb], dp[i] + ga)
    if i + sb <= p:
        dp[i + sb] = max(dp[i + sb], dp[i] + sa)
    if i + bb <= p:
        dp[i + bb] = max(dp[i + bb], dp[i] + ba)
res = 0
for i in range(p + 1):
    res = max(res, dp[i] + ans - i)
print(res)

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

Solution (Python):
def solve(n, a, b):
  ab = [(ai, bi) for ai, bi in zip(a, b) if ai < bi]
  if len(ab) == 0:
    return n
  elif len(ab) == 1:
    a0, b0 = ab[0]
    return (n//a0)*b0 + n%a0
  elif len(ab) == 2:
    (a0, b0), (a1, b1) = ab
    if b0/a0 < b1/a1:
      a0, b0, a1, b1 = a1, b1, a0, b0
    return max(((n-a1*i)//a0)*b0 + (n-a1*i)%a0 + i*b1 for i in range(a0))
  else:
    dp = [j for j in range(n+1)]
    for ai, bi in ab:
      for j in range(ai, n+1):
        dp[j] = max(dp[j], dp[j-ai]+bi)
    return dp[-1]

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

N = solve(N, A, B)
N = solve(N, B, A)
print(N)

Solution (Python):
import sys
input = sys.stdin.readline

N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
INF = 10**18
A = [INF] * 3
B = [INF] * 3
values = [abs(ga - gb), abs(sa - sb), abs(ba - bb)]
if ga < gb:
    A[0] = ga
elif ga > gb:
    B[0] = gb
if sa < sb:
    A[1] = sa
elif sa > sb:
    B[1] = sb
if ba < bb:
    A[2] = ba
elif ba > bb:
    B[2] = bb

def calc(X):
    dp = [0] * (N+1)
    dp[0] = 0
    for x, v in zip(X, values):
        for i in range(N+1-x):
            dp[i+x] = max(dp[i+x], dp[i] + v)
    return dp[-1]

N += calc(A)
N += calc(B)
print(N)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    N = int(rl())
    ga, sa, ba = map(int, rl().split())
    gb, sb, bb = map(int, rl().split())
    
    dp1 = [0] * 30000000
    dp1[0] = N
    M = N
    for i in range(N + 1):
        dp1[i + ga] = max(dp1[i + ga], dp1[i] - ga + gb)
        dp1[i + sa] = max(dp1[i + sa], dp1[i] - sa + sb)
        dp1[i + ba] = max(dp1[i + ba], dp1[i] - ba + bb)
        M = max(M, dp1[i])
    
    dp2 = [0] * 30000000
    dp2[0] = M
    ans = M
    for i in range(M + 1):
        dp2[i + gb] = max(dp2[i + gb], dp2[i] - gb + ga)
        dp2[i + sb] = max(dp2[i + sb], dp2[i] - sb + sa)
        dp2[i + bb] = max(dp2[i + bb], dp2[i] - bb + ba)
        ans = max(ans, dp2[i])
    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):
import sys
input = sys.stdin.buffer.readline

def main():
    N = int(input())

    a = list(map(int,input().split()))
    b = list(map(int,input().split()))

    dp = [0]*(N+1)
	
    for ua,ub in zip(a,b):
        for i in range(N):
            if i+ua > N:
                continue
            dp[i+ua] = max(dp[i+ua],dp[i]+(ub-ua))
    
    N += dp[-1]
    dp = [0]*(N+1)

    for ua,ub in zip(a,b):
        for i in range(N):
            if i+ub > N:
                continue
            dp[i+ub] = max(dp[i+ub],dp[i]+(ua-ub))
            
    print(N+dp[-1])
    
if __name__ == "__main__":
	main()

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

check=(gb>ga)+(sb>sa)+(bb>ba)
if check==3:
    donguri=N

    inc=0
    for i in range(0,donguri//ga+1):
        x=(ga<gb)*i
        for j in range(0,donguri//sa+1):
            y=(sa<sb)*j
            if donguri>=x*ga+y*sa:
                z=((donguri-x*ga-y*sa)//ba)*(bb>ba)
                inc=max(inc,x*(gb-ga)+y*(sb-sa)+z*(bb-ba))

    donguri+=inc
    print(donguri)
elif check==0:
    donguri=N
    inc=0
    for i in range(0,donguri//gb+1):
        x=(ga>gb)*i
        for j in range(0,donguri//sb+1):
            y=(sa>sb)*j
            if donguri>=x*gb+y*sb:
                z=((donguri-x*gb-y*sb)//bb)*(ba>bb)
                inc=max(inc,-x*(gb-ga)-y*(sb-sa)-z*(bb-ba))

    donguri+=inc
    print(donguri)

elif check==2:
    donguri=N
    inc=0
    for i in range(0,donguri//ga+1):
        x=(ga<gb)*i
        for j in range(0,donguri//sa+1):
            y=(sa<sb)*j
            if donguri>=x*ga+y*sa:
                z=((donguri-x*ga-y*sa)//ba)*(bb>ba)
                inc=max(inc,x*(gb-ga)+y*(sb-sa)+z*(bb-ba))

    donguri+=inc
    if ga>gb:
        plus=(donguri//gb)*(ga-gb)
        print(donguri+plus)
    elif sa>sb:
        plus=(donguri//sb)*(sa-sb)
        print(donguri+plus)
    elif ba>bb:
        plus=(donguri//bb)*(ba-bb)
        print(donguri+plus)
    else:
        print(donguri)

else:
    donguri=N
    inc=0
    if ga<gb:
        plus=(donguri//ga)*(gb-ga)
        donguri+=plus
        if sa>sb:
            for i in range(0,donguri//sb+1):
                j=(donguri-i*sb)//bb
                inc=max(inc,i*(sa-sb)+j*(ba-bb))
            print(donguri+inc)
        else:
            inc=(donguri//bb)*(ba-bb)
            print(donguri+inc)

    elif sa<sb:
        plus=(donguri//sa)*(sb-sa)
        donguri+=plus
        if ga>gb:
            for i in range(0,donguri//gb+1):
                j=(donguri-i*gb)//bb
                inc=max(inc,i*(ga-gb)+j*(ba-bb))
            print(donguri+inc)
        else:
            inc=(donguri//bb)*(ba-bb)
            print(donguri+inc)
    else:
        plus=(donguri//ba)*(bb-ba)
        donguri+=plus
        if ga>gb:
            for i in range(0,donguri//gb+1):
                j=(donguri-i*gb)//sb
                inc=max(inc,i*(ga-gb)+j*(sa-sb))
            print(donguri+inc)
        else:
            inc=(donguri//sb)*(sa-sb)
            print(donguri+inc)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
change_a = [i for i in range(3) if a[i] < b[i]]
change_b = [i for i in range(3) if a[i] > b[i]]
if len(change_a) == 0:
  if len(change_b) == 0:
    print(n)
  elif len(change_b) == 1:
    i = n // b[change_b[0]]
    n = n % b[change_b[0]]
    n += i * a[change_b[0]]
    print(n)
  elif len(change_b) == 2:
    tmp = n
    for i in range(5001):
      m = n
      m -= i * b[change_b[0]]
      if m < 0:
        break
      j = m // b[change_b[1]]
      m = m % b[change_b[1]]
      m += i * a[change_b[0]] + j * a[change_b[1]]
      tmp = max(tmp, m)
    print(tmp)
  else:      
    tmp = n
    for i in range(5001):
      for j in range(5001):
        m = n
        m -= i * b[change_b[0]]
        if m < 0:
          break
        m -= j * b[change_b[1]]
        if m < 0:
          break
        k = m // b[change_b[2]]
        m = m % b[change_b[2]]
        m += i * a[change_b[0]] + j * a[change_b[1]] + k * a[change_b[2]]
        tmp = max(tmp, m)
    print(tmp)
elif len(change_a) == 1:
  i = n // a[change_a[0]]
  n = n % a[change_a[0]]
  n += i * b[change_a[0]]
  if len(change_b) == 0:
    print(n)
  elif len(change_b) == 1:
    i = n // b[change_b[0]]
    n = n % b[change_b[0]]
    n += i * a[change_b[0]]
    print(n)
  elif len(change_b) == 2:
    tmp = n
    for i in range(n // b[change_b[0]] + 1):
      m = n
      m -= i * b[change_b[0]]
      if m < 0:
        break
      j = m // b[change_b[1]]
      m = m % b[change_b[1]]
      m += i * a[change_b[0]] + j * a[change_b[1]]
      tmp = max(tmp, m)
    print(tmp)
elif len(change_a) == 2:
  tmp = n
  for i in range(5001):
    m = n
    m -= i * a[change_a[0]]
    if m < 0:
      break
    j = m // a[change_a[1]]
    m = m % a[change_a[1]]
    m += i * b[change_a[0]] + j * b[change_a[1]]
    tmp = max(tmp, m)
  n = tmp
  if len(change_b) == 0:
    print(n)
  elif len(change_b) == 1:
    i = n // b[change_b[0]]
    n = n % b[change_b[0]]
    n += i * a[change_b[0]]
    print(n)
else:    
  tmp = n
  for i in range(5001):
    for j in range(5001):
      m = n
      m -= i * a[change_a[0]]
      if m < 0:
        break
      m -= j * a[change_a[1]]
      if m < 0:
        break
      k = m // a[change_a[2]]
      m = m % a[change_a[2]]
      m += i * b[change_a[0]] + j * b[change_a[1]] + k * b[change_a[2]]
      tmp = max(tmp, m)
  print(tmp)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
U = 5000 * 5000 + 10

inf = 10 ** 18
dp1 = [-inf] * U
dp1[0] = N
for a, b in zip(A, B):
    for i in range(N - a + 1):
        dp1[i + a] = max(dp1[i + a], dp1[i] + b - a)

for i in range(N + 1):
    N = max(N, dp1[i])

dp2 = [-inf] * U
dp2[0] = N
for a, b in zip(A, B):
    for i in range(N - b + 1):
        dp2[i + b] = max(dp2[i + b], dp2[i] + a - b)

for i in range(N + 1):
    N = max(N, dp2[i])

print(N)

Solution (Python):
def examA():
    N = I()
    X = [LI()for _ in range(N)]
    l = 0; r = inf
    for _ in range(100):
        now = (l+r)/2
        x_l = -inf; x_r = inf
        y_l = -inf; y_r = inf
        for x,y,c in X:
            move = now / c
            x_l = max(x_l,x-move)
            x_r = min(x_r,x+move)
            y_l = max(y_l,y-move)
            y_r = min(y_r,y+move)
        if x_l<x_r and y_l<y_r:
            r = now
        else:
            l = now
    ans = r
    print(ans)
    return

def examB():
    N = I()
    C = LI()
    C.sort()
    base = pow(2,2*N-1,mod)
    inv = pow(2,mod-2,mod)
    ans = 0
    for i in range(N):
        cur = C[i]*base*(1+(N-1-i)*inv)
        ans += cur
        ans %= mod
    print(ans)
    return

def examC():
    N = I()
    Ag,As,Ab = LI()
    Bg,Bs,Bb = LI()
    dp = [i for i in range(N+1)]
    for i in range(N):
        if dp[i+1]<dp[i]:
            dp[i+1] = dp[i]
        if i+Ag<=N and dp[i+Ag]<dp[i]+Bg:
            dp[i+Ag] = dp[i]+Bg
        if i+As<=N and dp[i+As]<dp[i]+Bs:
            dp[i+As] = dp[i]+Bs
        if i+Ab<=N and dp[i+Ab]<dp[i]+Bb:
            dp[i+Ab] = dp[i]+Bb
    ne_N = dp[-1]
    
    dp2 = [i for i in range(ne_N+1)]
    for i in range(ne_N):
        if dp2[i+1]<dp2[i]:
            dp2[i+1] = dp2[i]
        if i+Bg<=ne_N and dp2[i+Bg]<dp2[i]+Ag:
            dp2[i+Bg] = dp2[i]+Ag
        if i+Bs<=ne_N and dp2[i+Bs]<dp2[i]+As:
            dp2[i+Bs] = dp2[i]+As
        if i+Bb<=ne_N and dp2[i+Bb]<dp2[i]+Ab:
            dp2[i+Bb] = dp2[i]+Ab
    ans = dp2[-1]
    print(ans)
    return


import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(readline())
def LI(): return list(map(int,readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-7)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examC()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

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

def calc(A, B, N):
    M = N
    if len([i for i, (a, b) in enumerate(zip(A, B)) if a < b]) == 1:
        i = [i for i, (a, b) in enumerate(zip(A, B)) if a < b][0]

        for k in range(N // A[i] + 1):
            D = N - k * A[i]
            if D < 0:
                continue

            D += k * B[i]
            M = max(M, D)
    elif len([i for i, (a, b) in enumerate(zip(A, B)) if a < b]) == 2:
        i, j = [i for i, (a, b) in enumerate(zip(A, B)) if a < b]

        for k in range(N // A[i] + 1):
            D = N - k * A[i]
            l, D = divmod(D, A[j])

            D += k * B[i]
            D += l * B[j]

            M = max(M, D)
    elif len([i for i, (a, b) in enumerate(zip(A, B)) if a < b]) == 3:
        for g in range(N // A[0] + 1):
            for s in range(N // A[1] + 1):
                D = N - g * A[0] - s * A[1]

                if D < 0:
                    break

                q, r = divmod(D, A[2])

                D += g * B[0]
                D += s * B[1]

                r += g * B[0]
                r += s * B[1]
                r += q * B[2]

                M = max(M, D, r)
    return M

print(calc(B, A, calc(A, B, N)))

Solution (Python):
def examA():
    N = I()
    X = [LI()for _ in range(N)]
    l = 0; r = inf
    while(r-l>_ep):
        now = (l+r)/2
        x_l = -inf; x_r = inf
        y_l = -inf; y_r = inf
        for x,y,c in X:
            move = now / c
            x_l = max(x_l,x-move)
            x_r = min(x_r,x+move)
            y_l = max(y_l,y-move)
            y_r = min(y_r,y+move)
        if x_l+_ep<x_r and y_l+_ep<y_r:
            r = now
        else:
            l = now
    ans = r
    print(ans)
    return

def examB():
    N = I()
    C = LI()
    C.sort()
    base = pow(2,2*N-1,mod)
    inv = pow(2,mod-2,mod)
    ans = 0
    for i in range(N):
        cur = C[i]*base*(1+(N-1-i)*inv)
        ans += cur
        ans %= mod
    print(ans)
    return

def examC():
    N = I()
    Ag,As,Ab = LI()
    Bg,Bs,Bb = LI()
    dp = [i for i in range(N+1)]
    for i in range(N):
        dp[i+1] = max(dp[i],dp[i+1])
        if i+Ag<=N:
            dp[i+Ag] = max(dp[i+Ag],dp[i]+Bg)
        if i+As<=N:
            dp[i+As] = max(dp[i+As],dp[i]+Bs)
        if i+Ab<=N:
            dp[i+Ab] = max(dp[i+Ab],dp[i]+Bb)
    ne_N = dp[-1]
    
    dp2 = [i for i in range(ne_N+1)]
    for i in range(ne_N):
        dp2[i+1] = max(dp2[i],dp2[i+1])
        if i+Bg<=ne_N:
            dp2[i+Bg] = max(dp2[i+Bg],dp2[i]+Ag)
        if i+Bs<=ne_N:
            dp2[i+Bs] = max(dp2[i+Bs],dp2[i]+As)
        if i+Bb<=ne_N:
            dp2[i+Bb] = max(dp2[i+Bb],dp2[i]+Ab)
    ans = dp2[-1]
    print(ans)
    return


import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(readline())
def LI(): return list(map(int,readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-4)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examC()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
l1=[i for i in range(3) if B[i]>A[i]]
l2=[i for i in range(3) if B[i]<A[i]]
r=[B[i]-A[i] for i in range(3)]
def b(l):
    global N
    if len(l)==3:
        M=0
        for i in range(N//A[0]+1):
            for j in range((N-i*A[0])//A[1]+1):
                k=(N-i*A[0]-j*A[1])//A[2]
                M=max(M,i*B[0]+j*B[1]+k*B[2]+(N-i*A[0]-j*A[1]-k*A[2]))
        N=M
    elif len(l)==2:
        M=0
        for i in range(N//A[l[0]]+1):
            j=(N-i*A[l[0]])//A[l[1]]
            M=max(M,i*B[l[0]]+j*B[l[1]]+(N-i*A[l[0]]-j*A[l[1]]))
        N=M
    elif len(l)==1:
        i=N//A[l[0]]
        N=i*B[l[0]]+N-i*A[l[0]]
def b2(l):
    global N
    if len(l)==3:
        M=0
        for i in range(N//B[0]+1):
            for j in range((N-i*B[0])//B[1]+1):
                k=(N-i*B[0]-j*B[1])//B[2]
                M=max(M,i*A[0]+j*A[1]+k*A[2]+(N-i*B[0]-j*B[1]-k*B[2]))
        N=M
    elif len(l)==2:
        M=0
        for i in range(N//B[l[0]]+1):
            j=(N-i*B[l[0]])//B[l[1]]
            M=max(M,i*A[l[0]]+j*A[l[1]]+(N-i*B[l[0]]-j*B[l[1]]))
        N=M
    elif len(l)==1:
        i=N//B[l[0]]
        N=i*A[l[0]]+N-i*B[l[0]]
b(l1)
r=list(map(lambda x: -x,r))
b2(l2)
print(N)

Solution (Python):
n= int(input())
gsb = [tuple(map(int,input().split())) for _ in range(2)]

def increase(num, gsb0, gsb1):
    dp = [ [num] * (num+1) for _ in range(3+1)]
    for i in range(1,4):
        w = gsb0[i-1]
        v = gsb1[i-1]
        for j in range(num+1):
            if(j < w):
                dp[i][j] = dp[i-1][j]
            else:
                dp[i][j] = max(dp[i-1][j], dp[i][j-w] + v-w)

    return(dp[-1][-1])

num = increase(n,gsb[0],gsb[1])
num = increase(num,gsb[1],gsb[0])

print(num)

Solution (Python):


import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
A = LIST()
B = LIST()

def solve(N):
    dp = [0] * (N+1)
    for i in range(N+1):
        dp[i] = i
    for i in range(N+1):
        for j in range(3):
            a, b = A[j], B[j]
            if i+a <= N:
                dp[i+a] = max(dp[i+a], dp[i] + b)
    return dp[N]

N2 = solve(N)
A, B = B, A
ans = solve(N2)
print(ans)