Problem:


Score : 600 points

Problem Statement

The squirrel Chokudai has N acorns.
One day, he decides to do some trades in multiple precious metal exchanges to make more acorns.

His plan is as follows:

Get out of the nest with N acorns in his hands.

Go to Exchange A and do some trades.

Go to Exchange B and do some trades.

Go to Exchange A and do some trades.

Go back to the nest.

In Exchange X (X = A, B), he can perform the following operations any integer number of times (possibly zero) in any order:

Lose g_{X} acorns and gain 1 gram of gold.

Gain g_{X} acorns and lose 1 gram of gold.

Lose s_{X} acorns and gain 1 gram of silver.

Gain s_{X} acorns and lose 1 gram of silver.

Lose b_{X} acorns and gain 1 gram of bronze.

Gain b_{X} acorns and lose 1 gram of bronze.

Naturally, he cannot perform an operation that would leave him with a negative amount of acorns, gold, silver, or bronze.

What is the maximum number of acorns that he can bring to the nest?
Note that gold, silver, or bronze brought to the nest would be worthless because he is just a squirrel.

Constraints

1 \leq N \leq 5000

1 \leq g_{X} \leq 5000

1 \leq s_{X} \leq 5000

1 \leq b_{X} \leq 5000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
g_A s_A b_A
g_B s_B b_B

Output

Print the maximum number of acorns that Chokudai can bring to the nest.

Sample Input 1

23
1 1 1
2 1 1

Sample Output 1

46

He can bring 46 acorns to the nest, as follows:

In Exchange A, trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 }

In Exchange B, trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 }

In Exchange A, trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 }

He cannot have 47 or more acorns, so the answer is 46.



Sample input:
23
1 1 1
2 1 1


Sample output:
46

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[] for _ in range(num)]
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

def solve():
    N = I()
    A = III()
    B = III()

    first_use = []
    second_use = []
    for i in range(3):
        if A[i]<B[i]:
            first_use.append(i)
        if A[i]>B[i]:
            second_use.append(i)

    def change(n,A,B,use_index):
        dp = [(n-i) for i in range(n+1)]
        count = 0
        for i in range(n+1):
            for j in use_index:
                if i-A[j]>=0:
                    dp[i] = max(dp[i-A[j]]+B[j]-A[j],dp[i])
            count = max(count,dp[i])
        return count

    count1 = change(N,A,B,first_use)
    count2 = change(count1,B,A,second_use)

    print(count2)

solve()

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[] for _ in range(num)]
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

N = I()
A = III()
B = III()

first_use = []
second_use = []
for i in range(3):
    if A[i]<B[i]:
        first_use.append(i)
    if A[i]>B[i]:
        second_use.append(i)

def change(n,A,B,use_index):
    dp = [(n-i) for i in range(n+1)]
    count = 0
    for i in range(n+1):
        for j in use_index:
            if i-A[j]>=0:
                dp[i] = max(dp[i-A[j]]+B[j]-A[j],dp[i])
        count = max(count,dp[i])
    return count

count1 = change(N,A,B,first_use)
count2 = change(count1,B,A,second_use)

print(count2)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[] for _ in range(num)]
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

N = I()
A = III()
B = III()

first_use = []
second_use = []
for i in range(3):
    if A[i]<B[i]:
        first_use.append(i)
    if A[i]>B[i]:
        second_use.append(i)

# Aでちょうどi個のドングリを交換したときの，Bでのドングリの最大数
dp = [(N-i) for i in range(N+1)]
for i in range(N+1):
    for j in first_use:
        if i-A[j]>=0:
            dp[i] = max(dp[i-A[j]]+B[j]-A[j],dp[i])

# B→A
count = max(dp)
dp = [(count-i) for i in range(count+1)]
for i in range(count+1):
    for j in second_use:
        if i-B[j]>=0:
            dp[i] = max(dp[i-B[j]]+A[j]-B[j],dp[i])

