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:
# -*- coding: utf-8 -*-
from itertools import product
from heapq import heappop, heappush

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

AA = list(map(int,input().split()))
BB = list(map(int,input().split()))

A = AA
B = BB
diff = [B[i]-A[i] for i in range(3)]
mxc = 0
mxarg = (0,0,0)

R1 = (N+1)//A[0]+1
R2 = (N+1)//A[1]+1
R3 = (N+1)//A[2]+1
for i_g in range( R1 ):
    if diff[0] < 0 and i_g != 0:
        break
    sm1 = A[0]*i_g
    if sm1 > N:
        break
    for i_s in range(R2):
        if diff[1] < 0 and i_s != 0:
            break
        sm2 = sm1 + A[1]*i_s
        if sm2 > N:
            break
        for i_b in range(R3):
            if diff[2] < 0 and i_b != 0:
                break
            sm3 = sm2 + A[2]*i_b
            if sm3 > N:
                break
            cost = diff[0]*i_g + diff[1] *i_s + diff[2]*i_b
            # print((i_g,i_s,i_b),cost)
            if cost > mxc:
                mxc = cost
                mxarg = (i_g,i_s,i_b)
# print(mxarg)
res = N-sum([A[i]*mxarg[i] for i in range(3)])
N = sum([B[i]*mxarg[i] for i in range(3)]) + res
# print(N)

A = BB
B = AA
diff = [B[i]-A[i] for i in range(3)]
mxc = 0
mxarg = (0,0,0)
R1 = (N+1)//A[0]+1
R2 = (N+1)//A[1]+1
R3 = (N+1)//A[2]+1
for i_g in range( R1 ):
    if diff[0] < 0 and i_g != 0:
        break
    sm1 = A[0]*i_g
    if sm1 > N:
        break
    for i_s in range(R2):
        if diff[1] < 0 and i_s != 0:
            break
        sm2 = sm1 + A[1]*i_s
        if sm2 > N:
            break
        for i_b in range(R3):
            if diff[2] < 0 and i_b != 0:
                break
            sm3 = sm2 + A[2]*i_b
            if sm3 > N:
                break
            cost = diff[0]*i_g + diff[1] *i_s + diff[2]*i_b
            # print((i_g,i_s,i_b),cost)
            if cost > mxc:
                mxc = cost
                mxarg = (i_g,i_s,i_b)
# print(mxarg)
res = N-sum([A[i]*mxarg[i] for i in range(3)])
N = sum([B[i]*mxarg[i] for i in range(3)]) + res
# print(N)

print(N)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

AA = list(map(int,input().split()))
BB = list(map(int,input().split()))


