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: Accepted

Solution code:
import sys

def trade(n, a, b):
    a0, a1, a2 = a
    b0, b1, b2 = b
    dp = [0] * (n+1)
    for i in range(1, n+1):
        gold, silver, copper = 0, 0, 0
        if i >= a0:
            gold = dp[i-a0] + b0
        if i >= a1:
            silver = dp[i-a1] + b1
        if i >= a2:
            copper = dp[i-a2] + b2
        dp[i] = max(i, gold, silver, copper)

    return dp[n]


def main():
    input = sys.stdin.readline
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    # Trade A -> B
    M = trade(N, A, B)
    # Trade B -> A
    return trade(M, B, A)


if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
# original: https://atcoder.jp/contests/diverta2019-2/submissions/6023476

inpl = lambda: list(map(int,input().split()))
N = int(input())
rate = []
rate.append(inpl())
rate.append(inpl())
ABc = []
ABr = []
BAc = []
BAr = []
for i in range(3):
    rA, rB = rate[0][i], rate[1][i]
    if rB > rA:
        ABc.append(rA)
        ABr.append(rB)
    elif rA > rB:
        BAc.append(rB)
        BAr.append(rA)

dpAB = [0]*(N+1)
q = [0]*(len(ABc)+1)
for i in range(1,N+1):
    dpAB[i] = dpAB[i-1]+1
    for j in range(len(ABc)): #cost, ret in AB:
        if i >= ABc[j]:
            dpAB[i] = max(dpAB[i], dpAB[i-ABc[j]] + ABr[j])

M = dpAB[N]
dpBA = [0]*(M+1)
q = [0]*(len(BAc)+1)
for i in range(1,M+1):
    k = 1
    dpBA[i] = dpBA[i-1]+1
    for j in range(len(BAc)):
        if i >= BAc[j]:
            dpBA[i] = max(dpBA[i], dpBA[i-BAc[j]] + BAr[j])

print(dpBA[M])

Submission status: Accepted

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

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))
for l in [AB, BA]:
    l.sort(key=lambda x: -x[1]/x[0])

def buy_and_sell(N, cost_ret):
    if len(cost_ret) == 0:
        return N
    else:
        minor_max = sum([ min([lcm(cost,cost_ret[i][0])-cost_ret[i][0] for cost,ret in cost_ret[:i]])
                          for i in range(1,len(cost_ret))])
        b0, s0 = cost_ret[0]
        n = max(0, N - minor_max) // b0
        Ndp = N - n*b0
        dp = [0]*(Ndp+1)
        w = [0]*(len(cost_ret)+1)
        for i in range(1,Ndp+1):
            k = 1
            for cost, ret in cost_ret:
                if i >= cost:
                    w[k] = dp[i-cost] + ret
                    k += 1
            dp[i] = max(dp[i-1]+1, max(w))
        return s0*n + dp[-1]

M = buy_and_sell(N, AB)
L = buy_and_sell(M, BA)
print(L)

Submission status: Accepted

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))
for l in [AB, BA]:
    l.sort(key=lambda x: -x[1]/x[0])

def buy_and_sell(N, cost_ret):
    if len(cost_ret) == 0:
        return N
    else:
        Nunit = lcm_list([ x[0] for x in cost_ret ])
        b0, s0 = cost_ret[0]
        q = N // b0
        n = max(0, q - (len(cost_ret)-1)*Nunit//b0)
        Ndp = N - n*b0
        dp = [0]*(Ndp+1)
        w = [0]*(len(cost_ret)+1)
        for i in range(1,Ndp+1):
            k = 1
            for cost, ret in cost_ret:
                if i >= cost:
                    w[k] = dp[i-cost] + ret
                    k += 1
            dp[i] = max(dp[i-1]+1, max(w))
        return s0*n + dp[-1]

M = buy_and_sell(N, AB)
L = buy_and_sell(M, BA)
print(L)

Submission status: Accepted

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))
for l in [AB, BA]:
    l.sort(key=lambda x: -x[1]/x[0])