print(max(dp))

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[] for _ in range(num)]
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

N = I()
A = III()
B = III()

first_use = []
second_use = []
for i in range(3):
    if A[i]<B[i]:
        first_use.append(i)
    if A[i]>B[i]:
        second_use.append(i)

# Aでちょうどi個のドングリを交換したときの，Bでのドングリの最大数
dp = [0]*(N+1)
for i in range(N+1):
    for j in first_use:
        if i-A[j]>=0:
            dp[i] = max(dp[i-A[j]]+B[j],dp[i])
for i in range(N+1):
    dp[i] = N-i+dp[i]

# B→A
count = max(dp)
dp = [0]*(count+1)
for i in range(count+1):
    for j in second_use:
        if i-B[j]>=0:
            dp[i] = max(dp[i-B[j]]+A[j],dp[i])
for i in range(count+1):
    dp[i] = count-i+dp[i]

print(max(dp))

Submission status: Time Limit Exceeded

Solution code:
def main():
    from collections import namedtuple

    Ratio = namedtuple('Ratio', 'gold, silver copper')

    def trade(n, Rfrm, Rto):
        Df = Ratio(*(t - f for f, t in zip(Rfrm, Rto)))
        dp = [n] * (n + 1)
        for acorn in range(1, n + 1):
            dp[acorn] = max(
                dp[acorn],
                0 if acorn < Rfrm.gold else dp[acorn - Rfrm.gold] + Df.gold,
                0 if acorn < Rfrm.silver else dp[acorn - Rfrm.silver] + Df.silver,
                0 if acorn < Rfrm.copper else dp[acorn - Rfrm.copper] + Df.copper
            )
        return dp[n]

    n = int(input())
    Ra = Ratio(*map(int, input().split()))
    Rb = Ratio(*map(int, input().split()))

    ret = trade(n=n, Rfrm=Ra, Rto=Rb)
    ret = trade(n=ret, Rfrm=Rb, Rto=Ra)
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    from collections import namedtuple

    Ratio = namedtuple('Ratio', 'gold, silver copper')

    def convert(n, Rfrm, Rto):
        Df = Ratio(*(t - f for f, t in zip(Rfrm, Rto)))

        ret = n

        g_max = n // Rfrm.gold if Df.gold > 0 else 0
        for g in range(g_max + 1):
            dg = Df.gold * g
            s_max = (n - Rfrm.gold * g) // Rfrm.silver if Df.silver > 0 else 0
            for s in range(s_max + 1):
                ds = Df.silver * s
                b = (n - Rfrm.gold * g - Rfrm.silver * s) // Rfrm.copper if Df.copper > 0 else 0

                db = Df.copper * b
                ret = max(ret, n + dg + ds + db)

        return ret

    n = int(input())
    Ra = Ratio(*map(int, input().split()))
    Rb = Ratio(*map(int, input().split()))

    ret = convert(n=n, Rfrm=Ra, Rto=Rb)
    ret = convert(n=ret, Rfrm=Rb, Rto=Ra)
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import namedtuple

Ratio = namedtuple('Ratio', 'gold, silver copper')


def convert(n, Rfrm, Rto):
    Df = Ratio(*(t - f for f, t in zip(Rfrm, Rto)))

    ret = n

    g_max = n // Rfrm.gold if Df.gold > 0 else 0
    for g in range(g_max + 1):
        dg = Df.gold * g
        s_max = (n - Rfrm.gold * g) // Rfrm.silver if Df.silver > 0 else 0
        for s in range(s_max + 1):
            ds = Df.silver * s
            b = (n - Rfrm.gold * g - Rfrm.silver * s) // Rfrm.copper if Df.copper > 0 else 0

            db = Df.copper * b
            ret = max(ret, n + dg + ds + db)

    return ret


n = int(input())
Ra = Ratio(*map(int, input().split()))
Rb = Ratio(*map(int, input().split()))

