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:
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=0
  for i in range(ans//s1+1):
    j=(ans-i*s1)//s2
    tmp=i*g1+j*g2+(n-i*s1-j*s2)
    ta=max(ta,tmp)
  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]
  r1=g1/s1
  r2=g2/s2
  r3=g3/s3
  tr=[[r1,idx1],[r2,idx2],[r3,idx3]]
  sorted(tr,reverse=True,key=lambda x:x[0])
  idx1=tr[0][1]
  idx2=tr[1][1]
  idx3=tr[2][1]
  s1=arr[0][idx1]
  s2=arr[0][idx2]
  s3=arr[0][idx3]
  g1=arr[1][idx1]
  g2=arr[1][idx2]
  g3=arr[1][idx3]
  r1=g1/s1
  r2=g2/s2
  r3=g3/s3
  ta=0
  for i in range(ans//s1+1):
    for j in range((ans-i*s1)//s2+1):
      k=(ans-i*s1-j*s2)//s3
      tmp=i*g1+j*g2+k*g3+(n-i*s1-j*s2-k*s3)
      ta=max(ta,tmp)
  ans=ta
if len(move2)==1:
  idx=move2[0]
  ans=arr[1][idx]*(ans//arr[0][idx])+ans%arr[0][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=0
  for i in range(ans//s1+1):
    j=(ans-i*s1)//s2
    tmp=i*g1+j*g2+(n-i*s1-j*s2)
    ta=max(ta,tmp)
  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]
  r1=g1/s1
  r2=g2/s2
  r3=g3/s3
  tr=[[r1,idx1],[r2,idx2],[r3,idx3]]
  sorted(tr,reverse=True,key=lambda x:x[0])
  idx1=tr[0][1]
  idx2=tr[1][1]
  idx3=tr[2][1]
  s1=arr[1][idx1]
  s2=arr[1][idx2]
  s3=arr[1][idx3]
  g1=arr[0][idx1]
  g2=arr[0][idx2]
  g3=arr[0][idx3]
  r1=g1/s1
  r2=g2/s2
  r3=g3/s3
  ta=0
  for i in range(ans//s1+1):
    for j in range((ans-i*s1)//s2+1):
      k=(ans-i*s1-j*s2)//s3
      tmp=i*g1+j*g2+k*g3+(n-i*s1-j*s2-k*s3)
      ta=max(ta,tmp)
  ans=ta
print(ans)

Submission status: Wrong Answer

Solution code:
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=0
  for i in range(ans//s1+1):
    j=(ans-i*s1)//s2
    tmp=i*g1+j*g2+(n-i*s1-j*s2)
    ta=max(ta,tmp)
  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=0
  for i in range(ans//s1+1):
    for j in range((ans-i*s1)//s2+1):
      k=(ans-i*s1-j*s2)//s3
      tmp=i*g1+j*g2+k*g3+(n-i*s1-j*s2-k*s3)
      ta=max(ta,tmp)
  ans=ta
if len(move2)==1:
  idx=move2[0]
  ans=arr[1][idx]*(ans//arr[0][idx])+ans%arr[0][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=0
  for i in range(ans//s1+1):
    j=(ans-i*s1)//s2
    tmp=i*g1+j*g2+(n-i*s1-j*s2)
    ta=max(ta,tmp)
  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=0
  for i in range(ans//s1+1):
    for j in range((ans-i*s1)//s2+1):
      k=(ans-i*s1-j*s2)//s3
      tmp=i*g1+j*g2+k*g3+(n-i*s1-j*s2-k*s3)
      ta=max(ta,tmp)
  ans=ta
print(ans)

Submission status: Wrong Answer

Solution code:
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[1][idx1]
  s2=arr[1][idx2]
  g1=arr[0][idx1]
  g2=arr[0][idx2]
  for i in range(ans//s1+1):
    j=(ans-i*s1)//s2
    tmp=i*g1+j*g2+(n-i*s1-j*s2)
    ans=max(ans,tmp)
elif len(move1)==3:
  idx1=move1[0]
  idx2=move1[1]
  idx3=move1[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]
  for i in range(ans//s1+1):
    for j in range((ans-i*s1)//s2+1):
      k=(ans-i*s1-j*s2)//s3
      tmp=i*g1+j*g2+k*g3+(n-i*s1-j*s2-k*s3)
      ans=max(ans,tmp)
if len(move2)==1:
  idx=move2[0]
  ans=arr[1][idx]*(ans//arr[0][idx])+ans%arr[0][idx]
elif len(move2)==2:
  idx1=move2[0]
  idx2=move2[1]
  s1=arr[0][idx1]
  s2=arr[0][idx2]
  g1=arr[1][idx1]
  g2=arr[1][idx2]
  for i in range(ans//s1+1):
    j=(ans-i*s1)//s2
    tmp=i*g1+j*g2+(n-i*s1-j*s2)
    ans=max(ans,tmp)
elif len(move2)==3:
  idx1=move2[0]
  idx2=move2[1]
  idx3=move2[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]
  for i in range(ans//s1+1):
    for j in range((ans-i*s1)//s2+1):
      k=(ans-i*s1-j*s2)//s3
      tmp=i*g1+j*g2+k*g3+(n-i*s1-j*s2-k*s3)
      ans=max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a,b,c = map(int,input().split())
d,e,f = map(int,input().split())
t = [a,b,c]
u = [d,e,f]
x=[]
y=[]
g=n
for i in range(3):
    if t[i]<u[i]:
        x.append([u[i]-t[i],t[i]])
    elif t[i]>u[i]:
        y.append([t[i]-u[i],u[i]])
x.sort(key=lambda z:(z[0],-z[1]),reverse=True)
y.sort(key=lambda z:(z[0],-z[1]),reverse=True)
for i in range(len(x)):
    g+=x[i][0]*(n//x[i][1])
    n%=x[i][1]
n=g
for i in range(len(y)):
    g+=y[i][0]*(n//y[i][1])
    n%=y[i][1]
print(g)

Submission status: Wrong Answer

Solution code:
n = int(input())
a,b,c = map(int,input().split())
d,e,f = map(int,input().split())
s = {0:a-d,1:b-e,2:c-f}
t = [[a,d],[b,e],[c,f]]
s = sorted(s.items(),key=lambda x:-x[1])
g=n
for i in range(3):
    if s[-1-i][1]>0:
        g+=s[-1-i][1]*n//t[s[-1-i][0]][1]
        n%=t[s[-1-i][0]][1]
    elif s[-1-i][1]<0:
        g+=-s[-1-i][1]*n//t[s[-1-i][0]][0]
        n%=t[s[-1-i][0]][0]
print(g)

Submission status: Wrong Answer

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

w1 = [ga,sa,ba]
v1 = [gb-ga,sb-sa,bb-ba]
w2 = [gb,sb,bb]
v2 = [ga-gb,sa-sb,ba-bb]

def napsac(w,v,u):
    dp = [[0]*(u+1) for _ in range(4)]
    for i in range(3):
        for j in range(u+1):
            if j >= w[i]:
                t = j // w[i]
                for k in range(t+1):
                    dp[i+1][j] = max(dp[i+1][j],dp[i][j-k*w[i]]+k*v[i])
    return dp[3][u]

m = napsac(w1,v1,n)
print(napsac(w2,v2,m+n)+m+n)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
m = n
for k in range(2):
    dp = [0 for j in range(m + 1)]
    dp[0] = m
    for i in range(3):
        if k == 0:
            w = a[i]
            v = b[i] - a[i]
        for j in range(w, m + 1):
            dp[j] = max(dp[j - w] + v, dp[j])
    m = max(dp)
    b, a = a, b
print(m)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
m = n
for k in range(2):
    dp = [0 for j in range(m + 1)]
    dp[0] = m
    for i in range(3):
        if k == 0:
            w = a[i]
            v = b[i] - a[i]
        for j in range(w, m + 1):
            dp[j] = max(dp[j - w] + v, dp[j])
    m = dp[-1]
    b, a = a, b
print(m)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
m = n
for k in range(2):
    dp = [[0 for j in range(m + 1)] for i in range(4)]
    dp[0][0] = m
    for i in range(3):
        if k == 0:
            w = a[i]
            v = b[i] - a[i]
        for j in range(m + 1):
            if j < w:
                dp[i + 1][j] = dp[i][j]
            else:
                dp[i + 1][j] = max(dp[i + 1][j - w] + v, dp[i][j])
    m = dp[-1][-1]
    b, a = a, b
print(m)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
m = n
for k in range(2):
    dp = [[m for j in range(m + 1)] for i in range(4)]
    for i in range(3):
        if k == 0:
            w = a[i]
            v = b[i] - a[i]
        for j in range(m + 1):
            if j < w:
                dp[i + 1][j] = dp[i][j]
            else:
                dp[i + 1][j] = max(dp[i + 1][j - w] + v, dp[i][j])
    m = max(dp[-1])
    b, a = a, b
print(m)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
from functools import reduce
inpl = lambda: list(map(int,input().split()))
def lcm(a,b):
    return a*b//gcd(a,b)
def lcm_list(numbers):
    return reduce(lcm, numbers)

N = int(input())
rate = []
rate.append(inpl())
rate.append(inpl())
AB = []
BA = []
for i in range(3):
    rA, rB = rate[0][i], rate[1][i]
    if rB > rA:
        AB.append((rA, rB))
    elif rA > rB:
        BA.append((rB, rA))

if len(AB) == 0:
    M = N
else:
    Nunit = lcm_list([ x[0] for x in AB ])
    q, r = N // Nunit, N % Nunit
    if q == 0:
        Ndp = N
    else:
        Ndp = Nunit
    dpAB = [0]*(Ndp+1)
    w = [0]*(len(AB)+1)
    for i in range(1,Ndp+1):
        k = 1
        for cost, ret in AB:
            if i >= cost:
                w[k] = dpAB[i-cost] + ret
                k += 1
        dpAB[i] = max(dpAB[i-1]+1, max(w))
    M = dpAB[N] if q==0 else dpAB[Nunit]*q + dpAB[r]

if len(BA) == 0:
    L = M
else:
    Munit = lcm_list([ x[0] for x in BA ])
    q, r = M // Munit, M % Munit
    if q == 0:
        Mdp = M
    else:
        Mdp = Munit
    dpBA = [0]*(Mdp+1)
    w = [0]*(len(BA)+1)
    for i in range(1,Mdp+1):
        k = 1
        for cost, ret in BA:
            if i >= cost:
                w[k] = dpBA[i-cost] + ret
                k += 1
        dpBA[i] = max(dpBA[i-1]+1, max(w))
    L = dpBA[M] if q==0 else dpBA[Munit]*q + dpBA[r]

print(L)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/diverta2019-2/tasks/diverta2019_2_d

import itertools
from collections import Counter
from collections import defaultdict
import bisect
from heapq import heappush, heappop


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

    # n = exchange(N, [ga, sa, ba], [gb, sb, bb])
    # exit()

    dp1 = [0] * (N + 1)

    # A で交換して B で全部どんぐりに戻す
    for i in range(1, N + 1):
        if i - ga >= 0:
            dp1[i] = max(dp1[i - 1] + 1, dp1[i - ga] + gb)
        if i - sa >= 0:
            dp1[i] = max(dp1[i], dp1[i - 1] + 1, dp1[i - sa] + sb)
        if i - ba >= 0:
            dp1[i] = max(dp1[i], dp1[i - 1] + 1, dp1[i - ba] + bb)
        # print(dp1)

    N2 = dp1[N]
    dp2 = [0] * (N2 + 1)

    # B で交換して A で全部どんぐりに戻す
    for i in range(1, N2 + 1):
        if i - ga >= 0:
            dp2[i] = max(dp2[i], dp2[i - 1] + 1, dp2[i - gb] + ga)
        if i - sa >= 0:
            dp2[i] = max(dp2[i], dp2[i - 1] + 1, dp2[i - sb] + sa)
        if i - ba >= 0:
            dp2[i] = max(dp2[i], dp2[i - 1] + 1, dp2[i - bb] + ba)
        # print(dp2)

    print(dp2[N2])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/diverta2019-2/tasks/diverta2019_2_d

import itertools
from collections import Counter
from collections import defaultdict
import bisect
from heapq import heappush, heappop


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

    # n = exchange(N, [ga, sa, ba], [gb, sb, bb])
    # exit()

    dp1 = [N] * (N + 1)

    # A で交換して B で全部どんぐりに戻す
    for i in range(1, N + 1):
        if i - ga >= 0:
            dp1[i] = max(dp1[i], dp1[i - ga] - ga + gb)
        if i - sa >= 0:
            dp1[i] = max(dp1[i], dp1[i - sa] - sa + sb)
        if i - ba >= 0:
            dp1[i] = max(dp1[i], dp1[i - ba] - ba + bb)
        # print(dp1)

    N2 = dp1[N]
    dp2 = [N2] * (N2 + 1)

    # B で交換して A で全部どんぐりに戻す
    for i in range(1, N2 + 1):
        if i - ga >= 0:
            dp2[i] = max(dp2[i], dp2[i - gb] - gb + ga)
        if i - sa >= 0:
            dp2[i] = max(dp2[i], dp2[i - sb] - sb + sa)
        if i - ba >= 0:
            dp2[i] = max(dp2[i], dp2[i - bb] - bb + ba)
        # print(dp2)

    print(dp2[N2])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/diverta2019-2/tasks/diverta2019_2_d

import itertools
from collections import Counter
from collections import defaultdict
import bisect
from heapq import heappush, heappop


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

    # n = exchange(N, [ga, sa, ba], [gb, sb, bb])
    # exit()

    dp1 = [N] * (N + 1)

    # A で交換して B で全部どんぐりに戻す
    for i in range(1, N + 1):
        if i - ga >= 0:
            dp1[i] = max(dp1[i - 1], dp1[i - ga] - ga + gb)
        if i - sa >= 0:
            dp1[i] = max(dp1[i], dp1[i - sa] - sa + sb)
        if i - ba >= 0:
            dp1[i] = max(dp1[i], dp1[i - ba] - ba + bb)
        # print(dp1)

    N2 = dp1[N]
    dp2 = [N2] * (N2 + 1)

    # B で交換して A で全部どんぐりに戻す
    for i in range(1, N2 + 1):
        if i - ga >= 0:
            dp2[i] = max(dp2[i - 1], dp2[i - gb] - gb + ga)
        if i - sa >= 0:
            dp2[i] = max(dp2[i], dp2[i - sb] - sb + sa)
        if i - ba >= 0:
            dp2[i] = max(dp2[i], dp2[i - bb] - bb + ba)
        # print(dp2)

    print(dp2[N2])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/diverta2019-2/tasks/diverta2019_2_d

import itertools
from collections import Counter
from collections import defaultdict
import bisect
from heapq import heappush, heappop

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

    dp1 = [0] * (N + 1)

    # A で交換して B で全部どんぐりに戻す
    for i in range(1, N + 1):
        if i - ga >= 0:
            dp1[i] = max(dp1[i], dp1[i - ga] + gb)
        if i - sa >= 0:
            dp1[i] = max(dp1[i], dp1[i - sa] + sb)
        if i - ba >= 0:
            dp1[i] = max(dp1[i], dp1[i - ba] + bb)

    N2 = dp1[N]
    dp2 = [0] * (N2 + 1)

    # B で交換して A で全部どんぐりに戻す
    for i in range(1, N2+ 1):
        if i - ga >= 0:
            dp2[i] = max(dp2[i], dp2[i - gb] + ga)
        if i - sa >= 0:
            dp2[i] = max(dp2[i], dp2[i - sb] + sa)
        if i - ba >= 0:
            dp2[i] = max(dp2[i], dp2[i - bb] + ba)

    print(dp2[N2])


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())

# A → B
dp = [[j for j in range(N+1)]for i in range(3)]

for i in range(N+1)[::-1]:
    dp[2][i] = (i//ba)*(bb-ba)
for i in range(2)[::-1]:
    if i == 0:
        weight = ga
        score = gb - ga
    else:
        weight = sa
        score = sb - sa
    for j in range(N+1):
        c = j // weight
        dp[i][j] = max(dp[i+1][j], dp[i+1][j-weight*c] + score*c)
n = N+dp[0][N]
# B → A r   r
dp = [[j for j in range(n+1)]for i in range(3)]

for i in range(n+1)[::-1]:
    dp[2][i] = (i//bb)*(ba-bb)
for i in range(2)[::-1]:
    if i == 0:
        weight = gb
        score = ga - gb
    else:
        weight = sb
        score = sa - sb
    for j in range(n+1):
        c = j // weight
        dp[i][j] = max(dp[i+1][j], dp[i+1][j-weight*c] + score*c)

print(n+dp[0][n])

Submission status: Wrong Answer

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

# A → B
dp = [[0 for j in range(N+1)]for i in range(3)]

for i in range(N+1)[::-1]:
    dp[2][i] = (i//ba)*(bb-ba)
for i in range(2)[::-1]:
    if i == 0:
        weight = ga
        score = gb - ga
    else:
        weight = sa
        score = sb - sa
    for j in range(N+1):
        c = j // weight
        dp[i][j] = max(dp[i+1][j], dp[i+1][j-weight*c] + score*c)
n = N+dp[0][N]
# B → A r   r
dp = [[0 for j in range(n+1)]for i in range(3)]

for i in range(n+1)[::-1]:
    dp[2][i] = (i//bb)*(ba-bb)
for i in range(2)[::-1]:
    if i == 0:
        weight = gb
        score = ga - gb
    else:
        weight = sb
        score = sa - sb
    for j in range(n+1):
        c = j // weight
        dp[i][j] = max(dp[i+1][j], dp[i+1][j-weight*c] + score*c)

print(n+dp[0][n])

Submission status: Wrong Answer

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

# A → B
dp = [[0 for j in range(N+1)]for i in range(3)]

for i in range(N+1)[::-1]:
    dp[2][i] = (i//ba)*(bb-ba)
for i in range(2)[::-1]:
    if i == 0:
        weight = ga
        score = gb - ga
    else:
        weight = sa
        score = sb - sa
    for j in range(N+1):
        if score > 0:
            cand = []
            for k in range((j//weight)+1):
                cand.append(dp[i+1][j-k*weight] + score*k)
            dp[i][j] = max(cand)
        else:
            dp[i][j] = dp[i+1][j]
n = N+dp[0][N]
# B → A r   r
dp = [[0 for j in range(n+1)]for i in range(3)]

for i in range(n+1)[::-1]:
    dp[2][i] = (i//bb)*(ba-bb)
for i in range(2)[::-1]:
    if i == 0:
        weight = gb
        score = ga - gb
    else:
        weight = sb
        score = sa - sb
    for j in range(n+1):
        if score > 0:
            cand = []
            for k in range((j//weight)+1):
                cand.append(dp[i+1][j-k*weight] + score*k)
            dp[i][j] = max(cand)
        else:
            dp[i][j] = dp[i+1][j]

print(n+dp[0][n])

Submission status: Wrong Answer

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

# A → B
dp = [[0 for j in range(N+1)]for i in range(3)]

for i in range(N+1)[::-1]:
    dp[2][i] = (i//ba)*(bb-ba)
for i in range(2)[::-1]:
    if i == 1:
        weight = ga
        score = gb - ga
    else:
        weight = sa
        score = sb - sa
    for j in range(N+1):
        cand = []
        for k in range((j//weight)+1):
            cand.append(dp[i+1][j-k*weight] + score*k)
        dp[i][j] = max(cand)
n = N+dp[0][N]

# B → A
dp = [[0 for j in range(n+1)]for i in range(3)]

for i in range(n+1)[::-1]:
    dp[2][i] = (i//bb)*(ba-bb)
for i in range(2)[::-1]:
    if i == 1:
        weight = gb
        score = ga - gb
    else:
        weight = sb
        score = sa - sb
    for j in range(n+1):
        cand = []
        for k in range((j//weight)+1):
            cand.append(dp[i+1][j-k*weight] + score*k)
        dp[i][j] = max(cand)

print(n+dp[0][n])

Submission status: Wrong Answer

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

r_list = [[ga, gb], [sa, sb], [ba, bb]]
a_list = list(filter(lambda x: x[0] > x[1], r_list))
a_list.sort(key=lambda x:x[0]/x[1], reverse=True)

b_list = list(filter(lambda x: x[1] > x[0], r_list))
b_list.sort(key=lambda x:x[1]/x[0], reverse=True)

# A->B
n = N
diff = 0
for b in b_list:
    if n > b[0]:
        c = n // b[0]
        n -= c*b[0]
        diff += c*b[1]
n += diff
# B -> A
diff = 0
for a in a_list:
    if n > a[1]:
        c = n // a[1]
        n -= c*a[1]
        diff += c*a[0]
n += diff
print(n)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)

def don(dk, rate, amin):
    if not rate:
        return dk
    if len(rate)==1:
        a,b=rate[0]
        p,m=divmod(dk,a)
        return p*b+m
    if len(rate)==2:
        a0,b0=rate[0]
        a1,b1=rate[1]
        if b0/a0<b1/a1:
            a0,b0,a1,b1=a1,b1,a0,b0
        p,m=divmod(dk,a0)
        re=b0*p
        p,m=divmod(m,a1)
        re+=b1*p+m
        return re
    if dk < amin:
        return dk
    if dp[dk] != -1:
        return dp[dk]
    mx = 0
    for a, b in rate:
        if a > dk: continue
        tmp = don(dk - a, rate, amin) + b
        if tmp > mx:
            mx = tmp
    dp[dk] = mx
    return mx

n = int(input())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))
atob = []
btoa = []
amin = 10 ** 7
bmin = 10 ** 7
for a, b in zip(aa, bb):
    if a < b:
        atob.append([a, b])
        if a < amin:
            amin = a
    elif a > b:
        btoa.append([b, a])
        if b < bmin:
            bmin = b
dp = [-1] * (n + 1)
n = don(n, atob, amin)
dp = [-1] * (n + 1)
print(don(n, btoa, bmin))

Submission status: Wrong Answer

Solution code:
N=int(input())
rate=[list(map(int,input().split())) for i in range(2)]
r=[]
d=N
for i in range(3):
    if rate[0][i]<rate[1][i]:
        r.append((rate[0][i],rate[1][i]))
l=len(r)
if l==1:
    d=d//r[0][0]*r[0][1]+d%r[0][0]
elif l>1:
    a=[0]*(N+1)
    w=[r[0][0],r[1][0]]
    v=[r[0][1]-r[0][0],r[1][1]-r[1][0]]
    if len(r)==3:
        w.append(r[2][0])
        v.append(r[2][1]-r[2][0])
    for i in range(N+1):
        for j in range(l):
            if i>=w[j]:
                a[i]=max(a[i],a[i-w[j]]+v[j])
    d+=a[-1]
r=[]
for i in range(3):
    if rate[0][i]>rate[1][i]:
        r.append((rate[1][i],rate[0][i]))
l=len(r)
if l==1:
    d=d//r[0][0]*r[0][1]+d%r[0][0]
elif l==2:
    a=0
    w=[r[0][0],r[1][0]]
    v=[r[0][1]-r[0][0],r[1][1]-r[1][0]]
    for i in range(d//w[0]+1):
        a=max(a,v[0]*i+v[1]*((d-w[0]*i)//w[1]))
    d+=a
print(d)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)

def don(dk, rate, amin):
    if not rate:
        return dk
    if dk < amin:
        return dk
    if dp[dk] != -1:
        return dp[dk]
    re = []
    for a, b in rate:
        re += [don(dk - a, rate, amin) + b]
    dp[dk] = re0 = max(re)
    return re0

n = int(input())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))
atob = []
btoa = []
amin = 10 ** 7
bmin = 10 ** 7
for a, b in zip(aa, bb):
    if a < b:
        atob.append([a, b])
        if a < amin:
            amin = a
    elif a > b:
        btoa.append([b, a])
        if b < bmin:
            bmin = b
dp = [-1] * (n + 1)
n = don(n, atob, amin)
dp = [-1] * (n + 1)
print(don(n, btoa, bmin))

Submission status: Wrong Answer

Solution code:
import sys
import itertools
import numpy as np


def main():
    n = int(input())
    ga, sa, ba = [int(e) for e in input().split()]
    gb, sb, bb = [int(e) for e in input().split()]
    rate = [(ga, gb), (sa, sb), (ba, bb)]

    m = n
    for rp in itertools.permutations(rate, 3):
        ntmp = n
        mtmp = 0
        for r in rp:
            if r[1] / r[0] < 1.0:
                continue
            mtmp += (ntmp // r[0]) * r[1]
            ntmp -= (ntmp // r[0]) * r[0]
        m = max(m, mtmp + ntmp)
    n = m
    for rp in itertools.permutations(rate, 3):
        mtmp = m
        ntmp = 0
        for r in rp:
            if r[0] / r[1] < 1.0:
                continue
            ntmp += (mtmp // r[1]) * r[0]
            mtmp -= (mtmp // r[1]) * r[1]
        n = max(n, ntmp + mtmp)
    print(n)


main()

Submission status: Wrong Answer

Solution code:
import sys
import itertools
import numpy as np


def main():
    n = int(input())
    ga, sa, ba = [int(e) for e in input().split()]
    gb, sb, bb = [int(e) for e in input().split()]
    rate = [(ga, gb), (sa, sb), (ba, bb)]

    m = 0
    for rp in itertools.permutations(rate, 3):
        ntmp = n
        mtmp = 0
        for r in rp:
            if r[1] / r[0] < 1.0:
                continue
            mtmp += (ntmp // r[0]) * r[1]
            ntmp -= (ntmp // r[0]) * r[0]
        m = max(m, mtmp + ntmp)
    n = 0
    for rp in itertools.permutations(rate, 3):
        mtmp = m
        ntmp = 0
        for r in rp:
            if r[0] / r[1] < 1.0:
                continue
            ntmp += (mtmp // r[1]) * r[0]
            mtmp -= (mtmp // r[1]) * r[1]
        n = max(n, ntmp + mtmp)
    print(n)


main()

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np


def main():
    n = int(input())
    ga, sa, ba = [int(e) for e in input().split()]
    gb, sb, bb = [int(e) for e in input().split()]
    rate = [(ga, gb), (sa, sb), (ba, bb)]
    rate.sort(key=lambda x: x[1] / x[0], reverse=True)
    m = 0
    for r in rate:
        if r[1] / r[0] < 1.0:
            break
        m += (n // r[0]) * r[1]
        n -= n // r[0]
    m += n
    n = 0
    rate.sort(key=lambda x: x[0] / x[1], reverse=True)
    for r in rate:
        if r[0] / r[1] < 1.0:
            break
        n += (m // r[1]) * r[0]
        m -= m // r[1]
    n += m
    print(n)


main()

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np


def main():
    n = int(input())
    ga, sa, ba = [int(e) for e in input().split()]
    gb, sb, bb = [int(e) for e in input().split()]
    rate = [(ga, gb), (sa, sb), (ba, bb)]
    rate.sort(key=lambda x: x[1] / x[0], reverse=True)
    m = 0
    for r in rate:
        m += (n // r[0]) * r[1]
        n -= n // r[0]
    m += n
    n = 0
    rate.sort(key=lambda x: x[0] / x[1], reverse=True)
    for r in rate:
        n += (m // r[1]) * r[0]
        m -= m // r[1]
    n += m
    print(n)


main()

Submission status: Wrong Answer

Solution code:
from collections import deque


def transaction(N, r):
    x = r[0]
    n = int(N / x[0])
    if len(r) == 1:
        return n * x[1] + N - n * x[0]
    if N % x[0] == 0:
        return n * x[1]
    r.popleft()
    s = N
    for i in range(n + 1):
        tmp = i * x[1] + transaction(N - i * x[0], r)
        if tmp > s:
            s = tmp
    return s

N = int(input())
A = [int(e) for e in input().split()]
B = [int(e) for e in input().split()]

r = {}
for i in range(3):
    (a, b) = (A[i], B[i])
    if b > a:
        r[(a, b)] = b / a

if len(r) > 0:
    dr = deque(maxlen=3)
    for k, v in sorted(r.items(), key=lambda x: -x[1]):
        dr.append(k)
    N = transaction(N, dr)

r = {}
for i in range(3):
    (a, b) = (A[i], B[i])
    if a > b:
        r[(b, a)] = a / b

if len(r) > 0:
    dr = deque(maxlen=3)
    for k, v in sorted(r.items(), key=lambda x: -x[1]):
        dr.append(k)
    N = transaction(N, dr)

print(N)

Submission status: Wrong Answer

Solution code:
from collections import deque


def transaction(N, r):
    x = r[0]
    n = int(N / x[1])
    if len(r) == 1:
        return n * x[2] + N - n * x[1]
    if N % x[1] == 0:
        return n * x[2]
    r.popleft()
    s = N
    for i in range(n + 1):
        tmp = i * x[2] + transaction(N - i * x[1], r)
        if tmp > s:
            s = tmp
    return s

N = int(input())
A = [int(e) for e in input().split()]
B = [int(e) for e in input().split()]

r = []
for i in range(3):
    (a, b) = (A[i], B[i])
    if b > a:
        r.append((b / a, a, b))

if len(r) > 0:
    r.sort(reverse=True)
    dr = deque(maxlen=3)
    for x in r:
        dr.append(x)
    N = transaction(N, dr)

r = []
for i in range(3):
    (a, b) = (A[i], B[i])
    if a > b:
        r.append((a / b, b, a))

if len(r) > 0:
    r.sort(reverse=True)
    dr = deque(maxlen=3)
    for x in r:
        dr.append(x)
    N = transaction(N, dr)

print(N)

Submission status: Wrong Answer

Solution code:
from collections import deque


def transaction(N, r):
    x = r[0]
    n = int(N / x[1])
    if N % x[1] == 0:
        return n * x[2]
    if len(r) == 1:
        return n * x[2] + N - n * x[1]
    r.popleft()
    s = N
    for i in range(n + 1):
        tmp = i * x[2] + transaction(N - i * x[1], r)
        if tmp > s:
            s = tmp
    return s

N = int(input())
A = [int(e) for e in input().split()]
B = [int(e) for e in input().split()]

r = []
for i in range(3):
    (a, b) = (A[i], B[i])
    if b > a:
        r.append((b / a, a, b))

if len(r) > 0:
    r.sort(reverse=True)
    dr = deque(maxlen=3)
    for x in r:
        dr.append(x)
    N = transaction(N, dr)

r = []
for i in range(3):
    (a, b) = (A[i], B[i])
    if a > b:
        r.append((a / b, b, a))

if len(r) > 0:
    r.sort(reverse=True)
    dr = deque(maxlen=3)
    for x in r:
        dr.append(x)
    N = transaction(N, dr)

print(N)

Submission status: Wrong Answer

Solution code:
from collections import deque


def transaction(N, r):
    if len(r) == 0:
        return N
    x = r[0]
    n = int(N / x[1])
    if N % x[1] == 0 or len(r) == 1:
        return n * x[2]
    r.popleft()
    s = N
    for i in range(n + 1):
        tmp = i * x[2] + transaction(N - i * x[1], r)
        if tmp > s:
            s = tmp
    return s

N = int(input())
A = [int(e) for e in input().split()]
B = [int(e) for e in input().split()]

r = []
for i in range(3):
    (a, b) = (A[i], B[i])
    if b > a:
        r.append((b / a, a, b))

r.sort(reverse=True)
dr = deque(maxlen=3)
for x in r:
    dr.append(x)
N = transaction(N, dr)

r = []
for i in range(3):
    (a, b) = (A[i], B[i])
    if a > b:
        r.append((a / b, b, a))

r.sort(reverse=True)
dr.clear()
for x in r:
    dr.append(x)

print(transaction(N, dr))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in input().split()]
def II(): return int(input())
INTMIN = -9223372036854775808

N=II()
RA=LI()
RB=LI()
GAIN1=[]
GAIN2=[]
for i in range(3):
    if RA[i] < RB[i]:
        GAIN1.append((RA[i], RB[i]-RA[i]))
    if RA[i] > RB[i]:
        GAIN2.append((RB[i], RA[i]-RB[i]))

dp1 = [0]*(N+1)
for i in range(N+1):
    for cost, gain in GAIN1:
        if (i+cost) <= N:
            dp1[i+cost] = max(dp1[i+cost],dp1[i]+gain)

M = dp1[N]+N
dp2 = [0]*(M+1)
for i in range(N+1):
    for cost, gain in GAIN2:
        if (i+cost) <= M:
            dp2[i+cost] = max(dp2[i+cost],dp2[i]+gain)

print(M+dp2[M])