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:
def main():
    n=int(input())
    ga,sa,ba=map(int,input().split())
    gb,sb,bb=map(int,input().split())
    
    def calc_1(dg,a0,a1):
        return (dg//a0)*a1+dg%a0

    def calc_2(dg,a0,a1,b0,b1):
        x=dg//a0
        visit=set()
        temp=0
        for i in range(x,-1,-1):
            amari=(dg-x*a0)%b0
            if amari in visit:
                break
            temp=max(x*a1+((dg-x*a0)//b0)*b1+amari,temp)
            visit.add(amari)
        return temp

    def calc_3(dg,a0,a1,b0,b1,c0,c1):
        return max([a1*i+calc_2(dg-a0*i,b0,b1,c0,c1) for i in range(dg//a0,-1,-1)])

    gsb=[[ga,gb],[sa,sb],[ba,bb]]
    gsb.sort(key=lambda x:x[1]/x[0])
    temp=[[i,j] for i,j in gsb if i<j]
    if len(temp)==1:
        n=calc_1(n,temp[0][0],temp[0][1])
    elif len(temp)==2:
        n=calc_2(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1])
    elif len(temp)==3:
        n=calc_3(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1],temp[2][0],temp[2][1])

    gsb=reversed(gsb)
    temp=[[j,i] for i,j in gsb if i>j]
    if len(temp)==1:
        n=calc_1(n,temp[0][0],temp[0][1])
    elif len(temp)==2:
        n=calc_2(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1])
    elif len(temp)==3:
        n=calc_3(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1],temp[2][0],temp[2][1])
    print(n)
main()

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 calc_1(dg,a0,a1):
        return (dg//a0)*a1+dg%a0

    def calc_2(dg,a0,a1,b0,b1):
        x=dg//a0
        visit=set()
        temp=0
        for i in range(x,-1,-1):
            amari=(dg-x*a0)%b0
            if amari in visit:
                break
            temp=max(x*a1+((dg-x*a0)//b0)*b1+amari,temp)
            visit.add(amari)
        return temp

    def calc_3(dg,a0,a1,b0,b1,c0,c1):
        return max([a1*i+calc_2(dg-a0*i,b0,b1,c0,c1) for i in range(dg//a0,-1,-1)])

    gsb=[[ga,gb],[sa,sb],[ba,bb]]
    gsb.sort(key=lambda x:x[0]/x[1])
    temp=[[i,j] for i,j in gsb if i<j]
    if len(temp)==1:
        n=calc_1(n,temp[0][0],temp[0][1])
    elif len(temp)==1:
        n=calc_2(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1])
    elif len(temp)==1:
        n=calc_3(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1],temp[2][0],temp[2][1])

    gsb=reversed(gsb)
    temp=[[j,i] for i,j in gsb if i>j]
    if len(temp)==1:
        n=calc_1(n,temp[0][0],temp[0][1])
    elif len(temp)==1:
        n=calc_2(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1])
    elif len(temp)==1:
        n=calc_3(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1],temp[2][0],temp[2][1])
    print(n)
main()

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
dp1 = [0] * (n+1)
for i in range(n+1):
    if i>=a[0]:
        dp1[i] = max(dp1[i],dp1[i-a[0]]+b[0])
    if i>=a[1]:
        dp1[i] = max(dp1[i],dp1[i-a[1]]+b[1])
    if i>=a[2]:
        dp1[i] = max(dp1[i],dp1[i-a[2]]+b[2])
n=dp1[n]
dp2 = [0] * (n+1)
for i in range(n+1):
    if i>=b[0]:
        dp2[i] = max(dp2[i],dp2[i-b[0]]+a[0])
    if i>=b[1]:
        dp2[i] = max(dp2[i],dp2[i-b[1]]+a[1])
    if i>=b[2]:
        dp2[i] = max(dp2[i],dp2[i-b[2]]+a[2])


print(dp2[n])

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ab = []
ba = []
for i in range(3):
    if a[i]>b[i]:ba.append(i)
    if a[i]<b[i]:ab.append(i)

t = [n,0,0,0]
if ab:
    t[ab[0]+1] += (t[0] // a[ab[0]])
    t[0] %= a[ab[0]]
if len(ab) >1:
    t[ab[1]+1] += (t[0] // a[ab[1]])
    t[0] %= a[ab[1]]
if len(ab) >2:
    t[ab[2]+1] += (t[0] // a[ab[2]])
    t[0] %= a[ab[2]]

t[0] += t[1]*b[0]+t[2]*b[1]+t[3]*b[2]
t[1] = 0
t[2] = 0
t[3] = 0

if ba:
    t[ba[0]+1] += (t[0] // a[ba[0]])
    t[0] %= a[ba[0]]
if len(ba) >1:
    t[ba[1]+1] += (t[0] // a[ba[1]])
    t[0] %= a[ba[1]]
if len(ba) >2:
    t[ba[2]+1] += (t[0] // a[ba[2]])
    t[0] %= a[ba[2]]

t[0] += t[1]*a[0]+t[2]*a[1]+t[3]*a[2]

print(t[0])

Submission status: Wrong Answer

Solution code:
def examD():
    N = I()
    g, s, c = [0]*2, [0]*2, [0]*2
    for i in range(2):
        g[i],s[i],c[i] = LI()
    dp1 = [0]*(N+1)
    v1 = [g[1]-g[0],s[1]-s[0],c[1]-c[0]]
    w1 = [g[0],s[0],c[0]]
    v2 = [g[0]-g[1],s[0]-s[1],c[0]-c[1]]
    w2 = [g[1],s[1],c[1]]
    for i in range(N):
        if i+w1[0]<=N:
            dp1[i+w1[0]] = max(dp1[i+w1[0]],dp1[i]+v1[0])
        if i+w1[1]<=N:
            dp1[i+w1[1]] = max(dp1[i+w1[1]],dp1[i]+v1[1])
        if i+w1[2]<=N:
            dp1[i+w1[2]] = max(dp1[i+w1[2]],dp1[i]+v1[2])
    newN = max(dp1)+N
#    print(newN)
    dp2 = [0]*(newN+1)
    for i in range(newN):
        if i+w2[0]<=N:
            dp2[i+w2[0]] = max(dp2[i+w2[0]],dp2[i]+v2[0])
        if i+w2[1]<=N:
            dp2[i+w2[1]] = max(dp2[i+w2[1]],dp2[i]+v2[1])
        if i+w2[2]<=N:
            dp2[i+w2[2]] = max(dp2[i+w2[2]],dp2[i]+v2[2])
    ans = max(dp2)+newN
    print(ans)

from string import digits
import sys,copy,bisect,itertools,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

dp = [[0, 0, 0, 0] for _ in range(N+1)]

for i in range(3):
    a, b = A[i], B[i]
    for w in range(N+1):
        if w < a:
            dp[w][i+1] = dp[w][i]
        else:
            dp[w][i+1] = max(dp[w][i], dp[w-a][i+1] + b)

M = dp[N][3]

dp2 = [[0, 0, 0, 0] for _ in range(M+1)]
for i in range(3):
    a, b = A[i], B[i]
    for w in range(M+1):
        if w < b:
            dp2[w][i+1] = dp2[w][i]
        else:
            dp2[w][i+1] = max(dp2[w][i], dp2[w-b][i+1] + a)

print(dp2[M][3])

Submission status: Wrong Answer

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

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

def solve_3(L):
    c = N
    for i in range(N+1):
        if i*L[0][0] > N: break
        for j in range(N+1):
            r = N - i*L[0][0] - j*L[1][0]
            if r < 0: break
            k = r // L[2][0]
            c = max(c, i*L[0][1]+j*L[1][1]+k*L[2][1]+r-L[2][0]*k)
    return c


ans = N
if len(F) == 3:
    ans = solve_3(F)
elif len(S) == 3:
    ans = solve_3(S)
elif len(F) == 2:
    c = N
    for i in range(N+1):
        if i*F[0][0] > N: break
        j = (N - i*F[0][0])//F[1][0]
        c = max(c, i*F[0][1]+j*F[1][1]+N-i*F[0][0]-j*F[1][0])
    if len(S) == 1:
        c = c//S[0][0] * S[0][1] + c%S[0][0]
    ans = c
elif len(F) == 1:
    c = N//F[0][0]*F[0][1] + N%F[0][0]
    if len(S) == 1:
        c = c//S[0][0]*S[0][1] + c%S[0][0]
    elif len(S) == 2:
        r1 = c//S[0][0]*S[0][1] + (c%S[0][0])//S[1][0]*S[1][1] + (c%S[0][0])%S[1][0]
        r2 = c//S[1][0]*S[1][1] + (c%S[1][0])//S[0][0]*S[0][1] + (c%S[1][0])%S[0][0]
        c = max([c, r1, r2])
    ans = c

print(ans)

Submission status: Wrong Answer

Solution code:
#12:40
n = int(input())
a1,a2,a3 = map(int,input().split())
b1,b2,b3 = map(int,input().split())
dp = [0] * (n+1)
for i in range(1,n+1):
  tmp = dp[-1]+1
  if i-a1 >= 0:
    if dp[i-a1]+b1 > tmp:
      tmp = dp[i-a1]+b1
  if i-a2 >= 0:
    if dp[i-a2]+b2 > tmp:
      tmp = dp[i-a2]+b2
  if i-a3 >= 0:
    if dp[i-a3]+b3 > tmp:
      tmp = dp[i-a3]+b3
  dp[i] = tmp
m = dp[-1]
#print(dp)
#print(m)
dp = [0] * (m+1)
for j in range(1,m+1):
  tmp = dp[-1]+1
  if j-b1 >= 0:
    if dp[j-b1]+a1 > tmp:
      tmp = dp[j-b1]+a1
  if j-b2 >= 0:
    if dp[j-b2]+a2 > tmp:
      tmp = dp[j-b2]+a2
  if j-b3 >= 0:
    if dp[j-b3]+a3 > tmp:
      tmp = dp[j-b3]+a3
  dp[j] = tmp
print(dp[-1])

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
a = B[0]/A[0]
b = B[1]/A[1]
c = B[2]/A[2]
C = [(A[0],a),(A[1],b),(A[2],c)]
C = sorted(C,key=lambda x:x[1],reverse=True)
D = [(B[0],a),(B[1],b),(B[2],c)]
D = sorted(D,key=lambda x:x[1],reverse=True)
if C[0][1]>=1:
    N0 = N//C[0][0]
    N1 = N%C[0][0]
    N2 = 0
    N3 = N1
    if C[1][1]>1:
        N2 = N1//C[1][0]
        N3 = N1%C[1][0]
    N4 = 0
    N5 = N3
    if C[2][1]>1:
        N4 = N3//C[2][0]
        N5 = N3%C[2][0]
    n = N0*D[0][0]
    n += N2*D[1][0]
    n += N4*D[2][0]
    n += N5
    E = [(A[0],1/a),(A[1],1/b),(A[2],1/c)]
    E = sorted(E,key=lambda x:x[1],reverse=True)
    F = [(B[0],1/a),(B[1],1/b),(B[2],1/c)]
    F = sorted(F,key=lambda x:x[1],reverse=True)
    m = 0
    if F[0][1]>1:
        n0 = n//F[0][0]
        n1 = n%F[0][0]
        n2 = 0
        n3 = n1
        if F[1][1]>1:
            n2 = n1//F[1][0]
            n3 = n1%F[1][0]
        n4 = 0
        n5 = n3
        m = n0*E[0][0]
        m += n2*E[1][0]
        m += n4*E[2][0]
        m += n5
    m = max(m,n)
else:
    E = [(A[0],1/a),(A[1],1/b),(A[2],1/c)]
    E = sorted(E,key=lambda x:x[1],reverse=True)
    F = [(B[0],1/a),(B[1],1/b),(B[2],1/c)]
    F = sorted(F,key=lambda x:x[1],reverse=True)
    if F[0][1]>1:
        N0 = N//F[0][0]
        N1 = N%F[0][0]
        N2 = 0
        N3 = N1
        if F[1][1]>1:
            N2 = N1//F[1][0]
            N3 = N1%F[1][0]
        N4 = 0
        N5 = N3
        if F[2][1]>1:
            N4 = N3//F[2][0]
            N5 = N3%F[2][0]
    m = N0*E[0][0]
    m += N2*E[1][0]
    m += N4*E[2][0]
    m += N5
print(m)

Submission status: Wrong Answer

Solution code:

N = int(input())

ga,sa,ba = map(int,input().split())

gb,sb,bb = map(int,input().split())

g = gb - ga
s = sb - sa
b = bb - ba

lis = [[g,ga],[s,sa],[b,ba]]
lis.sort()

plnum = 0

for i in lis:
    if i[0] > 0:
        plnum += 1

if plnum == 3:

    nexN = N

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

        amariA = N - ga * i

        for j in range(amariA // sa + 1):

            amariB = amariA - sa * i
            k = amariB // ba

            nexN = max(nexN , N + g*i + s*j + b * k)

    N = nexN

elif plnum == 2:

    nexN = N

    for i in range(N // lis[2][1] + 1):

            amariB = N - lis[2][1] * i
            j = amariB // lis[1][1]

            nexN = max(nexN , N + lis[2][0] * i + lis[1][0] * j)

    N = nexN

elif plnum == 1:
    
    N += (N // lis[2][1]) * lis[2][0]



g = ga - gb
s = sa - sb
b = ba - bb

lis = [[g,gb],[s,sb],[b,bb]]
lis.sort()

plnum = 0

for i in lis:
    if i[0] > 0:
        plnum += 1

if plnum == 3:

    nexN = N

    for i in range(N // gb +1 ):

        amariA = N - gb * i

        for j in range(amariA // sb + 1):

            amariB = amariA - sb * i
            k = amariB // bb

            nexN = max(nexN , N + g*i + s*j + b * k)

    N = nexN

elif plnum == 2:

    nexN = N

    for i in range(N // lis[2][1] + 1):

            amariB = N - lis[2][1] * i
            j = amariB // lis[1][1]

            nexN = max(nexN , N + lis[2][0] * i + lis[1][0] * j)

    N = nexN

elif plnum == 1:
    
    N += (N // lis[2][1]) * lis[2][0]

print (N)

Submission status: Wrong Answer

Solution code:

N = int(input())

ga,sa,ba = map(int,input().split())

gb,sb,bb = map(int,input().split())

g = gb - ga
s = sb - sa
b = bb - ba

lis = [[g,ga],[s,sa],[b,ba]]
lis.sort()

plnum = 0

for i in lis:
    if i[0] > 0:
        plnum += 1

if plnum == 3:

    nexN = N

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

        amariA = N - ba * i

        for j in range(amariA // sa + 1):

            amariB = amariA - sa * i
            k = amariB // ba

            nexN = max(nexN , N + g*i + s*j + b * k)

    N = nexN

elif plnum == 2:

    nexN = N

    for i in range(N // lis[2][1] + 1):

            amariB = N - lis[2][1] * i
            j = amariB // lis[1][1]

            nexN = max(nexN , N + lis[2][0] * i + lis[1][0] * j)

    N = nexN

elif plnum == 1:
    
    N += (N // lis[2][1]) * lis[2][0]



g = ga - gb
s = sa - sb
b = ba - bb

lis = [[g,gb],[s,sb],[b,bb]]
lis.sort()

plnum = 0

for i in lis:
    if i[0] > 0:
        plnum += 1

if plnum == 3:

    nexN = N

    for i in range(N // gb +1 ):

        amariA = N - bb * i

        for j in range(amariA // sb + 1):

            amariB = amariA - sb * i
            k = amariB // bb

            nexN = max(nexN , N + g*i + s*j + b * k)

    N = nexN

elif plnum == 2:

    nexN = N

    for i in range(N // lis[2][1] + 1):

            amariB = N - lis[2][1] * i
            j = amariB // lis[1][1]

            nexN = max(nexN , N + lis[2][0] * i + lis[1][0] * j)

    N = nexN

elif plnum == 1:
    
    N += (N // lis[2][1]) * lis[2][0]

print (N)

Submission status: Wrong Answer

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

cur = N
tmp_tuples = [(gb * sa * ba, ga, gb), (ga * sb * ba, sa, sb), (ga * sa * bb, ba, bb)]
tmp_tuples.sort()
tmp_tuples.reverse()

next = 0
for prod, a, b in tmp_tuples:
    if prod <= ga * sa * ba:
        break
    next += cur // a * b
    cur = cur % a  
next += cur
cur = next

tmp_tuples = [(ga * sb * bb, gb, ga), (gb * sa * bb, sb, sa), (gb * sb * ba, bb, ba)]
tmp_tuples.sort()
tmp_tuples.reverse()

ans = 0
for prod, a, b in tmp_tuples:
    if prod <= gb * sb * bb:
        break
    ans += cur // a * b 
    cur = cur % a
ans += cur
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())

cur = N
tmp_tuples = [(gb * sa * ba, ga, gb), (ga * sb * ba, sa, sb), (ga * sa * bb, ba, bb)]
tmp_tuples.sort()
tmp_tuples.reverse()

for prod, a, b in tmp_tuples:
    if prod <= ga * sa * ba:
        break
    cur = cur // a * b + cur % a
    
tmp_tuples = [(ga * sb * bb, gb, ga), (gb * sa * bb, sb, sa), (gb * sb * ba, bb, ba)]
tmp_tuples.sort()
tmp_tuples.reverse()

for prod, a, b in tmp_tuples:
    if prod <= gb * sb * bb:
        break
    cur = cur // a * b + cur % a
    
print(cur)

Submission status: Wrong Answer

Solution code:
n=int(input())
a,b,c=map(int,input().split())
d,e,f=list(map(int,input().split()))
s=[0]*(n+1)
for i in range(a,n+1):
    s[i]=max(s[i],s[i-a]+d-a)
for i in range(b,n+1):
    s[i]=max(s[i],s[i-b]+e-b)
for i in range(c,n+1):
    s[i]=max(s[i],s[i-c]+f-c)
for i in range(d,n+1):
    s[i]=max(s[i],s[i-d]+a-d)
for i in range(e,n+1):
    s[i]=max(s[i],s[i-e]+b-e)
for i in range(f,n+1):
    s[i]=max(s[i],s[i-f]+c-f)
print(s[-1]+n)

Submission status: Wrong Answer

Solution code:
n=int(input())
a,b,c=map(int,input().split())
d,e,f=list(map(int,input().split()))
s=[0]*(n+1)
for i in range(a,n+1):
    s[i]=max(s[i],s[i-a]+d-a)
for i in range(b,n+1):
    s[i]=max(s[i],s[i-b]+e-b)
for i in range(c,n+1):
    s[i]=max(s[i],s[i-c]+f-c)
print(s[-1]+n)

Submission status: Wrong Answer

Solution code:
# W
# N K
# A1 B1
# A2 B2
# :
# AN BN

n = int(input())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))



dp = [0] * (n+1)

for ci in range(3):
    for wi in range(a_list[ci], n + 1):
        dp[wi] = max(dp[wi - a_list[ci]] + b_list[ci], dp[wi])



n = dp[-1]
dp = [0] * (n+1)


for ci in range(3):
    for wi in range(b_list[ci], n + 1):
        dp[wi] = max(dp[wi - b_list[ci]] + a_list[ci], dp[wi])




print(dp[-1])

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
def f(a,b,n):
    s=sorted([[a[i],b[i]] for i in range(3) if a[i]<b[i]])
    dp=[0]*(n+1)
    if len(s)==0:
        return n
    if len(s)==1:
        return (s[0][1]-s[0][0])*(n//s[0][0])+n
    else:
        for p,q in s:
            for j in range(p,n+1):
                dp[j]=max(dp[j],dp[j-p]+q)
        return dp[n]
print(f(b,a,f(a,b,n)))

Submission status: Wrong Answer

Solution code:
n=int(input())
ga,sa,ba=map(int,input().split())
gb,sb,bb=map(int,input().split())
def f(a,b,c,d,e,f,n):
    s=[[a,d-a],[b,e-b],[c,f-c]]
    s=[i for i in s if i[1]>0]
    dp=[0]*(n+1)
    if len(s)==0:
        return n
    if len(s)==1:
        return s[0][1]*(n//s[0][0])+n
    else:
        for p,q in s:
            for j in range(p,n+1):
                dp[j]=max(dp[j],dp[j-p]+q)
        return dp[n]
print(f(gb,sb,bb,ga,sa,ba,f(ga,sa,ba,gb,sb,bb,n)))

Submission status: Wrong Answer

Solution code:
n=int(input())
ga,sa,ba=map(int,input().split())
gb,sb,bb=map(int,input().split())
def f(a,b,c,d,e,f,n):
    s=[[a,d-a],[b,e-b],[c,f-c]]
    s=[i for i in s if i[1]>0]
    dp=[0]*(n+1)
    if len(s)==0:
        return n
    if len(s)==1:
        return s[0][1]*(n//s[0][0])+n
    else:
        for p,q in s:
            for j in range(a,n+1):
                dp[j]=max(dp[j],dp[j-p]+q)
        return dp[n]
print(f(gb,sb,bb,ga,sa,ba,f(ga,sa,ba,gb,sb,bb,n)))

Submission status: Wrong Answer

Solution code:
def dfs():
    n = int(input())
    weight = list(map(int, input().split()))
    value = list(map(int, input().split()))
    dp = [i for i in range(n+1)]
    for i in range(3):
        for j in range(weight[i], n+1):
            dp[j] = max(dp[j], dp[j-weight[i]] + value[i])
    return(dp[-1])
  
  
print(dfs())

Submission status: Wrong Answer

Solution code:
n = int(input())
weight = list(map(int, input().split()))
value = list(map(int, input().split()))


dp = [0] * (n+1)

for i in range(n+1):
    dp[i] = (i // weight[0]) * value[0]
    


for i in range(1, 3):
    for j in range(weight[i], n+1):
        max(dp[j], dp[j-weight[i]] + value[i])
        

        
print(dp[-1])

Submission status: Wrong Answer

Solution code:
def calc():
  n=int(input())
  arr=[list(map(int,input().split())) for _ in range(2)]
  move1=[]
  move2=[]
  ans=n
  for i in range(3):
    if arr[0][i]-arr[1][i]>0:
      move2+=[i]
    if arr[1][i]-arr[0][i]>0:
      move1+=[i]
  if len(move1)==1:
    idx=move1[0]
    ans=arr[1][idx]*(ans//arr[0][idx])+ans%arr[0][idx]
  elif len(move1)==2:
    idx1=move1[0]
    idx2=move1[1]
    s1=arr[0][idx1]
    s2=arr[0][idx2]
    g1=arr[1][idx1]
    g2=arr[1][idx2]
    d1=g1-s1
    d2=g2-s2
    ta=max(ans+i*d1+((ans-i*s1)//s2)*d2 for i in range(ans//s1+1))
    ans=ta
  elif len(move1)==3:
    idx1=move1[0]
    idx2=move1[1]
    idx3=move1[2]
    s1=arr[0][idx1]
    s2=arr[0][idx2]
    s3=arr[0][idx3]
    g1=arr[1][idx1]
    g2=arr[1][idx2]
    g3=arr[1][idx3]
    if g1/s1<g2/s2:
      g1,g2=g2,g1
      s1,s2=s2,s1
    if g1/s1<g3/s3:
      g1,g3=g3,g1
      s1,s3=s3,s1
    if g2/s2<g3/s3:
      g2,g3=g3,g2
      s2,s3=s3,s2
    d1=g1-s1
    d2=g2-s2
    d3=g3-s3
    ta=max(ans+i*d1+j*d2+((ans-i*s1-j*s2)//s3)*d3 for i in range((ans//s1)//2,ans//s1+1) for j in range((ans-i*s1)//s2+1))
    ans=ta
  if len(move2)==1:
    idx=move2[0]
    ans=arr[0][idx]*(ans//arr[1][idx])+ans%arr[1][idx]
  elif len(move2)==2:
    idx1=move2[0]
    idx2=move2[1]
    s1=arr[1][idx1]
    s2=arr[1][idx2]
    g1=arr[0][idx1]
    g2=arr[0][idx2]
    if g1/s1<g2/s2:
      g1,g2=g2,g1
      s1,s2=s2,s1
    d1=g1-s1
    d2=g2-s2
    ta=max(ans+i*d1+((ans-i*s1)//s2)*d2 for i in range((ans//s1)//2,ans//s1+1))
    ans=ta
  elif len(move2)==3:
    idx1=move2[0]
    idx2=move2[1]
    idx3=move2[2]
    s1=arr[1][idx1]
    s2=arr[1][idx2]
    s3=arr[1][idx3]
    g1=arr[0][idx1]
    g2=arr[0][idx2]
    g3=arr[0][idx3]
    if g1/s1<g2/s2:
      g1,g2=g2,g1
      s1,s2=s2,s1
    if g1/s1<g3/s3:
      g1,g3=g3,g1
      s1,s3=s3,s1
    if g2/s2<g3/s3:
      g2,g3=g3,g2
      s2,s3=s3,s2
    d1=g1-s1
    d2=g2-s2
    d3=g3-s3
    ta=max(ans+i*d1+j*d2+((ans-i*s1-j*s2)//s3)*d3 for i in range((ans//s1)//2,ans//s1+1) for j in range((ans-i*s1)//s2+1))
    ans=ta
  print(ans)
calc()

Submission status: Wrong Answer

Solution code:
def calc():
  n=int(input())
  arr=[list(map(int,input().split())) for _ in range(2)]
  move1=[]
  move2=[]
  ans=n
  for i in range(3):
    if arr[0][i]-arr[1][i]>0:
      move2+=[i]
    if arr[1][i]-arr[0][i]>0:
      move1+=[i]
  if len(move1)==1:
    idx=move1[0]
    ans=arr[1][idx]*(ans//arr[0][idx])+ans%arr[0][idx]
  elif len(move1)==2:
    idx1=move1[0]
    idx2=move1[1]
    s1=arr[0][idx1]
    s2=arr[0][idx2]
    g1=arr[1][idx1]
    g2=arr[1][idx2]
    d1=g1-s1
    d2=g2-s2
    ta=max(ans+i*d1+((ans-i*s1)//s2)*d2 for i in range(ans//s1+1))
    ans=ta
  elif len(move1)==3:
    idx1=move1[0]
    idx2=move1[1]
    idx3=move1[2]
    s1=arr[0][idx1]
    s2=arr[0][idx2]
    s3=arr[0][idx3]
    g1=arr[1][idx1]
    g2=arr[1][idx2]
    g3=arr[1][idx3]
    if g1/s1<g2/s2:
      g1,g2=g2,g1
      s1,s2=s2,s1
    if g1/s1<g3/s3:
      g1,g3=g3,g1
      s1,s3=s3,s1
    if g2/s2<g3/s3:
      g2,g3=g3,g2
      s2,s3=s3,s2
    d1=g1-s1
    d2=g2-s2
    d3=g3-s3
    ta=max(ans+i*d1+j*d2+((ans-i*s1-j*s2)//s3)*d3 for i in range((3*(ans//s1))//4,ans//s1+1) for j in range((ans-i*s1)//s2+1))
    ans=ta
  if len(move2)==1:
    idx=move2[0]
    ans=arr[0][idx]*(ans//arr[1][idx])+ans%arr[1][idx]
  elif len(move2)==2:
    idx1=move2[0]
    idx2=move2[1]
    s1=arr[1][idx1]
    s2=arr[1][idx2]
    g1=arr[0][idx1]
    g2=arr[0][idx2]
    if g1/s1<g2/s2:
      g1,g2=g2,g1
      s1,s2=s2,s1
    d1=g1-s1
    d2=g2-s2
    ta=max(ans+i*d1+((ans-i*s1)//s2)*d2 for i in range((3*(ans//s1))//4,ans//s1+1))
    ans=ta
  elif len(move2)==3:
    idx1=move2[0]
    idx2=move2[1]
    idx3=move2[2]
    s1=arr[1][idx1]
    s2=arr[1][idx2]
    s3=arr[1][idx3]
    g1=arr[0][idx1]
    g2=arr[0][idx2]
    g3=arr[0][idx3]
    if g1/s1<g2/s2:
      g1,g2=g2,g1
      s1,s2=s2,s1
    if g1/s1<g3/s3:
      g1,g3=g3,g1
      s1,s3=s3,s1
    if g2/s2<g3/s3:
      g2,g3=g3,g2
      s2,s3=s3,s2
    d1=g1-s1
    d2=g2-s2
    d3=g3-s3
    ta=max(ans+i*d1+j*d2+((ans-i*s1-j*s2)//s3)*d3 for i in range((3*(ans//s1))//4,ans//s1+1) for j in range((ans-i*s1)//s2+1))
    ans=ta
  print(ans)
calc()

Submission status: Wrong Answer

Solution code:
def calc():
  n=int(input())
  arr=[list(map(int,input().split())) for _ in range(2)]
  move1=[]
  move2=[]
  ans=n
  for i in range(3):
    if arr[0][i]-arr[1][i]>0:
      move2+=[i]
    if arr[1][i]-arr[0][i]>0:
      move1+=[i]
  if len(move1)==1:
    idx=move1[0]
    ans=arr[1][idx]*(ans//arr[0][idx])+ans%arr[0][idx]
  elif len(move1)==2:
    idx1=move1[0]
    idx2=move1[1]
    s1=arr[0][idx1]
    s2=arr[0][idx2]
    g1=arr[1][idx1]
    g2=arr[1][idx2]
    d1=g1-s1
    d2=g2-s2
    ta=max(ans+i*d1+((ans-i*s1)//s2)*d2 for i in range(ans//s1+1))
    ans=ta
  elif len(move1)==3:
    idx1=move1[0]
    idx2=move1[1]
    idx3=move1[2]
    s1=arr[0][idx1]
    s2=arr[0][idx2]
    s3=arr[0][idx3]
    g1=arr[1][idx1]
    g2=arr[1][idx2]
    g3=arr[1][idx3]
    if g1/s1<g2/s2:
      g1,g2=g2,g1
      s1,s2=s2,s1
    if g1/s1<g3/s3:
      g1,g3=g3,g1
      s1,s3=s3,s1
    if g2/s2<g3/s3:
      g2,g3=g3,g2
      s2,s3=s3,s2
    d1=g1-s1
    d2=g2-s2
    d3=g3-s3
    ta=max(ans+i*d1+j*d2+((ans-i*s1-j*s2)//s3)*d3 for i in range((2*(ans//s1))//3,ans//s1+1) for j in range((ans-i*s1)//s2+1))
    ans=ta
  if len(move2)==1:
    idx=move2[0]
    ans=arr[0][idx]*(ans//arr[1][idx])+ans%arr[1][idx]
  elif len(move2)==2:
    idx1=move2[0]
    idx2=move2[1]
    s1=arr[1][idx1]
    s2=arr[1][idx2]
    g1=arr[0][idx1]
    g2=arr[0][idx2]
    if g1/s1<g2/s2:
      g1,g2=g2,g1
      s1,s2=s2,s1
    d1=g1-s1
    d2=g2-s2
    ta=max(ans+i*d1+((ans-i*s1)//s2)*d2 for i in range((2*(ans//s1))//3,ans//s1+1))
    ans=ta
  elif len(move2)==3:
    idx1=move2[0]
    idx2=move2[1]
    idx3=move2[2]
    s1=arr[1][idx1]
    s2=arr[1][idx2]
    s3=arr[1][idx3]
    g1=arr[0][idx1]
    g2=arr[0][idx2]
    g3=arr[0][idx3]
    if g1/s1<g2/s2:
      g1,g2=g2,g1
      s1,s2=s2,s1
    if g1/s1<g3/s3:
      g1,g3=g3,g1
      s1,s3=s3,s1
    if g2/s2<g3/s3:
      g2,g3=g3,g2
      s2,s3=s3,s2
    d1=g1-s1
    d2=g2-s2
    d3=g3-s3
    ta=max(ans+i*d1+j*d2+((ans-i*s1-j*s2)//s3)*d3 for i in range((2*(ans//s1))//3,ans//s1+1) for j in range((ans-i*s1)//s2+1))
    ans=ta
  print(ans)
calc()

Submission status: Wrong Answer

Solution code:
def calc():
  n=int(input())
  arr=[list(map(int,input().split())) for _ in range(2)]
  move1=[]
  move2=[]
  ans=n
  for i in range(3):
    if arr[0][i]-arr[1][i]>0:
      move2+=[i]
    if arr[1][i]-arr[0][i]>0:
      move1+=[i]
  if len(move1)==1:
    idx=move1[0]
    ans=arr[1][idx]*(ans//arr[0][idx])+ans%arr[0][idx]
  elif len(move1)==2:
    idx1=move1[0]
    idx2=move1[1]
    s1=arr[0][idx1]
    s2=arr[0][idx2]
    g1=arr[1][idx1]
    g2=arr[1][idx2]
    ta=max(ans+i*(g1-s1)+((ans-i*s1)//s2)*(g2-s2) for i in range(ans//s1+1))
    ans=ta
  elif len(move1)==3:
    idx1=move1[0]
    idx2=move1[1]
    idx3=move1[2]
    s1=arr[0][idx1]
    s2=arr[0][idx2]
    s3=arr[0][idx3]
    g1=arr[1][idx1]
    g2=arr[1][idx2]
    g3=arr[1][idx3]    
    if ans//s1<1000:
      pos=0
    else:
      pos=(ans//s1)//2
    ta=max(ans+i*(g1-s1)+j*(g2-s2)+((ans-i*s1-j*s2)//s3)*(g3-s3) for i in range(pos,ans//s1+1) for j in range((ans-i*s1)//s2+1))
    ans=ta
  if len(move2)==1:
    idx=move2[0]
    ans=arr[0][idx]*(ans//arr[1][idx])+ans%arr[1][idx]
  elif len(move2)==2:
    idx1=move2[0]
    idx2=move2[1]
    s1=arr[1][idx1]
    s2=arr[1][idx2]
    g1=arr[0][idx1]
    g2=arr[0][idx2]
    ta=max(ans+i*(g1-s1)+((ans-i*s1)//s2)*(g2-s2) for i in range(ans//s1+1))
    ans=ta
  elif len(move2)==3:
    idx1=move2[0]
    idx2=move2[1]
    idx3=move2[2]
    s1=arr[1][idx1]
    s2=arr[1][idx2]
    s3=arr[1][idx3]
    g1=arr[0][idx1]
    g2=arr[0][idx2]
    g3=arr[0][idx3]
    if ans//s1<1000:
      pos=0
    else:
      pos=(ans//s1)//2
    ta=max(ans+i*(g1-s1)+j*(g2-s2)+((ans-i*s1-j*s2)//s3)*(g3-s3) for i in range(pos,ans//s1+1) for j in range((ans-i*s1)//s2+1))
    ans=ta
  print(ans)
calc()

Submission status: Wrong Answer

Solution code:
def calc():
  n=int(input())
  arr=[list(map(int,input().split())) for _ in range(2)]
  move1=[]
  move2=[]
  ans=n
  for i in range(3):
    if arr[0][i]-arr[1][i]>0:
      move2+=[i]
    if arr[1][i]-arr[0][i]>0:
      move1+=[i]
  if len(move1)==1:
    idx=move1[0]
    ans=arr[1][idx]*(ans//arr[0][idx])+ans%arr[0][idx]
  elif len(move1)==2:
    idx1=move1[0]
    idx2=move1[1]
    s1=arr[0][idx1]
    s2=arr[0][idx2]
    g1=arr[1][idx1]
    g2=arr[1][idx2]
    ta=max(ans+i*(g1-s1)+((ans-i*s1)//s2)*(g2-s2) for i in range(ans//s1+1))
    ans=ta
  elif len(move1)==3:
    idx1=move1[0]
    idx2=move1[1]
    idx3=move1[2]
    s1=arr[0][idx1]
    s2=arr[0][idx2]
    s3=arr[0][idx3]
    g1=arr[1][idx1]
    g2=arr[1][idx2]
    g3=arr[1][idx3]
    ta=max(ans+i*(g1-s1)+j*(g2-s2)+((ans-i*s1-j*s2)//s3)*(g3-s3) for i in range((ans//s1)//2,ans//s1+1) for j in range((ans-i*s1)//s2+1))
    ans=ta
  if len(move2)==1:
    idx=move2[0]
    ans=arr[0][idx]*(ans//arr[1][idx])+ans%arr[1][idx]
  elif len(move2)==2:
    idx1=move2[0]
    idx2=move2[1]
    s1=arr[1][idx1]
    s2=arr[1][idx2]
    g1=arr[0][idx1]
    g2=arr[0][idx2]
    ta=max(ans+i*(g1-s1)+((ans-i*s1)//s2)*(g2-s2) for i in range(ans//s1+1))
    ans=ta
  elif len(move2)==3:
    idx1=move2[0]
    idx2=move2[1]
    idx3=move2[2]
    s1=arr[1][idx1]
    s2=arr[1][idx2]
    s3=arr[1][idx3]
    g1=arr[0][idx1]
    g2=arr[0][idx2]
    g3=arr[0][idx3]
    ta=max(ans+i*(g1-s1)+j*(g2-s2)+((ans-i*s1-j*s2)//s3)*(g3-s3) for i in range((ans//s1)//2,ans//s1+1) for j in range((ans-i*s1)//s2+1))
    ans=ta
  print(ans)
calc()

Submission status: Wrong Answer

Solution code:
n=int(input())
arr=[list(map(int,input().split())) for _ in range(2)]
move1=[]
move2=[]
ans=n
for i in range(3):
  if arr[0][i]-arr[1][i]>0:
    move2+=[i]
  if arr[1][i]-arr[0][i]>0:
    move1+=[i]
if len(move1)==1:
  idx=move1[0]
  ans=arr[1][idx]*(ans//arr[0][idx])+ans%arr[0][idx]
elif len(move1)==2:
  idx1=move1[0]
  idx2=move1[1]
  s1=arr[0][idx1]
  s2=arr[0][idx2]
  g1=arr[1][idx1]
  g2=arr[1][idx2]
  ta=0
  for i in range(ans//s1+1):
    j=(ans-i*s1)//s2
    tmp=i*g1+j*g2+(n-i*s1-j*s2)
    ta=max(ta,tmp)
  ans=ta
elif len(move1)==3:
  idx1=move1[0]
  idx2=move1[1]
  idx3=move1[2]
  s1=arr[0][idx1]
  s2=arr[0][idx2]
  s3=arr[0][idx3]
  g1=arr[1][idx1]
  g2=arr[1][idx2]
  g3=arr[1][idx3]
  r1=g1/s1
  r2=g2/s2
  r3=g3/s3
  tr=[[r1,idx1],[r2,idx2],[r3,idx3]]
  sorted(tr,reverse=True,key=lambda x:x[0])
  idx1=tr[0][1]
  idx2=tr[1][1]
  idx3=tr[2][1]
  s1=arr[0][idx1]
  s2=arr[0][idx2]
  s3=arr[0][idx3]
  g1=arr[1][idx1]
  g2=arr[1][idx2]
  g3=arr[1][idx3]
  r1=g1/s1
  r2=g2/s2
  r3=g3/s3
  ta=0
  for i in range(ans//s1+1):
    for j in range((ans-i*s1)//s2+1):
      k=(ans-i*s1-j*s2)//s3
      tmp=i*g1+j*g2+k*g3+(n-i*s1-j*s2-k*s3)
      ta=max(ta,tmp)
  ans=ta
if len(move2)==1:
  idx=move2[0]
  ans=arr[0][idx]*(ans//arr[1][idx])+ans%arr[1][idx]
elif len(move2)==2:
  idx1=move2[0]
  idx2=move2[1]
  s1=arr[1][idx1]
  s2=arr[1][idx2]
  g1=arr[0][idx1]
  g2=arr[0][idx2]
  ta=0
  for i in range(ans//s1+1):
    j=(ans-i*s1)//s2
    tmp=i*g1+j*g2+(n-i*s1-j*s2)
    ta=max(ta,tmp)
  ans=ta
elif len(move2)==3:
  idx1=move2[0]
  idx2=move2[1]
  idx3=move2[2]
  s1=arr[1][idx1]
  s2=arr[1][idx2]
  s3=arr[1][idx3]
  g1=arr[0][idx1]
  g2=arr[0][idx2]
  g3=arr[0][idx3]
  r1=g1/s1
  r2=g2/s2
  r3=g3/s3
  tr=[[r1,idx1],[r2,idx2],[r3,idx3]]
  sorted(tr,reverse=True,key=lambda x:x[0])
  idx1=tr[0][1]
  idx2=tr[1][1]
  idx3=tr[2][1]
  s1=arr[1][idx1]
  s2=arr[1][idx2]
  s3=arr[1][idx3]
  g1=arr[0][idx1]
  g2=arr[0][idx2]
  g3=arr[0][idx3]
  r1=g1/s1
  r2=g2/s2
  r3=g3/s3
  ta=0
  for i in range(ans//s1+1):
    for j in range((ans-i*s1)//s2+1):
      k=(ans-i*s1-j*s2)//s3
      tmp=i*g1+j*g2+k*g3+(n-i*s1-j*s2-k*s3)
      ta=max(ta,tmp)
  ans=ta
print(ans)