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:
## A→Bでどんぐりが最大になるように調整し、
## B→Aでどんぐりが最大になるように調整する。
## 各遷移では、どんぐりの数が増えるような金属種しか選ばない

## 重さと価値の情報を持つN個の荷物を重さがW以下になるように詰め込む時の価値の和を最大化
def knapsack(N,W,weight,value):
  ## 初期化
  inf=float("inf")
  dp=[[-inf for i in range(W+1)] for j in range(N+1)]
  for i in range(W+1): dp[0][i]=0
  ## DP
  for i in range(N):
    for w in range(W+1):
      if weight[i] <= w: ## dp[i][w-weight[i]の状態にi番目の荷物が入る可能性がある
        dp[i+1][w] = max(dp[i][w], dp[i][w-weight[i]]+value[i])
      else: ## 入る可能性はない
        dp[i+1][w] = dp[i][w]
  return dp[N][W]

## 同じ荷物を重複を許して選んで良い場合のナップサック
## dp[i][j]: i種類の荷物を用いて大きさjのカバンに入れる場合の最大価値     
def knapsack_repeated(N,W,weight,value):
  ## 初期化
  inf=float("inf")
  dp = [[-inf for i in range(W+1)] for j in range(N+1)]
  for i in range(W+1): dp[0][i] = 0
  ## DP
  for i in range(N): 
    for w in range(W+1): 
      if weight[i] <= w: ## dp[i][w-weight[i]の状態にi番目の荷物が入る可能性がある
        dp[i+1][w] = max(dp[i][w], dp[i][w-weight[i]]+value[i], dp[i+1][w-weight[i]]+value[i])
      else: ## 入る可能性はない
        dp[i+1][w] = dp[i][w]
  return dp[N][W]

"""
## 同じ荷物を重複を許して選んで良い場合のナップサック
## dp[i][j]: i種類の荷物を用いて大きさjのカバンに入れる場合の最大価値     
def knapsack_repeated(N,W,weight,value):
  ## 初期化
  inf=float("inf")
  dp = [[inf for i in range(W+1)] for j in range(N+1)]
  for i in range(W+1): dp[0][i] = 0
  for i in range(N+1): dp[i][0] = 0
  for i in range(1,N+1): 
    for j in range(1, W+1): 
      if j - weight[i-1] >= 0:
        dp[i][j] = max(dp[i-1][j], dp[i-1][j-weight[i-1]]+value[i-1], dp[i][j-weight[i-1]]+value[i-1])
      else:
        dp[i][j] = dp[i-1][j]
  return dp[N][W]
"""

## ナップサックでの重さ→pre_donguriとして計算
def DivCon2D(pre_don, exchange_pre, exchange_post):
  lst_pre = []
  lst_profit = []
  for i in range(3): ## 得になる交換だけを考える
    if exchange_pre[i] < exchange_post[i]:
      lst_pre.append(exchange_pre[i])
      lst_profit.append(exchange_post[i] - exchange_pre[i])
  if len(lst_pre) == 0: return pre_don
  profit = knapsack_repeated(len(lst_pre), pre_don, lst_pre, lst_profit)
  return pre_don + profit
  

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

don_AB = int(DivCon2D(N, (ga, sa, ba), (gb, sb, bb)))
don_BA = int(DivCon2D(don_AB, (gb, sb, bb), (ga, sa, ba)))
print(don_BA)

Submission status: Time Limit Exceeded

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


def count(n, a, b):
    a0, a1, a2 = a
    b0, b1, b2 = b
    dp = [0] * (n + 1)
    for i in range(n + 1):
        c0 = dp[i - a0] + b0 if i - a0 >= 0 else 0
        c1 = dp[i - a1] + b1 if i - a1 >= 0 else 0
        c2 = dp[i - a2] + b2 if i - a2 >= 0 else 0
        dp[i] = max([i, c0, c1, c2])
    return dp[n]


print(count(count(N, A, B), B, A))

Submission status: Time Limit Exceeded

