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:
#!/usr/bin/env pypy
from __future__ import division, print_function

import os
import sys
from __builtin__ import xrange as range
from cStringIO import StringIO
from future_builtins import ascii, filter, hex, map, oct, zip
from io import IOBase

import __pypy__


def main():
    n = int(input())
    a = [int(x) for x in input().split()]
    a.sort()

    if a[-1] <= 0:
        s = max(a)
        a.remove(s)

        X, Y = [], []
        for _ in range(n - 2):
            ai = a.pop()
            X.append(s), Y.append(ai)
            s -= ai

        ai = a.pop()
        X.append(s), Y.append(ai)

        print(s - ai)
        for x, y in zip(X, Y):
            print(x, y)
    elif a[0] >= 0:
        s = min(a)
        a.remove(s)

        X, Y = [], []
        for _ in range(n - 2):
            ai = a.pop()
            X.append(s), Y.append(ai)
            s -= ai

        ai = a.pop()
        X.append(ai), Y.append(s)

        print(ai - s)
        for x, y in zip(X, Y):
            print(x, y)
    else:
        s = min(a)
        a.remove(s)

        X, Y = [], []
        while a[-1] >= 0 and len(a) > 1:
            ai = a.pop()
            X.append(s), Y.append(ai)
            s -= ai

        a = [s] + a
        s = max(a)
        a.remove(s)

        while len(a) > 1:
            ai = a.pop()
            X.append(s), Y.append(ai)
            s -= ai

        ai = a.pop()
        X.append(s), Y.append(ai)

        print(s - ai)
        for x, y in zip(X, Y):
            print(x, y)


# region fastio

BUFSIZE = 8192


class FastI(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = StringIO()
        self.newlines = 0

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count("\n") + (not b)
            ptr = self._buffer.tell()
            self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr)
        self.newlines -= 1
        return self._buffer.readline()


class FastO(IOBase):
    def __init__(self, file):
        self._fd = file.fileno()
        self._buffer = __pypy__.builders.StringBuilder()
        self.write = lambda s: self._buffer.append(s)

    def flush(self):
        os.write(self._fd, self._buffer.build())
        self._buffer = __pypy__.builders.StringBuilder()


def print(*args, **kwargs):
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastI(sys.stdin), FastO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n=int(input())
ga,sa,ba=map(int,input().split())
gb,sb,bb=map(int,input().split())
g=-ga+gb,-sa+sb,-ba+bb
dp=[-10000 for i in range(5000)]+[0 for i in range(n+1)]
for i in range(5000,n+5001):
  dp[i]=max(dp[i-ga]+g[0],dp[i-sa]+g[1],dp[i-ba]+g[2],0)
print(d)
dp2=[-10000 for i in range(5000)]+[0 for i in range(d+1)]
for i in range(5000,n+5001):
  dp2[i]=max(dp2[i-gb]-g[0],dp2[i-sb]-g[1],dp2[i-bb]-g[2],0)
print(dp2[d+5000]+d)

Submission status: Runtime Error

Solution code:
N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
para = [gb/(ga+1), sb/(sa+1), bb/(ba+1)]
pm = sorted(para, reverse=True)
ans = 0
i = 0
while(N):
    pi = para.index(pm[i])
    pp = para[pi]
    if pp >= 1:
        if pi == 0:
            ans += N // ga * gb
            N = N % ga
        elif pi == 1:
            ans += N // sa * sb
            N = N % sa
        elif pi == 2:
            ans += N // ba * bb
            N = N % ba
    else:
        break
    i += 1
N += ans
ansb = 0
i = 0
para = [ga/(gb+1), sa/(sb+1), ba/(bb+1)]
pm = sorted(para, reverse=True)
while(N):
    pi = para.index(pm[i])
    pp = para[pi]
    if pp >= 1:
        if pi == 0:
            ansb += N // gb * ga
            N = N % gb
        elif pi == 1:
            ansb += N // sb * sa
            N = N % sb
        elif pi == 2:
            ansb += N // bb * ba
            N = N % bb
    else:
        break
    i += 1
if ansb:
    print(ansb)
else:
    print(ans)

Submission status: Runtime Error

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

