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: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N = I()
    g, s, b = MI()
    G, S, B = MI()
    x,y,z = G - g, S - s, B - b    
    X,Y,Z = -x,-y,-z
    
    ans = 0
    for i in range(N//g+1):
        for j in range(N // s+1):
            if g * i + s * j < 0:
                break
            k =( N - (g * i + s * j))//b
            if z < 0:
                ans =max(ans,i * x + j * y)
            else:
                ans = max(ans, i * x + j * y + k * z)
    for i in range(N//G+1):
        for j in range(N // S+1):
            if G * i + S * j < 0:
                break
            k = (N-(G * i + S * j))//B
            if Z < 0:
                ans = max(ans,i * X + j * Y)
            else:
                ans = max(ans, i * X + j * Y + k * Z)
    print(ans+N)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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][0]*i + X[1][0]*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: Wrong Answer

Solution code:
def main():
    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(N)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N = I()
    g, s, b = MI()
    G, S, B = MI()
    a, b, c = G - g, S - s, B - b
    A, B, C = -a, -b, -c
    
    ans = 0
    for i in range(N//g+1):
        for j in range(N // s+1):
            if g * i + s * j < 0:
                break
            k = N - (g * i + s * j)
            if c < 0:
                ans =max(ans,i * a + j * b)
            else:
                ans = max(ans, i * a + j * b + k * c)
    for i in range(N//G+1):
        for j in range(N // S+1):
            if G * i + S * j < 0:
                break
            k = N-(G * i + S * j)
            if C < 0:
                ans = max(ans,i * A + j * B)
            else:
                ans = max(ans, i * A + j * B + k * C)
    print(ans+N)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N = I()
    g, s, b = MI()
    G, S, B = MI()
    a, b, c = G - g, S - s, B - b
    A, B, C = -a, -b, -c
    
    ans = 0
    for i in range(N//g+1):
        for j in range(N // s+1):
            if g * i + s * j < 0:
                break
            k = N - (g * i + s * j)
            ans = max(ans, i * a + j * b + k * c)
    for i in range(N//G+1):
        for j in range(N // S+1):
            if G * i + S * j < 0:
                break
            k = N-(G * i + S * j)
            ans = max(ans, i * A + j * B + k * C)
    print(ans+N)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

# A : ドングリと金属に変える
# B : 全部ドングリに変える
# B : ドングリと金属に変える（無駄な操作でロスがないので、いったんドングリ化してよい）
# A : ドングリに変える

dp1 = [0] * (N + 1)  # dp1[巣から持ってきたドングリ数] = 取引所 B で持てる最大ドングリ数
dp1[0] = 0
for i in range(N + 1):
    if i >= ga:
        dp1[i] = max(dp1[i], dp1[i - ga] + gb)
    if i >= sa:
        dp1[i] = max(dp1[i], dp1[i - sa] + sb)
    if i >= ba:
        dp1[i] = max(dp1[i], dp1[i - ba] + bb)

N = dp1[N]
dp2 = [0] * (N + 1)  # dp2[取引所 B から持ってきたドングリ数] = 巣に持ち帰れる最大ドングリ数
for i in range(N + 1):
    if i >= gb:
        dp2[i] = max(dp2[i], dp2[i - gb] + ga)
    if i >= sb:
        dp2[i] = max(dp2[i], dp2[i - sb] + sa)
    if i >= bb:
        dp2[i] = max(dp2[i], dp2[i - bb] + ba)
print(dp2[N])

Submission status: Wrong Answer

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):
            for w in range(N+1):
                a = w // gsb[0][i]
                b = a * gsb[1][i]
                dp[i+1][w] = max(dp[i][w], dp[i][w-a*gsb[0][i]] + b)
        # for r in dp:
        #     print(r)
        return dp[3][N]
    tmp = f(N, GSB)
    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))

    # import time
    # for _ in range(10000):
    #     N = 5000
    #     GSB = [
    #         [random.randint(1, 5000), random.randint(
    #             1, 5000), random.randint(1, 5000)],
    #         [random.randint(1, 5000), random.randint(
    #             1, 5000), random.randint(1, 5000)],
    #     ]
    #     GSB = [[1, 1739, 2511], [4042, 4870, 1731]]
    #     s = time.time()
    #     print(slv(N, GSB))
    #     e = time.time()
    #     if e - s > 1:
    #         print(GSB)
    #         break


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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=[0]*(M+1)
C=[A[i]-B[i] for i in range(3) if A[i]-B[i]>=0]
for i in range(M):
    dp[i+1]=max(dp[i+1],dp[i])
    for j in range(len(C)):
        if i+1-B[j]>=0:
            dp[i+1]=max(dp[i+1],dp[i+1-B[j]]+C[j])
print(M+dp[M])

Submission status: Wrong Answer

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 in range(n+1):
    for a, b in zip(A, B):
        if i + a <= n:
            dp[i + a] = max(dp[i + a], dp[i] + b)
n = dp[n]
dp = [0] * (n+1)
for i in range(n+1):
    for b, a in zip(B, A):
        if i + b <= n:
            dp[i + b] = max(dp[i + b], dp[i] + a)
print(dp[n])

Submission status: Wrong Answer

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):
    GSB.append(GSB[1])
    GSB.append(GSB[0])
    @lru_cache(maxsize=None)
    def f(N, i, j):
        if j == 3:
            return 0
        ans = N
        for k in range(N//GSB[i][j]+1):
            ans = max(ans, f(N-(k*GSB[i][j]), i, j+1) + k*GSB[i+1][j])
        return ans

    ans = f(N, 0, 0)
    ans = f(ans, 2, 0)
    return ans


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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
ga, sa, ba = [int(item) for item in input().split()]
gb, sb, bb = [int(item) for item in input().split()]
 
w = [ga, sa, ba]
score = [gb-ga, sb-sa, bb-ba]
dp = [[0] * (n+1) for _ in range(4)]
val = 0
for i in range(1, 4):
    s = score[i-1]
    for j in range(n+1):
        if j >= w[i-1]:
            dp[i][j] = max(dp[i][j], dp[i][j-w[i-1]] + s, dp[i-1][j-w[i-1]] + s)
        val = max(val, dp[i][j])
n += val
w = [gb, sb, bb]
score = [ga-gb, sa-sb, ba-bb]
dp = [[0] * (n+1) for _ in range(4)]
val = 0
for i in range(1, 4):
    s = score[i-1]
    for j in range(n+1):
        if j >= w[i-1]:
            dp[i][j] = max(dp[i][j], dp[i][j-w[i-1]] + s, dp[i-1][j-w[i-1]] + s)
        val = max(val, dp[i][j])
print(n+val)

Submission status: Wrong Answer

Solution code:
n = int(input())
ga, sa, ba = [int(item) for item in input().split()]
gb, sb, bb = [int(item) for item in input().split()]
 
w = [ga, sa, ba]
score = [gb-ga, sb-sa, bb-ba]
dp = [[0] * (n+1) for _ in range(4)]
val = 0
for i in range(1, 4):
    for j in range(n+1):
        if j >= w[i-1]:
            dp[i][j] = max(dp[i][j], dp[i][j-w[i-1]] + score[i-1])
            dp[i][j] = max(dp[i][j], dp[i-1][j-w[i-1]] + score[i-1])
        val = max(val, dp[i][j])
n += val
w = [gb, sb, bb]
score = [ga-gb, sa-sb, ba-bb]
dp = [[0] * (n+1) for _ in range(4)]
val = 0
for i in range(1, 4):
    for j in range(n+1):
        if j >= w[i-1]:
            dp[i][j] = max(dp[i][j], dp[i][j-w[i-1]] + score[i-1])
            dp[i][j] = max(dp[i][j], dp[i-1][j-w[i-1]] + score[i-1])
        val = max(val, dp[i][j])
print(n+val)

Submission status: Wrong Answer

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

for i in range(N+1):
	for j in gsb_delta:
		if i-j >= 0:
			dp[i] = max(dp[i], dp[i-j]+gsb_delta[j])
		elif i >= 1 and i-j < 0:
			dp[i] = max(dp[i], dp[i-1] + 1)
# print(dp)
print(dp[N])

Submission status: Wrong Answer

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):
    GSB.append(GSB[1])
    GSB.append(GSB[0])
    @lru_cache(maxsize=None)
    def f(N, i, j):
        if j == 2:
            return 0
        ans = N
        for k in range(N//GSB[i][j]+1):
            ans = max(ans, f(N-(k*GSB[i][j]), i, j+1) + k*GSB[i+1][j])
        return ans

    ans = f(N, 0, 0)
    ans = f(ans, 2, 0)
    return ans


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

    # N = 5000
    # GSB = [
    #     [random.randint(1, 5000), random.randint(
    #         1, 5000), random.randint(1, 5000)],
    #     [random.randint(1, 5000), random.randint(
    #         1, 5000), random.randint(1, 5000)],
    # ]
    # print(slv(N, GSB))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math


def f(a, b, n):
    ga, sa, ba = a
    gb, sb, bb = b

    max_v = n
    max_s = 0, 0, 0

    for i in range(n + 1):
        v1 = ga * i
        if v1 > n:
            break
        for j in range(n + 1):
            v2 = sa * j
            if v1 + v2 > n:
                break
            k = (n - v1 - v2) // ba
            v3 = ba * k
            v = v1 * (gb - ga) + v2 * (sb - sa) + v3 * (bb - ba)
            if v >= max_v:
                max_v = v
                max_s = i, j, k
    return max_s


def main():
    N = int(input())
    ga, sa, ba = [int(a) for a in input().split()]
    gb, sb, bb = [int(a) for a in input().split()]

    n = N
    gkoukan, skoukan, bkoukan = f((ga, sa, ba), (gb, sb, bb), n)
    n += gkoukan * (gb - ga)
    n += skoukan * (sb - sa)
    n += bkoukan * (bb - ba)

    gkoukan, skoukan, bkoukan = f((gb, sb, bb), (ga, sa, ba), n)
    n += gkoukan * -(gb - ga)
    n += skoukan * -(sb - sa)
    n += bkoukan * -(bb - ba)
    print(n)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math


def f(a, b, n):
    ga, sa, ba = a
    gb, sb, bb = b

    max_v = -1
    max_s = None

    for i in range(n):
        v1 = ga * i
        if v1 > n:
            break
        for j in range(n):
            v2 = sa * j
            if v1 + v2 > n:
                break
            k = (n - v1 - v2) // ba
            v3 = ba * k
            v = v1 * gb + v2 * sb + v3 * bb
            if v >= max_v:
                max_v = v
                max_s = i, j, k
    return max_s


def main():
    N = int(input())
    ga, sa, ba = [int(a) for a in input().split()]
    gb, sb, bb = [int(a) for a in input().split()]

    n = N
    gkoukan, skoukan, bkoukan = f((ga, sa, ba), (gb, sb, bb), n)
    n += gkoukan * gb
    n += skoukan * sb
    n += bkoukan * bb

    gkoukan, skoukan, bkoukan = f((gb, sb, bb), (ga, sa, ba), n)
    n += gkoukan * gb
    n += skoukan * sb
    n += bkoukan * bb
    print(n)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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))
        if X:
            dp = np.zeros((len(X), N+1), dtype=int)
            dp[0] = X[0][1]*np.repeat(np.arange(N//X[0][0]+1), X[0][0])[:N+1]
            dp[0] = np.max((dp[0], np.arange(N+1)), axis=0)
            for i,w in product(range(1,len(X)), range(1,N+1)):
                dp[i][w] = (dp[i-1, w-X[i][0]*np.arange(w//X[i][0]+1)]+X[i][1]*np.arange(w//X[i][0]+1)).max()
            N = dp[-1][N]
    res = N

    return res

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

Submission status: Wrong Answer

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):
        ga, sa, ba = GSB[0]
        gb, sb, bb = GSB[1]
        ans = N
        for i in range((N//ga+1) if ga < gb else 1):
            M = N - i*ga
            assert(M >= 0)
            g = i*gb
            for j in range((M//sa+1) if sa < sb else 1):
                L = M - j*sa
                assert(L >= 0)
                s = j*sb
                if ba < bb:
                    b = (L//ba) * bb
                else:
                    b = L
                ans = max(ans, g+s+b)
        return ans

    ans = f(N, GSB)
    GSB[0], GSB[1] = GSB[1], GSB[0]
    ans = f(ans, GSB)
    return ans


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

    # N = 5000
    # GSB = [
    #     [random.randint(1, 5000), random.randint(
    #         1, 5000), random.randint(1, 5000)],
    #     [random.randint(1, 5000), random.randint(
    #         1, 5000), random.randint(1, 5000)],
    # ]
    # print(slv(N, GSB))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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, b1, b2), 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: Wrong Answer

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):
        ga, sa, ba = GSB[0]
        gb, sb, bb = GSB[1]
        ans = N
        for i in range((N//ga+1) if ga < gb else 1):
            M = N - i*ga
            g = i*gb
            for j in range((M//sa+1) if sa < sb else 1):
                L = M - j*sa
                s = j*sb
                if ba < bb:
                    b = L//ba * bb
                else:
                    b = L
                ans = max(ans, g+s+b)
        return ans

    ans = f(N, GSB)
    GSB[0], GSB[1] = GSB[1], GSB[0]
    ans = f(ans, GSB)
    return ans


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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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):
        ga, sa, ba = GSB[0]
        gb, sb, bb = GSB[1]
        ans = N
        for i in range((N//ga+1) if ga < gb else 1):
            M = N - i*ga
            g = i*gb
            for j in range((M//sa+1) if sa < sb else 1):
                L = M - j*sa
                s = j*sb
                if ba < bb:
                    b = L//ba * bb
                else:
                    b = L
                ans = max(ans, g+s+b)
        return ans

    ans = f(N, GSB)
    print(ans)
    GSB[0], GSB[1] = GSB[1], GSB[0]
    ans = f(ans, GSB)
    return ans


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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
gsbA = list(map(int,input().split()))
gsbB = list(map(int,input().split()))
ans = N
gRange = range(N+1)
if gsbA[0]>gsbB[0]:
    gRange = [0]
sRange = range(N+1)
if gsbA[1]>gsbB[1]:
    sRange = [0]

for g in gRange:
    for s in sRange:
        b = N-g-s
        if b<0:break
        ans =max(ans,N+sum(max(0,gsbB[i]-gsbA[i])*[g,s,b][i] for i in range(3)))
M = ans

gRange = range(M+1)
if gsbA[0]<gsbB[0]:
    gRange = [0]
sRange = range(M+1)
if gsbA[1]<gsbB[1]:
    sRange = [0]

for g in gRange:
    for s in sRange:
        b = M-g-s
        if b<0:break
        ans =max(ans,M+sum(max(0,gsbA[i]-gsbB[i])*[g,s,b][i] for i in range(3)))
print(ans)