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 bisect
import math
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
from operator import itemgetter

def calc(n,p,q,r,s,t,u):
    dp=[-1]*(n+1)
    for i in range(n+1):
        dp[i]=i
    dp[0]=0
    for i in range(1,n+1):
        if i>=p and dp[i-p]>=0:
            if dp[i]<dp[i-p]+s:
                dp[i]=dp[i-p]+s
        if i>=q and dp[i-q]>=0:
            if dp[i]<dp[i-q]+t:
                dp[i]=dp[i-q]+t
        if i>=r and dp[i-r]>=0:
            if dp[i]<dp[i-r]+u:
                dp[i]=dp[i-r]+u
    
    return max(dp)

n=int(input())
p,q,r=map(int,readline().split())
s,t,u=map(int,readline().split())

res=calc(n,p,q,r,s,t,u)
ans=calc(res,s,t,u,p,q,r)
print(ans)

Submission status: Time Limit Exceeded

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

def main():
  n = int(input())
  A = tuple(map(int, input().split()))
  B = tuple(map(int, input().split()))
  def f(n):
    dp = list(range(n+1))
    for a, b in zip(A, B):
      if b <= a:
        continue
      for i in range(n+1):
        if i-a >= 0:
          dp[i] = max(dp[i], dp[i-a]+b)
    return dp[n]
  m = f(n)
  A, B = B, A
  ans = f(m)
  print(ans)

if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

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

def main():
  n = int(input())
  A = tuple(map(int, input().split()))
  B = tuple(map(int, input().split()))
  def f(n):
    dp = list(range(n+1))
    for a, b in zip(A, B):
      if b <= a:
        continue
      for i in range(n+1):
        if i-a >= 0:
          dp[i] = max(dp[i], dp[i-a]+b)
    return max(dp)
  m = f(n)
  A, B = B, A
  ans = f(m)
  print(ans)

if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

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

def main():
  n = int(input())
  A = tuple(map(int, input().split()))
  B = tuple(map(int, input().split()))
  def f(n):
    dp = list(range(n+1))
    for i in range(n+1):
      for a, b in zip(A, B):
        if i-a >= 0:
          dp[i] = max(dp[i], dp[i-a]+b)
    return max(dp)
  m = f(n)
  A, B = B, A
  ans = f(m)
  print(ans)

if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

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

n = int(input())
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))
def f(n):
  dp = list(range(n+1))
  for i in range(n+1):
    for a, b in zip(A, B):
      if i-a >= 0:
        dp[i] = max(dp[i], dp[i-a]+b)
  return max(dp)
m = f(n)
A, B = B, A
ans = f(m)
print(ans)

Submission status: Time Limit Exceeded

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

def main():
  n = int(input())
  A = tuple(map(int, input().split()))
  B = tuple(map(int, input().split()))
  def f(n):
    dp = [0]*(n+1)
    for i in range(n+1):
      for a, b in zip(A, B):
        if i-a >= 0:
          dp[i] = max(dp[i], dp[i-a]+b)
    return max(dp[i]+n-i for i in range(n+1))
  m = f(n)
  A, B = B, A
  ans = f(m)
  print(ans)

if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

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

n = int(input())
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))
def f(n):
  dp = [0]*(n+1)
  for i in range(n+1):
    for a, b in zip(A, B):
      if i-a >= 0:
        dp[i] = max(dp[i], dp[i-a]+b)
  return max(dp[i]+n-i for i in range(n+1))
m = f(n)
A, B = B, A
ans = f(m)
print(ans)

Submission status: Time Limit Exceeded

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

n = int(input())
a1, a2, a3 = map(int, input().split())
b1, b2, b3 = map(int, input().split())
def f(n, a1, a2, a3, b1, b2, b3):
  dp = [0]*(n+1)
  for i in range(n+1):
    for a, b in zip((a1, a2, a3), (b1, b2, b3)):
      if i-a >= 0:
        dp[i] = max(dp[i], dp[i-a]+b)
  return max(dp[i]+n-i for i in range(n+1))