def buy_and_sell(N, cost_ret):
    if len(cost_ret) == 0:
        return N
    else:
        Nunit = lcm_list([ x[0] for x in cost_ret ])
        b0, s0 = cost_ret[0]
        q = N // b0
        n = max(0, q - 2*Nunit//b0 + 1)
        Ndp = N - n*b0
        dp = [0]*(Ndp+1)
        w = [0]*(len(cost_ret)+1)
        for i in range(1,Ndp+1):
            k = 1
            for cost, ret in cost_ret:
                if i >= cost:
                    w[k] = dp[i-cost] + ret
                    k += 1
            dp[i] = max(dp[i-1]+1, max(w))
        return s0*n + dp[-1]

M = buy_and_sell(N, AB)
L = buy_and_sell(M, BA)
print(L)

Submission status: Accepted

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):
        dp1[i] = max(dp1[i - 1] + 1,
                     (dp1[i - ga] + gb) if i - ga >= 0 else 0,
                     (dp1[i - sa] + sb) if i - sa >= 0 else 0,
                     (dp1[i - ba] + bb) if i - ba >= 0 else 0)
        # print(dp1)

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

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

    print(dp2[N2])


if __name__ == '__main__':
    main()

Submission status: Accepted

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
        p0,m=divmod(dk,a0)
        re=b0*p0
        p1,m=divmod(m,a1)
        re+=b1*p1+m
        while p0>0 and m>0:
            p0-=1
            m+=a0
            p2,m=divmod(m,a1)
            p1+=p2
            tmp=p0*b0+p1*b1+m
            if tmp>re:
                re=tmp
        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: Accepted

Solution code:
N=int(input())
rate=[list(map(int,input().split())) for i in range(2)]

def solve(d,r):
    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
    elif l==3:
        a=[0]*(N+1)
        w=[r[0][0],r[1][0],r[2][0]]
        v=[r[0][1]-r[0][0],r[1][1]-r[1][0],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]
    return d
r=[]
for i in range(3):
    if rate[0][i]<rate[1][i]:
        r.append((rate[0][i],rate[1][i]))
d=solve(N,r)
r=[]
for i in range(3):
    if rate[0][i]>rate[1][i]:
        r.append((rate[1][i],rate[0][i]))
d=solve(d,r)
print(d)

Submission status: Accepted

Solution code:
import math
import sys

N = int(input())
GA, SA, BA = list(map(int, input().split()))
GB, SB, BB = list(map(int, input().split()))

def knapsack(n, ga, sa, ba, gb, sb, bb):
    values = []
    weights = []
    if gb / ga > 1:
        values.append(gb - ga)
        weights.append(ga)
    if sb / sa > 1:
        values.append(sb - sa)
        weights.append(sa)
    if bb / ba > 1:
        values.append(bb - ba)
        weights.append(ba)
    if len(values) == 3:
        # dp[i]: 重さ i 以下で選ぶのの価値の最大値
        dp = [0] * (n + 1)
        dp[0] = n
        for i in range(1, n + 1):
            x = dp[i - 1]
            for w, v in zip(weights, values):
                if i - w >= 0:
                    x = max(x, dp[i - w] + v)
            dp[i] = x
        return dp[n]
    if len(values) == 2:
        w1, w2 = weights
        v1, v2 = values
        if (v1 + w1) / w1 < (v2 + w2) / w2:
            v1, v2 = v2, v1
            w1, w2 = w2, w1
        c1 = n // w1
        c2 = (n - w1 * c1) // w2
        ret = n + v1 * c1 + v2 * c2
        while c1 >= 0:
            a = n - w1 * c1 - w2 * c2
            c1 -= max(1, math.ceil((w2 - a) / w1))
            c2 = (n - w1 * c1) // w2
            ret = max(ret, n + v1 * c1 + v2 * c2)
            if w1 * c1 + w2 * c2 == n:
                break
        return ret
    if len(values) == 1:
        return n + n // weights[0] * values[0]
    return n

