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())
a1, a2, a3 = map(int, input().split())
b1, b2, b3 = map(int, input().split())
A = [a1, a2, a3]
B = [b1, b2, b3]
if (a1<=b1 and a2<=b2 and a3<=b3) or (a1>=b1 and a2>=b2 and a3>=b3):
    if a1>b1 or a2>b2 or a3>b3:
        A, B = B, A
    dp = [[-float("inf")]*(N+1+5050) for _ in range(4)]
    dp[0][0] = 0
    for i in range(3):
        ai = A[i]
        bi = B[i]
        for j in range(N+1):

            dp[i+1][j] = max(dp[i][j], dp[i+1][j-ai]+bi)
    print(max(dp[3]))
    #print(dp[3][:30])
else:
    C, D = [], []
    for a, b in zip(A, B):
        if a <= b:
            C.append([a,b])
        else:
            D.append([b, a])
            
    if len(C)==1:
        a, b = C[0]
        q, n = divmod(N, a)
        N_ = q*b + n
    else:
        N_ = N
        (a1, b1), (a2, b2) = C
        for i in range(N + 1):
            j = N - i
            q1, n1 = divmod(i, a1)
            q2, n2 = divmod(i, a2)
            N_ = max(N_, q1 * b1 + q2 * b2 + n1 + n2)
        
    if len(D)==1:
        a, b = D[0]
        q, n = divmod(N_, a)
        print(q*b + n)
        #print("a")
    else:
        ans = 0
        (a1, b1), (a2, b2) = D
        for i in range(N_+1):
            j = N_ - i
            q1, n1 = divmod(i, a1)
            q2, n2 = divmod(i, a2)
            ans = max(ans, q1*b1+q2*b2+n1+n2)
        print(ans)

Submission status: Wrong Answer

