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: Time Limit Exceeded

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


def solve(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

    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]


n = solve(a, b, N)
n = solve(b, a, n)
print(n)

Submission status: Time Limit Exceeded

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 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: Time Limit Exceeded

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]
    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: Time Limit Exceeded

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

# A -> Bで増えるドングリの最大値
def solve(n, ga, sa, ba, gb, sb, bb):
    gd, sd, bd = gb - ga, sb - sa, bb - ba

    if bd <= 0 and sd <= 0 and gd <= 0:
        return n

    elif bd <= 0 and sd <= 0 and gd > 0:
        return n + gd * (n // ga)

    elif bd <= 0 and sd > 0 and gd<= 0:
        return n + sd * (n // sa)

    elif bd <= 0 and sd > 0 and gd > 0:
        val = 0
        for k in range(n//ga + 1):
            l = (n - k * ga) // sa
            val = max(val, n + k * gd + l * sd)
        return val
    
    elif bd > 0 and sd <= 0 and gd <= 0:
        return n + bd * (n // ba)
    
    elif bd > 0 and sd <= 0 and gd > 0:
        val = 0
        for k in range(n//ga + 1):
            l = (n - k * ga) // ba
            val = max(val, n + k * gd + l * bd)
        return val

    elif bd > 0 and sd > 0 and gd <= 0:
        val = 0
        for k in range(n//sa + 1):
            l = (n - k * sa) // ba
            val = max(val, n + k * sd + l * bd)
        return val
    
    elif bd > 0 and sd > 0 and gd > 0:
        val = 0
        for k in range(n//ga + 1):
            for l in range((n - k * ga) // sa + 1):
                m = (n - k * ga - l * sa) // ba
                val = max(val, n + k * gd + l * sd + m * bd)
        return val


n1 = solve(N, ga, sa, ba, gb, sb, bb)

n2 = solve(n1, gb, sb, bb, ga, sa, ba)
print(n2)

Submission status: Time Limit Exceeded

Solution code:

SIZE=3
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
bHIKUa=[[a[i],b[i],b[i]-a[i]] for i in range(SIZE)]
bHIKUa.sort(key=lambda x:-x[1])
weigh=[bHIKUa[i][0] for i in range(SIZE)]
value=[bHIKUa[i][2] for i in range(SIZE)]
dp=[[-1 for j in range(n+1)]for i in range(SIZE+1)]
for i in range(SIZE,-1,-1):
    for j in range(n+1):
        if i==SIZE:
            dp[i][j]=0
        elif j<weigh[i]:
            dp[i][j]=dp[i+1][j]
        else:
            dp[i][j]=max(dp[i+1][j],dp[i][j-weigh[i]]+value[i])
newN=n+dp[0][n]
# print(newN)

weigh=[bHIKUa[SIZE-i-1][1] for i in range(SIZE)]
value=[-bHIKUa[SIZE-i-1][2] for i in range(SIZE)]
dp=[[-1 for j in range(newN+1)]for i in range(SIZE+1)]
for i in range(SIZE,-1,-1):
    for j in range(newN+1):
        if i==SIZE:
            dp[i][j]=0
        elif j<weigh[i]:
            dp[i][j]=dp[i+1][j]
        else:
            dp[i][j]=max(dp[i+1][j],dp[i][j-weigh[i]]+value[i])
ans=newN+dp[0][newN]
print(ans)

Submission status: Time Limit Exceeded

Solution code:

SIZE=3
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
bHIKUa=[[a[i],b[i],b[i]-a[i]] for i in range(SIZE)]
bHIKUa.sort(key=lambda x:-x[1])
weigh=[bHIKUa[i][0] for i in range(SIZE)]
value=[bHIKUa[i][2] for i in range(SIZE)]
dp=[[-1 for j in range(n+1)]for i in range(SIZE+1)]
for i in range(SIZE,-1,-1):
    for j in range(n+1):
        if i==SIZE:
            dp[i][j]=0
        elif j<weigh[i]:
            dp[i][j]=dp[i+1][j]
        else:
            dp[i][j]=max(dp[i+1][j],dp[i][j-weigh[i]]+value[i])
newN=n+dp[0][n]
# print(newN)

weigh=[bHIKUa[SIZE-i-1][1] for i in range(SIZE)]
value=[-bHIKUa[SIZE-i-1][2] for i in range(SIZE)]
dp=[[-1 for j in range(newN+1)]for i in range(SIZE+1)]
for i in range(SIZE,-1,-1):
    for j in range(newN+1):
        if i==SIZE:
            dp[i][j]=0
        elif j<weigh[i]:
            dp[i][j]=dp[i+1][j]
        else:
            dp[i][j]=max(dp[i+1][j],dp[i][j-weigh[i]]+value[i])
ans=newN+dp[0][newN]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def f(n):
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))
    atob=[]
    btoa=[]
    for a,b in zip(aa,bb):
        if a<b:
            atob.append([a,b])
        elif a>b:
            btoa.append([a,b])
    dp = [0] * (n + 1)
    for i in range(1, n + 1):
        mx = i
        for a, b in atob:
            if i >= a:
                tmp = dp[i - a] + b
                if tmp > mx:
                    mx = tmp
        dp[i] = mx
    n = dp[-1]
    dp = [0] * (n + 1)
    for i in range(1, n + 1):
        mx = i
        for a, b in btoa:
            if i >= b:
                tmp = dp[i - b] + a
                if tmp > mx:
                    mx = tmp
        dp[i] = mx
    print(dp[-1])

n = int(input())
f(n)

Submission status: Time Limit Exceeded

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

minusinf = -10**18

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

memo1 = {}
def dp1(n):
    if n == 0: return 0
    if n < 0: return minusinf
    if n in memo1:
        return memo1[n]
    sol = 0
    for cost, g in GAIN1:
        sol = max(dp1(n-cost)+g, sol)
    memo1[n] = sol
    return sol

memo2 = {}
def dp2(n):
    if n == 0: return 0
    if n < 0: return minusinf
    if n in memo2:
        return memo2[n]
    sol = 0
    for cost, g in GAIN2:
        sol = max(dp2(n-cost)+g, sol)
    memo2[n] = sol
    return sol

med = N+dp1(N)
print(med+dp2(med))

Submission status: Time Limit Exceeded

Solution code:
def f(n):
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))
    dp = [0] * (n + 1)
    for i in range(1, n + 1):
        mx = i
        for a, b in zip(aa, bb):
            if a>=b:continue
            if i >= a:
                tmp = dp[i - a] + b
                if tmp > mx:
                    mx = tmp
        dp[i] = mx
    n = dp[-1]
    dp = [0] * (n + 1)
    for i in range(1, n + 1):
        mx = i
        for a, b in zip(aa, bb):
            if a <= b: continue
            if i >= b:
                tmp = dp[i - b] + a
                if tmp > mx:
                    mx = tmp
        dp[i] = mx
    print(dp[-1])

n = int(input())
f(n)

Submission status: Time Limit Exceeded

Solution code:
def f(n):
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))
    dp = [0] * (n + 1)
    for i in range(1, n + 1):
        mx = i
        for a, b in zip(aa, bb):
            if i >= a:
                tmp = dp[i - a] + b
                if tmp > mx:
                    mx = tmp
        dp[i] = mx
    n = dp[-1]
    dp = [0] * (n + 1)
    for i in range(1, n + 1):
        mx = i
        for a, b in zip(aa, bb):
            if i >= b:
                tmp = dp[i - b] + a
                if tmp > mx:
                    mx = tmp
        dp[i] = mx
    print(dp[-1])

n = int(input())
f(n)

Submission status: Time Limit Exceeded

Solution code:
def f(n):
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))
    dp = [0] * (n + 1)
    for i in range(1, n + 1):
        mx = i
        for a, b in zip(aa, bb):
            if i >= a:
                tmp = dp[i - a] + b
                if tmp > mx:
                    mx = tmp
            dp[i] = mx
    n = dp[-1]
    dp = [0] * (n + 1)
    for i in range(1, n + 1):
        mx = i
        for a, b in zip(aa, bb):
            if i >= b:
                tmp = dp[i - b] + a
                if tmp > mx:
                    mx = tmp
            dp[i] = mx
    print(dp[-1])