ret = convert(n=n, Rfrm=Ra, Rto=Rb)
ret = convert(n=ret, Rfrm=Rb, Rto=Ra)
print(ret)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from itertools import repeat
N = int(input())
A = tuple(map(int,input().split()))
B = tuple(map(int,input().split()))

def helper(A, B, N):
    res = deque(repeat(-1000000, max(A)+1),max(A)+1)

    for v in range(N+1):
        res.append(max(0,max(res[-x]+y-x for x,y in zip(A,B))))
    return max(res)+N

M = helper(A,B,N)
O = helper(B,A,M)
print(O)

Submission status: Time Limit Exceeded

Solution code:

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

def helper(A, B, N):
    res = [0]*(N+1)
    for v in range(min(A),N+1):
        res[v] = max(res[v-x]+y-x for x,y in zip(A,B) if v >= x)
    return max(res)+N

M = helper(A,B,N)
O = helper(B,A,M)
print(O)

Submission status: Time Limit Exceeded

Solution code:

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

def helper(A, B, N):
    res = [0]*(N+1)
    for v in range(1,N+1):
        res[v] = max((res[v-x]+y-x for x,y in zip(A,B) if v >= x), default=0)
    return max(res)+N

M = helper(A,B,N)
O = helper(B,A,M)
print(O)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    A=list(map(int,input().split()))
    B=list(map(int,input().split()))

    # A->B
    dp=[None]*(n+1)
    for i in range(n+1):
        res=i
        if(i>=A[0]): res=max(res,dp[i-A[0]]+B[0])
        if(i>=A[1]): res=max(res,dp[i-A[1]]+B[1])
        if(i>=A[2]): res=max(res,dp[i-A[2]]+B[2])
        dp[i]=res

    n=max(dp)
    dp=[None]*(n+1)
    for i in range(n+1):
        res=i
        if(i>=B[0]): res=max(res,dp[i-B[0]]+A[0])
        if(i>=B[1]): res=max(res,dp[i-B[1]]+A[1])
        if(i>=B[2]): res=max(res,dp[i-B[2]]+A[2])
        dp[i]=res

    print(max(dp))
resolve()

Submission status: Time Limit Exceeded

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 v2[0]>0 and i+w2[0]<=newN:
            dp2[i+w2[0]] = max(dp2[i+w2[0]],dp2[i]+v2[0])
        if v2[1]>0 and i+w2[1]<=newN:
            dp2[i+w2[1]] = max(dp2[i+w2[1]],dp2[i]+v2[1])
        if v2[2]>0 and i+w2[2]<=newN:
            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: Time Limit Exceeded

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]<=newN:
            dp2[i+w2[0]] = max(dp2[i+w2[0]],dp2[i]+v2[0])
        if i+w2[1]<=newN:
            dp2[i+w2[1]] = max(dp2[i+w2[1]],dp2[i]+v2[1])
        if i+w2[2]<=newN:
            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: Time Limit Exceeded

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

dp = [i for i in range(N+1)]

for i in range(1,N+1):
    for j in range(3):
        if i-A[j] >= 0:
            dp[i] = max(dp[i],dp[i-A[j]]+B[j])

M = dp[N]            
dp = [i for i in range(M+1)]

for i in range(1,M+1):
    for j in range(3):
        if i-B[j] >= 0:
            dp[i] = max(dp[i],dp[i-B[j]]+A[j])

print(dp[M])

Submission status: Time Limit Exceeded

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

dp = [0]*(N+1)
for i in range(1,N+1):
    for j in range(3):
        if i-A[j] >= 0:
            dp[i] = max(dp[i],dp[i-1]+1,dp[i-A[j]]+B[j])
        else:
            dp[i] = max(dp[i],dp[i-1]+1)

M = dp[N]            
dp = [0]*(M+1)