A = AA
B = BB
diff = [B[i]-A[i] for i in range(3)]
mxc = 0
mxarg = (0,0,0)
for i_g in range( (N+1)//A[0]+1 ):
    if diff[0] < 0 and i_g != 0:
        break
    if A[0]*i_g > N:
        break
    for i_s in range((N+1)//A[1]+1):
        if diff[1] < 0 and i_s != 0:
            break
        if A[0]*i_g+A[1]*i_s > N:
            break
        for i_b in range((N+1)//A[2]+1):
            if diff[2] < 0 and i_b != 0:
                break
            if A[0]*i_g+A[1]*i_s+A[2]*i_b > N:
                break
            cost = diff[0]*i_g + diff[1] *i_s + diff[2]*i_b
            # print((i_g,i_s,i_b),cost)
            if cost > mxc:
                mxc = cost
                mxarg = (i_g,i_s,i_b)
# print(mxarg)
res = N-sum([A[i]*mxarg[i] for i in range(3)])
N = sum([B[i]*mxarg[i] for i in range(3)]) + res
# print(N)

B = AA
A = BB
diff = [B[i]-A[i] for i in range(3)]
mxc = 0
mxarg = (0,0,0)
for i_g in range( (N+1)//A[0]+1 ):
    if diff[0] < 0 and i_g != 0:
        break
    if A[0]*i_g > N:
        break
    for i_s in range((N+1)//A[1]+1):
        if diff[1] < 0 and i_s != 0:
            break
        if A[0]*i_g+A[1]*i_s > N:
            break
        for i_b in range((N+1)//A[2]+1):
            if diff[2] < 0 and i_b != 0:
                break
            if A[0]*i_g+A[1]*i_s+A[2]*i_b > N:
                break
            cost = diff[0]*i_g + diff[1] *i_s + diff[2]*i_b
            # print((i_g,i_s,i_b),cost)
            if cost > mxc:
                mxc = cost
                mxarg = (i_g,i_s,i_b)
# print(mxarg)
res = N-sum([A[i]*mxarg[i] for i in range(3)])
N = sum([B[i]*mxarg[i] for i in range(3)]) + res
# print(N)
print(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,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)
        dp[0] = np.max((X[0][1]*np.repeat(np.arange(N//X[0][0]+1), X[0][0])[:N+1]+np.tile(np.arange(X[0][0]),N//X[0][0]+1)[:N+1], np.arange(N+1)), axis=0)
        for i,w in product(range(1,3), range(1,N+1)):
            dp[i,w] = np.max((dp[i-1,w::-1] + X[i][1]*np.repeat(np.arange(w//X[i][0]+1), X[i][0])[:w+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:
N = int(input())
a = tuple(map(int, input().split()))
b = tuple(map(int, input().split()))

# 1 周目 : dp[巣から持ってきたドングリ数] = 取引所 B で持てる最大ドングリ数
dp = list(range(N + 1))
mas = tuple(zip(a, b))
for i in range(N + 1):
    for to_metal, to_acorn in mas:
        if i >= to_metal:
            dp[i] = max(dp[i], dp[i - to_metal] + to_acorn)
N = dp[N]

# 2 周目 : dp[取引所 B から持ってきたドングリ数] = 巣に持ち帰れる最大ドングリ数
dp = list(range(N + 1))
mas = tuple(zip(b, a))
for i in range(N + 1):
    for to_metal, to_acorn in mas:
        if i >= to_metal:
            dp[i] = max(dp[i], dp[i - to_metal] + to_acorn)
print(dp[N])

Submission status: Time Limit Exceeded

Solution code:
N = int(input())						# 5
A = list( map(int, input().split()) )	# 1 2 3 4 5 ...
B = list( map(int, input().split()) )	# 1 2 3 4 5 ...


def F(N,A,B):
	dp=[0]*(N+1)
	for n in range(N+1):
		v=n
		for a,b in zip(A,B):
			if n>=a:
				v = max(v, dp[n-a]+b)
		dp[n]=v

		#dp[n] = max(n, max(dp[n-a]+b for a,b in zip(A,B) if n>=a))
	
	return dp[N]

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

Submission status: Time Limit Exceeded

Solution code:
def calc(n, to_metal, to_acorn):
    dp = list(range(n + 1))
    for i in range(n + 1):
        for m, a in zip(to_metal, to_acorn):
            if i >= m:
                dp[i] = max(dp[i], dp[i - m] + a)
    return dp[n]


def main():
    N = int(input())
    a = tuple(map(int, input().split()))
    b = tuple(map(int, input().split()))

    N = calc(N, a, b)  # 1 周目 : dp[巣から持ってきたドングリ数] = 取引所 B で持てる最大ドングリ数
    N = calc(N, b, a)  # 2 周目 : dp[取引所 B から持ってきたドングリ数] = 巣に持ち帰れる最大ドングリ数
    print(N)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def calc(n, to_metal, to_acorn):
    mas = tuple(zip(to_metal, to_acorn))
    dp = list(range(n + 1))
    for i in range(n + 1):
        for m, a in mas:
            if i >= m:
                dp[i] = max(dp[i], dp[i - m] + a)
    return dp[n]


def main():
    N = int(input())
    a = tuple(map(int, input().split()))
    b = tuple(map(int, input().split()))

    N = calc(N, a, b)  # 1 周目 : dp[巣から持ってきたドングリ数] = 取引所 B で持てる最大ドングリ数
    N = calc(N, b, a)  # 2 周目 : dp[取引所 B から持ってきたドングリ数] = 巣に持ち帰れる最大ドングリ数
    print(N)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, GSB):
    def f(N, gsb):
        dp = [[0] * (N+1) for _ in range(4)]
        for i in range(3):
            v = gsb[1][i] - gsb[0][i]
            a = gsb[0][i]
            if v > 0:
                for w in range(N+1):
                    dp[i+1][w] = dp[i][w]
                    if w >= a:
                        dp[i+1][w] = max(dp[i][w], dp[i+1][w-a] + v)
            else:
                dp[i+1] = dp[i]
        return dp[3][N] + N
    tmp = f(N, GSB)
    # print(tmp)
    GSB[0], GSB[1] = GSB[1], GSB[0]
    return f(tmp, GSB)


def main():
    N = read_int()
    GSB = [read_int_n() for _ in range(2)]
    print(slv(N, GSB))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, GSB):
    def f(N, gsb):
        dp = [[0] * (N+1) for _ in range(4)]
        for i in range(3):
            v = gsb[1][i] - gsb[0][i]
            a = gsb[0][i]
            for w in range(N+1):
                dp[i+1][w] = dp[i][w]
                if w >= a:
                    dp[i+1][w] = max(dp[i][w], dp[i+1][w-a] + v)
        return dp[3][N] + N
    tmp = f(N, GSB)
    # print(tmp)
    GSB[0], GSB[1] = GSB[1], GSB[0]
    return f(tmp, GSB)


def main():
    N = read_int()
    GSB = [read_int_n() for _ in range(2)]
    print(slv(N, GSB))


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

ABget =[]
BAget =[]
for i in range(3):
    if A[i] < B[i]:
        ABget.append([A[i],B[i]])
    elif A[i] > B[i]:
        BAget.append([B[i],A[i]])

N2 = N



if len(ABget)==1:
    i,j =ABget[0]
    N2 = N + (N//i)*(j-i)
elif len(ABget)>1:
    DPlis = [i+1 for i in range(N)]
    DPlis.append(0)
    for n,m in ABget:
        if n <= N:
            DPlis[n-1] = max(DPlis[n-1],m)
    for i in range(N):
        DPlis[i] = max(DPlis[i-1]+1,DPlis[i])
        for n,m in ABget:
            if i+n < N:
                DPlis[i+n] = max(DPlis[i+n],DPlis[i]+m)
    N2 =DPlis[-2]

if len(BAget)==1:
    i,j = BAget[0]
    N2 += (N2//i)*(j-i)
elif len(BAget)>1:
    for n,m in BAget:
        DPlis = [i+1 for i in range(N2)]
        if n < N2:
            DPlis[n] = m
    DPlis.append(0)
    for n,m in BAget:
        if n <= N2:
            DPlis[n-1] = max(DPlis[n-1],m)
    for i in range(N2):
        DPlis[i] = max(DPlis[i],DPlis[i-1]+1)
        for n,m in BAget:
            if i+n < N2:
                DPlis[i+n] = max(DPlis[i+n],DPlis[i]+m)
    N2 = DPlis[-2]
print(N2)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = [int(item) for item in input().split()]
B = [int(item) for item in input().split()]
 
dp = [0] * (n+1)
AB = tuple(zip(A, B))
for i in range(n+1):
    for a, b in AB:
        if i - a >= 0:
            y = dp[i-a] + b - a
            if y > dp[i]:
                dp[i] = y
n += dp[n]
dp = [0] * (n+1)
BA = tuple(zip(B, A))
for i in range(n+1):
    for b, a in BA:
        if i - b >= 0:
            y = dp[i-b] + a - b
            if y > dp[i]:
                dp[i] = y 
print(n + dp[n])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = [int(item) for item in input().split()]
B = [int(item) for item in input().split()]
 
dp = [0] * (n+1)
AB = tuple(zip(A, B))
for i in range(n+1):
    for a, b in AB:
        if i - a >= 0:
            dp[i] = max(dp[i], dp[i-a] + b - a)
n += dp[n]
dp = [0] * (n+1)
BA = tuple(zip(B, A))
for i in range(n+1):
    for b, a in BA:
        if i - b >= 0:
            dp[i] = max(dp[i], dp[i-b] + a - b)
print(n + dp[n])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = [int(item) for item in input().split()]
B = [int(item) for item in input().split()]
 
dp = [0] * (n+1)
for i, x in enumerate(dp):
    for a, b in zip(A, B):
        if i - a >= 0:
            dp[i] = max(dp[i], dp[i-a] + b - a)
n += dp[n]
dp = [0] * (n+1)
for i, x in enumerate(dp):
    for b, a in zip(B, A):
        if i - b >= 0:
            dp[i] = max(dp[i], dp[i-b] + a - b)
print(n + dp[n])

Submission status: Time Limit Exceeded

Solution code:
import sys
if sys.version[0] == '2':
    range, input = xrange, raw_input
N = int(input())

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

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

M = max(dp) + N
dp = [0] * (M+1)
for a, b in ((a, b) for a, b in zip(A, B) if a > b):
    for w in range(b, M+1):
        dp[w] = max(dp[w], dp[w-b] + a-b)

print(max(dp) + M)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
dp=[N]*(N+1)
#dp[i];=i個目まで選んで交換したときの最大
C=[B[i]-A[i] for i in range(3)]
for i in range(N):
    dp[i+1]=max(dp[i+1],dp[i])
    for j in range(3):
        if i+1-A[j]>=0:
            dp[i+1]=max(dp[i+1],dp[i+1-A[j]]+C[j])
M=dp[N]
dp=[M]*(M+1)
C=[A[i]-B[i] for i in range(3)]
for i in range(M):
    dp[i+1]=max(dp[i+1],dp[i])
    for j in range(3):
        if i+1-B[j]>=0:
            dp[i+1]=max(dp[i+1],dp[i+1-B[j]]+C[j])
print(dp[M])

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:
                dp[i] = max(dp[i], 0 if i - m[0] < 0 else 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:
import sys
input = sys.stdin.readline

N = int(input())
ca = list(map(int,input().split()))
cb = list(map(int,input().split()))

ab = [b-a for a, b in zip(ca, cb)]
ba = [a-b for a, b in zip(ca, cb)]

def cal(c,d):
    global N
    gx, sx, bx = c
    dg, ds, db = d

    #pair = (0,0,0)
    res = 0

    if dg < 0:
        p = 0
    else:
        p = N // gx

    for k in range(p + 1):
        if ds < 0:
            q = 0
        else:
            q = (N - k*gx) // sx
        for l in range(q + 1):
            m = (N - k*gx - l*sx) // bx
            loss = k*gx + l*sx + m*bx
            if loss > N:
                continue
            score = k*dg + l*ds + m*db
            if score > res:
                res = score
                #pair = (k, l, m)

    N += res

cal(ca,ab)
cal(cb,ba)

print(N)

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

        for i in range(1,x+1):
            dp[i] = max(i, 0 if i - g1 < 0 else dp[i-g1] + g2,
                        0 if i - s1 < 0 else dp[i-s1] + s2,
                        0 if i - b1 < 0 else dp[i-b1] + b2)
            
        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:
import sys
if sys.version[0] == '2':
    range, input = xrange, raw_input
N = int(input())

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

dp = [0] * (N+1)
for a, b in ((a, b) for a, b in zip(A, B) if a < b):
    for w in range(1, N+1):
        if w-a >= 0:
            dp[w] = max(dp[w], dp[w-a] + b-a)

M = max(dp) + N
dp = [0] * (M+1)
for a, b in ((a, b) for a, b in zip(A, B) if a > b):
    for w in range(1, M+1):
        if w-b >= 0:
            dp[w] = max(dp[w], dp[w-b] + a-b)

print(max(dp) + M)

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

    for i in range(1,x+1):
        dp[i] = max(i, 0 if i - g1 < 0 else dp[i-g1] + g2,
                       0 if i - s1 < 0 else dp[i-s1] + s2,
                       0 if i - b1 < 0 else dp[i-b1] + b2)
        
    return dp[-1]

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
ca = list(map(int,input().split()))
cb = list(map(int,input().split()))

ab = [b-a for a, b in zip(ca, cb)]
ba = [a-b for a, b in zip(ca, cb)]

def cal(c,d):
    global N
    gx, sx, bx = c
    dg, ds, db = d

    #pair = (0,0,0)
    res = 0

    for k in range(N // gx + 1):
        for l in range((N - k*gx) // sx + 1):
            m = (N - k*gx - l*sx) // bx
            loss = k*gx + l*sx + m*bx
            if loss > N:
                continue
            score = k*dg + l*ds + m*db
            if score > res:
                res = score
                #pair = (k, l, m)

    N += res

cal(ca,ab)
cal(cb,ba)

print(N)

Submission status: Time Limit Exceeded

Solution code:
import sys
if sys.version[0] == '2':
    range, input = xrange, raw_input
N = int(input())

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

dp = [N]
for w in range(1, N+1):
    dp.append(max([N] + [dp[w-a] + b-a for a, b in zip(A, B) if w-a >= 0]))

M = max(dp)
dp = [M]
for w in range(1, M+1):
    dp.append(max([M] + [dp[w-b] + a-b for a, b in zip(A, B) if w-b >= 0]))

print(max(dp))

Submission status: Time Limit Exceeded

Solution code:
import sys
if sys.version[0] == '2':
    range, input = xrange, raw_input
N = int(input())

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

dp = [N] * (N+1)
for w in range(N+1):
    dp[w] = max([N] + [dp[w-a] + b-a for a, b in zip(A, B) if w-a >= 0])

M = max(dp)
dp = [M] * (M+1)
for w in range(M+1):
    dp[w] = max([M] + [dp[w-b] + a-b for a, b in zip(A, B) if w-b >= 0])

print(max(dp))

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

    return dp[-1]

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

Submission status: Time Limit Exceeded

Solution code:
import sys
if sys.version[0] == '2':
    range, input = xrange, raw_input
N = int(input())

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

dp = [N] * (N+1)
for w in range(N+1):
    for a, b in zip(A, B):
        if w >= a:
            dp[w] = max(dp[w], dp[w-a] + b-a)

M = max(dp)
dp = [M] * (M+1)
for w in range(M+1):
    for a, b in zip(A, B):
        if w >= b:
            dp[w] = max(dp[w], dp[w-b] + a-b)

print(max(dp))

Submission status: Time Limit Exceeded

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
gsb_A = LIST()
gsb_B = LIST()
gsb_delta = {}
# for i in range(3):
# 		if min(gsb_A[i], gsb_B[i]) in gsb_delta:
# 			gsb_delta[min(gsb_A[i], gsb_B[i])] = max(max(gsb_A[i], gsb_B[i]), gsb_delta[min(gsb_A[i], gsb_B[i])])
# 		else:
# 			gsb_delta[min(gsb_A[i], gsb_B[i])] = max(gsb_A[i], gsb_B[i])
# print(gsb_delta)
dp = [0] * (N+1)
for i in range(N+1):
	if i-gsb_A[0] >= 0:
		dp[i] = max(dp[i], dp[i-gsb_A[0]]+gsb_B[0])
	if i-gsb_A[1] >= 0:
		dp[i] = max(dp[i], dp[i-gsb_A[1]]+gsb_B[1])
	if i-gsb_A[2] >= 0:
		dp[i] = max(dp[i], dp[i-gsb_A[2]]+gsb_B[2])
	if i >= 1 and i-gsb_A[0] < 0 and i-gsb_A[1] < 0 and i-gsb_A[2] < 0:
		dp[i] = dp[i-1]+1
# print(dp)
# print(dp[N])
M = max(N, dp[N])
dp = [0] * (M+1)
for i in range(M+1):
	if i-gsb_B[0] >= 0:
		dp[i] = max(dp[i], dp[i-gsb_B[0]]+gsb_A[0])
	if i-gsb_B[1] >= 0:
		dp[i] = max(dp[i], dp[i-gsb_B[1]]+gsb_A[1])
	if i-gsb_B[2] >= 0:
		dp[i] = max(dp[i], dp[i-gsb_B[2]]+gsb_A[2])
	if i >= 1 and i-gsb_B[0] < 0 and i-gsb_B[1] < 0 and i-gsb_B[2] < 0:
		dp[i] = dp[i-1]+1
# print(dp)
print(max(M, dp[M]))