n = int(input())
f(n)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
aa=list(map(int,input().split()))
bb=list(map(int,input().split()))
dp=[0]*(n+1)
for i in range(1,n+1):
    mx=i
    for a,b in zip(aa,bb):
        if i>=a:
            tmp=dp[i-a]+b
            if tmp>mx:
                mx=tmp
        dp[i]=mx
n=dp[-1]
dp=[0]*(n+1)
for i in range(1,n+1):
    mx=i
    for a,b in zip(aa,bb):
        if i>=b:
            tmp=dp[i-b]+a
            if tmp>mx:
                mx=tmp
        dp[i]=mx
print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
def exchange(n, s, d):
  v = [n] * (n + 1)
  s = [1] + s
  d = [1] + d

  for i in range(len(v) - 1):
    for a, b in zip(s, d):
      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: Time Limit Exceeded

Solution code:
N=int(input())
dp1=[i for i in range(N+1)]
GA,SA,BA=map(int,input().split())
GB,SB,BB=map(int,input().split())

for i in range(N+1):
    for j in range(3):
        if j==0 and i>=GA:
            dp1[i]=max(dp1[i],dp1[i-GA]+GB)
        elif j==1 and i>=SA:
            dp1[i]=max(dp1[i],dp1[i-SA]+SB)
        elif j==2 and i>=BA:
            dp1[i]=max(dp1[i],dp1[i-BA]+BB)
