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.



Sample input:
23
1 1 1
2 1 1


Sample output:
46

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
A = [10**18] * 3
B = [10**18] * 3
dg = abs(ga - gb)
ds = abs(sa - sb)
db = 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):
    res = -1
    for i in range(N+1):
        if X[0] * i > N:
            break
        M = N - X[0] * i
        jj = M // X[1]
        x = 500 if x < 10000 else 100
        for j in range(max(jj - x, 0), jj+1):
            k = (M - X[1] * j) // X[2]
            res = max(res, dg * i + ds * j + db * k)
        kk = M // X[2]
        for k in range(max(kk - x, 0), kk+1):
            j = (M - X[2] * k) // X[1]
            res = max(res, dg * i + ds * j + db * k)
    return res

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

Submission status: Runtime Error

Solution code:
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] * 10000000
    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] * 10000000
    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()

Submission status: Runtime Error

Solution code:
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] * 100000
    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] * 100000
    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()

Submission status: Runtime Error

Solution code:
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] * 20000
    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] * 20000
    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()

Submission status: Runtime Error

Solution code:
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] * 10001
    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] * 10001
    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()

Submission status: Runtime Error

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

Submission status: Runtime Error

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

if len([i for i, (a, b) in enumerate(zip(A, B)) if a != b]) == 0:
    print(N)
    exit()