l = set()
max_item = N
l.add(N)
for i in [0,1,2]:
  if A[i] == B[i]:
    pass
  else:
    d = max(A[i],B[i])
    n = min(A[i],B[i])
    if d%n==0:
      l.add(N*d)
    else:  
      l = [x*d for x in range(1,N+1) if x*d%n==0]
      l.add(max(l))
      
print(max(l))

Submission status: Runtime Error

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

ans = 0
if (ga < gb) and (sa < sb) and (ba < bb):
    for G in range(5001):
        for S in range(5001):
            if ga * G + sa * S > N:
                continue
            B = (N - (ga * G + sa * S)) // ba
            tmp_ans = (N - (ga * G + sa * S + ba * B))
            tmp_ans += (G * gb) + (S * sb) + (B * bb)
            ans = max(ans, tmp_ans)

elif (ga < gb) and (sa < sb) and (ba >= bb):
    for G in range(5001):
        for S in range(5001):
            if ga * G + sa * S > N:
                continue
            tmp_ans = (N - (ga * G + sa * S))

            NB = tmp_ans // bb
            tmp_ans = tmp_ans % bb

            tmp_ans += (G * gb) + (S * sb) + (NB * ba)
            ans = max(ans, tmp_ans)

elif (ga < gb) and (sa >= sb) and (ba < bb):
    for G in range(5001):
        for B in range(5001):
            if ga * G + ba * B > N:
                continue
            tmp_ans = (N - (ga * G + ba * B))

            NS = tmp_ans // sb
            tmp_ans = tmp_ans % sb

            tmp_ans += (G * gb) + (NS * sa) + (B * bb)
            ans = max(ans, tmp_ans)

elif (ga >= gb) and (sa < sb) and (ba < bb):
    for S in range(5001):
        for B in range(5001):
            if sa * S + ba * B > N:
                continue
            remain = (N - (sa * S + ba * B))

            NG = tmp_ans // gb
            tmp_ans = remain % gb

            tmp_ans += (NG * ga) + (S * sb) + (B * bb)
            ans = max(ans, tmp_ans)

elif (ga >= gb) and (sa >= sb) and (ba < bb):
    for B in range(5001):
        if ba * B > N:
            continue
        remain = (N - ba * B)
        for NG in range(5001):
            if NG * gb > remain:
                continue
            NS = (remain - NG * gb) // sb
            tmp_ans = remain % sb

            tmp_ans += (NG * ga) + (NS * sa) + (B * bb)
            ans = max(ans, tmp_ans)

elif (ga >= gb) and (sa < sb) and (ba >= bb):
    for S in range(5001):
        if sa * S > N:
            continue
        remain = (N - sa * S)
        for NG in range(5001):
            if NG * gb > remain:
                continue
            NB = (remain - NG * gb) // bb
            tmp_ans = remain % bb

            tmp_ans += (NG * ga) + (S * sb) + (NB * ba)
            ans = max(ans, tmp_ans)

elif (ga < gb) and (sa >= sb) and (ba >= bb):
    for G in range(5001):
        if ga * G > N:
            continue
        remain = (N - ga * G)
        for NS in range(5001):
            if NS * sb > remain:
                continue
            NB = (remain - NS * sb) // bb
            tmp_ans = remain % bb

            tmp_ans += (G * gb) + (NS * sa) + (NB * ba)
            ans = max(ans, tmp_ans)

else:
    for G in range(5001):
        for S in range(5001):
            if gb * G + sb * S > N:
                continue
            B = (N - (gb * G + sb * S)) // bb
            tmp_ans = (N - (gb * G + sb * S + bb * B))
            tmp_ans += (G * ga) + (S * sa) + (B * ba)
            ans = max(ans, tmp_ans)

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
para = [gb/(ga+1), sb/(sa+1), bb/(ba+1)]
pm = sorted(para, reverse=True)
ans = 0
i = 0
while(N):
    pi = para.index(pm[i])
    pp = para[pi]
    if pp >= 1:
        if pi == 0:
            ans += N // ga * gb
            N = N % ga
        elif pi == 1:
            ans += N // sa * sb
            N = N % sa
        elif pi == 2:
            ans += N // ba * bb
            N = N % ba
    else:
        break
    i += 1
