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:
# -*- coding: utf-8 -*-

import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
A = LIST()
B = LIST()

def solve(N):
    dp = [0] * (N+1)
    for i in range(N+1):
        dp[i] = i
    for i in range(N+1):
        for j in range(3):
            a, b = A[j], B[j]
            if i+a <= N:
                dp[i+a] = max(dp[i+a], dp[i] + b)
    return dp[N]

N2 = solve(N)
A, B = B, A
ans = solve(N2)
print(ans)

Submission status: Accepted

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

import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
A = LIST()
B = LIST()

S1 = []
S2 = []
for i in range(3):
    if A[i] < B[i]:
        S1.append(i)
    else:
        S2.append(i)
        A[i], B[i] = B[i], A[i]

def calc(cnt, i):
    d = cnt // A[i]
    m = cnt % A[i]
    return d * B[i] + m

def count(S, N):
    if len(S) == 0:
        return N
    elif len(S) == 1:
        return calc(N, S[0])
    elif len(S) == 2:
        res = 0
        for x in range(N+1):
            y = N - x
            if y < 0:
                break
            cnt = calc(x, S[0]) + calc(y, S[1])
            res = max(res, cnt)
        return res
    else:
        res = 0
        for x in range(N+1):
            for y in range(N+1):
                z = N - x - y
                if z < 0:
                    break
                cnt = calc(x, S[0]) + calc(y, S[1]) + calc(z, S[2])
                res = max(res, cnt)
        return res

N2 = count(S1, N)
ans = count(S2, N2)
print(ans)

Submission status: Accepted

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

import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

def divmod(a, b):
    d = a // b
    m = a % b
    return (d, m)

N = INT()
A = LIST()
B = LIST()

S1 = []
S2 = []
for i in range(3):
    if A[i] < B[i]:
        S1.append(i)
    else:
        S2.append(i)
        A[i], B[i] = B[i], A[i]

def calc(cnt, i):
    d, m = divmod(cnt, A[i])
    return d * B[i] + m

def count(S, N):
    if len(S) == 0:
        return N
    elif len(S) == 1:
        return calc(N, S[0])
    elif len(S) == 2:
        res = 0
        for x in range(N+1):
            y = N - x
            if y < 0:
                break
            cnt = calc(x, S[0]) + calc(y, S[1])
            res = max(res, cnt)
        return res
    else:
        res = 0
        for x in range(N+1):
            for y in range(N+1):
                z = N - x - y
                if z < 0:
                    break
                cnt = calc(x, S[0]) + calc(y, S[1]) + calc(z, S[2])
                res = max(res, cnt)
        return res

N2 = count(S1, N)
ans = count(S2, N2)
print(ans)

Submission status: Accepted

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

import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
A = LIST()
B = LIST()

S1 = []
S2 = []
for i in range(3):
    if A[i] < B[i]:
        S1.append(i)
    else:
        S2.append(i)
        A[i], B[i] = B[i], A[i]

def calc(cnt, i):
    d, m = divmod(cnt, A[i])
    return d * B[i] + m

def count(S, N):
    if len(S) == 0:
        return N
    elif len(S) == 1:
        return calc(N, S[0])
    elif len(S) == 2:
        res = 0
        for x in range(N+1):
            y = N - x
            if y < 0:
                break
            cnt = calc(x, S[0]) + calc(y, S[1])
            res = max(res, cnt)
        return res
    else:
        res = 0
        for x in range(N+1):
            for y in range(N+1):
                z = N - x - y
                if z < 0:
                    break
                cnt = calc(x, S[0]) + calc(y, S[1]) + calc(z, S[2])
                res = max(res, cnt)
        return res

N2 = count(S1, N)
ans = count(S2, N2)
print(ans)

Submission status: Accepted

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

ans1 = trade(n,g1,s1,b1,g2,s2,b2)
ans2 = trade(ans1,g2,s2,b2,g1,s1,b1)
print(ans2)

Submission status: Accepted

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

dp = [0] * (N + 1)

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