#print(dp1)

dp2=[i for i in range(dp1[N]+5)]
for i in range(dp1[N]+1):
    for j in range(3):
        if j==0 and i>=GB:
            dp2[i]=max(dp2[i],dp2[i-GB]+GA)
        elif j==1 and i>=SB:
            dp2[i]=max(dp2[i],dp2[i-SB]+SA)
        elif j==2 and i>=BB:
            dp2[i]=max(dp2[i],dp2[i-BB]+BA)
print(dp2[dp1[N]])

Submission status: Time Limit Exceeded

Solution code:
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 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]
print(int(N))

Submission status: Time Limit Exceeded

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:
		dp = [rates[0][1] * (d // rates[0][0]) + rates[1][1] * ((max_d - d + d % rates[0][0]) // rates[1][0]) + ((max_d - d + d % rates[0][0]) % rates[1][0]) for d in range(max_d + 1)]
		max_d = max(dp)
	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: Time Limit Exceeded

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) > 0:
		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: Time Limit Exceeded

Solution code:
import numpy as np

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) > 0:
		#print(max_d)
		dp = np.zeros(max_d + 1, dtype = int)
		for d in range(max_d + 1):
			dp[d] = max([dp[d - rates[i][0]] + rates[i][1] if d >= rates[i][0] else d for i in range(len(rates))])
		
		max_d = dp[-1]
print(max_d)