for i in range(1,M+1):
    for j in range(3):
        if i-B[j] >= 0:
            dp[i] = max(dp[i],dp[i-1]+1,dp[i-B[j]]+A[j])
        else:
            dp[i] = max(dp[i],dp[i-1]+1)
    
print(dp[M])

Submission status: Time Limit Exceeded

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

M = dp[N][3] + N

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

print(dp2[M][3]+M)

Submission status: Time Limit Exceeded

Solution code:
#12:40
n = int(input())
a1,a2,a3 = map(int,input().split())
b1,b2,b3 = map(int,input().split())
dp = [0]
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.append(tmp)
m = dp[-1]
#print(dp)
#print(m)
dp = [0]
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.append(tmp)
print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
#12:40
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
dp = [0]
for i in range(1,n+1):
  tmp = [dp[-1]+1]
  for k in range(3):
    if i - a[k] >= 0:
      tmp.append(dp[i-a[k]]+b[k])
  #print(dp,tmp)
  dp.append(max(tmp))
m = dp[-1]
#print(dp)
#print(m)
dp = [0]
for j in range(1,m+1):
  tmp = [dp[-1]+1]
  for k in range(3):
    if j - b[k] >= 0:
      #print(j,b[k],dp)
      tmp.append(dp[j-b[k]]+a[k])
  dp.append(max(tmp))
print(dp[-1])

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    rate = [list(map(int, input().split())) for _ in range(2)]
    ans = N

    for buying_rate, selling_rate in ((rate[0], rate[1]), (rate[1], rate[0])):
        dp = list(range(ans + 1))
        for t in range(3):
            if buying_rate[t] > selling_rate[t]:
                continue
            for d in range(ans - buying_rate[t] + 1):
                dp[d + buying_rate[t]] = max(
                    dp[d + buying_rate[t]], dp[d] + selling_rate[t]
                )
        ans = max(dp)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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)
n+=s[-1]
s=[0]*(n+1)
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: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(pow(10, 6))


def main():
  n = int(input())
  ga, sa, ba = map(int, input().split())
  gb, sb, bb = map(int, input().split())
  aaa = [ga, sa, ba]
  bbb = [gb, sb, bb]
  a2b = [ga-gb, sa-sb, ba-bb]
  b2a = [gb-ga, sb-sa, bb-ba]
  dp = [[0 for _ in range(n+1)] for _ in range(4)]
  for i in range(3):
    for j in range(n+1):
      if j < aaa[i]:
        dp[i+1][j] = dp[i][j]
      else:
        dp[i+1][j] = max(dp[i][j], dp[i+1][j-aaa[i]] + b2a[i])
  n += dp[-1][-1]
  dp = [[0 for _ in range(n+1)] for _ in range(4)]
  for i in range(3):
    for j in range(n+1):
      if j < bbb[i]:
        dp[i+1][j] = dp[i][j]
      else:
        dp[i+1][j] = max(dp[i][j], dp[i+1][j-bbb[i]] + a2b[i])
  n += dp[-1][-1]
  print(n)


if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
def dfs():
    n = int(input())
    weight = list(map(int, input().split()))
    value = list(map(int, input().split()))
    
    
    def knapsack(n, weight, value):
        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])
    
    
    new_n=knapsack(n=n, weight=weight, value=value)
    print(knapsack(n=new_n, weight=value, value=weight))

    
dfs()