m = f(n, a1, a2, a3, b1, b2, b3)
ans = f(m, b1, b2, b3, a1, a2, a3)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def max_convert(n, rate):
    ret = n
    for i in range(n//rate[0][0]+1):
        if rate[0][0]*i > n: break
        for j in range((n-i*rate[0][0])//rate[1][0]+1):
            d_num = rate[0][0]*i + rate[1][0]*j
            if d_num > n: break
            k = (n-d_num)//rate[2][0]
            d_num += rate[2][0]*k
            ret = max(ret, i*rate[0][1] + j*rate[1][1] + k*rate[2][1] + (n-d_num))
    
    return ret

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

go = [(1,1)]*3
back = [(1,1)]*3

go.sort(reverse=True)
back.sort(reverse=True)

for i,(a,b) in enumerate(((ga,gb),(sa,sb),(ba,bb))):
    if a>b: back[i] = (b,a)
    if b>a: go[i] = (a,b)

n = max_convert(n, go)
n = max_convert(n, back)

print(n)

Submission status: Time Limit Exceeded

Solution code:
def max_convert(n, rate):
    ret = n
    for i in range(5001):
        if rate[0][0]*i > n: break
        for j in range(5001):
            d_num = rate[0][0]*i + rate[1][0]*j
            if d_num > n: break
            k = (n-d_num)//rate[2][0]
            d_num += rate[2][0]*k
            ret = max(ret, i*rate[0][1] + j*rate[1][1] + k*rate[2][1] + (n-d_num))
    
    return ret

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

go = [(1,1)]*3
back = [(1,1)]*3

for i,(a,b) in enumerate(((ga,gb),(sa,sb),(ba,bb))):
    if a>b: back[i] = (b,a)
    if b>a: go[i] = (a,b)

n = max_convert(n, go)
n = max_convert(n, back)

print(n)

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)
ni = lambda: int(sys.stdin.readline())
nm = lambda: map(int, sys.stdin.readline().split())

N = ni()
g1, s1, b1 = nm()
g2, s2, b2 = nm()

INF = 10 ** 15


def solve():
    x1 = [g1, s1, b1]
    x2 = [g2, s2, b2]

    # dp[i] := max future donguri when we use i donguri
    dp = [-INF] * (N + 1)
    dp[0] = N
    for i in range(3):
        t1 = x1[i]
        t2 = x2[i]
        if t1 >= t2:
            continue
        for j in range(t1, N + 1):
            if dp[j - t1] >= N:
                dp[j] = max(dp[j], dp[j - t1] - t1 + t2)

    don = max(dp)
    dp = [0] * (don + 1)
    dp[0] = don
    for i in range(3):
        t1 = x1[i]
        t2 = x2[i]
        if t1 <= t2:
            continue
        for j in range(t2, don + 1):
            if dp[j - t2] >= N:
                dp[j] = max(dp[j], dp[j - t2] - t2 + t1)
    return max(dp)


print(solve())

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)
ni = lambda: int(sys.stdin.readline())
nm = lambda: map(int, sys.stdin.readline().split())

N = ni()
g1, s1, b1 = nm()
g2, s2, b2 = nm()