ans = knapsack(N, GA, SA, BA, GB, SB, BB)
ans = knapsack(ans, GB, SB, BB, GA, SA, BA)
print(int(ans))

Submission status: Accepted

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(M+1):
    for cost, gain in GAIN2:
        if (i+cost) <= M:
            dp2[i+cost] = max(dp2[i+cost],dp2[i]+gain)

print(M+dp2[M])

Submission status: Accepted

Solution code:
def exchange(n, s, d):
  v = [n] * (n + 1)
  r = [(1, 1)] + [(a, b) for a, b in zip(s, d) if a < b]

  for i in range(len(v) - 1):
    for a, b in r:
      if i + a < len(v):
        v[i + a] = max(v[i] - a + b, v[i + a])

  return v[-1]


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

  n = exchange(n, a, b)
  n = exchange(n, b, a)

  print(n)


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import math
import os
import sys
 
if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")
 
sys.setrecursionlimit(2147483647)
INF = float("inf")
 
N = int(sys.stdin.readline())
GA, SA, BA = list(map(int, sys.stdin.readline().split()))
GB, SB, BB = list(map(int, sys.stdin.readline().split()))
 
 
def knapsack(n, ga, sa, ba, gb, sb, bb):
    values = []
    weights = []
    if gb / ga > 1:
        values.append(gb - ga)
        weights.append(ga)
    if sb / sa > 1:
        values.append(sb - sa)
        weights.append(sa)
    if bb / ba > 1:
        values.append(bb - ba)
        weights.append(ba)
    if len(values) == 3:
        # dp[i]: 重さ i 以下で選ぶのの価値の最大値
        dp = [0] * (n + 1)
        dp[0] = n
        for i in range(1, n + 1):
            x = dp[i - 1]
            for w, v in zip(weights, values):
                if i - w >= 0:
                    x = max(x, dp[i - w] + v)
            dp[i] = x
        return dp[n]
    if len(values) == 2:
        w1, w2 = weights
        v1, v2 = values
        if (v1 + w1) / w1 < (v2 + w2) / w2:
            v1, v2 = v2, v1
            w1, w2 = w2, w1
        c1 = n // w1
        c2 = (n - w1 * c1) // w2
        ret = n + v1 * c1 + v2 * c2
        while c1 >= 0:
            a = n - w1 * c1 - w2 * c2
            c1 -= max(1, math.ceil((w2 - a) / w1))
            c2 = (n - w1 * c1) // w2
            ret = max(ret, n + v1 * c1 + v2 * c2)
            if w1 * c1 + w2 * c2 == n:
                break
        return ret
    if len(values) == 1:
        return n + n // weights[0] * values[0]
    return n
 
 
ans = knapsack(N, GA, SA, BA, GB, SB, BB)
ans = knapsack(ans, GB, SB, BB, GA, SA, BA)
print(int(ans))

Submission status: Accepted

Solution code:
import math
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")

N = int(sys.stdin.readline())
GA, SA, BA = list(map(int, sys.stdin.readline().split()))
GB, SB, BB = list(map(int, sys.stdin.readline().split()))


