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: Wrong Answer

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip


def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    a.append(1)
    b.append(1)

    c = sorted(zip(a, b), key=lambda x: (abs(x[0] - x[1]), min(x[0], x[1])))

    for ai, bi in c:
        mi = min(ai, bi)
        ma = max(ai, bi)

        t = ma * (n // mi)
        n %= mi
        n += t

    print(n)



# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    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(b"\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()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    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()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
ga,sa,ba=map(int,input().split())
gb,sb,bb=map(int,input().split())
la=[ga,sa,ba]
lb=[gb,sb,bb]
ll=[0,0,0]
s1=[gb/ga,sb/sa,bb/ba]
s1s=sorted(s1)
#step1
ind=s1.index(s1s[2])
if n>la[ind] and s1s[2]>1:
  ll[ind]+=n//la[ind]
  n=n%la[ind]
ind=s1.index(s1s[1])
if n>la[ind] and s1s[1]>1:
  ll[ind]+=n//la[ind]
  n=n%la[ind]
ind=s1.index(s1s[0])
if n>la[ind] and s1s[0]>1:
  ll[ind]+=n//la[ind]
  n=n%la[ind]
#step2kankin
n+=sum([ll[_]*lb[_] for _ in range(3)])
ll=[0,0,0]
#step2buy
ind=s1.index(s1s[0])
if n>la[ind] and s1s[0]<1:
  ll[ind]+=n//lb[ind]
  n=n%lb[ind]
ind=s1.index(s1s[1])
if n>la[ind] and s1s[1]<1:
  ll[ind]+=n//lb[ind]
  n=n%lb[ind]
ind=s1.index(s1s[2])
if n>lb[ind] and s1s[2]<1:
  ll[ind]+=n//lb[ind]
  n=n%lb[ind]
#step3kankin
n+=sum([ll[_]*la[_] for _ in range(3)])
print(n)

Submission status: Wrong Answer

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

rs = [(ga,gb),(sa,sb),(ba,bb)]
rs.sort(key = lambda x:x[1]/x[0])

d = N
e = 0
for a,b in reversed(rs):
    if a >= b: break
    n,d = divmod(d,a)
    e += n*b
d += e

e = 0
for a,b in rs:
    if b >= a: break
    n,d = divmod(d,b)
    e += n*a
print(d+e)

Submission status: Wrong Answer

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


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]
print(ans+M)

Submission status: Wrong Answer

Solution code:
def getN():
    return int(input())

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

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



n = getN()
pra = getlist()
prb = getlist()

rita = []
ritb = []
for a, b in zip(pra, prb):
    rita.append(a/b)
    ritb.append(b/a)

if max(rita) < max(ritb):
    # こちらですべて金に帰ろ(問題文と同じ)
    idx = sorted(range(len(ritb)), key=lambda k: -ritb[k])
    pra = [pra[k] for k in idx]
    prb = [prb[k] for k in idx]
    kin, gin, dou = 0, 0, 0
    kin = n // pra[0]
    n -= kin * pra[0]

    if ritb[idx[1]] > 1:
        gin = n // pra[1]
        n -= gin * pra[1]

    if ritb[idx[2]] > 1:
        dou = n // pra[2]
        n -= dou * pra[2]

    print(kin, gin, dou)
    print(n + kin*prb[0] + gin*prb[1] + dou*prb[2])
    import sys; sys.exit()

else:
    # 向こうで仕入れ
    idx = sorted(range(len(rita)), key=lambda k: -rita[k])
    pra = [pra[k] for k in idx]
    prb = [prb[k] for k in idx]

    if rita[idx[2]] > 1:
        # こちらで買うものなし
        kin = n // prb[0]
        n -= kin * prb[0]
        n += kin * pra[1]
        print(n)
        
        import sys;sys.exit()
        
    else:
        #銅かっとけ
        dou = n // pra[2]
        n -= dou * pra[2]
        
        if rita[idx[1]] > 1:
            #銀も買っとけ
            gin = n // pra[1]
            n -= gin * pra[1]
            
            n += gin * prb[1]
            
        n += dou * prb[2]
        
        kin = n // prb[0]
        n -= kin * prb[0]
        n += kin * pra[0]
        
        print(n)

Submission status: Wrong Answer

Solution code:
import itertools