Solution code:
def exchange(n, s, d):
  v = n

  for i in range(n // s[0] + 1):
    x0 = n - s[0] * i
    y0 = d[0] * i

    for j in range(x0 // s[1] + 1):
      x1 = x0 - s[1] * j
      y1 = y0 + d[1] * j

      k = x1 // s[2]
      x1 -= s[2] * k
      y1 += d[2] * k

      v = max(x1 + y1, v)

  return v


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:
import collections
N = int(input())
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]

Q1 = [[1, 1]]
Q2 = [[1, 1]]
for i in range(3):
    if A[i] < B[i]:
        Q1 += [[A[i], B[i]]]
    elif A[i] > B[i]:
        Q2 += [[A[i], B[i]]]
dp1 = collections.defaultdict(lambda: -float('inf'))
dp1[0] = 0
for i in range(N + 1):
    for xs in Q1:
        dp1[i] = max([dp1[i], dp1[i - xs[0]] + xs[1]])
dp2 = collections.defaultdict(lambda: -float('inf'))
dp2[0] = 0
M = dp1[N]
for i in range(M + 1):
    for xs in Q2:
        dp2[i] = max([dp2[i], dp2[i - xs[1]] + xs[0]])
print(dp2[M])

Submission status: Time Limit Exceeded

Solution code:
## A→Bでどんぐりが最大になるように調整し、
## B→Aでどんぐりが最大になるように調整する。
## 各遷移では、どんぐりの数が増えるような金属種しか選ばない

## 重さと価値の情報を持つN個の荷物を重さがW以下になるように詰め込む時の価値の和を最大化
def knapsack(N,W,weight,value):
  ## 初期化
  inf=float("inf")
  dp=[[-inf for i in range(W+1)] for j in range(N+1)]
  for i in range(W+1): dp[0][i]=0
  ## DP
  for i in range(N):
    for w in range(W+1):
      if weight[i]<=w: ## dp[i][w-weight[i]の状態にi番目の荷物が入る可能性がある
        dp[i+1][w]=max(dp[i][w-weight[i]]+value[i], dp[i][w])
      else: ## 入る可能性はない
        dp[i+1][w]=dp[i][w]
  return dp[N][W]

## 同じ荷物を重複を許して選んで良い場合のナップサック
def knapsack_repeated(N,W,weight,value):
  ## 初期化
  inf=float("inf")
  dp = [[inf for i in range(W+1)] for j in range(N+1)]
  for i in range(W+1): dp[0][i] = 0
  for i in range(N+1): dp[i][0] = 0
  for i in range(1,N+1): 
    for j in range(1, W+1): 
      if j - weight[i-1] >= 0:
        dp[i][j] = max(dp[i-1][j], dp[i-1][j-weight[i-1]] + value[i-1], dp[i][j-weight[i-1]] + value[i-1])
      else:
        dp[i][j] = dp[i-1][j]
  return dp[N][W]

## ナップサックでの重さ→pre_donguriとして計算
def DivCon2D(pre_don, exchange_pre, exchange_post):
  lst_pre = []
  lst_profit = []
  for i in range(3): ## 得になる交換だけを考える
    if exchange_pre[i] < exchange_post[i]:
      lst_pre.append(exchange_pre[i])
      lst_profit.append(exchange_post[i] - exchange_pre[i])
  if len(lst_pre) == 0: return pre_don
  profit = knapsack_repeated(len(lst_pre), pre_don, lst_pre, lst_profit)
  return pre_don + profit
  

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

don_AB = int(DivCon2D(N, (ga, sa, ba), (gb, sb, bb)))
don_BA = int(DivCon2D(don_AB, (gb, sb, bb), (ga, sa, ba)))
print(don_BA)

Submission status: Time Limit Exceeded

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()))
    dpOne = [0]*(N+1)
    for i in range(3):
        a, b = A[i], B[i]
        for j in range(N-a+1):
            if dpOne[j+a] < dpOne[j]+b:
                dpOne[j+a] = dpOne[j]+b
    donguri = N
    for i in range(N+1):
        if donguri < N-i+dpOne[i]:
            donguri = N - i +dpOne[i]
    K = donguri
    dpTwo = [0]*(K+1)
    for i in range(3):
        a, b  = A[i], B[i]
        for j in range(K-b+1):
            if dpTwo[j+b] < dpTwo[j]+a:
                dpTwo[j+b] = dpTwo[j]+a
    ans = K
    for i in range(K+1):
        if ans < K - i + dpTwo[i]:
            ans = K-i+dpTwo[i]
    print( ans)