Submission status: Time Limit Exceeded

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]
    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+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]
    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+1) for j in range((ans-i*s1)//s2+1))
    ans=ta
  print(ans)
calc()

Submission status: Time Limit Exceeded

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+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
    ta=max(ans+i*(g1-s1)+((ans-i*s1)//s2)*(g2-s2) 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]
    ta=max(ans+i*(g1-s1)+j*(g2-s2)+((ans-i*s1-j*s2)//s3)*(g3-s3) for i in range(ans//s1+1) for j in range((ans-i*s1)//s2+1))
    ans=ta
  print(ans)
calc()

Submission status: Time Limit Exceeded

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+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+1) for j in range((ans-i*s1)//s2+1))
    ans=ta
  print(ans)
calc()

Submission status: Time Limit Exceeded

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=0
    for i in range(ans//s1+1):
      j=(ans-i*s1)//s2
      tmp=i*g1+j*g2+(ans-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]
    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+(ans-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+(ans-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]
    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+(ans-i*s1-j*s2-k*s3)
        ta=max(ta,tmp)
    ans=ta
  print(ans)
calc()

Submission status: Time Limit Exceeded

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+(ans-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+(ans-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+(ans-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+(ans-i*s1-j*s2-k*s3)
      ta=max(ta,tmp)
  ans=ta
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def solve_dp(m, a, b):
    dp = [0] * (m + 1)
    dp[0] = m
    for ai, bi in zip(a, b):
        w = ai
        v = bi - ai
        for j in range(w, m + 1):
            dp[j] = max(dp[j - w] + v, dp[j])
    return max(dp)

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
m = solve_dp(n, a, b)
ans = solve_dp(m, b, a)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
m = n
for k in range(2):
    dp = [0 for j in range(m + 1)]
    dp[0] = m
    for i in range(3):
        w = a[i]
        v = b[i] - a[i]
        for j in range(w, m + 1):
            dp[j] = max(dp[j - w] + v, dp[j])
    m = max(dp)
    b, a = a, b
print(m)

Submission status: Time Limit Exceeded

Solution code:
def solve(n,a,b):
    dp=[0]*(n+1)
    for w in range(n+1): dp[w]=max(w,dp[w-a[0]]+b[0]if w-a[0]>=0 else w,dp[w-a[1]]+b[1]if w-a[1]>=0 else w,dp[w-a[2]]+b[2]if w-a[2]>=0 else w)
    return dp[-1]
N,A,B=int(input()),list(map(int, input().split())),list(map(int, input().split()))
print(solve(solve(N,A,B),B,A))

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    dp = [0] * (N+1)
    for i in range(1, N+1):
        gold, silver, copper = 0, 0, 0
        if i >= A[0]:
            gold = dp[i-A[0]] + B[0]
        if i >= A[1]:
            silver = dp[i-A[1]] + B[1]
        if i >= A[2]:
            copper = dp[i-A[2]] + B[2]
        dp[i] = max(i, gold, silver, copper)

    M = dp[N]
    dp = [0] * (M+1)
    for i in range(1, M+1):
        gold, silver, copper = 0, 0, 0
        if i >= B[0]:
            gold = dp[i-B[0]] + A[0]
        if i >= B[1]:
            silver = dp[i-B[1]] + A[1]
        if i >= B[2]:
            copper = dp[i-B[2]] + A[2]
        dp[i] = max(i, gold, silver, copper)

    return dp[M]


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

Submission status: Time Limit Exceeded

Solution code:
inpl = lambda: list(map(int,input().split()))
N = int(input())
rate = []
rate.append(inpl())
rate.append(inpl())
AB = []
BA = []
for i in range(3):
    rA, rB = rate[0][i], rate[1][i]
    if rB > rA:
        AB.append((rA, rB))
    elif rA > rB:
        BA.append((rB, rA))

dpAB = [0]*(N+1)
q = [0]*(len(AB)+1)
for i in range(1,N+1):
    k = 1
    for cost, ret in AB:
        if i >= cost:
            q[k] = dpAB[i-cost] + ret
            k += 1
    dpAB[i]=max(dpAB[i-1]+1, max(q))

M = dpAB[N]
dpBA = [0]*(M+1)
q = [0]*(len(BA)+1)
for i in range(1,M+1):
    k = 1
    for cost, ret in BA:
        if i >= cost:
            q[k] = dpBA[i-cost] + ret
            k += 1
    dpBA[i] = max(dpBA[i-1]+1, max(q))

print(dpBA[M])