Solution code:
#D問題
N = int(input())
GSB1 = list(map(int,input().split()))
GSB2 = list(map(int,input().split()))
gper = N-(GSB1[0]*(N//GSB1[0]))+(N//GSB1[0])*GSB2[0]
sper = N-(GSB1[1]*(N//GSB1[1]))+(N//GSB1[1])*GSB2[1]
bper = N-(GSB1[2]*(N//GSB1[2]))+(N//GSB1[2])*GSB2[2]
if gper >= sper and gper >= bper:
    n1 = N//GSB1[0]
    N-=n1
    sper = N-(GSB1[1]*(N//GSB1[1]))+(N//GSB1[1])*GSB2[1]
    bper = N-(GSB1[2]*(N//GSB1[2]))+(N//GSB1[2])*GSB2[2]
    if sper >= bper:
        n2 = N//GSB1[1]
        n3 = N//GSB1[2]
        N+=n3*GSB2[2]
        N+=n2*GSB2[1]
    else:
        n2 = N//GSB1[2]
        n3 = N//GSB1[1]
        N+=n3*GSB2[1]
        N+=n2*GSB2[2]
    N+=n1*GSB2[0]
    
elif sper >= gper and sper >= bper:
    n1 = N//GSB1[1]
    N-=n1
    gper = N-(GSB1[0]*(N//GSB1[0]))+(N//GSB1[0])*GSB2[0]
    bper = N-(GSB1[2]*(N//GSB1[2]))+(N//GSB1[2])*GSB2[2]
    if gper >= bper:
        n2 = N//GSB1[0]
        n3 = N//GSB1[2]
        N+=n3*GSB2[2]
        N+=n2*GSB2[0]
    else:
        n2 = N//GSB1[2]
        n3 = N//GSB1[0]
        N+=n3*GSB2[0]
        N+=n2*GSB2[2]
    N+=n1*GSB2[1]
    
elif bper >= sper and bper >= gper:
    n1 = N//GSB1[2]
    N-=n1
    gper = N-(GSB1[0]*(N//GSB1[0]))+(N//GSB1[0])*GSB2[0]
    sper = N-(GSB1[1]*(N//GSB1[1]))+(N//GSB1[1])*GSB2[1]
    if gper >= sper:
        n2 = N//GSB1[0]
        n3 = N//GSB1[1]
        N+=n3*GSB2[1]
        N+=n2*GSB2[0]
    else:
        n2 = N//GSB1[1]
        n3 = N//GSB1[0]
        N+=n3*GSB2[0]
        N+=n2*GSB2[1]
    N+=n1*GSB2[2]
    
gper = N-(GSB2[0]*(N//GSB2[0]))+(N//GSB2[0])*GSB1[0]
sper = N-(GSB2[1]*(N//GSB2[1]))+(N//GSB2[1])*GSB1[1]
bper = N-(GSB2[2]*(N//GSB2[2]))+(N//GSB2[2])*GSB1[2]
if gper >= sper and gper >= bper:
    n1 = N//GSB2[0]
    N-=n1
    sper = N-(GSB2[1]*(N//GSB2[1]))+(N//GSB2[1])*GSB1[1]
    bper = N-(GSB2[2]*(N//GSB2[2]))+(N//GSB2[2])*GSB1[2]
    if sper >= bper:
        n2 = N//GSB2[1]
        n3 = N//GSB2[2]
        N+=n3*GSB1[2]
        N+=n2*GSB1[1]
    else:
        n2 = N//GSB2[2]
        n3 = N//GSB2[1]
        N+=n3*GSB1[1]
        N+=n2*GSB1[2]
    N+=n1*GSB1[0]
    
elif sper >= gper and sper >= bper:
    n1 = N//GSB2[1]
    N-=n1
    gper = N-(GSB2[0]*(N//GSB2[0]))+(N//GSB2[0])*GSB1[0]
    bper = N-(GSB2[2]*(N//GSB2[2]))+(N//GSB2[2])*GSB1[2]
    if gper >= bper:
        n2 = N//GSB2[0]
        n3 = N//GSB2[2]
        N+=n3*GSB1[2]
        N+=n2*GSB1[0]
    else:
        n2 = N//GSB2[2]
        n3 = N//GSB2[0]
        N+=n3*GSB1[0]
        N+=n2*GSB1[2]
    N+=n1*GSB1[1]
    
elif bper >= sper and bper >= gper:
    n1 = N//GSB2[2]
    N-=n1
    gper = N-(GSB2[0]*(N//GSB2[0]))+(N//GSB2[0])*GSB1[0]
    sper = N-(GSB2[1]*(N//GSB2[1]))+(N//GSB2[1])*GSB1[1]
    if gper >= sper:
        n2 = N//GSB2[0]
        n3 = N//GSB2[1]
        N+=n3*GSB1[1]
        N+=n2*GSB1[0]
    else:
        n2 = N//GSB2[1]
        n3 = N//GSB2[0]
        N+=n3*GSB1[0]
        N+=n2*GSB1[1]
    N+=n1*GSB1[2]
    
print(N)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, ga: int, sa: int, ba: int, gb: int, sb: int, bb: int):
    #if ga > gb:
    #    ga, gb = gb, ga
    #if sa > sb:
    #    sa, sb = sb, sa
    #if ba > bb:
    #    ba, bb = bb, ba
    #tmp = [[gb / ga, ga, gb], [sb / sa, sa, sb], [bb / ba, ba, bb]]
    #tmp.sort(reverse=True, key=lambda x: x[0])
    #print(tmp)

    tmp = [[ga, gb], [sa, sb], [ba, bb]]
    tmp.sort(reverse=True, key=lambda x: (x[1] / x[0]))
    ret = N
    rest = N
    for i in range(3):
        if tmp[i][0] < tmp[i][1] and rest >= tmp[i][0]:
            cnt = rest // tmp[i][0]
            ret = ret - cnt + cnt * tmp[i][1]
            rest -= cnt

    rest = ret
    tmp.sort(reverse=True, key=lambda x: (x[0] / x[1]))
    for i in range(3):
        if tmp[i][1] < tmp[i][0] and rest >= tmp[i][1]:
            cnt = rest // tmp[i][1]
            ret = ret - cnt + cnt * tmp[i][0]
            rest -= cnt
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    g_A = int(next(tokens))  # type: int
    s_A = int(next(tokens))  # type: int
    b_A = int(next(tokens))  # type: int
    g_B = int(next(tokens))  # type: int
    s_B = int(next(tokens))  # type: int
    b_B = int(next(tokens))  # type: int
    solve(N, g_A, s_A, b_A, g_B, s_B, b_B)

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=[[0]*(n+1) for i in range(7)]
for i in range(n+1):
    dp[0][i]=i
for i in range(n):
    dp[1][i+1]=max(dp[0][i+1],dp[1][(i+1)%a[0]]+(i+1)//a[0]*b[0])
for i in range(n):
    dp[2][i+1]=max(dp[1][i+1],dp[2][(i+1)%a[1]]+(i+1)//a[1]*b[1])
for i in range(n):
    dp[3][i+1]=max(dp[2][i+1],dp[3][(i+1)%a[2]]+(i+1)//a[2]*b[2])

for i in range(n):
    dp[4][i+1]=max(dp[3][i+1],dp[4][(i+1)%b[0]]+(i+1)//b[0]*a[0])
for i in range(n):
    dp[5][i+1]=max(dp[4][i+1],dp[5][(i+1)%b[1]]+(i+1)//b[1]*a[1])
for i in range(n):
    dp[6][i+1]=max(dp[5][i+1],dp[6][(i+1)%b[2]]+(i+1)//b[2]*a[2])
print(dp[-1][-1])

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(3):
    a[i], b[i] = sorted((a[i], b[i]))
res = n
for x in range(n + 1):
    for y in range(n - x + 1):
        z = n - x - y
        ans = 0
        ans += (x // a[0]) * b[0]
        ans += (y // a[1]) * b[1]
        ans += (z // a[2]) * b[2]
        res = max(res, ans)
print(res)

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<d-c:
        a,b=c,d
    elif b-a==d-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<bord:
            break
        else:
            ans=max(ans,now)
    print(ans)

Submission status: Wrong Answer

Solution code:
# Enter your code here. Read input from STDIN. Print output to STDOUT

def find3(N,a,b,c,A,B,C):
    MAX=N
    for x in xrange(5001):
        for y in xrange(5001):
            temp=N-(a*x+b*y)
            if temp>=0:
                z=temp//c
                MAX=max(MAX,N-(a*x+b*y+c*z)+(A*x+B*y+C*z))
            else:
                break
    return MAX

def find2(N,a,b,A,B):
    MAX=N
    for x in xrange(5001):
        temp=N-a*x
        if temp>=0:
            y=temp//b
            MAX=max(MAX,N-(a*x+b*y)+(A*x+B*y))
    return MAX

def find1(N,a,A):
    MAX=N
    num=N//a
    return N-num*a+num*A

N=int(raw_input())
X=list(map(int,raw_input().strip().split(' ')))
Y=list(map(int,raw_input().strip().split(' ')))

##stage1
index=[0]*3

for i in xrange(3):
    if X[i]<Y[i]:
        index[i]=1
post_1=[]
post_2=[]
for i in xrange(len(index)):
    if index[i]>0:
        post_1+=[i]    
    else:
        post_2+=[i]
number=sum(index)
if number==0:
    print(find3(N,Y[0],Y[1],Y[2],X[0],X[1],X[2]))
elif number==1:
    N=find1(N,X[post_1[0]],Y[post_1[0]])
    N=find2(N,Y[post_2[0]],Y[post_2[1]],X[post_2[0]],X[post_2[1]])
    print(N)
elif number==2:
    N=find2(N,X[post_1[0]],X[post_1[1]],Y[post_1[0]],Y[post_1[1]])
    N=find1(N,Y[post_2[0]],X[post_2[0]])
    print(N)
else:
    N=find3(N,X[0],X[1],X[2],Y[0],Y[1],Y[2])
    print(N)

Submission status: Wrong Answer

Solution code:

n = int(input())
ra = [ int(v) for v in input().split() ]
rb = [ int(v) for v in input().split() ]
ab_rate = sorted([ (rb[i] / ra[i],i,ra[i],rb[i]) for i in range(3)],reverse = True)
ba_rate = sorted([ (ra[i] / rb[i],i,rb[i],ra[i]) for i in range(3)],reverse = True)




def excange(r,x):

    if r[0] <= 1:
        return x, 0
    else:
        y = (x // r[2]) * r[3]
        x = x % r[2]
        return x, y


comb = [[0,1,2],[1,2,0],[2,0,1],[2,1,0],[1,0,2],[0,2,1]]

profmax = n
for k in comb:
    prof,amari = 0, n
    for i in k:
        res = excange(ab_rate[i],amari)
        amari = res[0]
        prof += res[1]
    profmax = max(profmax, prof+amari)

for k in comb:
    prof,amari = 0, profmax
    for i in k:
        res = excange(ba_rate[i],amari)
        amari = res[0]
        prof += res[1]
    profmax = max(profmax, prof+amari)
    
print(profmax)

Submission status: Wrong Answer

Solution code:
import itertools

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

a = [1]+a
b = [1]+b


ans = 0

l = [0,1,2,3]

for v in itertools.permutations(l,4):
    p = [n, 0, 0, 0]
    q = [0, 0, 0, 0]
    for x in v:
        for i in range(4):
            q[x] += p[0]//a[i]
            p[0] = p[0]%a[i]
    p=[p[i]+q[i] for i in range(4)]

    q = [0,0,0,0]
    for i in range(4):
        q[0] += p[i] * b[i]
    p=[q[i] for i in range(4)]

    for w in itertools.permutations(l,4):
        q = [0, 0, 0, 0]
        for y in w:
            for i in range(4):
                q[y] += p[0] // b[i]
                p[0] = p[0] % b[i]
        p = [p[i] + q[i] for i in range(4)]

        for i in range(4):
            q = [0,0,0,0]
            for i in range(4):
                q[0] += p[i] * a[i]
            p = [q[i] for i in range(4)]
        ans = max(ans, p[0])

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<d-c:
        a,b=c,d
    elif b-a==d-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<bord:
            break
        else:
            ans=max(ans,now)
    print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
List_atob = sorted([gb/ga,  sb/sa, bb/ba])
List_btoa =sorted([ga/gb,sa/sb,ba/bb])
res = N
for i in range(5010):
    for j in range(5010):
        if ga*i+sa*j<=N:
            k = (N-ga*i-sa*j)//ba
            res = max(res, gb*i+sb*j+bb*k)
N=res  
#%%
for i in range(5010):
    for j in range(5010):
        if gb*i+sb*j<=N:
            k = (N-gb*i-sb*j)//bb
            res = max(res, ga*i+sa*j+ba*k)
print(res)

Submission status: Wrong Answer

Solution code:
n = int(input())

ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
changes = [(ga, gb), (sa, sb), (ba, bb)]

changes.sort(key=lambda x: x[1] / x[0], reverse=True)

new_n = 0

if changes[0][1] < changes[0][0]:
    new_n = n
elif changes[1][1] < changes[1][0]:
    new_n += (n // changes[0][0]) * changes[0][1]
    n %= changes[0][0]
else:
    x1, y1 = changes[0]
    x2, y2 = changes[1]
    new_curr_n = n
    for i in range(1 + n // x1):
        curr = i * y1
        curr_n = n - x1 * i
        plus = (curr_n // x2) * y2
        curr_n %= x2
        if curr + plus > new_n:
            new_n = curr + plus
            new_curr_n = curr_n
    n = new_curr_n

n += new_n

changes = [(gb, ga), (sb, sa), (bb, ba)]
changes.sort(key=lambda x: x[1] / x[0], reverse=True)
new_n = 0

if changes[0][1] < changes[0][0]:
    new_n = n
elif changes[1][1] < changes[1][0]:
    new_n += (n // changes[0][0]) * changes[0][1]
    n %= changes[0][0]
else:
    x1, y1 = changes[0]
    x2, y2 = changes[1]
    new_curr_n = n
    for i in range(1 + n // x1):
        curr = i * y1
        curr_n = n - x1 * i
        plus = (curr_n // x2) * y2
        curr_n %= x2
        if curr + plus > new_n:
            new_n = curr + plus
            new_curr_n = curr_n
    n = new_curr_n

n += new_n

print(n)

Submission status: Wrong Answer

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

w_ab = []
v_ab = []

if ga < gb:
    w_ab.append(ga)
    v_ab.append(gb)
if sa < sb:
    w_ab.append(sa)
    v_ab.append(sb)
if ba < bb:
    w_ab.append(ba)
    v_ab.append(bb)

num = (n+1)*(len(w_ab)+1)
dp = [0]*num
for i in range(len(w_ab)):
    for j in range(n+1):
        if j < w_ab[i]:
            dp[(i+1)*(n+1) + j] = dp[i*(n+1) + j]
        else:
            dp[(i+1)*(n+1) + j] = max(dp[i*(n+1) + j], dp[(i+1)*(n+1) + j - w_ab[i]] + v_ab[i])
ans = 0
length = len(w_ab)
for j in range(n+1):
    ans = max(ans, dp[length*(n+1) + j] + n-j)
n = ans

w_ab = []
v_ab = []

if ga > gb:
    w_ab.append(gb)
    v_ab.append(ga)
if sa > sb:
    w_ab.append(sb)
    v_ab.append(sa)
if ba > bb:
    w_ab.append(bb)
    v_ab.append(ba)

num = (n+1)*(len(w_ab)+1)
dp = [0]*num
for i in range(len(w_ab)):
    for j in range(n+1):
        if j < w_ab[i]:
            dp[(i+1)*(n+1) + j] = dp[i*(n+1) + j]
        else:
            dp[(i+1)*(n+1) + j] = max(dp[i*(n+1) + j], dp[(i+1)*(n+1) + j - w_ab[i]] + v_ab[i])

length = len(w_ab)
for i in range(n+1):
    ans = max(ans, dp[length*(n+1) + j] + n-j)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
ra = [ int(v) for v in input().split() ]
rb = [ int(v) for v in input().split() ]
ab_rate = sorted([ (rb[i] / ra[i],i,ra[i],rb[i]) for i in range(3)],reverse = True)
ba_rate = sorted([ (ra[i] / rb[i],i,rb[i],ra[i]) for i in range(3)],reverse = True)




def excange(r,x):

    if r[0] <= 1:
        return x, 0
    else:
        y = (x // r[2]) * r[3]
        x = x % r[2]
        return x, y


comb = [[0,1,2],[1,0,2],[2,0,1],[2,1,0],[0,2,1],[1,2,1]]

profmax = n
for k in comb:
    prof,amari = 0, n
    for i in k:
        res = excange(ab_rate[i],amari)
        amari = res[0]
        prof += res[1]
    profmax = max(profmax, prof+amari)

for k in comb:
    prof,amari = 0, profmax
    for i in k:
        res = excange(ba_rate[i],amari)
        amari = res[0]
        prof += res[1]
    profmax = max(profmax, prof+amari)
    
print(profmax)

Submission status: Wrong Answer

Solution code:
N = int(input())
a1, a2, a3 = map(int, input().split())
b1, b2, b3 = map(int, input().split())
A = [a1, a2, a3]
B = [b1, b2, b3]
if (a1<=b1 and a2<=b2 and a3<=b3) or (a1>=b1 and a2>=b2 and a3>=b3):
    if a1>b1 or a2>b2 or a3>b3:
        A, B = B, A
    dp = [[-float("inf")]*(N+1+5050) for _ in range(4)]
    dp[0][0] = 0
    for i in range(3):
        ai = A[i]
        bi = B[i]
        for j in range(N+1):

            dp[i+1][j] = max(dp[i][j], dp[i+1][j-ai]+bi)
    print(max(dp[3]))
    #print(dp[3][:30])
else:
    C, D = [], []
    for a, b in zip(A, B):
        if a <= b:
            C.append([a,b])
        else:
            D.append([b, a])
    dp = [[-float("inf")]*(N+1+5050) for _ in range(len(C)+1)]
    dp[0][0] = 0
    for i in range(len(C)):
        ai, bi = C[i]
        for j in range(N+1):
            dp[i+1][j] = max(dp[i][j], dp[i+1][j-ai]+bi)
    N_ = max(dp[-1])
    if len(D)==1:
        a, b = D[0]
        q, n = divmod(N_, a)
        print(q*b + n)
        #print("a")
    else:
        ans = 0
        (a1, b1), (a2, b2) = D
        for i in range(N_+1):
            j = N_ - i
            q1, n1 = divmod(i, a1)
            q2, n2 = divmod(i, a2)
            ans = max(ans, q1*b1+q2*b2+n1+n1)
        print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
ri = lambda: int(input())
#rl = lambda: [int(x) if x.isdecimal() else x for x in input().split()]
#rl = lambda: list(input().split()))
rl = lambda: map(int,input().split())
rr = lambda N: [list(l) for l in zip(*[rl() for _ in range(N)])]
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')
OE = lambda x: print('Odd') if x%2 else print('Even')
INF = 10**18
MOD = 10**9+7

N = ri()
ga,sa,ba = rl()
gb,sb,bb = rl()
G = int(ga>=gb)
S = int(sa>=sb)
B = int(ba>=bb)
GSB =G+S+B
xa,xb,ya,yb,za,zb=ga,gb,sa,sb,ba,bb
mode = 0
if GSB==3:
    mode = 1
elif GSB==2:
    mode = 1
    if G==0:
        None
    elif S==0:
        xa,xb,ya,yb = sa,sb,ga,gb
    elif B==0:
        xa,xy,za,zb = ba,bb,ga,gb
elif GSB==1:
    mode = 0
    if G==1:
        None
    elif S==1:
        xa,xb,ya,yb = sa,sb,ga,gb
    elif B==1:
        xa,xy,za,zb = ba,bb,ga,gb
elif GSB==0:
    mode = 0

ans = 0
if GSB==0 or GSB==3:
    xa,xb,ya,yb,za,zb=min(xa,xb),max(xa,xb),min(ya,yb),max(ya,yb),min(za,zb),max(za,zb)
    for x in range(N//xa+1):
        n0 = N - x*xa
        for y in range(n0//ya+1):
            n1 = n0 - y*ya
            z = n1//za
            n1 = n1 - z*za
            ans = max(ans,n1+x*xb+y*yb+z*zb)
elif GSB==1:
    for y in range(N//ya+1):
        n = N - y*ya
        z = n//za
        n = n - z*za
        n = n + y*yb + z*zb
        x = n//xb
        n = n - x*xb
        ans = max(ans,n+x*xa)
elif GSB==2:
    for x in range(N//xa+1):
        n0 = N - x*xa + x*xb
        for y in range(n0//yb+1):
            n1 = n0 - y*yb
            z = n1//zb
            n1 = n1 - z*zb
            ans = max(ans,n1+y*ya+z*za)
print(ans)

Submission status: Wrong Answer

Solution code:
inpl = lambda: list(map(int,input().split()))
N = int(input())
rate = []
rate.append(inpl())
rate.append(inpl())

ans = 0
for i in range(3):
    r1, r2 = rate[0][i], rate[1][i]
    if r1 < r2:
        r1, r2 = r2, r1
    n = (N % r2) + (N//r2)*r1
    if n > ans:
        ans = n
print(ans)

Submission status: Wrong Answer

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

# 最低N個
res = N

for g in range(N // ga + 1):

    rest1 = N - g
    for s in range(rest1 // sa + 1):
        rest2 = rest1 - s
        b, rest3 = divmod(rest2, ba)
        res = max(res, g * gb + s * sb + b * bb + rest3)
        # print(res, g, s, b)

M = res
for g in range(M // gb + 1):
    rest1 = M - g
    for s in range(rest1 // sb + 1):
        rest2 = rest1 - s
        b, rest3 = divmod(rest2, bb)
        res = max(res, g * ga + s * sa + b * ba + rest3)
        # print(res, g, s, b)

print(res)

Submission status: Wrong Answer

Solution code:
# Enter your code here. Read input from STDIN. Print output to STDOUT

def find3(N,a,b,c,A,B,C):
    MAX=N
    for x in xrange(5001):
        for y in xrange(5001):
            temp=N-(a*x+b*y)
            if temp>=0:
                z=temp//c
                MAX=max(MAX,N-(a*x+b*y+c*z)+(A*x+B*y+C*z))
            else:
                break
    return MAX

def find2(N,a,b,A,B):
    MAX=N
    for x in xrange(5001):
        temp=N-a*x
        if temp>=0:
            y=temp//b
            MAX=max(MAX,N-(a*x+b*y)+(A*x+B*y))
    return MAX

def find1(N,a,A):
    MAX=N
    num=N//a
    return N-num*a+num*A

N=int(raw_input())
X=list(map(int,raw_input().strip().split(' ')))
Y=list(map(int,raw_input().strip().split(' ')))

##stage1
index=[0]*3

for i in xrange(3):
    if X[i]<Y[i]:
        index[i]=1
post_1=[]
post_2=[]
for i in xrange(len(index)):
    if index[i]>0:
        post_1+=[i]    
    else:
        post_2+=[i]
number=sum(index)
if number==0:
    print(find3(N,Y[0],Y[1],Y[2],X[0],X[1],X[2]))
elif number==1:
    N=find1(N,X[post_1[0]],Y[post_1[0]])
    N=find2(N,X[post_2[0]],X[post_2[1]],Y[post_2[0]],Y[post_2[1]])
    print(N)
elif number==2:
    N=find2(N,X[post_1[0]],X[post_1[1]],Y[post_1[0]],Y[post_1[1]])
    N=find1(N,X[post_2[0]],Y[post_2[0]])
    print(N)
else:
    N=find3(N,X[0],X[1],X[2],Y[0],Y[1],Y[2])
    print(N)

Submission status: Wrong Answer

Solution code:
N = int(input())
*A, = list(map(int, input().split()))
*B, = list(map(int, input().split()))

D = []

for i in range(len(A)):
  a = A[i]
  b = B[i]
  d = N
  if (a < b):
    D.append((N // a) * b)
    d -= (N//a)
  else:
    D.append(0)
  D2 = [0]
  for j in range(len(A)):
    if (i == j):
      continue
    a2 = A[j]
    b2 = B[j]
    if (b2 < a2):
      D2.append((d // b2) * a2)
  D[i] += max(D2)

print(max(D))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 5)


def LI(): return [int(x) for x in sys.stdin.readline().split()]


def main():
    N = int(input())
    A = LI()
    B = LI()
    if A == B:
        print(N)
        return
    BA = [B[i]-A[i] for i in range(3)]

    ans = []
    if BA[0] > 0:
        l1 = range(0, N//A[0]+1)
    else:
        l1 = [0]
    for i in l1:
        now = N - A[0] * i
        if BA[1] > 0:
            l2 = range(0, now//A[1]+1)
        else:
            l2 = [0]
        for j in l2:
            now -= A[1] * j
            if BA[2] > 0:
                k = now//A[2]
                now = now % A[2]
            else:
                k = 0
            ans.append(now + i*B[0] + j*B[1] + k*B[2])
    N = max(ans)

    A, B = B, A
    BA = [B[i]-A[i] for i in range(3)]
    ans = []
    if BA[0] > 0:
        l1 = range(0, N//A[0]+1)
    else:
        l1 = [0]
    for i in l1:
        now = N - A[0] * i
        if BA[1] > 0:
            l2 = range(0, now//A[1]+1)
        else:
            l2 = [0]
        for j in l2:
            now -= A[1] * j
            if BA[2] > 0:
                k = now//A[2]
                now = now % A[2]
            else:
                k = 0
            ans.append(now + i*B[0] + j*B[1] + k*B[2])
    print(max(ans))


main()

Submission status: Wrong Answer

Solution code:
def solve():
    N = int(input())
    As = list(map(int, input().split()))
    Bs = list(map(int, input().split()))

    ans = N
    for g in range(N+1):
        for s in range(N-g+1):
            b = N-g-s
            xs = (g, s, b)
            num = 0
            for x, A, B in zip(xs, As, Bs):
                if A > B:
                    num += x//B*A
                elif A < B:
                    num += x//A*B
                else:
                    num += x
            ans = max(ans, num)

    print(ans)

solve()

Submission status: Wrong Answer

Solution code:
import itertools

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

a = [1]+a
b = [1]+b


ans = 0

l = [0,1,2,3]

for v in itertools.permutations(l,4):
    p = [n, 0, 0, 0]
    q = [0, 0, 0, 0]
    for x in v:
        for i in range(4):
            q[x] += p[i]//a[i]
            p[i] = p[i]%a[i]
    p=[p[i]+q[i] for i in range(4)]

    q = [0,0,0,0]
    for i in range(4):
        q[0] += p[i] * b[i]
    p=[q[i] for i in range(4)]

    for w in itertools.permutations(l,4):
        q = [0, 0, 0, 0]
        for y in w:
            for i in range(4):
                q[y] += p[i] // b[i]
                p[i] = p[i] % b[i]
        p = [p[i] + q[i] for i in range(4)]

        for i in range(4):
            q = [0,0,0,0]
            for i in range(4):
                q[0] += p[i] * a[i]
            p = [q[i] for i in range(4)]
        ans = max(ans, p[0])

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]
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<d-c:
        a,b=c,d
    elif b-a==d-c and a>c:
        a,b=c,d
    bord=(N//a)*b+((N%a)//c)*d
    x=N//a
    ans=bord
    for i in range(1,1+x):
        now=(x-i)*b+((N-(x-i)*a)//c)*d
        if now+d<bord:
            break
        else:
            ans=max(ans,now)
    print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
N=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
def times(M,a,b):
    tmp=M
    tmp-=a*(M//a)
    tmp+=b*(M//a)
    return tmp
L=[0,0,0]
for i in range(3):
    if a[i]<b[i]:
        L[i]=-1
    elif a[i]==b[i]:
        L[i]=0
    else:
        L[i]=1
ans=N
for seq in itertools.permutations(range(3),3):
    if not(L[seq[0]]<=L[seq[1]] and L[seq[1]]<=L[seq[2]]):
        continue
    tmp=N
    for i in range(3):
        if a[seq[i]]<b[seq[i]]:
            tmp=times(tmp,a[seq[i]],b[seq[i]])
        elif a[seq[i]]==b[seq[i]]:
            pass
        else:
            tmp=times(tmp,b[seq[i]],a[seq[i]])
    ans=max(ans,tmp)
print(ans)

Submission status: Wrong Answer

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

ans1 = n
ans2 = n

for a in range(3):
    for b in range(3):
        for c in range(3):
            for i in range(n + 1 if a == 1 else 1):
                d = n
                d += (B[0] - A[0]) * (i // A[0])
                for j in range(n + 1 - i if b == 1 else 1):
                    d += (B[1] - A[1]) * (j // A[1])
                    k = n - i - j if c == 1 else 0
                    # assert k >= 0
                    d += (B[2] - A[2]) * (k // A[2])
                
                ans1 = max(ans1, d)

            for i in range(ans1 + 1 if a == 2 else 1):
                d = ans1
                d += (A[0] - B[0]) * (i // B[0])
                for j in range(ans1 + 1 - i if b == 2 else 1):
                    d += (A[1] - B[1]) * (j // B[1])
                    k = ans1 - i - j if c == 2 else 0
                    # assert k >= 0
                    d += (A[2] - B[2]) * (k // B[2])
                
                ans2 = max(ans2, d)

print(ans2)

Submission status: Wrong Answer

Solution code:
n=int(input())
ga,sa,ba=map(int,input().split())
gb,sb,bb=map(int,input().split())
ans=n
if gb>ga:
  ga,gb=gb,ga
if sb>sa:
  sa,sb=sb,sa
if bb>ba:
  ba,bb=bb,ba
for i in range(n+1):
  for j in range(n-i+1):
    k=n-i-j
    ans=max(i//gb*ga+j//sb*sa+k//bb*ba,ans)
print(ans)