if __name__ == '__main__':
    main()

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

DP = [0] * (n + 5001)
DP[0] = n

for i in range(n):
    for j in range(3):
        DP[i + A[j]] = max(DP[i + A[j]], DP[i] + B[j] - A[j])

ans1 = max(DP[:n + 1])

DP = [0] * (ans1 + 1)
DP[0] = ans1

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

print(max(DP[:ans1 + 1]))

Submission status: Time Limit Exceeded

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)

def main():
    n = I()
    a = LI()
    b = LI()

    def f(a,b,n):
        c = sorted([(ai,bi) for ai,bi in zip(a,b) if ai < bi], reverse=True)
        if len(c) == 0:
            return n
        if len(c) == 1:
            return n % c[0][0] + n // c[0][0] * c[0][1]

        a = [_[0] for _ in c]
        b = [_[1] for _ in c]
        r = n
        for i in range(0,n+1,a[0]):
            t = i // a[0] * b[0] + f(a[1:], b[1:], n-i)
            if r < t:
                r = t
        return r

    t = f(a,b,n)
    r = f(b,a,t)

    return r


print(main())

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

DP = [0] * (n + 5001)
DP[0] = n

for i in range(n):
    for j in range(3):
        DP[i + A[j]] = max(DP[i + A[j]], DP[i] + B[j] - A[j])

ans1 = max(DP[:n + 1])

DP = [0] * (ans1 + 5001)
DP[0] = ans1

for i in range(ans1):
    for j in range(3):
        DP[i + B[j]] = max(DP[i + B[j]], DP[i] + A[j] - B[j])

print(max(DP[:ans1 + 1]))

Submission status: Time Limit Exceeded

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)

def main():
    n = I()
    a = LI()
    b = LI()

    def f(a,b,n):
        c = sorted([(ai,bi) for ai,bi in zip(a,b) if ai < bi], reverse=True)
        a = [_[0] for _ in c]
        b = [_[1] for _ in c]
        if len(c) == 0:
            return n
        if len(c) == 1:
            return n % c[0][0] + n // c[0][0] * c[0][1]

        r = n
        for i in range(0,n+1,a[0]):
            t = i // a[0] * b[0] + f(a[1:], b[1:], n-i)
            if r < t:
                r = t
        return r

    t = f(a,b,n)
    r = f(b,a,t)

    return r


print(main())

Submission status: Time Limit Exceeded

Solution code:
def napzak(w,v,limit):
    dp = [0]*(limit+1)
    for i in range(1,limit+1):
        for j,value in zip(w,v):
            if value<=j:
                continue
            if (i-j)>=0:
                dp[i] = max(dp[i],dp[i-j]+value-j)
    return dp[limit]+limit

n = int(input())
wa = list(map(int,input().split()))
wb = list(map(int,input().split()))

m = napzak(wa,wb,n)
ans = napzak(wb,wa,m)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
def solve(N, ga, gb, sa, sb, ba, bb):
    V = [gb, sb, bb]
    W = [ga, sa, ba]
    dp = [i for i in range(N+1)]
    for v, w in zip(V, W):
        if v <= w:
            continue
        for k in range(w, N+1):
            dp[k] = max(dp[k], dp[k-w] + v)
    return max(dp)

A = solve(N, ga, gb, sa, sb, ba, bb)
B = solve(A, gb, ga, sb, sa, bb, ba)
print(B)

Submission status: Time Limit Exceeded

Solution code:
inpl = lambda: list(map(int,input().split()))
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))

dpAB = [0]
for i in range(1,N+1):
    q = [0]
    for cost, ret in AB:
        if i >= cost:
            q.append(dpAB[i-cost] + ret)
    dpAB.append(max(dpAB[i-1]+1, max(q)))

M = dpAB[N]
dpBA = [0]
for i in range(1,M+1):
    q = [0]
    for cost, ret in BA:
        if i >= cost:
            q.append(dpBA[i-cost] + ret)
    dpBA.append(max(dpBA[i-1]+1, max(q)))