def solve():
    # Start
    don = N
    # A
    don_max = N
    for kg in range(don // g1 + 1):
        don1 = don - kg * g1
        temp1 = kg * g2
        for ks in range(don1 // s1 + 1):
            don2 = don1 - ks * s1
            temp2 = ks * s2
            if b2 > b1:
                kb = don2 // b1
                don3 = don2 - kb * b1
                temp3 = kb * b2
            else:
                kb = 0
                don3 = don2
                temp3 = 0
            if don_max < don3 + temp1 + temp2 + temp3:
                don_max = don3 + temp1 + temp2 + temp3
                # print(f"1> don_max={don_max} g:{kg} s:{ks} b:{kb}")

    don = don_max
    for kg in range(don // g2 + 1):
        don1 = don - kg * g2
        temp1 = kg * g1
        for ks in range(don1 // s2 + 1):
            don2 = don1 - ks * s2
            temp2 = ks * s1
            if b1 > b2:
                kb = don2 // b2
                don3 = don2 - kb * b2
                temp3 = kb * b1
            else:
                kb = 0
                don3 = don2
                temp3 = 0
            if don_max < don3 + temp1 + temp2 + temp3:
                don_max = don3 + temp1 + temp2 + temp3
                # print(f"2> don_max={don_max} g:{kg} s:{ks} b:{kb}")
    return don_max


print(solve())

Submission status: Time Limit Exceeded

Solution code:
import sys
from fractions import Fraction

sys.setrecursionlimit(10 ** 8)
ni = lambda: int(sys.stdin.readline())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
ns = lambda: sys.stdin.readline().rstrip()

N = ni()
g1, s1, b1 = nm()
g2, s2, b2 = nm()


def solve():
    gr, sr, br = (g2, g1), (s2, s1), (b2, b1)
    rp = sorted([(Fraction(n, d), n, d) for (n, d) in [gr, sr, br] if n > d])
    rn = sorted([(Fraction(n, d), n, d) for (n, d) in [gr, sr, br] if n < d])

    # Start
    don = N
    # A
    don_max = N
    for kg in range(don // g1 + 1):
        don1 = don - kg * g1
        temp1 = kg * g2
        for ks in range(don1 // s1 + 1):
            don2 = don1 - ks * s1
            temp2 = ks * s2
            if b2 > b1:
                kb = don2 // b1
                don3 = don2 - kb * b1
                temp3 = kb * b2
            else:
                kb = 0
                don3 = don2
                temp3 = 0
            if don_max < don3 + temp1 + temp2 + temp3:
                don_max = don3 + temp1 + temp2 + temp3
                # print(f"1> don_max={don_max} g:{kg} s:{ks} b:{kb}")

    don = don_max
    for kg in range(don // g2 + 1):
        don1 = don - kg * g2
        temp1 = kg * g1
        for ks in range(don1 // s2 + 1):
            don2 = don1 - ks * s2
            temp2 = ks * s1
            if b1 > b2:
                kb = don2 // b2
                don3 = don2 - kb * b2
                temp3 = kb * b1
            else:
                kb = 0
                don3 = don2
                temp3 = 0
            if don_max < don3 + temp1 + temp2 + temp3:
                don_max = don3 + temp1 + temp2 + temp3
                # print(f"2> don_max={don_max} g:{kg} s:{ks} b:{kb}")
    return don_max


print(solve())

Submission status: Time Limit Exceeded

Solution code:
def solve(n, a, b):
  ab = [(ai, bi) for ai, bi in zip(a, b) if ai < bi]
  dp = [[j for j in range(n+1)] for i in range(len(ab)+1)]
  if len(ab) == 0:
    return n
  if len(ab) == 1:
    a0, b0 = ab[0]
    return (n//a0)*b0 + n%a0
  if len(ab) == 2:
    a0, b0 = ab[0]; a1, b1 = ab[1]
    if a0 < a1: a0, b0, a1, b1 = a1, b1, a0, b0
    t = n // a0
    return max(((n-a0*i)//a1)*b1 + (n-a0*i)%a1 + i*b0 for i in range(t+1))
  else:
    dp = [j for j in range(n+1)]
    for ai, bi in ab:
      for j in range(ai, n+1):
        dp[j] = max(dp[j], dp[j-ai]+bi)
    return dp[-1]

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

N = solve(N, A, B)
N = solve(N, B, A)
print(N)

Submission status: Time Limit Exceeded

Solution code:
def solve(n, a, b):
  ab = [(ai, bi) for ai, bi in zip(a, b) if ai < bi]
  dp = [[j for j in range(n+1)] for i in range(len(ab)+1)]
  for i, (ai, bi) in enumerate(ab, 1):
    for j in range(n+1):
      if j >= ai:
        dp[i][j] = max(dp[i-1][j], dp[i][j-ai]+bi)
      else:
        dp[i][j] = dp[i-1][j]
  return dp[-1][-1]  

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

N = solve(N, A, B)
N = solve(N, B, A)
print(N)

Submission status: Time Limit Exceeded

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

N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
A = [10**18] * 3
B = [10**18] * 3
dg = abs(ga - gb)
ds = abs(sa - sb)
db = abs(ba - bb)
if ga < gb:
    A[0] = ga
elif ga > gb:
    B[0] = gb
if sa < sb:
    A[1] = sa
elif sa > sb:
    B[1] = sb
if ba < bb:
    A[2] = ba
elif ba > bb:
    B[2] = bb

def calc(X):
    res = -1
    for i in range(N+1):
        if X[0] * i > N:
            break
        M = N - X[0] * i
        jj = M // X[1]
        x = 150
        for j in range(max(jj - x, 0), jj+1):
            k = (M - X[1] * j) // X[2]
            res = max(res, dg * i + ds * j + db * k)
        kk = M // X[2]
        for k in range(max(kk - x, 0), kk+1):
            j = (M - X[2] * k) // X[1]
            res = max(res, dg * i + ds * j + db * k)
    return res

N += calc(A)
N += calc(B)
print(N)

Submission status: Time Limit Exceeded

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

N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
A = [10**18] * 3
B = [10**18] * 3
dg = abs(ga - gb)
ds = abs(sa - sb)
db = abs(ba - bb)
if ga < gb:
    A[0] = ga
elif ga > gb:
    B[0] = gb
if sa < sb:
    A[1] = sa
elif sa > sb:
    B[1] = sb
if ba < bb:
    A[2] = ba
elif ba > bb:
    B[2] = bb

def calc(X):
    res = -1
    for i in range(N+1):
        if X[0] * i > N:
            break
        M = N - X[0] * i
        jj = M // X[1]
        x = 200
        for j in range(max(jj - x, 0), jj+1):
            k = (M - X[1] * j) // X[2]
            res = max(res, dg * i + ds * j + db * k)
        kk = M // X[2]
        for k in range(max(kk - x, 0), kk+1):
            j = (M - X[2] * k) // X[1]
            res = max(res, dg * i + ds * j + db * k)
    return res

N += calc(A)
N += calc(B)
print(N)

Submission status: Time Limit Exceeded

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

N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
A = [10**18] * 3
B = [10**18] * 3
dg = abs(ga - gb)
ds = abs(sa - sb)
db = abs(ba - bb)
if ga < gb:
    A[0] = ga
elif ga > gb:
    B[0] = gb
if sa < sb:
    A[1] = sa
elif sa > sb:
    B[1] = sb
if ba < bb:
    A[2] = ba
elif ba > bb:
    B[2] = bb

def calc(X):
    res = -1
    for i in range(N+1):
        if X[0] * i > N:
            break
        M = N - X[0] * i
        jj = M // X[1]
        x = 300
        for j in range(max(jj - x, 0), jj+1):
            k = (M - X[1] * j) // X[2]
            res = max(res, dg * i + ds * j + db * k)
        kk = M // X[2]
        for k in range(max(kk - x, 0), kk+1):
            j = (M - X[2] * k) // X[1]
            res = max(res, dg * i + ds * j + db * k)
    return res

N += calc(A)
N += calc(B)
print(N)

Submission status: Time Limit Exceeded

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

N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
A = [10**18] * 3
B = [10**18] * 3
dg = abs(ga - gb)
ds = abs(sa - sb)
db = abs(ba - bb)
if ga < gb:
    A[0] = ga
elif ga > gb:
    B[0] = gb
if sa < sb:
    A[1] = sa
elif sa > sb:
    B[1] = sb
if ba < bb:
    A[2] = ba
elif ba > bb:
    B[2] = bb

def calc(X):
    res = -1
    for i in range(N+1):
        if X[0] * i > N:
            break
        M = N - X[0] * i
        jj = M // X[1]
        for j in range(max(jj - 500, 0), jj+1):
            k = (M - X[1] * j) // X[2]
            res = max(res, dg * i + ds * j + db * k)
        kk = M // X[2]
        for k in range(max(kk - 500, 0), kk+1):
            j = (M - X[2] * k) // X[1]
            res = max(res, dg * i + ds * j + db * k)
    return res

N += calc(A)
N += calc(B)
print(N)

Submission status: Time Limit Exceeded

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

N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
A = [10**18] * 3
B = [10**18] * 3
dg = abs(ga - gb)
ds = abs(sa - sb)
db = abs(ba - bb)
if ga < gb:
    A[0] = ga
elif ga > gb:
    B[0] = gb
if sa < sb:
    A[1] = sa
elif sa > sb:
    B[1] = sb
if ba < bb:
    A[2] = ba
elif ba > bb:
    B[2] = bb

def calc(X):
    res = -1
    for i in range(N+1):
        if X[0] * i > N:
            break
        M = N - X[0] * i
        jj = M // X[1]
        for j in range(max(jj - 1000, 0), jj+1):
            k = (M - X[1] * j) // X[2]
            res = max(res, dg * i + ds * j + db * k)
        kk = M // X[2]
        for k in range(max(kk - 1000, 0), kk+1):
            j = (M - X[2] * k) // X[1]
            res = max(res, dg * i + ds * j + db * k)
    return res

N += calc(A)
N += calc(B)
print(N)

Submission status: Time Limit Exceeded

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

N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
A = [10**18] * 3
B = [10**18] * 3
dg = abs(ga - gb)
ds = abs(sa - sb)
db = abs(ba - bb)
if ga < gb:
    A[0] = ga
elif ga > gb:
    B[0] = gb
if sa < sb:
    A[1] = sa
elif sa > sb:
    B[1] = sb
if ba < bb:
    A[2] = ba
elif ba > bb:
    B[2] = bb
m = -1
for i in range(N+1):
    if A[0] * i > N:
        break
    for j in range(N+1):
        if A[0] * i + A[1] * j > N:
            break
        k = (N - A[0] * i - A[1] * j) // A[2]
        m = max(m, dg * i + ds * j + db * k)
N += m
m = -1
for i in range(N+1):
    if B[0] * i > N:
        break
    for j in range(N+1):
        if B[0] * i + B[1] * j > N:
            break
        k = (N - B[0] * i - B[1] * j) // B[2]
        m = max(m, dg * i + ds * j + db * k)
N += m
print(N)

Submission status: Time Limit Exceeded

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

donguri=N

inc=0
for i in range(0,donguri//ga+1):
    x=(ga<gb)*i
    for j in range(0,donguri//sa+1):
        y=(sa<sb)*j
        if donguri>=x*ga+y*sa:
            z=((donguri-x*ga-y*sa)//ba)*(ba<bb)
            inc=max(inc,x*(gb-ga)+y*(sb-sa)+z*(bb-ba))

donguri+=inc

inc=0
for i in range(0,donguri//gb+1):
    x=(ga>gb)*i
    for j in range(0,donguri//sb+1):
        y=(sa>sb)*j
        if donguri>=x*gb+y*sb:
            z=((donguri-x*gb-y*sb)//bb)*(ba>bb)
            inc=max(inc,-x*(gb-ga)-y*(sb-sa)-z*(bb-ba))

donguri+=inc
print(donguri)

Submission status: Time Limit Exceeded

Solution code:
def trades(acorns, pricesSrc, pricesDst):
    n = len(pricesSrc)
    profits = [pricesDst[i] - pricesSrc[i] for i in range(n)]
    metals = [(pricesSrc[i], profits[i]) for i in range(n)]
    metals = sorted(metals, key = lambda x: x[1]/float(x[0]), reverse=True)

    gains = [0 for i in range(acorns + 1)]

    for i, metal in enumerate(metals):
        price, profit = metal
        for gain in range(price, acorns + 1):
            gains_without = gains[gain - price]
            candidate = gains_without + profit
            if gains[gain] < candidate:
                gains[gain] = candidate

    return acorns + gains[acorns]

acorns = int(input())
pricesA = list(map(int, input().split()))
pricesB = list(map(int, input().split()))

acornsAB = trades(acorns, pricesA, pricesB)
acornsBA = trades(acornsAB, pricesB, pricesA)

print(acornsBA)

Submission status: Time Limit Exceeded

Solution code:
def trades(acorns, pricesSrc, pricesDst):
    n = len(pricesSrc)
    profit = [pricesDst[i] - pricesSrc[i] for i in range(n)]
    gains = [0 for i in range(acorns + 1)]
    for i in range(acorns + 1):
        for j in range(n):
            if (pricesSrc[j] <= i):
                gains[i] = max(gains[i], gains[i - pricesSrc[j]] + profit[j])
    return acorns + gains[acorns]

acorns = int(input())
pricesA = list(map(int, input().split()))
pricesB = list(map(int, input().split()))

acornsAB = trades(acorns, pricesA, pricesB)
acornsBA = trades(acornsAB, pricesB, pricesA)

print(acornsBA)

Submission status: Time Limit Exceeded

Solution code:
def trades(acorns, pricesSrc, pricesDst):
    profit = list(map(lambda x: x[1] - x[0], zip(pricesSrc, pricesDst)))
    gains = [0 for i in range(acorns + 1)]
    n = len(pricesSrc)
    for i in range(acorns + 1):
        for j in range(n):
            if (pricesSrc[j] <= i):
                gains[i] = max(gains[i], gains[i - pricesSrc[j]] + profit[j])
    return acorns + gains[acorns]

acorns = int(input())
pricesA = list(map(int, input().split()))
pricesB = list(map(int, input().split()))

acornsAB = trades(acorns, pricesA, pricesB)
acornsBA = trades(acornsAB, pricesB, pricesA)

print(acornsBA)

Submission status: Time Limit Exceeded

Solution code:
def trades(acorns, pricesSrc, pricesDst):
    profit = list(map(lambda x: x[1] - x[0], zip(pricesSrc, pricesDst)))
    gains = [0 for i in range(acorns + 1)]
    for i in range(acorns + 1):
        for j in range(len(pricesSrc)):
            if (pricesSrc[j] <= i):
                gains[i] = max(gains[i], gains[i - pricesSrc[j]] + profit[j])
    return acorns + gains[acorns]

acorns = int(input())
pricesA = list(map(int, input().split()))
pricesB = list(map(int, input().split()))

acornsAB = trades(acorns, pricesA, pricesB)
acornsBA = trades(acornsAB, pricesB, pricesA)

print(acornsBA)

Submission status: Time Limit Exceeded

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

inf = 10 ** 18
dp1 = [-inf] * U
dp1[0] = N
for a, b in zip(A, B):
    for i in range(N - a + 1):
        dp1[i + a] = max(dp1[i + a], dp1[i] + b - a)

for i in range(N + 1):
    N = max(N, dp1[i])

dp2 = [-inf] * U
dp2[0] = N
for a, b in zip(A, B):
    for i in range(N - b + 1):
        dp2[i + b] = max(dp2[i + b], dp2[i] + a - b)

for i in range(N + 1):
    N = max(N, dp2[i])

print(N)

Submission status: Time Limit Exceeded

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

def calc(A, B, N):
    M = N
    if len([i for i, (a, b) in enumerate(zip(A, B)) if a < b]) == 1:
        i = [i for i, (a, b) in enumerate(zip(A, B)) if a < b][0]

        for k in range(N // A[i] + 1):
            D = N - k * A[i]
            if D < 0:
                continue

            D += k * B[i]
            M = max(M, D)
    elif len([i for i, (a, b) in enumerate(zip(A, B)) if a < b]) == 2:
        i, j = [i for i, (a, b) in enumerate(zip(A, B)) if a < b]

        for k in range(N // A[i] + 1):
            D = N - k * A[i]
            l, D = divmod(D, A[j])

            D += k * B[i]
            D += l * B[j]

            M = max(M, D)
    elif len([i for i, (a, b) in enumerate(zip(A, B)) if a < b]) == 3:
        for g in range(N // A[0] + 1):
            for s in range(N // A[1] + 1):
                D = N - g * A[0] - s * A[1]

                if D < 0:
                    break

                q, r = divmod(D, A[2])

                D += g * B[0]
                D += s * B[1]

                r += g * B[0]
                r += s * B[1]
                r += q * B[2]

                M = max(M, D, r)
    return M

print(calc(B, A, calc(A, B, N)))

Submission status: Time Limit Exceeded

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

def calc(fr, to):
    ret = N
    for g in range(N // fr[0] + 1):
        for s in range(N // fr[1] + 1):
            D = N - g * fr[0] - s * fr[1]

            if D < 0:
                break

            q, r = divmod(D, fr[2])

            D += g * to[0]
            D += s * to[1]

            r += g * to[0]
            r += s * to[1]
            r += q * to[2]

            ret = max(ret, D, r)
    return ret

N = calc(A, B)
print(calc(B, A))

Submission status: Time Limit Exceeded

Solution code:
n= int(input())
gsb = [tuple(map(int,input().split())) for _ in range(2)]

def increase(num, gsb0, gsb1):
    max_num = 0
    g0,s0,b0 = gsb0
    g1,s1,b1 = gsb1
    g_max = num//g0 if (g0 < g1) else 0
    for i in range(g_max + 1):
        remain = num - i*g0
        d_g = i*g1
        s_max = remain//s0 if (s0 < s1) else 0
        for j in range( s_max + 1 ):
            remain2 = remain - j*s0
            k = remain2//b0 if (b0 < b1) else 0
            tmp = (remain2 - k*b0) + d_g + j*s1 + k*b1
            max_num = max(max_num, tmp)

    return(max_num)

num = increase(n,gsb[0],gsb[1])
num = increase(num,gsb[1],gsb[0])

print(num)

Submission status: Time Limit Exceeded

Solution code:
n= int(input())
gsb = [tuple(map(int,input().split())) for _ in range(2)]

def increase(num, gsb0, gsb1):
    max_num = 0
    g_max = num//gsb0[0] if (gsb0[0] < gsb1[0]) else 0
    for i in range(g_max + 1):
        remain = num - i*gsb0[0]
        s_max = remain//gsb0[1] if (gsb0[1] < gsb1[1]) else 0
        for j in range( s_max + 1 ):
            remain2 = remain - j*gsb0[1]
            k = remain2//gsb0[2] if (gsb0[2] < gsb1[2]) else 0
            tmp = (remain2 - k*gsb0[2]) + i*gsb1[0] + j*gsb1[1] + k*gsb1[2]
            max_num = max(max_num, tmp)

    return(max_num)

num = increase(n,gsb[0],gsb[1])
num = increase(num,gsb[1],gsb[0])

print(num)

Submission status: Time Limit Exceeded

Solution code:
n= int(input())
gsb = [tuple(map(int,input().split())) for _ in range(2)]

def increase(num, gsb0, gsb1):
    max_num = 0
    g_max = num//gsb0[0] if (gsb0[0] < gsb1[0]) else 0
    for i in range(g_max + 1):
        remain = num - i*gsb0[0]
        s_max = remain//gsb0[1] if (gsb0[1] < gsb1[1]) else 0
        for j in range( s_max + 1 ):
            k = (remain - j*gsb0[1])//gsb0[2] if (gsb0[2] < gsb1[2]) else 0

            tmp = (num - i*gsb0[0] - j*gsb0[1] - k*gsb0[2]) + i*gsb1[0] + j*gsb1[1] + k*gsb1[2]
            max_num = max(max_num, tmp)

    return(max_num)

num = increase(n,gsb[0],gsb[1])
num = increase(num,gsb[1],gsb[0])

print(num)

Submission status: Time Limit Exceeded

Solution code:
n= int(input())
gsb = [list(map(int,input().split())) for _ in range(2)]

def increase(num, gsb0, gsb1):
    max_num = 0
    g_max = num//gsb0[0] if (gsb0[0] < gsb1[0]) else 0
    for i in range(g_max + 1):
        remain = num - i*gsb0[0]
        s_max = remain//gsb0[1] if (gsb0[1] < gsb1[1]) else 0
        for j in range( s_max + 1 ):
            k = (remain - j*gsb0[1])//gsb0[2] if (gsb0[2] < gsb1[2]) else 0

            tmp = (num - i*gsb0[0] - j*gsb0[1] - k*gsb0[2]) + i*gsb1[0] + j*gsb1[1] + k*gsb1[2]
            max_num = max(max_num, tmp)

    return(max_num)

num = increase(n,gsb[0],gsb[1])
num = increase(num,gsb[1],gsb[0])

print(num)

Submission status: Time Limit Exceeded

Solution code:
def solve(n):
    dp = [0]*(n+1)
    
    for i in range(n+1):
        dp[i] = i
    
    for i in range(n+1):
        for a, b in [(ga, gb), (sa, sb), (ba, bb)]:
            if i-a>=0:
                dp[i] = max(dp[i], dp[i-a]+b)
    
    return dp[n]

N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
M = solve(N)
ga, gb = gb, ga
sa, sb = sb, sa
ba, bb = bb, ba

print(solve(M))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
A = LIST()
B = LIST()

def solve(N):
    dp = list2d(4, N+1, -INF)
    for i in range(N+1):
        dp[0][i] = i
    for i in range(3):
        a, b = A[i], B[i]
        for j in range(N+1):
            dp[i+1][j] = max(dp[i+1][j], dp[i][j])
            if j+a <= N:
                dp[i][j+a] = max(dp[i][j+a], dp[i][j] + b)
                dp[i+1][j+a] = max(dp[i+1][j+a], dp[i][j] + b)
    return dp[3][N]

N2 = solve(N)
A, B = B, A
ans = solve(N2)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a,b,c=map(int,input().split())
d,e,f=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(n+s[-1])

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()))
d1 = []
d2 = []
for i in range(3):
  if a[i] < b[i]: d1.append((a[i], b[i]))
  if a[i] > b[i]: d2.append((b[i], a[i]))

dp = [0] * (N + 1)

for i in range(N, -1, -1):
  for t in d1:
    x = t[0]
    y = t[1]
    if i - x >= 0:
      dp[i - x] = max(dp[i - x], y + dp[i])
for i in range(N + 1): dp[i] += i
res1 = max(dp)
#print(dp)
dp2 = [0] * (res1 + 1)
for i in range(res1, -1, -1):
  for t in d2:
    x = t[0]
    y = t[1]
    if i - x >= 0:
      dp2[i - x] = max(dp2[i - x], y + dp2[i])
for i in range(res1 + 1): dp2[i] += i
print(max(dp2))

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] * (N + 1)
for i in range(N, -1, -1):
  for j in range(3):
    x = a[j]
    y = b[j]
    if i - x >= 0:
      dp[i - x] = max(dp[i - x], dp[i] + y)
for i in range(N + 1): dp[i] += i
#print(dp)
t = max(dp)
dp2 = [0] * (t + 1)
for i in range(t, -1, -1):
  for j in range(3):
    y = a[j]
    x = b[j]
    if i - x >= 0:
      dp2[i - x] = max(dp2[i - x], dp2[i] + y)
for i in range(t + 1): dp2[i] += i
print(max(dp2))

Submission status: Time Limit Exceeded

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

def solve(n,fr,to):
    rates = [(f,t) for f,t in zip(fr,to) if f < t]
    rates.sort()
    if not rates:
        return n
    dp = [i for i in range(n+1)]
    for f,t in rates:
        for i in range(f,n+1):
            dp[i] = max(dp[i], dp[i-f] + t)
    return dp[-1]

n = solve(N,A,B)
print(solve(n,B,A))