def knapsack(n, ga, sa, ba, gb, sb, bb):
    values = []
    weights = []
    if gb / ga > 1:
        values.append(gb - ga)
        weights.append(ga)
    if sb / sa > 1:
        values.append(sb - sa)
        weights.append(sa)
    if bb / ba > 1:
        values.append(bb - ba)
        weights.append(ba)
    if len(values) == 3:
        # dp[i]: 重さ i 以下で選ぶのの価値の最大値
        dp = [0] * (n + 1)
        dp[0] = n
        for i in range(1, n + 1):
            x = dp[i - 1]
            for w, v in zip(weights, values):
                if i - w >= 0:
                    x = max(x, dp[i - w] + v)
            dp[i] = x
        return dp[n]
    if len(values) == 2:
        w1, w2 = weights
        v1, v2 = values
        if (v1 + w1) / w1 < (v2 + w2) / w2:
            v1, v2 = v2, v1
            w1, w2 = w2, w1
        c1 = n // w1
        c2 = (n - w1 * c1) // w2
        ret = n + v1 * c1 + v2 * c2
        while c1 >= 0:
            a = n - w1 * c1 - w2 * c2
            c1 -= max(1, math.ceil((w2 - a) / w1))
            c2 = (n - w1 * c1) // w2
            ret = max(ret, n + v1 * c1 + v2 * c2)
            if w1 * c1 + w2 * c2 == n:
                break
        return ret
    if len(values) == 1:
        return n + n // weights[0] * values[0]
    return n


ans = knapsack(N, GA, SA, BA, GB, SB, BB)
ans = knapsack(ans, GB, SB, BB, GA, SA, BA)
print(int(ans))

Submission status: Accepted

Solution code:
import math
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")

N = int(sys.stdin.readline())
GA, SA, BA = list(map(int, sys.stdin.readline().split()))
GB, SB, BB = list(map(int, sys.stdin.readline().split()))

values = []
weights = []
if GB / GA > 1:
    values.append(GB - GA)
    weights.append(GA)
if SB / SA > 1:
    values.append(SB - SA)
    weights.append(SA)
if BB / BA > 1:
    values.append(BB - BA)
    weights.append(BA)
# dp[i]: 重さ i 以下で選ぶのの価値の最大値
dp = [0] * (N + 1)
dp[0] = N
if values:
    for i in range(1, N + 1):
        x = dp[i - 1]
        for w, v in zip(weights, values):
            if i - w >= 0:
                x = max(x, dp[i - w] + v)
        dp[i] = x
    N = dp[N]

values = []
weights = []
if GA / GB > 1:
    values.append(GA - GB)
    weights.append(GB)
if SA / SB > 1:
    values.append(SA - SB)
    weights.append(SB)
if BA / BB > 1:
    values.append(BA - BB)
    weights.append(BB)
# dp[i]: 重さ i 以下で選ぶのの価値の最大値
dp = [0] * (N + 1)
dp[0] = N
if len(values) == 3:
    for i in range(1, N + 1):
        x = dp[i - 1]
        for w, v in zip(weights, values):
            if i - w >= 0:
                x = max(x, dp[i - w] + v)
        dp[i] = x
    N = dp[N]
if len(values) == 2:
    w1, w2 = weights
    v1, v2 = values
    if (v1 + w1) / w1 < (v2 + w2) / w2:
        v1, v2 = v2, v1
        w1, w2 = w2, w1
    c1 = N // w1
    c2 = (N - w1 * c1) // w2
    ret = N + v1 * c1 + v2 * c2
    while c1 >= 0:
        a = N - w1 * c1 - w2 * c2
        c1 -= max(1, math.ceil((w2 - a) / w1))
        c2 = (N - w1 * c1) // w2
        ret = max(ret, N + v1 * c1 + v2 * c2)
        if w1 * c1 + w2 * c2 == N:
            break
    N = ret
if len(values) == 1:
    N += N // weights[0] * values[0]
print(int(N))

Submission status: Accepted

Solution code:
n = int(input())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))

rates_list = [[],[]]
for i in range(3):
	if aa[i] > bb[i]:
		rates_list[1].append([bb[i], aa[i]])
	else:
		rates_list[0].append([aa[i], bb[i]])