print(dpBA[M])

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
Arate = [int(i) for i in input().split()] 
Brate = [int(i) for i in input().split()] 
def Napsac(N,buyrate,sellrate):#どんぐりN個で増やせるどんぐりのマックス
    go,si,br = zip(buyrate,sellrate)
    DP = list(range(N+1)) #DP[x] = どんぐりk個をちょうど使って増やせるどんぐり資産価値の最大値
    nicemetal = []
    for metal in go,si,br:
        if metal[1]>metal[0]:
            nicemetal.append(metal)
    if nicemetal == []:return N
    for i in range(N):
        for metal in nicemetal:
            if i + metal[0] > N:continue
            DP[i + metal[0]] = max(DP[i + metal[0]] , DP[i] + metal[1])
    return max(DP)
donguriAB=Napsac(N,Arate,Brate)
Ans = Napsac(donguriAB,Brate,Arate)
print(Ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
Arate = [int(i) for i in input().split()] 
Brate = [int(i) for i in input().split()] 
def Napsac(N,buyrate,sellrate):#どんぐりN個で増やせるどんぐりのマックス
    go,si,br = zip(buyrate,sellrate)
    DP = list(range(N+1)) #DP[x] = どんぐりk個をちょうど使って増やせるどんぐり資産価値の最大値
    for i in range(N):
        for metal in go,si,br:
            if i + metal[0] > N:continue
            DP[i + metal[0]] = max(DP[i + metal[0]] , DP[i] + metal[1])
    return max(DP)
donguriAB=Napsac(N,Arate,Brate)
Ans = Napsac(donguriAB,Brate,Arate)
print(Ans)

Submission status: Time Limit Exceeded

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

def arbitrage(g1, s1, b1, g2, s2, b2, n):
    before = [g1, s1, b1]
    after = [g2, s2, b2]
    dp = []
    for i in range(4):
        dp_plus = [0] * (n + 1)
        dp.append(dp_plus)
    for i in range(3):
        for j in range(n + 1):
            if j < before[i]:
                dp[i + 1][j] = dp[i][j]
            else:
                dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - before[i]] + after[i])
    return max(n, max([dp[3][k] + (n - k) for k in range(n + 1)]))

trade1 = arbitrage(ga, sa, ba, gb, sb, bb, n)
trade2 = arbitrage(gb, sb, bb, ga, sa, ba, trade1)
print(trade2)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    GA, SA, BA = map(int, input().split())
    GB, SB, BB = map(int, input().split())

    def napsac(x, g1, s1, b1, g2, s2, b2):
        dp = [None for _ in range(x+1)]
        dp[0] = 0

        # def case(i, m1, m2):
        #     return 0 if i - m1 < 0 else dp[i-m1] + m2

        # for i in range(1,x+1):
        #     dp[i] = max(i, case(i, g1, g2), case(i, s1, s2), case(i, b1, b2))

        gsbs = ((g1, g2), (s1, s2), (b1, b2))

        for i in range(1,x+1):
            dp[i] = i
            for m in gsbs:
                if m[0] <= i:
                    dp[i] = max(dp[i], dp[i-m[0]] + m[1])
                    
        return dp[-1]

    print(napsac(napsac(N, GA, SA, BA, GB, SB, BB), GB, SB, BB, GA, SA, BA))