N += ans
ans = 0
i = 0
para = [ga/(gb+1), sa/(sb+1), ba/(bb+1)]
pm = sorted(para, reverse=True)
while(N):
    pi = para.index(pm[i])
    pp = para[pi]
    if pp >= 1:
        if pi == 0:
            ans += N // gb * ga
            N = N % gb
        elif pi == 1:
            ans += N // sb * sa
            N = N % sb
        elif pi == 2:
            ans += N // bb * ba
            N = N % bb
    else:
        break
    i += 1
print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
from itertools import product
from heapq import heappop, heappush

def solve():
    N = int(input())
    A = [0, *map(int, input().split())]
    B = [0, *map(int, input().split())]

    dp = [[0,0,0,0] for _ in range(N+max(A+B)+5)]
    X, Y = A, B
    for i,w in product(range(1,4), range(1,N+1)):
        dp[w][i] = max(dp[w-X[i]*j][i-1]+Y[i]*j for j in range(0,w//X[i]+1) if w>=X[i]*j) if w>=X[i] else dp[w][i-1]
    N = dp[N][3]
    dp = [[0,0,0,0] for _ in range(N+1)]
    X, Y = B, A
    for i,w in product(range(1,4), range(1,N+1)):
        dp[w][i] = max(dp[w-X[i]*j][i-1]+Y[i]*j for j in range(0,w//X[i]+1)  if w>=X[i]*j) if w>=X[i] else dp[w][i-1]
    res = dp[N][3]
    return res





    return res
if __name__ == '__main__':
    print(solve())

Submission status: Runtime Error

Solution code:

def find3(N,a,b,c,A,B,C):
    MAX=N
    for x in xrange(5001):
        if N-a*x<0:
            break
        for y in xrange(5001):
            temp=N-(a*x+b*y)
            if temp>=0:
                z=temp//c
                MAX=max(MAX,N-(a*x+b*y+c*z)+(A*x+B*y+C*z))
            else:
                break

    a,A,b,B=b,B,a,A 
    for x in xrange(5001):
        if N-a*x<0:
            break
        for y in xrange(5001):
            temp=N-(a*x+b*y)
            if temp>=0:
                z=temp//c
                MAX=max(MAX,N-(a*x+b*y+c*z)+(A*x+B*y+C*z))
            else:
                break

    a,A,b,B,c,C=c,C,a,A,b,B
    for x in xrange(5001):
        if N-a*x<0:
            break
        for y in xrange(5001):
            temp=N-(a*x+b*y)
            if temp>=0:
                z=temp//c
                MAX=max(MAX,N-(a*x+b*y+c*z)+(A*x+B*y+C*z))
            else:
                break
    return MAX

def find2(N,a,b,A,B):
    MAX=N
    
    for x in xrange(N+1):
        temp=N-a*x
        if temp>=0:
            y=temp//b
            MAX=max(MAX,N-(a*x+b*y)+(A*x+B*y))
        else:
            break
   
    return max(MAX,N+(A-a)*x+(B-b)*y)
    

def find1(N,a,A):
    MAX=N
    num=N//a
    return N-num*a+num*A

N=int(raw_input())
X=list(map(int,raw_input().strip().split(' ')))
Y=list(map(int,raw_input().strip().split(' ')))

##stage1
index=[0]*3

for i in xrange(3):
    if X[i]<Y[i]:
        index[i]=1
post_1=[]
post_2=[]
for i in xrange(len(index)):
    if index[i]>0:
        post_1+=[i]    
    else:
        post_2+=[i]
number=sum(index)
if number==0:
    print(find3(N,Y[0],Y[1],Y[2],X[0],X[1],X[2]))
elif number==1:
    N=find1(N,X[post_1[0]],Y[post_1[0]])
    N=find2(N,Y[post_2[0]],Y[post_2[1]],X[post_2[0]],X[post_2[1]])
    print(N)
elif number==2:
    N=find2(N,X[post_1[0]],X[post_1[1]],Y[post_1[0]],Y[post_1[1]])
    N=find1(N,Y[post_2[0]],X[post_2[0]])
    print(N)
else:
    N=find3(N,X[0],X[1],X[2],Y[0],Y[1],Y[2])
    print(N)
    
    

    
    
    
    


                
                
    return MAX

def find2(N,a,b,A,B):
    MAX=N
    
    for x in xrange(N+1):
        temp=N-a*x
        if temp>=0:
            y=temp//b
            MAX=max(MAX,N-(a*x+b*y)+(A*x+B*y))
        else:
            break
   
    return max(MAX,N+(A-a)*x+(B-b)*y)
    

def find1(N,a,A):
    MAX=N
    num=N//a
    return N-num*a+num*A

N=int(raw_input())
X=list(map(int,raw_input().strip().split(' ')))
Y=list(map(int,raw_input().strip().split(' ')))

##stage1
index=[0]*3

for i in xrange(3):
    if X[i]<Y[i]:
        index[i]=1
post_1=[]
post_2=[]
for i in xrange(len(index)):
    if index[i]>0:
        post_1+=[i]    
    else:
        post_2+=[i]
number=sum(index)
if number==0:
    print(find3(N,Y[0],Y[1],Y[2],X[0],X[1],X[2]))
elif number==1:
    N=find1(N,X[post_1[0]],Y[post_1[0]])
    N=find2(N,Y[post_2[0]],Y[post_2[1]],X[post_2[0]],X[post_2[1]])
    print(N)
elif number==2:
    N=find2(N,X[post_1[0]],X[post_1[1]],Y[post_1[0]],Y[post_1[1]])
    N=find1(N,Y[post_2[0]],X[post_2[0]])
    print(N)
else:
    N=find3(N,X[0],X[1],X[2],Y[0],Y[1],Y[2])
    print(N)

Submission status: Runtime Error

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

wa_orig = [1, ga, sa, ba]
wb_orig = [1, gb, sb, bb]

wa = []
wb = []
for i in range(len(wa_orig)):
  if wa_orig[i] <= wb_orig[i]:
    wa.append(wa_orig[i])
    wb.append(wb_orig[i])

dp = [0] * (n+1)
for i in range(1, n+1):
  d = []
  for ai in range(len(wa)):
    w1 = wa[ai]
    w2 = wb[ai]
    if i-w1 <0:
      continue
    d.append(dp[i-w1] + w2)
  dp[i] = max(d)


wa = []
wb = []
for i in range(len(wa_orig)):
  if wa_orig[i] >= wb_orig[i]:
    wa.append(wa_orig[i])
    wb.append(wb_orig[i])


nn = dp[-1]

if len(wa) == 2:
  # greedy
  w1 = wb[1]
  w2 = wa[1]
  value = (nn // w1) * w2 + (nn % w1)
  print(value)
  exit(0)
start = 0
start_v = 0
if len(wa) == 3:
  v1 = wb[1]
  v2 = wa[1]
  w1 = wb[2]
  w2 = wa[2]
  if v2/v1 > w2/w1:
    v1, v2 = w1, w2
  l = (v1 * w1) // math.gcd(v1, w1)
  lv = w2 * (l//w1)
  start = (nn // l) * l
  start_v = (nn // l) * lv


dp = [0] * (nn+1)
dp[start] = start_v
for i in range(start+1, nn+1):
  d = []
  for ai in range(len(wb)):
    w1 = wb[ai]
    w2 = wa[ai]
    if i-w1 <0:
      continue
    d.append(dp[i-w1] + w2)
  dp[i] = max(d)
print(dp[-1])

Submission status: Runtime Error

Solution code:
N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
dp = [0 for i in range(N+1)]
for i in range(1, N+1):
    koho = i
    if i >= ga:
        koho = max(koho, dp[i-ga] + gb)
    if i >= gb:
        koho = max(koho, dp[i-gb] + ga)
    if i >= sa:
        koho = max(koho, dp[i-sa] + sb)
    if i >= sb:
        koho = max(koho, dp[i-sb] + sa)
    if i >= ba:
        koho = max(koho, dp[i-ba] + bb)
    if i >= gb:
        koho = max(koho, dp[i-bb] + ba)
    dp[i] = koho
print(dp[N])

Submission status: Runtime Error

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

ans = 0
if (ga < gb) and (sa < sb) and (ba < bb):
    for G in range(5001):
        for S in range(5001):
            if ga * G + sa * S > N:
                continue
            B = (N - (ga * G + sa * S)) // ba
            tmp_ans = (N - (ga * G + sa * S + ba * B))
            tmp_ans += (G * gb) + (S * sb) + (B * bb)
            ans = max(ans, tmp_ans)

elif (ga < gb) and (sa < sb) and (ba >= bb):
    for G in range(5001):
        for S in range(5001):
            if ga * G + sa * S > N:
                continue
            tmp_ans = (N - (ga * G + sa * S))

            NB = tmp_ans // bb
            tmp_ans = tmp_ans % bb

            tmp_ans += (G * gb) + (S * sb) + (NB * ba)
            ans = max(ans, tmp_ans)

elif (ga < gb) and (sa >= sb) and (ba < bb):
    for G in range(5001):
        for B in range(5001):
            if ga * G + ba * B > N:
                continue
            tmp_ans = (N - (ga * G + ba * B))

            NS = tmp_ans // sb
            tmp_ans = tmp_ans % sb

            tmp_ans += (G * gb) + (NS * sa) + (B * bb)
            ans = max(ans, tmp_ans)

elif (ga >= gb) and (sa < sb) and (ba < bb):
    for S in range(5001):
        for B in range(5001):
            if sa * S + ba * B > N:
                continue
            remain = (N - (sa * S + ba * B))

            NG = tmp_ans // gb
            tmp_ans = remain % gb

            tmp_ans += (NG * ga) + (S * sb) + (B * bb)
            ans = max(ans, tmp_ans)

elif (ga >= gb) and (sa >= sb) and (ba < bb):
    for B in range(5001):
        if ba * B > N:
            continue
        remain = (N - ba * B)
        for NG in range(5001):
            if NG * gb > remain:
                continue
            NS = (remain - NG * gb) // sb
            tmp_ans = remain % sb

            tmp_ans += (NG * ga) + (NS * sa) + (B * bb)
            ans = max(ans, tmp_ans)

elif (ga >= gb) and (sa < sb) and (ba >= bb):
    for S in range(5001):
        if sa * S > N:
            continue
        remain = (N - sa * S)
        for NG in range(5001):
            if NG * gb > remain:
                continue
            NB = (remain - NG * gb) // bb
            tmp_ans = remain % bb

            tmp_ans += (NG * ga) + (S * sb) + (NB * ba)
            ans = max(ans, tmp_ans)

elif (ga < gb) and (sa >= sb) and (ba >= bb):
    for G in range(5001):
        if ga * G > N:
            continue
        remain = (N - ga * G)
        for NS in range(5001):
            if NS * sb > remain:
                continue
            NB = (remain - NS * sb) // bb
            tmp_ans = remain % bb

            tmp_ans += (G * gb) + (NS * sa) + (NB * ba)
            ans = max(ans, tmp_ans)

else:
    for G in range(3):
        for S in range(3):
            if gb * G + sb * S > N:
                continue
            B = (N - (gb * G + sb * S)) // bb
            tmp_ans = (N - (gb * G + sb * S + bb * B))
            tmp_ans += (G * ga) + (S * sa) + (B * ba)
            ans = max(ans, tmp_ans)

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
AB = [(A[i]/B[i],i) for i in range(3)]
AB.sort()
AB1 = deepcopy(AB)
ans = 0
M = N
for i in range(3):
    ab,j = AB1.pop(0)
    if ab >= 1:
        break
    else:
        ans += (M//A[j])*(B[j])
        M = M % A[j]
        ans += M
M = ans
ans = 0
for i in range(3):
    ab,j = AB.pop()
    if ab < 1:
        break
    else:
        ans += (M//B[j])*(A[j])
        M = M % B[j]
        ans += M
print(ans)

Submission status: Runtime Error

Solution code:
def func1(rem,arr1,arr2,typ):
	arr11=[]
	for i in range(3):
		arr11.append((arr1[i],i))
	arr22=[]
	for i in range(3):
		arr22.append((arr2[i],i))

	if(typ==0):
		arr11.sort(reeverse=True)
		ans=0
		for i in range(3):
			index=arr11[i][1]
			ans+=(rem//arr2[index])*arr1[i]
			rem=rem%arr2[index]
		ans+=rem

	else:
		arr22.sort(reeverse=True)
		ans=0
		for i in range(3):
			index=arr22[i][1]
			ans+=(rem//arr1[index])*arr2[i]
			rem=rem%arr1[index]
		ans+=rem
	return ans


def func2(rem,arr1,arr2,bet):
	arr11=[]
	for i in range(3):
		arr11.append((arr1[i],i))
	arr22=[]
	for i in range(3):
		arr22.append((arr2[i],i))

	first=[]
	second=[]
	for i in range(3):
		if(bet[i]==0):
			first.append(arr11[i])
		else:
			second.append(arr22[i])

	first.sort(reverse=True)
	second.sort(reverse=True)
	ans=0
	for i in range(len(second)):
		index=second[i][1]
		ans+=(rem//arr1[index])*arr2[i]
		rem=rem%arr1[index]
	ans+=rem

	rem=ans
	ans=0
	for i in range(len(first)):
		index=first[i][1]
		ans+=(rem//arr2[index])*arr1[i]
		rem=rem%arr2[index]
	ans+=rem

	return ans

n=int(input())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
bet=[-1]*3
if(arr1[0]>=arr2[0]):
	bet[0]=0
elif(arr1[0]<=arr2[0]):
	bet[0]=1

if(arr1[1]>=arr2[1]):
	bet[1]=0
elif(arr1[1]<=arr2[1]):
	bet[1]=1

if(arr1[2]>=arr2[2]):
	bet[2]=0
elif(arr1[2]<=arr2[2]):
	bet[2]=1

ans=0
count1=bet.count(0)
count2=bet.count(1)
if(count2==0):
	ans=func1(n,arr1,arr2,0)
	print(ans)
elif(count1==0):
	ans=func1(n,arr1,arr2,1)
	print(ans)
else:
	ans=func2(n,arr1,arr2,bet)
	print(ans)

Submission status: Runtime Error

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

wa_orig = [1, ga, sa, ba]
wb_orig = [1, gb, sb, bb]

wa = []
wb = []
for i in range(len(wa_orig)):
  if wa_orig[i] <= wb_orig[i]:
    wa.append(wa_orig[i])
    wb.append(wb_orig[i])

dp = [0] * (n+1)
for i in range(1, n+1):
  d = []
  for ai in range(len(wa)):
    w1 = wa[ai]
    w2 = wb[ai]
    if i-w1 <0:
      continue
    d.append(dp[i-w1] + w2)
  dp[i] = max(d)


wa = []
wb = []
for i in range(len(wa_orig)):
  if wa_orig[i] >= wb_orig[i]:
    wa.append(wa_orig[i])
    wb.append(wb_orig[i])


nn = dp[-1]

if len(wa) == 2:
  # greedy
  w1 = wb[1]
  w2 = wa[1]
  value = (nn // w1) * w2 + (nn % w1)
  print(value)
  exit(0)
start = 0
if len(wa) == 3:
  v1 = wb[1]
  v2 = wa[1]
  w1 = wb[2]
  w2 = wa[2]
  if v2/v1 > w2/w1:
    v1, v2 = w1, w2
  l = (v1 * w1) // math.gcd(v1, w1)
  lv = w2 * (l//w1)
  start = (nn // l) * l
  start_v = (nn // l) * lv


dp = [0] * (nn+1)
dp[start] = start_v
for i in range(start+1, nn+1):
  d = []
  for ai in range(len(wb)):
    w1 = wb[ai]
    w2 = wa[ai]
    if i-w1 <0:
      continue
    d.append(dp[i-w1] + w2)
  dp[i] = max(d)
print(dp[-1])

Submission status: Runtime Error

Solution code:
n = int(input())
metal_a = [int(x) for x in input().split()]
metal_b = [int(x) for x in input().split()]

sup_a = []
sup_b = []
for i in range(3):
    a_i = metal_a[i]
    b_i = metal_b[i]
    if a_i > b_i:
        sup_a.append((a_i, b_i))
    elif a_i < b_i:
        sup_b.append((a_i, b_i))

len_a = len(sup_a)
len_b = len(sup_b)

if len_a + len_b == 0:
    pass
elif len_a + len_b == 1:
    if len_a == 1:
        a, b = sup_a[0]
        n = int(n / b) * a + n % b
    else:
        a, b = sup_b[0]
        n = int(n / a) * b + n % a
elif len_a + len_b == 2:
    if len_a == 1 and len_b == 1:
        a, b = sup_b[0]
        n = int(n / a) * b + n % a
        a, b = sup_a[0]
        n = int(n / b) * a + n % b
    elif len_a == 2:
        a1, b1 = sup_a[0]
        a2, b2 = sup_a[1]
        
        n1 = int(n / b1) * a1 + int((n % b1) / b2) * a2 + (n % b1) % b2
        n2 = int(n / b2) * a2 + int((n % b2) / b1) * a1 + (n % b2) % b1
        n = max(n1, n2)
    else:
        a1, b1 = sup_b[0]
        a2, b2 = sup_b[1]

        n1 = int(n / a1) * b1 + int((n % a1) / a2) * b2 + (n % a1) % a2
        n2 = int(n / a2) * b2 + int((n % a2) / a1) * b1 + (n % a2) % a1
        n = max(n1, n2)
else:
    if len_a == 0:
        r = [(i, sup_b[i][1] / sup_b[i][0], sup_b[i][0], sup_b[i][1]) for i in range(3)]
        r.sort(key=lambda x: x[1], reverse=True)
        ns = []
        rem = n
        for _, _, a, b in r:
            ns.append(int(rem / a)) * b
            rem = rem % a
        n = sum(ns) + rem
    elif len_a == 1:
        a1, b1 = sup_b[0]
        a2, b2 = sup_b[1]

        n1 = int(n / a1) * b1 + int((n % a1) / a2) * b2 + (n % a1) % a2
        n2 = int(n / a2) * b2 + int((n % a2) / a1) * b1 + (n % a2) % a1
        n = max(n1, n2)
        a, b = sup_a[0]
        n = int(n / b) * a + n % b
    elif len_a == 2:
        a, b = sup_b[0]
        n = int(n / a) * b + n % a

        a1, b1 = sup_a[0]
        a2, b2 = sup_a[1]
        
        n1 = int(n / b1) * a1 + int((n % b1) / b2) * a2 + (n % b1) % b2
        n2 = int(n / b2) * a2 + int((n % b2) / b1) * a1 + (n % b2) % b1
        n = max(n1, n2)
    else:
        r = [(i, sup_a[i][0] / sup_a[i][1], sup_a[i][0], sup_a[i][1]) for i in range(3)]
        r.sort(key=lambda x: x[1], reverse=True)
        ns = []
        rem = n
        for _, _, a, b in r:
            ns.append(int(rem / a)) * b
            rem = rem % a
        n = sum(ns) + rem

print(n)

Submission status: Runtime Error

Solution code:
def getlist():
	return list(map(int, input().split()))

N = int(input())
A = getlist()
B = getlist()


L = [0 for i in range(N + 10)]
l1 = []
l2 = []
for i in range(3):
	if B[i] - A[i] > 0:
		l1.append([A[i], B[i]])
	elif B[i] - A[i] < 0:
		l2.append([B[i], A[i]])

c = 0
n = 0
for i in range(N + 1):
	for j in range(len(l1)):
		x = l1[j][0]
		if i + x <= N:
			if L[i] + l1[j][1] > L[i + x]:
				L[i + x] = L[i] + l1[j][1]
		if L[i] > c:
			c = L[i]
			n = i

N += c - n
if len(l2) != 2:
	c = 0
	n = 0
	L = [0 for i in range(N + 10)]
	for i in range(N + 1):
		for j in range(len(l2)):
			x = l2[j][0]
			if i + x <= N:
				if L[i] + l2[j][1] > L[i + x]:
					L[i + x] = L[i] + l2[j][1]
			if L[i] > c:
				c = L[i]
				n = i
	N += c - n
	print(N)
else:
	if l2[2][0] < l2[1][0] and l2[2][1] / l2[2][0] > l2[1][1] / l2[1][0]:
		N += int(N // l2[2][0]) * l2[2][1] + (N % l2[2][0])
		print(N)
	elif l2[1][0] < l2[0][0] and l2[1][1] / l2[1][0] > l2[2][1] / l2[2][0]:
		N += int(N // l2[1][0]) * l2[1][1] + (N % l2[1][0])
		print(N)
	else:
		pass

Submission status: Runtime Error

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


ups = []
downs = []
eqs = []
for i in range(3):
    if A[i] < B[i]:
        ups.append((A[i], B[i]))
    elif A[i] == B[i]:
        eqs.append((A[i], B[i]))
    else:
        downs.append((A[i], B[i]))
ans = N

if len(eqs) == 3:
    print(N)
    exit()

if len(ups) == 0:
    tmp = A
    A = B
    B = tmp
    tmp = ups
    ups = downs
    downs = tmp

if len(ups) == 3:
    gold_diff = B[0]-A[0]
    silver_diff = B[1]-A[1]
    cupper_diff = B[2]-A[2]
    for gold in range(N//A[0]+1):
        left = N - gold*A[0]
        for silver in range(left//A[1]+1):
            cupper = (left - silver*A[1])//A[2]
            tmp_ans = (N
                       + gold*gold_diff
                       + silver*silver_diff
                       + cupper*cupper_diff)
            ans = max(ans, tmp_ans)
    print(ans)
elif len(ups) == 2:
    gold_diff = ups[0][1]-ups[0][0]
    silver_diff = ups[1][1]-ups[1][0]
    for gold in range(N//ups[0][0]+1):
        left = N - gold*ups[0][0]
        silver = left // ups[1][0]
        tmp_ans = (N
                   + gold*gold_diff
                   + silver*silver_diff)
        ans = max(ans, tmp_ans)
    if len(downs) == 1:
        diff = downs[0][0]-downs[0][1]
        ans += (ans // downs[0][1]) * diff
    print(ans)
elif len(ups) == 1:
    bbans += (N//ups[0][0])*(ups[0][1]-ups[0][0])
    if len(downs) == 2:
        tmp_ans = bbans
        gold_diff = downs[0][0]-downs[0][1]
        silver_diff = downs[1][0]-downs[1][1]
        for gold in range(tmp_ans//downs[0][1]+1):
            left = tmp_ans - gold*downs[0][1]
            silver = left // downs[1][1]
            tmp_ans = (tmp_ans
                       + gold*gold_diff
                       + silver*silver_diff)
            ans = max(ans, tmp_ans)
    elif len(downs) == 1:
        ans += (ans//downs[0][1])*(downs[0][0]-downs[0][1])
    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())

    changes = [(ga, gb), (sa, sb), (ba, bb)]
    changes.sort(key=lambda x: x[1] / x[0], reverse=True)
    new_n = 0
    if changes[0][1] < changes[0][0]:
        pass
    elif changes[1][1] < changes[1][0]:
        new_n += (n // changes[0][0]) * changes[0][1]
        n %= changes[0][0]
    else:
        x1, y1 = changes[0]
        x2, y2 = changes[1]
        x3, y3 = changes[2]
        new_curr_n = 0
        for i in range(1 + n // x1):
            curr = i * y1
            curr_n = n - x1 * i
            
            for j in range(1 + curr_n // x2):
            plus = (curr_n // x2) * y2
            curr_n %= x2

            if y3 > x3:
                plus += (curr_n // x3) * y3
                curr_n %= x3
                
                if curr_n:
                    curr_n += 1
                
            if curr + plus > new_n:
                new_n = curr + plus
                new_curr_n = curr_n
            elif curr + plus == new_n:
                new_curr_n = max(new_curr_n, curr_n)
                
        if new_n:
            n = new_curr_n
    n += new_n

    changes = [(gb, ga), (sb, sa), (bb, ba)]
    changes.sort(key=lambda x: x[1] / x[0], reverse=True)
    new_n = 0
    if changes[0][1] < changes[0][0]:
        pass
    elif changes[1][1] < changes[1][0]:
        new_n += (n // changes[0][0]) * changes[0][1]
        n %= changes[0][0]
    else:
        x1, y1 = changes[0]
        x2, y2 = changes[1]
        x3, y3 = changes[2]
        new_curr_n = 0
        for i in range(1 + n // x1):
            curr = i * y1
            curr_n = n - x1 * i
            plus = (curr_n // x2) * y2
            curr_n %= x2

            if y3 > x3:
                plus += (curr_n // x3) * y3
                curr_n %= x3

                if curr_n:
                    curr_n += 1

            if curr + plus > new_n:
                new_n = curr + plus
                new_curr_n = curr_n
            elif curr + plus == new_n:
                new_curr_n = max(new_curr_n, curr_n)
        if new_n:
            n = new_curr_n
    n += new_n
    print(n)


main()