def calc(x, r1):
  z = r1 - d2[0][0] * x
  return (z // d2[1][0]) * d2[1][1] + z % d2[1][0] + d2[0][1] * x

res1 = max(dp)
if len(d2) == 0:
  print(res1)
if len(d2) == 1:
  x, y = d2.pop()
  print((res1 // x) * y + (res1 % x))
if len(d2) == 2:
  x0 = 0
  x3 = res1 // d2[0][0]
  res2 = res1 + 0
  for x in range(x0, x3 + 1):
    res2 = max(res2, calc(x, res1))
  print(res2)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[] for _ in range(num)]
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

def solve():
    N = I()
    A = III()
    B = III()

    first_use = []
    second_use = []
    for i in range(3):
        if A[i]<B[i]:
            first_use.append(i)
        if A[i]>B[i]:
            second_use.append(i)

    def change(n,A,B,use_index):
        dp = [n]*(n+1)
        for i in range(n+1):
            for j in use_index:
                if i-A[j]>=0:
                    dp[i] = max(dp[i-A[j]]+B[j]-A[j],dp[i])
        return dp[n]

    count1 = change(N,A,B,first_use)
    count2 = change(count1,B,A,second_use)

    print(count2)

solve()

Submission status: Accepted

Solution code:
n = int(input())
g1, s1, b1 = map(int, input().split())
g2, s2, b2 = map(int, input().split())

go, back = [], []

if g1 > g2:
	back.append([g2, g1-g2])
elif g1 < g2:
	go.append([g1, g2-g1])

if s1 > s2:
	back.append([s2, s1-s2])
elif s1 < s2:
	go.append([s1, s2-s1])

if b1 > b2:
	back.append([b2, b1-b2])
elif b1 < b2:
	go.append([b1, b2-b1])

if len(go) == 0:
	m = n
elif len(go) == 1:
	m = go[0][1] * (n // go[0][0]) + n
else:
	dp1 = [n for _ in range(n+1)]
	for cost, value in go:
		for i in range(n-cost+1):
			dp1[i+cost] = max(dp1[i+cost], dp1[i] + value)
	m = dp1[n]

if len(back) == 0:
	ans = m
elif len(back) == 1:
	ans = back[0][1] * (m // back[0][0]) + m
else:
	dp2 = [m for _ in range(m+1)]
	for cost, value in back:
		for i in range(m-cost+1):
			dp2[i+cost] = max(dp2[i+cost], dp2[i] + value)
	ans = dp2[m]

print(ans)

Submission status: Accepted

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

for i in range(3):
    if A[0][i] > A[1][i]:
        D[1].append([A[1][i], A[0][i]])
    else:
        D[0].append([A[0][i], A[1][i]])

def d1(x, in_0, out_0):
    return out_0 * (x // in_0) + (x % in_0)

def d2(x, in_0, out_0, in_1, out_1):
    ans = 0
    for i in range((x // in_0) + 1):
        ans = max(ans, out_0 * i + out_1 * ((x - i * in_0) // in_1) + ((x - i * in_0) % in_1))
    return ans

def d3(x, in_0, out_0, in_1, out_1, in_2, out_2):
    ans = 0
    for i in range((x // in_0) + 1):
        for j in range(((x - i * in_0) // in_1) + 1):
            ans = max(ans, out_0 * i + out_1 * j + out_2 * ((x - i * in_0 - j * in_1) // in_2) \
                + ((x - i * in_0 - j * in_1) % in_2))
            # print(i,j,ans)
    return ans

# print(D)

ans = 0
l = len(D[0])
if l == 3:
    ans = d3(n, D[0][0][0], D[0][0][1], D[0][1][0], D[0][1][1], D[0][2][0], D[0][2][1])
elif l == 2:
    ans = d1(d2(n, D[0][0][0], D[0][0][1], D[0][1][0], D[0][1][1]), D[1][0][0], D[1][0][1])
elif l == 1:
    ans = d2(d1(n, D[0][0][0], D[0][0][1]), D[1][0][0], D[1][0][1], D[1][1][0], D[1][1][1])
elif l == 0:
    ans = d3(n, D[1][0][0], D[1][0][1], D[1][1][0], D[1][1][1], D[1][2][0], D[1][2][1])

print(ans)

Submission status: Accepted

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

dp=[i for i in range(N+1)]
for i in range(ga,N+1):
    dp[i]=max(dp[i],dp[i-ga]+gb)
for i in range(sa,N+1):
    dp[i]=max(dp[i],dp[i-sa]+sb)
for i in range(ba,N+1):
    dp[i]=max(dp[i],dp[i-ba]+bb)
N=dp[-1]
dp=[i for i in range(N+1)]
for i in range(gb,N+1):
    dp[i]=max(dp[i],dp[i-gb]+ga)
for i in range(sb,N+1):
    dp[i]=max(dp[i],dp[i-sb]+sa)
for i in range(bb,N+1):
    dp[i]=max(dp[i],dp[i-bb]+ba)
print(dp[-1])

Submission status: Accepted

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

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

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

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

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

print(ans)

Submission status: Accepted

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

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

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

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

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

Submission status: Accepted

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


print(dp2[n])

Submission status: Accepted

Solution code:
def examD():
    N = I()
    g, s, c = [0]*2, [0]*2, [0]*2
    for i in range(2):
        g[i],s[i],c[i] = LI()
    dp1 = [0]*(N+1)
    v1 = [g[1]-g[0],s[1]-s[0],c[1]-c[0]]
    w1 = [g[0],s[0],c[0]]
    v2 = [g[0]-g[1],s[0]-s[1],c[0]-c[1]]
    w2 = [g[1],s[1],c[1]]
    for i in range(N):
        if i+w1[0]<=N:
            dp1[i+w1[0]] = max(dp1[i+w1[0]],dp1[i]+v1[0])
        if i+w1[1]<=N:
            dp1[i+w1[1]] = max(dp1[i+w1[1]],dp1[i]+v1[1])
        if i+w1[2]<=N:
            dp1[i+w1[2]] = max(dp1[i+w1[2]],dp1[i]+v1[2])
    newN = max(dp1)+N
#    print(newN)
    dp2 = [0]*(newN+1)
    for i in range(newN):
        if v2[0]>0 and i+w2[0]<=newN:
            dp2[i+w2[0]] = max(dp2[i+w2[0]],dp2[i]+v2[0])
        if v2[1]>0 and i+w2[1]<=newN:
            dp2[i+w2[1]] = max(dp2[i+w2[1]],dp2[i]+v2[1])
        if v2[2]>0 and i+w2[2]<=newN:
            dp2[i+w2[2]] = max(dp2[i+w2[2]],dp2[i]+v2[2])
    ans = dp2[newN]+newN
    print(ans)

from string import digits
import sys,copy,bisect,itertools,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD()

Submission status: Accepted

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

dp = [0]*(N+1)
for j in range(3):
    for i in range(1,N+1):
        if i-A[j] >= 0:
            dp[i] = max(dp[i],dp[i-1]+1,dp[i-A[j]]+B[j])
        else:
            dp[i] = max(dp[i],dp[i-1]+1)

M = dp[N]            
dp = [0]*(M+1)

for j in range(3):
    for i in range(1,M+1):
        if i-B[j] >= 0:
            dp[i] = max(dp[i],dp[i-1]+1,dp[i-B[j]]+A[j])
        else:
            dp[i] = max(dp[i],dp[i-1]+1)
    
print(dp[M])

Submission status: Accepted

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


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

    dp = [0]*((N+1)*4)

    for i in range(3):
        a, b = A[i], B[i]
        for w in range(N+1):
            if w < a:
                dp[w*4+i+1] = dp[w*4+i]
            else:
                dp[w*4+i+1] = max(dp[w*4+i], dp[(w-a)*4+i+1] + b-a)

    M = dp[N*4+3] + N

    dp2 = [0]*((M+1)*4)
    for i in range(3):
        a, b = A[i], B[i]
        for w in range(M+1):
            if w < b:
                dp2[w*4+i+1] = dp2[w*4+i]
            else:
                dp2[w*4+i+1] = max(dp2[w*4+i], dp2[(w-b)*4+i+1] + a-b)

    print(dp2[M*4+3]+M)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = I()
#　最初はaでのやつが価値に当たる。
weight = LI()
value = LI()
dp = list(range(n + 1))
for i in range(3):
    for j in range(weight[i], n + 1):
        dp[j] = max(dp[j], dp[j - weight[i]] + value[i])


n = dp[-1]
weight, value = value, weight
dp = list(range(n + 1))
for i in range(3):
    for j in range(weight[i], n + 1):
        dp[j] = max(dp[j], dp[j - weight[i]] + value[i])

print(dp[-1])

Submission status: Accepted

Solution code:
#12:40
n = int(input())
a1,a2,a3 = map(int,input().split())
b1,b2,b3 = map(int,input().split())
dp = [0] * (n+1)
for i in range(1,n+1):
  tmp = dp[i-1]+1
  if i-a1 >= 0:
    if dp[i-a1]+b1 > tmp:
      tmp = dp[i-a1]+b1
  if i-a2 >= 0:
    if dp[i-a2]+b2 > tmp:
      tmp = dp[i-a2]+b2
  if i-a3 >= 0:
    if dp[i-a3]+b3 > tmp:
      tmp = dp[i-a3]+b3
  dp[i] = tmp
m = dp[-1]
#print(dp)
#print(m)
dp = [0] * (m+1)
for j in range(1,m+1):
  tmp = dp[j-1]+1
  if j-b1 >= 0:
    if dp[j-b1]+a1 > tmp:
      tmp = dp[j-b1]+a1
  if j-b2 >= 0:
    if dp[j-b2]+a2 > tmp:
      tmp = dp[j-b2]+a2
  if j-b3 >= 0:
    if dp[j-b3]+a3 > tmp:
      tmp = dp[j-b3]+a3
  dp[j] = tmp
print(dp[-1])

Submission status: Accepted

Solution code:

N = int(input())

ga,sa,ba = map(int,input().split())

gb,sb,bb = map(int,input().split())

g = gb - ga
s = sb - sa
b = bb - ba

lis = [[g,ga],[s,sa],[b,ba]]
lis.sort()

plnum = 0

for i in lis:
    if i[0] > 0:
        plnum += 1

if plnum == 3:

    nexN = N

    for i in range(N // ga +1 ):

        amariA = N - ga * i

        for j in range(amariA // sa + 1):

            amariB = amariA - sa * j
            k = amariB // ba

            nexN = max(nexN , N + g*i + s*j + b * k)

    N = nexN

elif plnum == 2:

    nexN = N

    for i in range(N // lis[2][1] + 1):

            amariB = N - lis[2][1] * i
            j = amariB // lis[1][1]

            nexN = max(nexN , N + lis[2][0] * i + lis[1][0] * j)

    N = nexN

elif plnum == 1:
    
    N += (N // lis[2][1]) * lis[2][0]

#print (N)

g = ga - gb
s = sa - sb
b = ba - bb

lis = [[g,gb],[s,sb],[b,bb]]
lis.sort()

plnum = 0

for i in lis:
    if i[0] > 0:
        plnum += 1

if plnum == 3:

    nexN = N

    for i in range(N // gb +1 ):

        amariA = N - gb * i

        for j in range(amariA // sb + 1):

            amariB = amariA - sb * j
            k = amariB // bb

            nexN = max(nexN , N + g*i + s*j + b * k)

    N = nexN

elif plnum == 2:

    nexN = N

    for i in range(N // lis[2][1] + 1):

            amariB = N - lis[2][1] * i
            j = amariB // lis[1][1]

            nexN = max(nexN , N + lis[2][0] * i + lis[1][0] * j)

    N = nexN

elif plnum == 1:
    
    N += (N // lis[2][1]) * lis[2][0]

print (N)

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    A1, A2, A3 = map(int, input().split())
    B1, B2, B3 = map(int, input().split())

    dp = list(range(N+1))
    for i in range(N+1):
        if i+A1 <= N:
            dp[i+A1] = max(dp[i+A1], dp[i]+B1)
        if i+A2 <= N:
            dp[i+A2] = max(dp[i+A2], dp[i]+B2)
        if i+A3 <= N:
            dp[i+A3] = max(dp[i+A3], dp[i]+B3)
    N2 = dp[N]

    dp = list(range(N2+1))
    for i in range(N2+1):
        if i+B1 <= N2:
            dp[i+B1] = max(dp[i+B1], dp[i]+A1)
        if i+B2 <= N2:
            dp[i+B2] = max(dp[i+B2], dp[i]+A2)
        if i+B3 <= N2:
            dp[i+B3] = max(dp[i+B3], dp[i]+A3)
    ans = dp[N2]

    print(ans)

solve()

Submission status: Accepted

Solution code:
# W
# N K
# A1 B1
# A2 B2
# :
# AN BN

n = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))



dp = list(range(n + 1))

for ci in range(3):
    for wi in range(a_list[ci], n + 1):
        dp[wi] = max(dp[wi - a_list[ci]] + b_list[ci], dp[wi])



n = dp[-1]
dp = list(range(n + 1))


for ci in range(3):
    for wi in range(b_list[ci], n + 1):
        dp[wi] = max(dp[wi - b_list[ci]] + a_list[ci], dp[wi])




print(dp[-1])

Submission status: Accepted

Solution code:
def dfs():
    n = int(input())
    weight = list(map(int, input().split()))
    value = list(map(int, input().split()))
    
    
    def knapsack(n, weight, value):
        dp = [i for i in range(n+1)]
        for i in range(3):
            for j in range(weight[i], n+1):
                dp[j] = max(dp[j], dp[j-weight[i]] + value[i])
        return(dp[-1])
    
    
    new_n=knapsack(n=n, weight=weight, value=value)
    print(knapsack(n=new_n, weight=value, value=weight))
 
    
dfs()

Submission status: Accepted

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

Submission status: Accepted

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

def F(N,A,B):
    AB = list(zip(A,B))
    AB = [(a,b) for a,b in AB if a < b] # 有用なやつだけ
    AB.sort(key = lambda x: x[1]/x[0], reverse = True)
    if len(AB) == 0:
        return N
    if len(AB) == 1:
        a,b = AB[0]
        x = N//a
        return N + (b-a) * x
    if len(AB) == 2:
        (a,b),(c,d) = AB
        # 2種類目の硬価はa回未満しか使わない：ac個はa優先なので
        return max(N + (N - i * c) // a * (b-a) + i * (d-c) for i in range(a))
    dp = list(range(N+1))
    for a,b in AB:
        for n in range(a,N+1):
            y = dp[n-a] + b
            if dp[n] < y:
                dp[n] = y
    return dp[N]

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

answer = F(F(N,A,B),B,A)
print(answer)

Submission status: Accepted

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

def F(N,A,B):
    AB = list(zip(A,B))
    AB = [(a,b) for a,b in AB if a < b] # 有用なやつだけ
    AB.sort(key = lambda x: x[1]/x[0], reverse = True)
    if len(AB) == 0:
        return N
    if len(AB) == 1:
        a,b = AB[0]
        x = N//a
        return N + (b-a) * x
    if len(AB) == 2:
        (a,b),(c,d) = AB
        # 2種類目の硬価はa回未満しか使わない：ac個はa優先なので
        return max(N + (N - i * c) // a * (b-a) + i * (d-c) for i in range(a))
    # 効率の良いものから並んでいる
    dp = [0] * (N+1)
    for n in range(N+1):
        x = n
        for a,b in AB:
            if n >= a:
                y = dp[n-a] + b
                if x < y:
                    x = y
        dp[n] = x
    return dp[N]

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

answer = F(F(N,A,B),B,A)
print(answer)


AB = [(1,2),(3,4)]
(a,b),(c,d) = AB

Submission status: Accepted

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

def F(N,A,B):
    # n 個のどんぐりを持っている
    # a個がbの価値に変換できる
    AB = [(x,y) for x,y in zip(A,B) if x < y]
    dp = [0] * (N+1)
    for n in range(N+1):
        x = n
        for a,b in AB:
            if n >= a:
                y = dp[n-a] + b
                if x < y:
                    x = y
        dp[n] = x
    return dp[N]

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

answer = F(F(N,A,B),B,A)
print(answer)

Submission status: Accepted

Solution code:
N = int(input())
CA = list(map(int, input().split()))
CB = list(map(int, input().split()))

def solve(n, w, v):
    dp = [0]*(n+1)
    for i in range(n):
        dp[i+1] = max(1+dp[i], dp[i+1])
        if i + w[0] < n+1:
            dp[i+w[0]] = max(dp[i] + v[0], dp[i+w[0]])
        if i + w[1] < n+1:
            dp[i+w[1]] = max(dp[i] + v[1], dp[i+w[1]])
        if i + w[2] < n+1:
            dp[i+w[2]] = max(dp[i] + v[2], dp[i+w[2]])
    return dp[-1]
print(solve(solve(N, CA, CB), CB, CA))