max_d = n
for j in range(2):
	rates = rates_list[j]
	if len(rates) == 1:
		max_d = (max_d // rates[0][0]) * rates[0][1] + max_d % rates[0][0]
	elif len(rates) == 2:
		val = [rates[0][1] * i for i in range(max_d // rates[0][0] + 1)]
		rest = [max_d - rates[0][0] * i for i in range(max_d // rates[0][0] + 1)]
		val = [val[i] + rates[1][1] * (rest[i] // rates[1][0]) + (rest[i] % rates[1][0]) if rest[i] > 0 else val[i] for i in range(len(val))]
		max_d = max(val)
	elif len(rates) == 3:
		dp = [(d // rates[0][0]) * rates[0][1] + d % rates[0][0] for d in range(max_d + 1)]
		for d in range(1, max_d + 1):
			dp[d] = max([dp[d]] + [dp[d - rates[i][0]] + rates[i][1] if d - rates[i][0] >= 0 else d for i in range(1, len(rates))])
		max_d = dp[-1]
print (max_d)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
ga, sa, ba = li()
gb, sb, bb = li()

vg1, vg2 = gb - ga, ga - gb
vs1, vs2 = sb - sa, sa - sb
vb1, vb2 = bb - ba, ba - bb

dp1 = [0]*(n+1)

for i in range(1,n+1):
    dp1[i] = max(dp1[i-1]+1,
                  dp1[i-ga] + gb if i-ga >= 0 else 0,
                  dp1[i-sa] + sb if i-sa >= 0 else 0,
                  dp1[i-ba] + bb if i-ba >= 0 else 0)

m = max(n, dp1[n])
dp2 = [0] * (m+1)

for i in range(1, m+1):
    dp2[i] = max(dp2[i-1]+1,
                 dp2[i-gb] + ga if i-gb >= 0 else 0,
                 dp2[i-sb] + sa if i-sb >= 0 else 0,
                 dp2[i-bb] + ba if i-bb >= 0 else 0)

print(max(m, dp2[m]))

Submission status: Accepted

Solution code:
def f(a,b,N):

    tmpa,tmpb=[],[]
    for i in range(3):
        if a[i]<b[i]:
            tmpa.append(a[i])
            tmpb.append(b[i])
    a,b=tmpa,tmpb

    if len(a)==0: return N
    elif len(a)==1: return (N//a[0])*b[0]+N%a[0]

    elif len(a)==2:
        if a[0]<a[1]: a,b=a[::-1],b[::-1]

        i,ans=0,N
        while i*a[0]<=N:
            j=(N-i*a[0])//a[1]
            ans=max(ans,i*b[0]+j*b[1]+(N-i*a[0]-j*a[1]))
            i+=1
        return ans

    else:
        memo=[i for i in range(N+1)]

        for i in range(1,N+1):
            tmp=[]
            for j in range(len(a)):
                if i-a[j]>=0: tmp.append(memo[i-a[j]]+b[j])
            if tmp!=[]: memo[i]=max(tmp)
        return memo[N]

import sys
N=int(input())

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

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

Submission status: Accepted

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[:]
    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]))

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)
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
elif len(ups) == 1:
    nowN = N + (N//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(nowN//downs[0][1]+1):
            left = nowN - gold*downs[0][1]
            silver = left // downs[1][1]
            tmp_ans = (nowN
                       + gold*gold_diff
                       + silver*silver_diff)
            ans = max(ans, tmp_ans)
    elif len(downs) == 1:
        ans = nowN + (nowN//downs[0][1])*(downs[0][0]-downs[0][1])
    else:
        ans = nowN
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

def small_dp(N,A,B):
    *X, = filter(lambda c: int.__sub__(*c)<0, zip(A,B))
    k = len(X)
    if k == 0:
        return N
    if k == 1:
        return X[0][1]*(N//X[0][0]) + N%X[0][0]
    if k == 2:
        c = 0
        for i in range(N//X[0][0]+1):
            j = (N-i*X[0][0])//X[1][0]
            c = max(X[0][1]*i + X[1][1]*j + (N-i*X[0][0])%X[1][0], c)
        return c
    if k == 3:
        if (N//X[1][0]+1)*(N//X[0][0]+1) <= 10**6 :
            return max(X[0][1]*i + max(X[1][1]*j + X[2][1]*((N-i*X[0][0]-j*X[1][0])//X[2][0]) + ((N-i*X[0][0]-j*X[1][0])%X[2][0]) for j in range((N-i*X[0][0])//X[1][0]+1)) for i in range(N//X[0][0]+1))
        else:
            dp = [0]*(N+1)
            for w in range(1,N+1):
                dp[w] = max(dp[w-X[i][0]]+X[i][1] if w>=X[i][0] else w for i in range(k))
            N = dp[-1]
            return N

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    res = small_dp(small_dp(N, A, B), B, A)

    return str(res)

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

Submission status: Accepted

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

def small_dp(N,A,B):
    *X, = filter(lambda c: int.__sub__(*c)<0, zip(A,B))
    k = len(X)
    if k == 0:
        return N
    if k == 1:
        return X[0][1]*(N//X[0][0]) + N%X[0][0]
    if k == 2:
        c = 0
        for i in range(N//X[0][0]+1):
            j = (N-i*X[0][0])//X[1][0]
            c = max(X[0][1]*i + X[1][1]*j + (N-i*X[0][0])%X[1][0], c)
        return c
    if k == 3:
        if (N//X[1][0]+1)*(N//X[0][0]+1) <= 10**6 :
            return max(X[0][1]*i + max(X[1][1]*j + X[2][1]*((N-i*X[0][0]-j*X[1][0])//X[2][0]) + ((N-i*X[0][0]-j*X[1][0])%X[2][0]) for j in range((N-i*X[0][0])//X[1][0]+1)) for i in range(N//X[0][0]+1))
        else:
            import numpy as np
            dp = np.zeros(N+1, dtype=int)
            for w in range(1,N+1):
                dp[w] = max(dp[w-X[i][0]]+X[i][1] if w>=X[i][0] else w for i in range(k))
            N = dp[-1]
            return N

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    res = small_dp(small_dp(N, A, B), B, A)

    return str(res)

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

def knapzap(N, A, B) :
    dp = [0] * (N + 1)

    for n in range(1, N + 1) :
        dp[n] = max(
            dp[n-1] + 1,
            dp[n-A[0]] + B[0] if n - A[0] >= 0 else 0,
            dp[n-A[1]] + B[1] if n - A[1] >= 0 else 0,
            dp[n-A[2]] + B[2] if n - A[2] >= 0 else 0
        )

    return dp[N]

M = knapzap(N, A, B)
ret = knapzap(M, B, A)
print(ret)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline

N = int(readline())

A = list(map(int, readline().split()))
B = list(map(int, readline().split()))
# g, s b

def nap(A, B, N):
    dp = [0] * (N + 1)

    # A -> B
    # dp[i] = max(dp[i - 1], dp[i - a] + diff[a], ...)
    for i in range(1, N + 1):
        dp[i] = max(
            dp[i - 1] + 1,
            dp[i - A[0]] + B[0] if i - A[0] >= 0 else -1,
            dp[i - A[1]] + B[1] if i - A[1] >= 0 else -1,
            dp[i - A[2]] + B[2] if i - A[2] >= 0 else -1
        )
    return dp[N]

A_to_B = nap(A, B, N)

B_to_A = nap(B, A, A_to_B)

print(B_to_A)

Submission status: Accepted

Solution code:
from functools import reduce

# 入力
N = int(input())
gA, sA, bA = map(int, input().split())
gB, sB, bB = map(int, input().split())

A = (gA, sA, bA)
B = (gB, sB, bB)


# 各貴金属について、買う／売る可能性のある取引所は一意に定まることを用いて
# 全探索により解を求める
def f(n, r):
    R = len(r)
    return (
        n if R == 0 else
        n + (r[-1][1] - r[-1][0]) * (n // r[-1][0]) if R == 1 else
        reduce(
            max,
            (
                n + (r[0][1] - r[0][0]) * x +
                (r[-1][1] - r[-1][0]) * ((n - r[0][0] * x) // r[-1][0])
                for x in range(n // r[0][0] + 1)
            ),
            n
        ) if R == 2 else
        reduce(
            max,
            (
                n + (r[0][1] - r[0][0]) * x + (r[1][1] - r[1][0]) * y +
                (r[-1][1] - r[-1][0]) *
                ((n - r[0][0] * x - r[1][0] * y) // r[-1][0])
                for x in range(n // r[0][0] + 1)
                for y in range((n - r[0][0] * x) // r[1][0] + 1)
            ),
            n
        )
    )


ans = f(
    f(
        N,
        [
            (a, b)
            for a, b in zip(A, B)
            if a < b
        ]
    ),
    [
        (b, a)
        for b, a in zip(B, A)
        if a > b
    ]
)

# 出力
print(ans)

Submission status: Accepted

Solution code:
from functools import reduce

# 入力
N = int(input())
gA, sA, bA = map(int, input().split())
gB, sB, bB = map(int, input().split())

A = (gA, sA, bA)
B = (gB, sB, bB)


# 各貴金属について、買う／売る可能性のある取引所は一意に定まることを用いて
# 全探索により解を求める
def f(n, r):
    R = len(r)
    if R == 0:
        return n
    elif R == 1:
        return n + (r[-1][1] - r[-1][0]) * (n // r[-1][0])
    elif R == 2:
        return reduce(
            max,
            (
                n + (r[0][1] - r[0][0]) * x +
                (r[-1][1] - r[-1][0]) * ((n - r[0][0] * x) // r[-1][0])
                for x in range(n // r[0][0] + 1)
            ),
            n
        )
    else:
        res = 0
        for x in range(n // r[0][0] + 1):
            m = n - r[0][0] * x
            for y in range(m // r[1][0] + 1):
                res = max(
                    res,
                    n + (r[0][1] - r[0][0]) * x + (r[1][1] - r[1][0]) * y +
                    (r[-1][1] - r[-1][0]) * ((m - r[1][0] * y) // r[-1][0])
                )
        return res


ans = f(
    f(
        N,
        [
            (a, b)
            for a, b in zip(A, B)
            if a < b
        ]
    ),
    [
        (b, a)
        for b, a in zip(B, A)
        if a > b
    ]
)

# 出力
print(ans)

Submission status: Accepted

Solution code:
# 入力
N = int(input())
gA, sA, bA = map(int, input().split())
gB, sB, bB = map(int, input().split())

A = (gA, sA, bA)
B = (gB, sB, bB)


# 各貴金属について、買う／売る可能性のある取引所は一意に定まることを用いて
# 全探索により解を求める
def f(n, r):
    R = len(r)
    if R == 0:
        return n
    elif R == 1:
        return n + (r[-1][1] - r[-1][0]) * (n // r[-1][0])
    elif R == 2:
        res = 0
        for x in range(n // r[0][0] + 1):
            res = max(
                res,
                n + (r[0][1] - r[0][0]) * x +
                (r[-1][1] - r[-1][0]) * ((n - r[0][0] * x) // r[-1][0])
            )
        return res
    else:
        res = 0
        for x in range(n // r[0][0] + 1):
            m = n - r[0][0] * x
            for y in range(m // r[1][0] + 1):
                res = max(
                    res,
                    n + (r[0][1] - r[0][0]) * x + (r[1][1] - r[1][0]) * y +
                    (r[-1][1] - r[-1][0]) * ((m - r[1][0] * y) // r[-1][0])
                )
        return res


ans = f(
    f(
        N,
        [
            (a, b)
            for a, b in zip(A, B)
            if a < b
        ]
    ),
    [
        (b, a)
        for b, a in zip(B, A)
        if a > b
    ]
)

# 出力
print(ans)