def main():
  n = int(input())
  a = list(map(int, input().split()))
  b = list(map(int, input().split()))

  # A -> B
  y = 0

  for s in itertools.permutations(range(3)):
    r = n
    x = 0

    for i in s:
      if a[i] >= b[i]:
        continue

      x += (r // a[i]) * b[i]
      r = r % a[i]

    x += r
    y = max(x, y)

  # B -> A
  n = y
  y = 0

  for s in itertools.permutations(range(3)):
    r = n
    x = 0

    for i in s:
      if b[i] >= a[i]:
        continue

      x += (r // b[i]) * a[i]
      r = r % b[i]

    x += r
    y = max(x, y)

  print(y)


if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
N = int(input())
Ag, As, Ab = map(int, input().split())
Bg, Bs, Bb = map(int, input().split())

ABg = N % Ag + (N // Ag) * Bg
ABb = N % Ab + (N // Ab) * Bb
ABs = N % As + (N // As) * Bs

N = max(ABg, ABb, ABs)

ABAg = N % Bg + (N // Bg) * Ag
ABAb = N % Bb + (N // Bb) * Ab
ABAs = N % Bs + (N // Bs) * As

ans = max(ABAg, ABAb, ABAs)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
ga,sa,ba = map(int, input().split())
gb,sb,bb = map(int, input().split())
gmax = max(gb,ga)
gmin = min(gb,ga)
smax = max(sb,sa)
smin = min(sb,sa)
bmax = max(bb,ba)
bmin = min(bb,ba)
count = 0
a = 0

if max(gmax/gmin,smax/smin,bmax/bmin)==gmax/gmin:
    count = (n//gmin)*gmax
    a = n%gmin
    if smax/smin >=bmax/bmin:
        if a>=smin:
            count = count + (a//smin)*smax
            a = a%smin
        if a >= bmin:
            count = count + (a//bmin)*bmax
    else:
        if a >= bmin:
            count = count + (a//bmin)*bmax
            a = a%bmin
        if a>=smin:
            count = count + (a//smin)*smax
elif max(gmax/gmin,smax/smin,bmax/bmin)==smax/smin:
    count = (n//smin)*smax
    a = n%smin
    if gmax/gmin >=bmax/bmin:
        if a>=gmin:
            count = count + (a//gmin)*gmax
            a = a%gmin
        if a >= bmin:
            count = count + (a//bmin)*bmax
    else:
        if a >= bmin:
            count = count + (a//bmin)*bmax
            a = a%bmin
        if a>=gmin:
            count = count + (a//gmin)*gmax
else:
    count = (n//bmin)*bmax
    a = n%bmin
    if gmax/gmin >=smax/smin:
        if a>=gmin:
            count = count + (a//gmin)*gmax
            a = a%gmin
        if a >= smin:
            count = count + (a//smin)*smax
    else:
        if a >= smin:
            count = count + (a//smin)*smax
            a = a%smin
        if a>=gmin:
            count = count + (a//gmin)*gmax

Submission status: Wrong Answer

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:
    ans += (ans//ups[0][0])*(ups[0][1]-ups[0][0])
    if len(downs) == 2:
        gold_diff = downs[0][0]-downs[0][1]
        silver_diff = downs[1][0]-downs[1][1]
        for gold in range(N//downs[0][1]+1):
            left = N - gold*downs[0][1]
            silver = left // downs[1][1]
            tmp_ans = (N
                       + 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: Wrong Answer

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
            plus = (curr_n // x2) * y2
            curr_n %= x2

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

            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_curr_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 + 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_curr_n:
            n = new_curr_n
    n += new_n
    print(n)


main()

Submission status: Wrong Answer

Solution code:
N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
para = [gb/ga, sb/sa, bb/ba]
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
        if (N < ga) and (N < sa) and (N < ba):
            break
    else:
        break
    i += 1
N += ans
i = 0
para = [ga/gb, sa/sb, ba/bb]
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
        if (N < gb) and (N < sb) and (N < bb):
            break
    else:
        break
    i += 1
print(ans)

Submission status: Wrong Answer

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

Achange = []
Bchange = []

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

#print(Achange, Bchange)

# 最初のA交換所→B交換所
Dmax1 = N
if Achange:
  if len(Achange)==3:
    i = Achange[0]
    j = Achange[1]
    k = Achange[2]
    for x in range(5001):
      for y in range(5001):
        if A[i]*x+B[j]*y <= N:
          Dmax1 = max(Dmax1, (N-A[i]*x-B[j]*y)%A[k] + (N-A[i]*x-B[j]*y)//A[k] * B[k])
    
  if len(Achange)==2:
    i = Achange[0]
    j = Achange[1]
    for x in range(5001):
      if A[i]*x <= N:
          Dmax1 = max(Dmax1, (N-A[i]*x)%A[j] + (N-A[i]*x)//A[j] * B[j])
    
  if len(Achange)==1:
    i = Achange[0]
    Dmax1 = max(Dmax1, N%A[i] + (N//A[i] * B[i]))
    
#print(Dmax1)    

#次のB交換所→A交換所
Achange = Bchange
Dmax2 = Dmax1

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

Submission status: Wrong Answer

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

g_ratio = gb / ga
s_ratio = sb / sa
b_ratio = bb / ba

lst = []
lst.append([0, ga, g_ratio])
lst.append([1, sa, s_ratio])
lst.append([2, ba, b_ratio])

lst2 = sorted(lst, key=lambda x:-x[2])
outs = [0, 0, 0]
n0s = [0, 0, 0]
n = N
for i in range(3):
    #print(lst2[i])
    a0 = lst2[i][1]
    ratio0 = lst2[i][2]
    if ratio0 <= 1:
        continue
    n0 = (n // a0) * a0
    #print(n0, a0)
    n -= n0
    n0s[i] = n0
    outs[i] = int(n0 * ratio0)
out = sum(outs) + (N - sum(n0s))
print(out)

Submission status: Wrong Answer

Solution code:
import itertools

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

a = [1]+a
b = [1]+b


ans = 0

l = [0,1,2,3]

for v in itertools.permutations(l,4):
    p = [n, 0, 0, 0]
    q = [0, 0, 0, 0]
    for x in v:
        for i in range(4):
            q[x] += p[0]//a[i]
            p[0] = p[0]%a[i]
    s=[p[i]+q[i] for i in range(4)]

    q = [0,0,0,0]
    for i in range(4):
        q[0] += s[i] * b[i]
    r=[q[i] for i in range(4)]

    for w in itertools.permutations(l,4):
        t = [r[i] for i in range(4)]
        q = [0, 0, 0, 0]
        for y in w:
            for i in range(4):
                q[y] += t[0] // b[i]
                t[0] = t[0] % b[i]
        u = [t[i] + q[i] for i in range(4)]


        q = [0,0,0,0]
        for i in range(4):
            q[0] += u[i] * a[i]
        o = [q[i] for i in range(4)]
        ans = max(ans, o[0])

print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

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

a2brate = []
b2arete = []
for i in range(3):
  a2brate.append(b[i] / a[i])
  b2arete.append(a[i] / b[i])

result = 0
itr = list(itertools.permutations([0, 1, 2]))

for ii in itr:
  beforN = N
  afterN = 0
  for i in ii:
    if a2brate[i] > 1:      
      afterN += beforN // a[i] * b[i]
      beforN -= beforN // a[i] * a[i]

  afterN += beforN
  if afterN > result:
    result = afterN

result2 = 0
for ii in itr:
  beforN = result
  afterN = 0
  for i in ii:
    if b2arete[i] > 1:      
      afterN += beforN // b[i] * a[i]
      beforN -= beforN // b[i] * b[i]

  afterN += beforN
  if afterN > result2:
    result2 = afterN

print(result2)

Submission status: Wrong Answer

Solution code:
def f(z, a, b):
    if a > b:
        return 0
    return z
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
res = n
for x in range(n + 1):
    for y in range(n - x + 1):
        z = f(n - x - y, a[2], b[2])
        ans = 0
        ans += (x // a[0]) * b[0]
        ans += (y // a[1]) * b[1]
        ans += (z // a[2]) * b[2]
        res = max(res, ans)
n = res
a, b = b, a
for x in range(n + 1):
    for y in range(n - x + 1):
        z = f(n - x - y, a[2], b[2])
        ans = 0
        ans += (x // a[0]) * b[0]
        ans += (y // a[1]) * b[1]
        ans += (z // a[2]) * b[2]
        res = max(res, ans)
print(res)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

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

N = INT()
gsb_A = LIST()
gsb_B = LIST()
gsb_delta = {}
for i in range(3):
		if min(gsb_A[i], gsb_B[i]) in gsb_delta:
			gsb_delta[min(gsb_A[i], gsb_B[i])] = max(max(gsb_A[i], gsb_B[i]), gsb_delta[min(gsb_A[i], gsb_B[i])])
		else:
			gsb_delta[min(gsb_A[i], gsb_B[i])] = max(gsb_A[i], gsb_B[i])
# print(gsb_delta)
if N < min(gsb_delta.values()):
	print(N)
else:
	dp = [0] * (N+1)
	for i in range(N+1):
		for j in gsb_delta:
			if i-j >= 0:
				dp[i] = max(dp[i], dp[i-j] + gsb_delta[j])
	# print(dp)
	# print(dp[N])
	ans = N
	for i in range(N+1):
		ans = max(ans, dp[i]+(N-i))
	print(ans)

Submission status: Wrong Answer

Solution code:
# Enter your code here. Read input from STDIN. Print output to STDOUT

def find3(N,a,b,c,A,B,C):
    MAX=N
    for x in xrange(5001):
        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(5001):
        temp=N-a*x
        if temp>=0:
            y=temp//b
            MAX=max(MAX,N-(a*x+b*y)+(A*x+B*y))
    
    if (B-b)>(A-a):
        a,A,b,B=b,B,a,A 
    x=N//a 
    y=(N-a*x)//b 
    for xx in range(x-5001,x+5001):
        temp=N-a*xx
        if temp>=0:
            y=temp//b
            MAX=max(MAX,N-(a*xx+b*y)+(A*xx+B*y))
    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: Wrong Answer

Solution code:
def change(N, A, B):
    ans = N
    for i in range(N//A[0]+1):
        if B[0] <= A[0] and i >= 1:
            break
        rem = N - A[0] * i
        for j in range(rem//A[1]+1):
            if B[1] <= A[1] and j >= 1:
                break
            remm = rem - A[1] * j
            if B[2] <= A[2]:
                k = 0
            else:
                k = remm // A[2]
            remmm = remm - k
            ans = max(N, remmm + i*B[0] + j*B[1] + k*B[2])
    return ans

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
a1, a2, a3 = map(int, input().split())
b1, b2, b3 = map(int, input().split())
A = [a1, a2, a3]
B = [b1, b2, b3]
if (a1<=b1 and a2<=b2 and a3<=b3) or (a1>=b1 and a2>=b2 and a3>=b3):
    if a1>b1 or a2>b2 or a3>b3:
        A, B = B, A
    dp = [[-float("inf")]*(N+1+5050) for _ in range(4)]
    dp[0][0] = 0
    for i in range(3):
        ai = A[i]
        bi = B[i]
        assert ai <= bi
        for j in range(N+1):
            if j-ai>=0:
                dp[i+1][j] = max(dp[i][j], dp[i+1][j-ai]+bi)
            else:
                dp[i+1][j] = dp[i][j]
    print(max(dp[3]))

    #print(dp[3][:30])
else:
    C, D = [], []
    for a, b in zip(A, B):
        if a <= b:
            C.append([a, b])
        else:
            D.append([b, a])

    if len(C)==1:
        a, b = C[0]
        q, n = divmod(N, a)
        N_ = q*b + n
    else:
        N_ = N
        (a1, b1), (a2, b2) = C
        for i in range(N + 1):
            j = N - i
            q1, n1 = divmod(i, a1)
            q2, n2 = divmod(j, a2)
            N_ = max(N_, q1 * b1 + q2 * b2 + n1 + n2)

    if len(D)==1:
        a, b = D[0]
        q, n = divmod(N_, a)
        print(q*b + n)
        #print("a")
    else:
        ans = N_
        (a1, b1), (a2, b2) = D
        for i in range(N_+1):
            j = N_ - i
            q1, n1 = divmod(i, a1)
            q2, n2 = divmod(j, a2)
            ans = max(ans, q1*b1+q2*b2+n1+n2)
        print(ans)

Submission status: Wrong Answer

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]):
            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:
    ans += (ans//ups[0][0])*(ups[0][1]-ups[0][0])
    if len(downs) == 2:
        gold_diff = downs[0][0]-downs[0][1]
        silver_diff = downs[1][0]-downs[1][1]
        for gold in range(N//downs[0][1]+1):
            left = N - gold*downs[0][1]
            silver = left // downs[1][1]
            tmp_ans = (N
                       + 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: Wrong Answer

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

gmin = min(ga,gb)
gmax = max(ga,gb)
smin = min(sa,sb)
smax = max(sa,sb)
bmin = min(ba,bb)
bmax = max(ba,bb)

if min(gmin,smin,bmin) > n:
    print(n)
else:
    ans = 0
    for i in range( (n//gmin) + 1):
        k = n - gmin*i
        for s in range( (k//smin) +1 ):
            ba = k - s*smin
            ja = ba//bmin
            if k >= 0 and s >= 0 and ja >= 0:
                ans = max(ans, i*gmax + s*smax + ja* bmax)
    print(ans)

Submission status: Wrong Answer

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
            plus = (curr_n // x2) * y2
            curr_n %= x2

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

            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)
        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 + 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)
        n = new_curr_n
    n += new_n
    print(n)


main()

Submission status: Wrong Answer

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

from operator import itemgetter
def AB(n):
    temp = [[int(n/ga)*gb,ga,gb],[int(n/sa)*sb,sa,sb],[int(n/ba)*bb,ba,bb]]
    temp.sort(reverse = True, key = itemgetter(0))
    return temp

def BA(n):
    temp = [[int(n/gb)*ga,ga,gb],[int(n/sb)*sa,sa,sb],[int(n/bb)*ba,ba,bb]]
    temp.sort(reverse = True, key = itemgetter(0))
    return temp


stock = 0
for nyan in AB(N):
    a = nyan[1]
    b = nyan[2]
    if N >= a and N < int(N/a)*b:
        stock += int(N/a)*b
        N %= a

N += stock
stock = 0
for nyan in BA(N):
    a = nyan[1]
    b = nyan[2]
    if N >= b and N < int(N/b)*a:
        stock += int(N/b)*a
        N %= b
print(N + stock)