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:
N=int(input())
gsbA = list(map(int,input().split()))
gsbB = list(map(int,input().split()))
ans = N
gsbAB = sorted(((b-a,a,b) for a,b in zip(gsbA,gsbB)),reverse=True)
if gsbAB[2][0]>=0:
    #全部A->B
    for i in range((N+1)//gsbAB[0][1]):
        for j in range((N-i*gsbAB[0][1]+1)//gsbAB[1][1]):
            k = (N-i*gsbAB[0][1] - j*gsbAB[1][1])//gsbAB[2][1]
            if k<0:break
            ans = max(ans,
                     N + sum(max(0,gsbAB[l][0])*[i,j,k][l] for l in range(3)))
elif gsbAB[0][0]<=0:
    #全部B->A
    for i in range((N+1)//gsbAB[0][2]):
        for j in range((N-i*gsbAB[0][2]+1)//gsbAB[1][2]):
            k = (N-i*gsbAB[0][2] - j*gsbAB[1][2])//gsbAB[2][2]
            if k<0:break
            ans = max(ans,
                     N + sum(max(0,-gsbAB[l][0])*[i,j,k][l] for l in range(3)))
elif gsbAB[1][0]>=0:
    #g,s を A->B, bをB->A
    for i in range((N+1)//gsbAB[0][1]):
        j = (N-i*gsbAB[0][1])//gsbAB[1][1]
        if j<0:break
        ans = max(ans,
                  N + sum(max(0,gsbAB[l][0])*[i,j][l] for l in range(2)))
    M = ans
    k = M//gsbAB[2][2]
    ans += max(0,-gsbAB[2][0] * k)
else:
    #gを A->B、s,bをB->A
    i = N//gsbAB[0][1]
    ans += max(0,gsbAB[0][0]*i)
    M=ans
    for j in range((M+1)//gsbAB[1][2]):
        k = (M-j*gsbAB[1][2])//gsbAB[2][2]
        if k<0:break
        ans = max(ans,
                 M + sum(max(0,-gsbAB[l][0])*[0,j,k][l] for l in (1,2)))
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
def nap(N,goods):
    dp=[-1*float('inf')]*(N+1)
    dp[0]=0
    for i in range(N):
        for w,v in goods:
            if i+w<=N:
                dp[i+w]=max(dp[i+w],dp[i]+v)
    return max([dp[i]+N-i for i in range(N+1)])
goods1=[(a,b) for a,b in zip(A,B) if a<b]
goods2=[(b,a) for a,b in zip(A,B) if a>b]
if len(goods1)>0:
    N=nap(N,[(a,b) for a,b in zip(A,B)])
if len(goods2)==0:
    print(N)
elif len(goods2)==1:
    print(N%goods2[0][0]+(N//goods2[0][0])*goods2[0][1])
elif len(goods2)==3:
    print(nap(N,goods2))
else:
    a,b,c,d=goods2[0][0],goods2[0][1],goods2[1][0],goods2[1][1]
    if (b-a)/a<(d-c)/c:
        a,b=c,d
    elif (b-a)/a==(d-c)/c and a>c:
        a,b=c,d
    bord=(N//a)*b+((N%a)//c)*d+(N%a)%c
    x=N//a
    ans=bord
    for i in range(1,1+x):
        now=(x-i)*b+((N-(x-i)*a)//c)*d+(N-(x-i)*a)%c
        if now+d<ans:
            break
        else:
            ans=max(ans,now)
    print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
Ga, Sa, Ba = map(int, input().split())
Gb, Sb, Bb = map(int, input().split())
M = max(Ga, Sa, Ba)
M2 = max(Gb, Sb, Bb)

dp = [0]*(M+1)
for i in range(M+1, N+M+1):
  dp.append(max(
    dp[-1] + 1,
    dp[-Ga] + Gb,
    dp[-Sa] + Sb,
    dp[-Ba] + Bb,
  ))
N2 = dp[-1]

dp = [0]*(M2+1)
for i in range(M2+1, N2+M2+1):
  dp.append(max(
    dp[-1] + 1,
    dp[-Gb] + Ga,
    dp[-Sb] + Sa,
    dp[-Bb] + Ba,
  ))
print(dp[-1])

Submission status: Wrong Answer

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

Solution code:
N = int(input())
Ga, Sa, Ba = map(int, input().split())
Gb, Sb, Bb = map(int, input().split())
M = max(Ga, Sa, Ba)
M2 = max(Gb, Sb, Bb)

dp = [0]*(M+1)
for i in range(M+1, N+M+1):
  dp.append(max(
    dp[-1] + 1,
    dp[-Ga] + Gb,
    dp[-Sa] + Sb,
    dp[-Ba] + Bb,
  ))
N2 = dp[-1]

dp = [0]*(M2+1)
for i in range(M+1, N2+M2+1):
  dp.append(max(
    dp[-1] + 1,
    dp[-Gb] + Ga,
    dp[-Sb] + Sa,
    dp[-Bb] + Ba,
  ))
print(dp[-1])

Submission status: Wrong Answer

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 = []
        if g1 < g2:
            gsbs.append((g1, g2))
        if s1 < s2:
            gsbs.append((s1, s2))
        if b1 < b2:
            gsbs.append((b1, g2))

        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: 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][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] = 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: Wrong Answer

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

dp1 = [0] * (n + 5001)
for i in range(5001, n+5001):
    dp1[i] = max(i-5000, dp1[i-ga]+gb, dp1[i-sa]+sb, dp1[i-ba]+bb)

B = dp1[-1]
dp2 = [0] * (B + 5001)
for i in range(5001, B+5001):
    dp2[i] = max(i-5000, dp2[i-gb]+ga, dp2[i-sb]+sa, dp2[i-bb]+ba)
print(dp2[-1])

Submission status: Wrong Answer

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

dp = [0] * (n + 5001)
for i in range(5001, n+5001):
    dp[i] = max(i-5000, dp[i-ga]+gb, dp[i-sa]+sb, dp[i-ba]+bb)

B = dp[-1]
dp = [0] * (B + 5001)
for i in range(5001, B+5001):
    dp[i] = max(i-5000, dp[i-gb]+ga, dp[i-sb]+sa, dp[i-bb]+ba)
print(dp[-1])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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]+ga)
    dp[i] = max(feat)
print(dp[n])

Submission status: Wrong Answer

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:
        c0 = X[1][1]*(N//X[1][0]) + N%X[1][0]
        c = 0
        for j in range(1, N//X[0][0]+1):
            d = (X[0][1]-X[0][0])*j + (X[1][1]-X[1][0])*((N-j*X[0][0])//X[1][0] - N//X[1][0])
            if d < 0:
                break
            else:
                c = d
        return c0 + c

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

Submission status: Wrong Answer

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:
        c0 = X[1][1]*(N//X[1][0]) + N%X[1][0]
        c = 0
        for j in range(1, N//X[0][0]+1):
            d = (X[0][1]-X[0][0])*j + (X[1][1]-X[1][0])*((N-j*X[0][0])//X[1][0] - N//X[1][0])
            if d < 0:
                break
            else:
                c = d
        return c0 + c

        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:
        """
        e = 0
        for i in range(N//X[0][0]+1):
            e = max(e, X[0][1]*i + small_dp(N-i*X[0][0], X[1:]))
        #return e
        """
        f = 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))
        #print(e,f)
        return f



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

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:
        c0 = X[1][1]*(N//X[1][0]) + N%X[1][0]
        c = 0
        for j in range(1, N//X[0][0]+1):
            d = (X[0][1]-X[0][0])*j + (X[1][1]-X[1][0])*((N-j*X[0][0])//X[1][0] - N//X[1][0])
            if d < 0:
                break
            else:
                c = d
        return c0 + c

        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:
        e = 0
        for i in range(N//X[0][0]+1):
            e = max(e, X[0][1]*i + small_dp(N-i*X[0][0], X[1:]))
        return e

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

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:
        c0 = X[1][1]*(N//X[1][0]) + N%X[1][0]
        c = 0
        for j in range(1, N//X[0][0]+1):
            d = (X[0][1]-X[0][0])*j + (X[1][1]-X[1][0])*((N-j*X[0][0])//X[1][0] - N//X[1][0])
            if d < 0:
                break
            else:
                c = d
        return c0 + c

        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:
        e = 0
        for i in range(N//X[0][0]+1):
            N_ = N - i*X[0][0]
            c0 = X[2][1]*(N_//X[2][0]) + N_%X[2][0]
            c = 0
            for j in range(1, N_//X[1][0]+1):
                d = (X[1][1]-X[1][0])*j + (X[2][1]-X[2][0])*((N_-j*X[1][0])//X[2][0] - N_//X[2][0])
                if d < 0:
                    break
                else:
                    c = d
            e = max(e,  X[0][1]*i + c0 + c)
        return e

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

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:
        c0 = X[1][1]*(N//X[1][0]) + N%X[1][0]
        c = 0
        for j in range(1, N//X[0][0]+1):
            d = (X[0][1]-X[0][0])*j + (X[1][1]-X[1][0])*((N-j*X[0][0])//X[1][0] - N//X[1][0])
            if d < 0:
                break
            else:
                c = d
        return c0 + c

        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:
        e = 0
        for i in range(N//X[0][0]+1):
            N_ = (N-i*X[0][0])
            c0 = X[0][1]*i + X[2][1]*(N_//X[2][0]) + N_%X[2][0]
            c = 0
            for j in range(1, N_//X[1][0]+1):
                d = (X[1][1]-X[1][0])*j + (X[2][1]-X[2][0])*((N_-j*X[1][0])//X[2][0] - N_//X[2][0])
                if d < 0:
                    break
                else:
                    c = d
            e =  max(e, c0+c)
        return e

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

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:
        c0 = X[1][1]*(N//X[1][0]) + N%X[1][0]
        c = 0
        for j in range(1, N//X[0][0]+1):
            d = (X[0][1]-X[0][0])*j + (X[1][1]-X[1][0])*((N-j*X[0][0])//X[1][0] - N//X[1][0])
            if d < 0:
                break
            else:
                c = d
        return c0 + c

        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:
        e = 0
        for i in range(N//X[0][0]+1):
            N_ = (N-i*X[0][0])
            c0 = X[0][1]*i + X[2][1]*(N_//X[2][0]) + N_%X[2][0]
            c = 0
            for j in range(N_//X[1][0]+1):
                d = (X[1][1]-X[1][0])*j + (X[2][1]-X[2][0])*((N_-j*X[1][0])//X[2][0] - N_//X[2][0])
                if d < 0:
                    break
                else:
                    c = d
            e =  max(e, c0+c)
        return e

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

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:
        c0 = X[1][1]*(N//X[1][0]) + N%X[1][0]
        c = 0
        for j in range(1, N//X[0][0]+1):
            d = (X[0][1]-X[0][0])*j + (X[1][1]-X[1][0])*((N-j*X[0][0])//X[1][0] - N//X[1][0])
            if d < 0:
                break
            else:
                c = d
        return c0 + c

        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:
        e = 0
        for i in range(N//X[0][0]+1):
            c0 = X[0][1]*i + X[2][1]*((N-i*X[0][0])//X[2][0]) + ((N-i*X[0][0])%X[2][0])
            c = 0
            for j in range((N-i*X[0][0])//X[1][0]+1):
                d = (X[1][1]-X[1][0])*j + (X[2][1]-X[2][0])*(((N-i*X[0][0])-j*X[1][0])//X[2][0] - (N-i*X[0][0])//X[2][0])
                if d < 0:
                    break
                else:
                    c = d
            e =  max(e,c0 + c)
        return e

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

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:
        c0 = X[1][1]*(N//X[1][0]) + N%X[1][0]
        c = 0
        for j in range(1, N//X[0][0]+1):
            d = (X[0][1]-X[0][0])*j + (X[1][1]-X[1][0])*((N-j*X[0][0])//X[1][0] - N//X[1][0])
            if d < 0:
                break
            else:
                c = d
        return c0 + c

        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:
        e = 0
        for i in range(N//X[0][0]+1):
            c0 = X[0][1]*i + X[2][1]*((N-i*X[0][0])//X[2][0]) + ((N-i*X[0][0])%X[2][0])
            c = 0
            for j in range((N-i*X[0][0])//X[1][0]+1):
                d = (X[1][1]-X[1][0])*j + (X[2][1]-X[2][0])*((N-j*X[1][0])//X[2][0] - N//X[2][0])
                if d < 0:
                    break
                else:
                    c = d
            e =  max(e,c0 + c)
        return e

        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: 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:
        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 0
        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: 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][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: 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][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)
        X[0][1]*np.arange(N+1)//X[0][0] + np.arange(N+1)%X[0][0]

        dp[0] = 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]
        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: 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 > N:
                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 > N:
                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][1]*i + X[1][1]*j + (N-i*X[0][0])%X[1][0], c)
        return c
    if k == 3:
        c = 0
        for i in range(N//X[0][0]+1):
            for j in range((N-i*X[0][0])//X[1][0]+1):
                k = (N-i*X[0][0]-j*X[1][0])//X[2][0]
            c = max(X[0][1]*i + X[1][1]*j + X[2][1]*k + (N-i*X[0][0]-j*X[1][0])%X[2][0], c)
        return c

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