if __name__ == "__main__":
    main()

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:
        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,k,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:
        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(3))
        N = dp[-1]
        return N

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    for C in ((A,B), (B,A)):
        *X, = filter(lambda c: int.__sub__(*c)<0, zip(*C))
        N = small_dp(N, len(X), X)
    res = N

    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,k,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:
        dp = np.zeros((3, N+1), dtype=int)
        M = np.arange(N+1)
        dp[0] = X[0][1]*(M//X[0][0]) + M%X[0][0]
        for i in range(1,3):
            dp[i,:X[i][0]] = dp[i-1,:X[i][0]]
            for w in range(X[i][0],N+1):
                dp[i,w] = max(dp[i-1,w], dp[i,w-X[i][0]]+X[i][1])
        N = dp[-1][N]
        return N

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    for C in ((A,B), (B,A)):
        *X, = filter(lambda c: int.__sub__(*c)<0, zip(*C))
        N = small_dp(N, len(X), X)
    res = N

    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,k,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:
        dp = np.zeros((3, N+1), dtype=int)
        M = np.arange(N+1)
        dp[0] = X[0][1]*(M//X[0][0]) + M%X[0][0]
        for i,w in product(range(1,3), range(1,N+1)):
            dp[i,w] = max(dp[i-1,w], dp[i,w-X[i][0]]+X[i][1] if w >= X[i][0] else 0)
        N = dp[-1][N]
        return N

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    for C in ((A,B), (B,A)):
        *X, = filter(lambda c: int.__sub__(*c)<0, zip(*C))
        N = small_dp(N, len(X), X)
    res = N

    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,k,X):
    if k == 0:
        return N
    if k == 1:
        return X[0][1]*(N//X[0][0]) + N%X[0][0]
    if k == 2:
        return max(X[1][1]*i + X[0][1]*((N-i*X[1][0])//X[0][0]) + (N-i*X[1][0])%X[0][0] for i in range(N//X[1][0]+1))
    if k == 3:
        Y = sorted([(-x[1]/x[0], -x[0], x[1]) for x in X])
        X = [(-y[1], y[2]) for y in Y]
        M = np.arange(N+1)
        dp0 = X[0][1]*(M//X[0][0]) + M%X[0][0]
        d = X[1][1]*(M//X[1][0]) + M%X[1][0]
        dp1 = [np.max(dp0[w::-1] + d[:w+1]) for w in range(N,-1,-1)]
        return np.max(dp1 + X[2][1]*(M//X[2][0]) + M%X[2][0])

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    for C in ((A,B), (B,A)):
        *X, = filter(lambda c: int.__sub__(*c)<0, zip(*C))

        N = small_dp(N, len(X), X)
    res = N

    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,k,X):
    if k == 0:
        return N
    if k == 1:
        return X[0][1]*(N//X[0][0]) + N%X[0][0]
    if k == 2:
        return max(X[1][1]*i + X[0][1]*((N-i*X[1][0])//X[0][0]) + (N-i*X[1][0])%X[0][0] for i in range(N//X[1][0]+1))
    if k == 3:
        M = np.arange(N+1)
        dp0 = X[0][1]*(M//X[0][0]) + M%X[0][0]
        d = X[1][1]*(M//X[1][0]) + M%X[1][0]
        dp1 = [np.max(dp0[w::-1] + d[:w+1]) for w in range(N,-1,-1)]
        return np.max(dp1 + X[2][1]*(M//X[2][0]) + M%X[2][0])

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    for C in ((A,B), (B,A)):
        *X, = filter(lambda c: int.__sub__(*c)<0, zip(*C))
        N = small_dp(N, len(X), X)
    res = N

    return str(res)

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

Submission status: Time Limit Exceeded

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

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

B = dp1[-1]
dp2 = [0] * (B + 1)
if B > 5001:
    for i in range(1, 5001):
        feat = [i]
        if i - gb >= 0:
            feat.append(dp2[i-gb]+ga)
        if i - sb >= 0:
            feat.append(dp2[i-sb]+sa)
        if i - bb >= 0:
            feat.append(dp2[i-bb]+ba)
        dp2[i] = max(feat)

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

print(dp2[-1])

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,k,X):
    if k == 0:
        return N
    if k == 1:
        return X[0][1]*(N//X[0][0]) + N%X[0][0]
    if k == 2:
        return max(X[1][1]*i + X[0][1]*((N-i*X[1][0])//X[0][0]) + (N-i*X[1][0])%X[0][0] for i in range(N//X[1][0]+1))
    if k == 3:
        dp = np.zeros((3, N+1), dtype=int)
        M = np.arange(N+1)
        dp[0] = X[0][1]*(M//X[0][0]) + M%X[0][0]
        d = X[1][1]*(M//X[1][0]) + M%X[1][0]
        for w in range(1, N+1):
            dp[1,w] = np.max(dp[0,w::-1] + d[:w+1])
        return np.max(dp[1,::-1] + X[2][1]*(M//X[2][0]) + M%X[2][0])

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    for C in ((A,B), (B,A)):
        *X, = filter(lambda c: int.__sub__(*c)<0, zip(*C))
        N = small_dp(N, len(X), X)
    res = N

    return str(res)

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

Submission status: Time Limit Exceeded

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

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

B = dp1[-1]
dp2 = [0] * (B + 1)
if n > 5001:
    for i in range(1, 5001):
        feat = [i]
        if i - gb >= 0:
            feat.append(dp2[i-gb]+ga)
        if i - sb >= 0:
            feat.append(dp2[i-sb]+sa)
        if i - bb >= 0:
            feat.append(dp2[i-bb]+ba)
        dp2[i] = max(feat)

    for i in range(5001, B+5001):
        dp2[i] = max(i-5000, dp2[i-gb]+ga, dp2[i-sb]+sa, dp2[i-bb]+ba)
else:
    for i in range(1, B+1):
        feat = [i]
        if i - gb >= 0:
            feat.append(dp2[i-gb]+ga)
        if i - sb >= 0:
            feat.append(dp2[i-sb]+sa)
        if i - bb >= 0:
            feat.append(dp2[i-bb]+ba)
        dp2[i] = max(feat)

print(dp2[-1])

Submission status: Time Limit Exceeded

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

def small_dp(N,X):
    if len(X) == 0:
        return N
    if len(X) == 1:
        return X[0][1]*(N//X[0][0]) + N%X[0][0]
    if len(X) == 2:
        return max(X[1][1]*i + small_dp(N-i*X[1][0], X[:-1]) for i in range(N//X[1][0]+1))
    if len(X) == 3:
        return max(X[-1][1]*i + small_dp(N-i*X[-1][0], X[:-1]) for i in range(N//X[-1][0]+1))
        
def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    for C in ((A,B), (B,A)):
        *X, = sorted(filter(lambda c: int.__sub__(*c)<0, zip(*C)), reverse=True)
        N = small_dp(N, X)
    res = N

    return str(res)

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

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] * (n + 1)
for i in range(1, n+1):
    feat = [i]
    if i - ga >= 0:
        feat.append(dp[i-ga]+gb)
    if i - sa >= 0:
        feat.append(dp[i-sa]+sb)
    if i - ba >= 0:
        feat.append(dp[i-ba]+bb)
    dp[i] = max(feat)

B = dp[-1]
dp = [0] * (B + 1)
for i in range(1, B+1):
    feat = [i]
    if i - gb >= 0:
        feat.append(dp[i-gb]+ga)
    if i - sb >= 0:
        feat.append(dp[i-sb]+sa)
    if i - bb >= 0:
        feat.append(dp[i-bb]+ba)
    dp[i] = max(feat)
print(dp[-1])

Submission status: Time Limit Exceeded

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

def small_dp(N,X):
    if len(X) == 0:
        return N
    if len(X) == 1:
        return X[0][1]*(N//X[0][0]) + N%X[0][0]
    if len(X) == 2:
        return max(X[0][1]*i + X[1][1]*((N-i*X[0][0])//X[1][0]) + (N-i*X[0][0])%X[1][0] for i in range(N//X[0][0]+1))
    if len(X) == 3:
        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))
        
def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    for C in ((A,B), (B,A)):
        *X, = sorted(filter(lambda c: int.__sub__(*c)<0, zip(*C)), reverse=True)
        N = small_dp(N, X)
    res = N

    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

def small_dp(N,X):
    if len(X) == 0:
        return N
    if len(X) == 1:
        return X[0][1]*(N//X[0][0]) + N%X[0][0]
    if len(X) == 2:
        return max(X[0][1]*i + X[1][1]*((N-i*X[0][0])//X[1][0]) + (N-i*X[0][0])%X[1][0] for i in range(N//X[0][0]+1))
    if len(X) == 3:
        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))
        
def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    for C in ((A,B), (B,A)):

        *X, = filter(lambda c: int.__sub__(*c)<0, zip(*C))
        N = small_dp(N, X)
    res = N

    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

def small_dp(N,X):
    if len(X) == 0:
        return N
    if len(X) == 1:
        return X[0][1]*(N//X[0][0]) + N%X[0][0]
    if len(X) == 2:
        return max(X[0][1]*i + X[1][1]*((N-i*X[0][0])//X[1][0]) + (N-i*X[0][0])%X[1][0] for i in range(N//X[0][0]+1))
    if len(X) == 3:
        return max(X[0][1]*i + 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 i in range(N//X[0][0]+1) for j in range((N-i*X[0][0])//X[1][0]+1))
        
def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    for C in ((A,B), (B,A)):

        *X, = filter(lambda c: int.__sub__(*c)<0, zip(*C))
        N = small_dp(N, X)
    res = N

    return str(res)

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