Submission status: Time Limit Exceeded

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) > 0:
		dp = [[0 for _ in range(max_d + 1)] for _ in range(len(rates))]
		dp[0] = [(d // rates[0][0]) * rates[0][1] + d % rates[0][0] for d in range(max_d + 1)]
		for i in range(1, len(rates)):
			for d in range(1, max_d + 1):
				if d - rates[i][0] >= 0:
					dp[i][d] = max(dp[i - 1][d], dp[i][d - rates[i][0]] + rates[i][1])
				else:
					dp[i][d] = dp[i - 1][d]
		#print(dp)
		max_d = dp[len(rates) - 1][max_d]
print (max_d)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(100000000)

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) > 0:
		dp = [[0 for _ in range(max_d + 1)] for _ in range(len(rates))]
		dp[0] = [(d // rates[0][0]) * rates[0][1] + d % rates[0][0] for d in range(max_d + 1)]
		for i in range(1, len(rates)):
			for d in range(1, max_d + 1):
				if d - rates[i][0] >= 0:
					dp[i][d] = max(dp[i - 1][d], dp[i][d - rates[i][0]] + rates[i][1])
				else:
					dp[i][d] = dp[i - 1][d]
		#print(dp)
		max_d = dp[len(rates) - 1][max_d]
print (max_d)

Submission status: Time Limit Exceeded

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]

    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: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


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 = 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: Time Limit Exceeded

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


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 = 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: Time Limit Exceeded

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)
    n_ = 0
    A_, B_ = [], []
    for a, b in zip(A, B) :
        if a < b :
            A_.append(a)
            B_.append(b)
            n_ += 1

    for n in range(1, N + 1) :
        dp[n] = max([dp[n-1] + 1] + [dp[n-A_[i]] + B_[i] if n - A_[i] >= 0  else 0 for i in range(n_)])

    return dp[N]

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

Submission status: Time Limit Exceeded

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

def search1(n) :
    ret = 0
    for g in range(n // A[0] + 1) :
        n -= g*A[0]
        for s in range(n // A[1] + 1) :
            n -= s*A[1]
            for b in range(n // A[2] + 1) :
                n -= b*A[2]
                ret = max(ret, n + g*B[0]+s*B[1]+b*B[2])
                n += b*A[2]
            n += s*A[1]
        n += g*A[0]

    return ret

def search2(n) :
    ret = 0
    for g in range(n // B[0] + 1) :
        n -= g*B[0]
        for s in range(n // B[1] + 1) :
            n -= s*B[1]
            for b in range(n // B[2] + 1) :
                n -= b*B[2]
                ret = max(ret, n + g*A[0]+s*A[1]+b*A[2])
                n += b*B[2]
            n += s*B[1]
        n += g*B[0]

    return ret

M = search1(N)
ret = search2(M)
print(ret)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
rateA = list(map(int, input().split()))
rateB = list(map(int, input().split()))

memo = {}
rate = (rateB[0] / rateA[0], rateB[1] / rateA[1], rateB[2] / rateA[2])


def search2(num):
  stack = [num]

  while(1):
    if len(stack) == 0: return

    num = stack[-1]
    if num in memo:
      stack.pop()
      continue
    

    temp=[-1, -1, -1]
    isEnd = True
    isLeaf = True
    for i in range(3):
      temp[i] = num - rateA[i]
      if rate[i] > 1 and temp[i] >= 0 : isLeaf = False
      if rate[i] > 1 and temp[i] not in memo and temp[i] >= 0: isEnd = False

    if isLeaf:
      memo[num] = num
      stack.pop()
      continue

    if isEnd:
      result = -1
      for i in range(3):
        temp = num - rateA[i]
        if rate[i] > 1 and temp >= 0 and memo[temp] + rateB[i] > result:
          result = memo[temp] + rateB[i]

      memo[num] = result
      stack.pop()
      continue

    for i in range(3):
      if rate[i] > 1:
        temp = num - rateA[i]
        if temp >= 0:
          stack.append(temp)

search2(N)

N = memo[N]
rateA, rateB = rateB, rateA
memo = {}
rate = (rateB[0] / rateA[0], rateB[1] / rateA[1], rateB[2] / rateA[2])

search2(N)
print(memo[N])

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def main():
    n = int(input())
    gsb_a = list(map(int, input().split(' ')))
    gsb_b = list(map(int, input().split(' ')))

    m = max(n, yeah(n, gsb_a, gsb_b))
    result = max(m, n, yeah(m, gsb_b, gsb_a))

    print(result)

def yeah(n, buy, sell):
    dp = np.array(range(n+1))
    for i in range(len(dp)):
        for j in range(len(buy)):
            if i >= buy[j]:
                dp[i] = max(dp[i], dp[i-buy[j]]+sell[j])
    return dp[n]

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def main():
    n = int(input())
    gsb_a = list(map(int, input().split(' ')))
    gsb_b = list(map(int, input().split(' ')))

    ## gsbをi番目まで利用して，j個のどんぐりを消費するときの価値の最大値 dpa[i,j]
    dpa = np.zeros([len(gsb_a)+1,n+1], dtype=int)
    dpa[0,:] = np.array(range(len(dpa[0])))
    for i in range(len(gsb_a)):
        for j in range(n+1):
            dpa[i+1,j] = dpa[i,j]
            if j >= gsb_a[i]:
                dpa[i+1,j] = max(dpa[i+1,j-gsb_a[i]]+gsb_b[i], dpa[i+1,j])

    m = max(n, dpa[len(gsb_a),n])
    del dpa

    dpb = np.zeros([len(gsb_b)+1,m+1], dtype=int)
    dpb[0,:] = np.array(range(len(dpb[0])))
    for i in range(len(gsb_b)):
        for j in range(m+1):
            dpb[i+1,j] = dpb[i,j]
            if j >= gsb_b[i]:
                dpb[i+1,j] = max(dpb[i+1,j-gsb_b[i]]+gsb_a[i], dpb[i+1,j])

    # print(dpa, dpb)
    print(max(n, m, dpb[len(gsb_b),m]))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def change(N, A, B):
    ans = N
    dp = [[0] * (N+1) for _ in range(4)]
    dp[0] = list(range(N+1))
    for i in range(1, 4):
        w = A[i-1]
        v = B[i-1]
        for j in range(N+1):
            dp[i][j] = dp[i-1][j]
            if w <= j:
                dp[i][j] = max(dp[i][j], dp[i][j-w] + v)

    return dp[3][N]

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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import math

def main():
    n = int(input())
    gsb_a = list(map(int, input().split(' ')))
    gsb_b = list(map(int, input().split(' ')))

    ## gsbをi番目まで利用して，j個のどんぐりを消費するときの価値の最大値 dpa[i,j]
    dpa = np.zeros([len(gsb_a)+1,n+1], dtype=int)
    for i in range(len(dpa[0])):
        dpa[0,i] = i
    for i in range(len(gsb_a)):
        for j in range(n+1):
            dpa[i+1,j] = dpa[i,j]
            if j >= gsb_a[i]:
                dpa[i+1,j] = max(dpa[i+1,j-gsb_a[i]]+gsb_b[i], dpa[i+1,j])

    m = max(n, dpa[len(gsb_a),n])

    dpb = np.zeros([len(gsb_b)+1,m+1], dtype=int)
    for i in range(len(dpb[0])):
        dpb[0,i] = i
    for i in range(len(gsb_b)):
        for j in range(m+1):
            dpb[i+1,j] = dpb[i,j]
            if j >= gsb_b[i]:
                dpb[i+1,j] = max(dpb[i+1,j-gsb_b[i]]+gsb_a[i], dpb[i+1,j])

    ## print(dpa, dpb)
    print(max(n, m, dpb[len(gsb_b),m]))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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)
    return (
        n if R == 0 else
        n + (r[-1][1] - r[-1][0]) * (n // r[-1][0]) if R == 1 else
        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)
        ) if R == 2 else
        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 - x * r[0][0]) // r[1][0] + 1)
        )
    )


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)