if len([i for i, (a, b) in enumerate(zip(A, B)) if a > b]) == 1:
    M = N
    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]
        D += k * B[i]
        M = max(M, D)

    i, j = [i for i, (a, b) in enumerate(zip(A, B)) if not a > b]
    ans = M
    for k in range(N // B[i] + 1):
        D = M - k * B[i]
        l, D = divmod(D, B[j])

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

        ans = max(ans, D)

    print(ans)
    exit()

if len([i for i, (a, b) in enumerate(zip(A, B)) if a > b]) == 2:
    M = N

    i, j = [i for i, (a, b) in enumerate(zip(A, B)) if not a > b]
    for k in range(N // B[i] + 1):
        D = N - k * B[i]
        l, D = divmod(D, B[j])

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

        M = max(M, D)

    i = [i for i, (a, b) in enumerate(zip(A, B)) if a > b][0]
    ans = M
    for k in range(N // A[i] + 1):
        D = M - k * A[i]
        D += k * B[i]
        ans = max(ans, D)

    print(ans)
    exit()

def calc(fr, to):
    ret = N
    for g in range(N // fr[0] + 1):
        for s in range(N // fr[1] + 1):
            D = N - g * fr[0] - s * fr[1]

            if D < 0:
                break

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

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

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

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

print(calc(A, B))

Submission status: Runtime Error

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

if len([0 for a, b in zip(A, B) if a != b]) == 0:
    print(N)
    exit()

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

    for k in range(N // A[i] + 1):
        D = N - k * A[i]
        D += k * B[i]
        M = max(M, D)

    i, j = [i for i, (a, b) in zip(A, B) if not a > b]
    ans = M
    for k in range(N // B[i] + 1):
        D = M - k * B[i]
        l, D = divmod(D, B[j])

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

        ans = max(ans, D)

    print(ans)
    exit()

if len([0 for a, b in zip(A, B) if a > b]) == 2:
    M = N

    i, j = [i for i, (a, b) in zip(A, B) if not a > b]
    for k in range(N // B[i] + 1):
        D = N - k * B[i]
        l, D = divmod(D, B[j])

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

        M = max(M, D)

    i = [i for i, (a, b) in zip(A, B) if a > b][0]
    ans = M
    for k in range(N // A[i] + 1):
        D = M - k * A[i]
        D += k * B[i]
        ans = max(ans, D)

    print(ans)
    exit()

def calc(fr, to):
    ret = N
    for g in range(N // fr[0] + 1):
        for s in range(N // fr[1] + 1):
            D = N - g * fr[0] - s * fr[1]

            if D < 0:
                break

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

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

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

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

print(calc(A, B))

Submission status: Runtime Error

Solution code:
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):
        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 = max(dp)
    #print(dp)
    dp2 = [i for i in range(ne_N+1)]
    for i in range(ne_N):
        if dp2[i]<0:
            continue
        if i+Bg<=ne_N:
            dp2[i+Bg] = max(dp2[i+Bg],dp2[i]+Ag)
        if i+Bs<=ne_N:
            dp2[i+As] = max(dp2[i+Bs],dp2[i]+As)
        if i+Bb<=ne_N:
            dp2[i+Bb] = max(dp2[i+Bb],dp2[i]+Ab)
    ans = max(dp2)
    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**9
_ep = 10**(-8)
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
"""

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())
gsb1 = list(map(int,input().split()))
gsb2 = list(map(int,input().split()))
def trade(n,g1,s1,b1,g2,s2,b2):
  tradelist = []
  for x,y in (g1,g2),(s1,s2),(b1,b2):
    if x < y:
      tradelist.append((x,y))
  if not tradelist:
    return n
  dp = [0]*(n+1)
  for w,v in tradelist:
    for i in range(n+1-w):
      dp[i+w] = max(dp[i+w],dp[i]+v)
  return dp[n]

ans1 = trade(n,*gsb1,*gsb2)
ans2 = trade(ans1,*gsb2,*gsb1)
print(ans2)

Submission status: Runtime Error

Solution code:
n=int(input())
a,b,c=map(int,input().split())
d,e,f=map(int,input().split())
s=[0]*(n+1)
for i in range(a,n+1):
  s[i]=max(s[i],s[i-a]+d-a)
for i in range(b,n+1):
  s[i]=max(s[i],s[i-b]+e-b)
for i in range(c,n+1):
  s[i]=max(s[i],s[i-b]+f-c)
n+=s[-1]
s=[0]*(n+1)
for i in range(d,n+1):
  s[i]=max(s[i],s[i-d]+a-d)
for i in range(e,n+1):
  s[i]=max(s[i],s[i-e]+b-e)
for i in range(f,n+1):
  s[i]=max(s[i],s[i-f]+c-f)
print(n+s[-1])

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
d1 = []
d2 = []
for i in range(3):
  if a[i] < b[i]: d1.append((a[i], b[i]))
  if a[i] > b[i]: d2.append((b[i], a[i]))

dp = [0] * (N + 1)

for i in range(N, -1, -1):
  for t in d1:
    x = t[0]
    y = t[1]
    if i - x >= 0:
      dp[i - x] += y + dp[i]
for i in range(N + 1): dp[i] += i
res1 = max(dp)
#print(dp)
dp2 = [0] * (res1 + 1)
for i in range(res1, -1, -1):
  for t in d2:
    x = t[0]
    y = t[1]
    if i - x >= 0:
      dp2[i - x] += y
for i in range(res1 + 1): dp2[i] += i
print(max(dp2))

Submission status: Runtime Error

Solution code:
import sys
def solve(n,a,b):
    arr = list(zip(a,b))
    arr = [(x,y) for x,y in arr if x < y]
    arr.sort(key = lambda x: x[1]/x[0], reverse = True)
    l = len(arr)
    if l == 0:
        return n
    if l == 1:
        x,y = arr[0]
        z = n//x
        return n+(y-x)*z
    if l == 2:
        (x,y),(z,t) = arr
        return max(n+(n-i*z)//x*(y-x)+i*(t-z)for i in range(a))
    dp = list(range(n+1))
    for x,y in arr:
        for i in range(x,n+1):
            z = dp[i-x]+y
            if dp[i]<z:
                dp[i] = z
    return dp[n]
 
n = int(sys.stdin.readline())
a = list(map(int,sys.stdin.readline().split()))
b = list(map(int,sys.stdin.readline().split()))
 
print(solve(solve(n,a,b),b,a))

Submission status: Runtime Error

Solution code:
import sys
def solve(n,a,b):
    arr = list(zip(a,b))
    arr = [(x,y) for x,y in arr if x < y]
    arr.sort(key = lambda x: x[1]/x[0], reverse = True)
    l = len(arr)
    if l == 0:
        return n
    if l == 1:
        x,y = arr[0]
        z = n//x
        return n+(y-x)*z
    if l == 2:
        (x,y),(z,t) = arr
        return max(n+(n-i*z)//x*(y-x)+i*(t-z)for i in range(a))
    dp = list(range(n+1))
    for x,y in arr:
        for i in range(x,n+1):
            z = dp[i-x]+y
            if dp[i]<z:
                dp[i] = z
    return dp[n]
 
n = int(sys.stdin.readline())
a = list(map(int,sys.stdin.readline().split()))
b = list(map(int,sys.stdin.readline().split()))
 print(solve(solve(n,a,b),b,a))

Submission status: Runtime Error

Solution code:
N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
metal = [(ga, gb), (sa, sb), (ba, bb)]
metal.sort(reverse = True, key = lambda x:x[1]-x[0])
ga, gb = metal[0]
sa, sb = metal[1]
ba, bb = metal[2]
if ba<=bb:
    res = N
    for i in range(N//ga+1):
        for j in range((N-i*ga)//sa+1):
            k = (N-i*ga-j*sa)//ba
            tmp = k*bb+j*sb+i*gb+(N-i*ga-j*sa-k*ba)
            res = max(res, tmp)
elif sa<=sb:
    res = N
    for i in range(N//ga+1):
        j = (N-i*ga)//sa
        tmp = gb*i+sb*j+(N-i*ga-sa*j)
        res = max(res, tmp)
    k = res//bb
    res += (ba-bb)*k
elif ga<=gb:
    i = N//ga
    N+=i*(gb-ga)
    res = N
    for i in range(N//sb):
        print(1/0)
        j = (N-i*sb)//bb
        tmp = N+(sa-sb)*i+(ba-bb)*j
        res = max(res, tmp)
else:
    res = N
    ga, gb = gb, ga
    sa, sb = sb, sa
    ba, bb = bb, ba
    for i in range(N//ga+1):
        for j in range((N-i*ga)//sa+1):
            k = (N-i*ga-j*sa)//ba
            tmp = k*bb+j*sb+i*gb+(N-i*ga-j*sa-k*ba)
            res = max(res, tmp)
print(res)

Submission status: Runtime Error

Solution code:
N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
metal = [(ga, gb), (sa, sb), (ba, bb)]
metal.sort(reverse = True, key = lambda x:x[1]-x[0])
ga, gb = metal[0]
sa, sb = metal[1]
ba, bb = metal[2]
if ba<=bb:
    res = 0
    for i in range(N//ga+1):
        for j in range((N-i*ga)//sa+1):
            k = (N-i*ga-j*sa)//ba
            tmp = k*bb+j*sb+i*gb+(N-i*ga-j*sa-k*ba)
            res = max(res, tmp)
elif sa<=sb:
    res = 0
    for i in range(N//ga+1):
        j = (N-i*ga)//sa
        tmp = gb*i+sb*j+(N-i*ga-sa*j)
        res = max(res, tmp)
    k = res//bb
    res += (ba-bb)*k
elif ga<=gb:
    print(1/0)
    i = N//ga
    N+=i*(gb-ga)
    res = 0
    for i in range(N//sb):
        j = (N-i*sb)//bb
        tmp = i*sa+j*ba+(N-i*sb-j*bb)
        res = max(res, tmp)
else:
    res = 0
    ga, gb = gb, ga
    sa, sb = sb, sa
    ba, bb = bb, ba
    for i in range(N//ga+1):
        for j in range((N-i*ga)//sa+1):
            k = (N-i*ga-j*sa)//ba
            tmp = k*bb+j*sb+i*gb+(N-i*ga-j*sa-k*ba)
            res = max(res, tmp)
print(res)

Submission status: Runtime Error

Solution code:
N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
metal = [(ga, gb), (sa, sb), (ba, bb)]
metal.sort(reverse = True, key = lambda x:x[1]-x[0])
ga, gb = metal[0]
sa, sb = metal[1]
ba, bb = metal[2]
if ba<=bb:
    res = 0
    for i in range(N//ga+1):
        for j in range((N-i*ga)//sa+1):
            k = (N-i*ga-j*sa)//ba
            tmp = k*bb+j*sb+i*gb+(N-i*ga-j*sa-k*ba)
            res = max(res, tmp)
elif sa<=sb:
    print(1/0)
    res = 0
    for i in range(N//ga+1):
        j = (N-i*ga)//sa
        tmp = gb*i+sb*j+(N-i*ga-sa*j)
        res = max(res, tmp)
    k = res//bb
    res += (ba-bb)*k
elif ga<=gb:
    i = N//ga
    N+=i*(gb-ga)
    res = 0
    for i in range(N//sb):
        j = (N-i*sb)//bb
        tmp = i*sa+j*ba+(N-i*sb-j*bb)
        res = max(res, tmp)
else:
    res = 0
    ga, gb = gb, ga
    sa, sb = sb, sa
    ba, bb = bb, ba
    for i in range(N//ga+1):
        for j in range((N-i*ga)//sa+1):
            k = (N-i*ga-j*sa)//ba
            tmp = k*bb+j*sb+i*gb+(N-i*ga-j*sa-k*ba)
            res = max(res, tmp)
print(res)

Submission status: Runtime Error

Solution code:
N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
metal = [(ga, gb), (sa, sb), (ba, bb)]
metal.sort(reverse = True, key = lambda x:x[1]-x[0])
ga, gb = metal[0]
sa, sb = metal[1]
ba, bb = metal[2]
if ba<=bb:
    print(1/0)
    res = 0
    for i in range(N//ga+1):
        for j in range((N-i*ga)//sa+1):
            k = (N-i*ga-j*sa)//ba
            tmp = k*bb+j*sb+i*gb+(N-i*ga-j*sa-k*ba)
            res = max(res, tmp)
elif sa<=sb:
    res = 0
    for i in range(N//ga+1):
        j = (N-i*ga)//sa
        tmp = gb*i+sb*j+(N-i*ga-sa*j)
        res = max(res, tmp)
    k = res//bb
    res += (ba-bb)*k
elif ga<=gb:
    i = N//ga
    N+=i*(gb-ga)
    res = 0
    for i in range(N//sb):
        j = (N-i*sb)//bb
        tmp = i*sa+j*ba+(N-i*sb-j*bb)
        res = max(res, tmp)
else:
    res = 0
    ga, gb = gb, ga
    sa, sb = sb, sa
    ba, bb = bb, ba
    for i in range(N//ga+1):
        for j in range((N-i*ga)//sa+1):
            k = (N-i*ga-j*sa)//ba
            tmp = k*bb+j*sb+i*gb+(N-i*ga-j*sa-k*ba)
            res = max(res, tmp)
print(res)

Submission status: Runtime Error

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

def search3(w,v,n):
    res = 0
    for n1 in range(n//w[0]+1):
        for n2 in range((n-w[0]*n1)//w[1]+1):
            n3 = (n-w[0]*n1-w[1]*n2)//w[2]
            res = max(res,n+(v[0]-w[0])*n1+(v[1]-w[1])*n2+(v[2]-w[2])*n3)
    return res

def search2(w,v,n):
    res = 0
    for n1 in range(n//w[0]+1):
        n2 = (n-w[0]*n1)//w[1]
        res = max(res,n+(v[0]-w[0])*n1+(v[1]-w[1]*n2))
    return res

AB = [[A[0]-B[0],A[0],B[0]],[A[1]-B[1],A[1],B[1]],[A[2]-B[2],A[2],B[2]]]
AB.sort()
A = [AB[i][1] for i in range(3)]
B = [AB[i][2] for i in range(3)]

if AB[0][0]>0:
    ans = search3(B,A,N)
elif AB[1][0]>0:
    n = N+(B[0]-A[0])*(N//A[0])
    ans = search2(B[1:],A[1:],n)
elif AB[2][0]>0:
    n = search2(A[:2],B[:2],N)
    n = n+(A[2]-B[2])*(n//B[2])
else:
    ans = search3(A,B,N)

print(ans)

Submission status: Runtime Error

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

def search3(w,v,n):
    res = 0
    for n1 in range(n//w[0]+1):
        for n2 in range((n-w[0]*n1)//w[1]+1):
            n3 = (n-w[0]*n1-w[1]*n2)//w[2]
            res = max(res,n+(v[0]-w[0])*n1+(v[1]-w[1])*n2+(v[2]-w[2])*n3)
    return res

def search2(w,v,W):
    res = 0
    for n1 in range(n//w[0]+1):
        n2 = (n-w[0]*n1)//w[1]
        res = max(res,n+(v[0]-w[0])*n1+(v[1]-w[1]*n2))
    return res

AB = [[A[0]-B[0],A[0],B[0]],[A[1]-B[1],A[1],B[1]],[A[2]-B[2],A[2],B[2]]]
AB.sort()
A = [AB[i][1] for i in range(3)]
B = [AB[i][2] for i in range(3)]

if AB[0][0]>0:
    ans = search3(B,A,N)
elif AB[1][0]>0:
    n = N+(B[0]-A[0])*(N//A[0])
    ans = search2(B[1:],A[1:],n)
elif AB[2][0]>0:
    n = search2(A[:2],B[:2],N)
    n = n+(A[2]-B[2])*(n//B[2])
else:
    ans = search3(A,B,N)

print(ans)

Submission status: Runtime Error

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

def search3(w,v,n):
    res = 0
    for n1 in range(n//w[0]+1):
        for n2 in range((n-w[0]*n1)//w[1]+1):
            n3 = (n-w[0]*n1-w[1]*n2)//w[2]
            res = max(res,n+(v[0]-w[0])*n1+(v[1]-w[1])*n2+(v[2]-w[2])*n3)
    return res

def search2(w,v,W):
    res = 0
    for n1 in range(n//w[0]+1):
        n2 = (n-w[0]*n1)//w[1]
        res = max(res,n+(v[0]-w[0])*n1+(v[1]-w[1]*n2))
    return res

AB = [[A[0]-B[0],A[0]],[A[1]-B[1],A[1]],[A[2]-B[2],A[2]]]
AB.sort()

if AB[0][0]>0:
    ans = search3(B,A,N)
elif AB[1][0]>0:
    n = N+(B[0]-A[0])*(N//A[0])
    ans = search2(B[1:],A[1:],n)
elif AB[2][0]>0:
    n = search2(A[:2],B[:2],N)
    n = n+(A[2]-B[2])*(n//B[2])
else:
    ans = search3(A,B,N)

print(ans)

Submission status: Runtime Error

Solution code:
def main():
    n=int(input())
    ga,sa,ba=map(int,input().split())
    gb,sb,bb=map(int,input().split())
    
    def calc_1(dg,a0,a1):
        return (dg//a0)*a1+dg%a0

    def calc_2(dg,a0,a1,b0,b1):
        x=dg//a0
        visit=set()
        temp=0
        for i in range(x,-1,-1):
            amari=(dg-x*a0)%b0
            #if amari in visit:
            #    break
            temp=max(x*a1+((dg-x*a0)//b0)*b1+amari,temp)
            #visit.add(amari)
        return temp

    #def calc_3(dg,a0,a1,b0,b1,c0,c1):
    #    return max([a1*i+calc_2(dg-a0*i,b0,b1,c0,c1) for i in range(dg//a0,-1,-1)])

    gsb=[[ga,gb],[sa,sb],[ba,bb]]
    gsb.sort(key=lambda x:x[1]/x[0],reverse=True)
    temp=[[i,j] for i,j in gsb if i<j]
    if len(temp)==1:
        n=calc_1(n,temp[0][0],temp[0][1])
    elif len(temp)==2:
        n=calc_2(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1])
    elif len(temp)==3:
        n=calc_3(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1],temp[2][0],temp[2][1])

    gsb=reversed(gsb)
    temp=[[j,i] for i,j in gsb if i>j]
    if len(temp)==1:
        n=calc_1(n,temp[0][0],temp[0][1])
    elif len(temp)==2:
        n=calc_2(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1])
    elif len(temp)==3:
        n=calc_3(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1],temp[2][0],temp[2][1])
    print(n)
main()

Submission status: Runtime Error

Solution code:
def main():
    n=int(input())
    ga,sa,ba=map(int,input().split())
    gb,sb,bb=map(int,input().split())
    
    def calc_1(dg,a0,a1):
        return (dg//a0)*a1+dg%a0

    def calc_2(dg,a0,a1,b0,b1):
        x=dg//a0
        visit=set()
        temp=0
        for i in range(x,-1,-1):
            amari=(dg-x*a0)%b0
            if amari in visit:
                break
            temp=max(x*a1+((dg-x*a0)//b0)*b1+amari,temp)
            visit.add(amari)
        return temp

    #def calc_3(dg,a0,a1,b0,b1,c0,c1):
    #    return max([a1*i+calc_2(dg-a0*i,b0,b1,c0,c1) for i in range(dg//a0,-1,-1)])

    gsb=[[ga,gb],[sa,sb],[ba,bb]]
    gsb.sort(key=lambda x:x[1]/x[0],reverse=True)
    temp=[[i,j] for i,j in gsb if i<j]
    if len(temp)==1:
        n=calc_1(n,temp[0][0],temp[0][1])
    elif len(temp)==2:
        n=calc_2(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1])
    elif len(temp)==3:
        n=calc_3(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1],temp[2][0],temp[2][1])

    gsb=reversed(gsb)
    temp=[[j,i] for i,j in gsb if i>j]
    if len(temp)==1:
        n=calc_1(n,temp[0][0],temp[0][1])
    elif len(temp)==2:
        n=calc_2(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1])
    elif len(temp)==3:
        n=calc_3(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1],temp[2][0],temp[2][1])
    print(n)
main()

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
dp1 = [0] * (n+1)
for i in range(n+1):
    if i>=a[0]:
        dp1[i] = max(dp1[i],dp1[i-a[0]]+b[0])
    if i>=a[1]:
        dp1[i] = max(dp1[i],dp1[i-a[1]]+b[1])
    if i>=a[2]:
        dp1[i] = max(dp1[i],dp1[i-a[1]]+b[2])
n=dp1[n]
dp2 = [0] * (n+1)
for i in range(n+1):
    if i>=b[0]:
        dp2[i] = max(dp2[i],dp2[i-b[0]]+a[0])
    if i>=b[1]:
        dp2[i] = max(dp2[i],dp2[i-b[1]]+a[1])
    if i>=b[2]:
        dp2[i] = max(dp2[i],dp2[i-b[1]]+a[2])


print(dp2[n])

Submission status: Runtime Error

Solution code:
n=int(input())
ga,sa,ba=map(int,input().split())
gb,sb,bb=map(int,input().split())
def f(a,b,c,d,e,f,n):
    s=[[a,d-a],[b,e-b],[c,f-c]]
    s=[i for i in s if i[1]>0]
    dp=[0]*(n+1)
    if len(s)==0:
        return n
    if len(s)==1:
        return d*(n//a)+n%a
    else:
        for p,q in s:
            for j in range(a,n+1):
                dp[j]=max(dp[j],dp[j-p]+q)
        return dp[n]
print(f(gb,sb,bb,ga,sa,ba,f(ga,sa,ba,gb,sb,bb,n)))

Submission status: Runtime Error

Solution code:
n=int(input())
ga,sa,ba=map(int,input().split())
gb,sb,bb=map(int,input().split())
def f(a,b,c,d,e,f,n):
    s=[[a,d-a],[b,e-b],[c,f-c]]
    s=[i for i in s if i[1]>0]
    dp=[0]*(n+1)
    if len(s)==0:
        return n
    if len(s)==1:
        return d*n//a+n%a
    else:
        for p,q in s:
            for j in range(a,n+1):
                dp[j]=max(dp[j],dp[j-p]+q)
        return dp[n]
print(f(gb,sb,bb,ga,sa,ba,f(ga,sa,ba,gb,sb,bb,n)))

Submission status: Runtime Error

Solution code:
def calc():
  n=int(input())
  arr=[list(map(int,input().split())) for _ in range(2)]
  move1=[]
  move2=[]
  ans=n
  for i in range(3):
    if arr[0][i]-arr[1][i]>0:
      move2+=[i]
    if arr[1][i]-arr[0][i]>0:
      move1+=[i]
  if len(move1)==1:
    idx=move1[0]
    ans=arr[1][idx]*(ans//arr[0][idx])+ans%arr[0][idx]
  elif len(move1)==2:
    idx1=move1[0]
    idx2=move1[1]
    s1=arr[0][idx1]
    s2=arr[0][idx2]
    g1=arr[1][idx1]
    g2=arr[1][idx2]
    ta=max(ans+i*(g1-s1)+((ans-i*s1)//s2)*(g2-s2) for i in range(ans//s1+1))
    ans=ta
  elif len(move1)==3:
    idx1=move1[0]
    idx2=move1[1]
    idx3=move1[2]
    s1=arr[0][idx1]
    s2=arr[0][idx2]
    s3=arr[0][idx3]
    g1=arr[1][idx1]
    g2=arr[1][idx2]
    g3=arr[1][idx3]
    ta=max(ans+i*(g1-s1)+j*(g2-s2)+((ans-i*s1-j*s2)//s3)*(g3-s3) for i in range(ans//s1+1) for j in range((ans-i*s1)//s2+1)
    ans=ta
  if len(move2)==1:
    idx=move2[0]
    ans=arr[0][idx]*(ans//arr[1][idx])+ans%arr[1][idx]
  elif len(move2)==2:
    idx1=move2[0]
    idx2=move2[1]
    s1=arr[1][idx1]
    s2=arr[1][idx2]
    g1=arr[0][idx1]
    g2=arr[0][idx2]
    ta=max(ans+i*(g1-s1)+((ans-i*s1)//s2)*(g2-s2) for i in range(ans//s1+1))
    ans=ta
  elif len(move2)==3:
    idx1=move2[0]
    idx2=move2[1]
    idx3=move2[2]
    s1=arr[1][idx1]
    s2=arr[1][idx2]
    s3=arr[1][idx3]
    g1=arr[0][idx1]
    g2=arr[0][idx2]
    g3=arr[0][idx3]
    ta=max(ans+i*(g1-s1)+j*(g2-s2)+((ans-i*s1-j*s2)//s3)*(g3-s3) for i in range(ans//s1+1) for j in range((ans-i*s1)//s2+1)
    ans=ta
  print(ans)
calc()