Problem:


Score : 600 points

Problem Statement

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

His plan is as follows:

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

Go to Exchange A and do some trades.

Go to Exchange B and do some trades.

Go to Exchange A and do some trades.

Go back to the nest.

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

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

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

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

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

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

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

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

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

Constraints

1 \leq N \leq 5000

1 \leq g_{X} \leq 5000

1 \leq s_{X} \leq 5000

1 \leq b_{X} \leq 5000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
g_A s_A b_A
g_B s_B b_B

Output

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

Sample Input 1

23
1 1 1
2 1 1

Sample Output 1

46

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

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

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

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

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



Sample input:
23
1 1 1
2 1 1


Sample output:
46

Submission status: Wrong Answer

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


ups = []
downs = []
eqs = []
for i in range(3):
    if A[i] < B[i]:
        ups.append((A[i], B[i]))
    elif A[i] == B[i]:
        eqs.append((A[i], B[i]))
    else:
        downs.append((A[i], B[i]))
ans = N

if len(eqs) == 3:
    print(N)
    exit()

if len(ups) == 0:
    tmp = A[:]
    A = B[:]
    B = tmp[:]
    ups = []
    downs = []
    eqs = []
    for i in range(3):
        if A[i] < B[i]:
            ups.append((A[i], B[i]))
        elif A[i] == B[i]:
            eqs.append((A[i], B[i]))
        else:
            downs.append((A[i], B[i]))

if len(ups) == 3:
    gold_diff = B[0]-A[0]
    silver_diff = B[1]-A[1]
    cupper_diff = B[2]-A[2]
    for gold in range(N//A[0]+1):
        left = N - gold*A[0]
        for silver in range(left//A[1]+1):
            cupper = (left - silver*A[1])//A[2]
            tmp_ans = (N
                       + gold*gold_diff
                       + silver*silver_diff
                       + cupper*cupper_diff)
            ans = max(ans, tmp_ans)
elif len(ups) == 2:
    gold_diff = ups[0][1]-ups[0][0]
    silver_diff = ups[1][1]-ups[1][0]
    for gold in range(N//ups[0][0]+1):
        left = N - gold*ups[0][0]
        silver = left // ups[1][0]
        tmp_ans = (N
                   + gold*gold_diff
                   + silver*silver_diff)
        ans = max(ans, tmp_ans)
    if len(downs) == 1:
        diff = downs[0][0]-downs[0][1]
        ans += (ans // downs[0][1]) * diff
    print(ans)
elif len(ups) == 1:
    nowN = N + (N//ups[0][0])*(ups[0][1]-ups[0][0])
    if len(downs) == 2:
        gold_diff = downs[0][0]-downs[0][1]
        silver_diff = downs[1][0]-downs[1][1]
        for gold in range(nowN//downs[0][1]+1):
            left = nowN - gold*downs[0][1]
            silver = left // downs[1][1]
            tmp_ans = (nowN
                       + gold*gold_diff
                       + silver*silver_diff)
            ans = max(ans, tmp_ans)
    elif len(downs) == 1:
        ans = nowN + (nowN//downs[0][1])*(downs[0][0]-downs[0][1])
    else:
        ans = nowN
    print(ans)

Submission status: Wrong Answer

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


ups = []
downs = []
eqs = []
for i in range(3):
    if A[i] < B[i]:
        ups.append((A[i], B[i]))
    elif A[i] == B[i]:
        eqs.append((A[i], B[i]))
    else:
        downs.append((A[i], B[i]))
ans = N

if len(eqs) == 3:
    print(N)
    exit()

if len(ups) == 0:
    tmp = A[:]
    A = B[:]
    B = tmp[:]
    ups = []
    downs = []
    eqs = []
    for i in range(3):
        if A[i] < B[i]:
            ups.append((A[i], B[i]))
        elif A[i] == B[i]:
            eqs.append((A[i], B[i]))
        else:
            downs.append((A[i], B[i]))

if len(ups) == 3:
    gold_diff = B[0]-A[0]
    silver_diff = B[1]-A[1]
    cupper_diff = B[2]-A[2]
    for gold in range(N//A[0]+1):
        left = N - gold*A[0]
        for silver in range(left//A[1]+1):
            cupper = (left - silver*A[1])//A[2]
            tmp_ans = (N
                       + gold*gold_diff
                       + silver*silver_diff
                       + cupper*cupper_diff)
            ans = max(ans, tmp_ans)
elif len(ups) == 2:
    gold_diff = ups[0][1]-ups[0][0]
    silver_diff = ups[1][1]-ups[1][0]
    for gold in range(N//ups[0][0]+1):
        left = N - gold*ups[0][0]
        silver = left // ups[1][0]
        tmp_ans = (N
                   + gold*gold_diff
                   + silver*silver_diff)
        print(tmp_ans)
        ans = max(ans, tmp_ans)
    if len(downs) == 1:
        diff = downs[0][0]-downs[0][1]
        ans += (ans // downs[0][1]) * diff
    print(ans)
elif len(ups) == 1:
    nowN = N + (N//ups[0][0])*(ups[0][1]-ups[0][0])
    if len(downs) == 2:
        gold_diff = downs[0][0]-downs[0][1]
        silver_diff = downs[1][0]-downs[1][1]
        for gold in range(nowN//downs[0][1]+1):
            left = nowN - gold*downs[0][1]
            silver = left // downs[1][1]
            tmp_ans = (nowN
                       + gold*gold_diff
                       + silver*silver_diff)
            ans = max(ans, tmp_ans)
    elif len(downs) == 1:
        ans = nowN + (nowN//downs[0][1])*(downs[0][0]-downs[0][1])
    else:
        ans = nowN
    print(ans)

Submission status: Wrong Answer

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


ups = []
downs = []
eqs = []
for i in range(3):
    if A[i] < B[i]:
        ups.append((A[i], B[i]))
    elif A[i] == B[i]:
        eqs.append((A[i], B[i]))
    else:
        downs.append((A[i], B[i]))
ans = N

if len(eqs) == 3:
    print(N)
    exit()

if len(ups) == 0:
    tmp = A
    A = B
    B = tmp
    tmp = ups
    ups = downs
    downs = tmp

if len(ups) == 3:
    gold_diff = B[0]-A[0]
    silver_diff = B[1]-A[1]
    cupper_diff = B[2]-A[2]
    for gold in range(N//A[0]+1):
        left = N - gold*A[0]
        for silver in range(left//A[1]+1):
            cupper = (left - silver*A[1])//A[2]
            tmp_ans = (N
                       + gold*gold_diff
                       + silver*silver_diff
                       + cupper*cupper_diff)
            ans = max(ans, tmp_ans)
    print(ans)
elif len(ups) == 2:
    gold_diff = ups[0][1]-ups[0][0]
    silver_diff = ups[1][1]-ups[1][0]
    for gold in range(N//ups[0][0]+1):
        left = N - gold*ups[0][0]
        silver = left // ups[1][0]
        tmp_ans = (N
                   + gold*gold_diff
                   + silver*silver_diff)
        ans = max(ans, tmp_ans)
    if len(downs) == 1:
        diff = downs[0][0]-downs[0][1]
        ans += (ans // downs[0][1]) * diff
    print(ans)
elif len(ups) == 1:
    nowN = N + (N//ups[0][0])*(ups[0][1]-ups[0][0])
    if len(downs) == 2:
        gold_diff = downs[0][0]-downs[0][1]
        silver_diff = downs[1][0]-downs[1][1]
        for gold in range(nowN//downs[0][1]+1):
            left = nowN - gold*downs[0][1]
            silver = left // downs[1][1]
            tmp_ans = (nowN
                       + gold*gold_diff
                       + silver*silver_diff)
            ans = max(ans, tmp_ans)
    elif len(downs) == 1:
        ans = nowN + (nowN//downs[0][1])*(downs[0][0]-downs[0][1])
    else:
        ans = nowN
    print(ans)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
import time

N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

ret = 0

def search1() :
    n = N
    for g in range(n // A[0] + 1) :
        n -= g*A[0]
        for s in range(n // A[1] + 1) :
            n -= s*A[1]
            for b in range(n // A[2] + 1) :
                n -= b*A[2]
                search2(n, g, s, b)
                n += b*A[2]
            n += s*A[1]
        n += g*A[0]

def search2(n, g1, s1, b1) :
    global ret
    for g2 in range(-g1, n // B[0] + 1) :
        n -= g2 * B[0]
        for s2 in range(-s1, n // B[1] + 1) :
            n -= s2 * B[1]
            for b2 in range(-b1, n // B[2] + 1) :
                n -= b2 * B[2]
                ret = max(ret, n + (g1+g2)*A[0] + (s1+s2) * A[1] + (b1+b2) * A[2])
                n += b2 * B[2]
            n += s2 * B[1]
        n += g2 * B[0]

search1()

print(ret)

Submission status: Wrong Answer

Solution code:
import time

N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

ret = 0

def search1() :
    for g in range(N // A[0] + 1) :
        for s in range((N - g*A[0]) // A[1] + 1) :
            for b in range((N - g*A[0] - s*A[1]) // A[2] + 1) :
                search2(g, s, b)

def search2(g1, s1, b1) :
    global ret
    n = N - (g1*A[0] + s1*A[1] + b1*A[2])
    for g2 in range(-g1, n // B[0] + 1) :
        for s2 in range(-s1, (n - g2*B[0]) // B[1] + 1) :
            for b2 in range(-b1, (n - g2*B[0] - s2*B[1]) // B[2] + 1) :
                # print(g1, s1, b1, g2, s2, b2, '|', (g1+g2)*A[0] + (s1+s2) * A[1] + (b1+b2) * A[2])
                # time.sleep(0.2)
                ret = max(ret, n - g2*B[0] - s2*B[1] - b2*B[2] + (g1+g2)*A[0] + (s1+s2) * A[1] + (b1+b2) * A[2])

search1()

print(ret)

Submission status: Wrong Answer

Solution code:
import time

N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

ret = 0

def search1() :
    for g in range(N // A[0] + 1) :
        for s in range((N - g*A[0]) // A[1] + 1) :
            for b in range((N - g*A[0] - s*A[1]) // A[2] + 1) :
                search2(g, s, b)

def search2(g1, s1, b1) :
    global ret
    n = N - (g1*A[0] + s1*A[1] + b1*A[2])
    for g2 in range(-g1, n // B[0] + 1) :
        for s2 in range(-s1, (n - g2*B[0]) // B[1] + 1) :
            for b2 in range(-b1, (n - g2*B[0] - s2*B[1]) // B[2] + 1) :
                ret = max(ret, (g1+g2)*A[0] + (s1+s2) * A[1] + (b1+b2) * A[2])

search1()

print(ret)

Submission status: Wrong Answer

Solution code:
from functools import reduce

# 入力
N = int(input())
gA, sA, bA = map(int, input().split())
gB, sB, bB = map(int, input().split())

A = (gA, sA, bA)
B = (gB, sB, bB)


# 各貴金属について、買う／売る可能性のある取引所は一意に定まることを用いて
# 全探索により解を求める
def f(n, r):
    R = len(r)
    return (
        n if R == 0 else
        n + (r[-1][1] - r[-1][0]) * (n // r[-1][0]) if R == 1 else
        reduce(
            max,
            (
                n + (r[0][1] - r[0][0]) * x +
                (r[-1][1] - r[-1][0]) * ((n - r[0][0] * x) // r[-1][0])
                for x in range(n // r[0][0] + 1)
            ),
            n
        ) if R == 2 else
        reduce(
            max,
            (
                n + (r[0][1] - r[0][0]) * x + (r[1][1] - r[1][0]) * y +
                (r[-1][1] - r[-1][0]) *
                ((n - r[0][0] * x - r[1][0] * y) // r[-1][0])
                for x in range(n // r[0][0] + 1)
                for y in range((n - r[0][0]) // r[1][0] + 1)
            ),
            n
        )
    )


ans = f(
    f(
        N,
        [
            (a, b)
            for a, b in zip(A, B)
            if a < b
        ]
    ),
    [
        (b, a)
        for b, a in zip(B, A)
        if a > b
    ]
)

# 出力
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import math

def main():
    n = int(input())
    gsb_a = list(map(int, input().split(' ')))
    gsb_b = list(map(int, input().split(' ')))

    ## gsbをi番目まで利用して，j個のどんぐりを消費するときの価値の最大値 dpa[i,j]
    dpa = np.zeros([len(gsb_a)+1,n+1], dtype=int)
    for i in range(len(dpa[0])):
        dpa[0,i] = i
    for i in range(len(gsb_a)):
        for j in range(n+1):
            dpa[i+1,j] = dpa[i,j]
            if j >= gsb_a[i]:
                dpa[i+1,j] = max(dpa[i+1,j-gsb_a[i]]+gsb_b[i], dpa[i+1,j])

    m = max(n, dpa[len(gsb_a),n])

    dpb = np.zeros([len(gsb_b)+1,m+1], dtype=int)
    for i in range(len(dpb[0])):
        dpb[0,i] = i
    for i in range(len(gsb_b)):
        for j in range(m+1):
            dpb[i+1,j] = dpb[i,j]
            if j >= gsb_b[i]:
                dpb[i+1,j] = max(dpb[i+1,j-gsb_b[i]]+gsb_a[i], dpb[i+1,j])

    print(dpa, dpb)
    print(max(n, m, dpb[len(gsb_b),m]))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np
import math

def main():
    n = int(input())
    gsb_a = list(map(int, input().split(' ')))
    gsb_b = list(map(int, input().split(' ')))

    ## gsbをi番目まで利用して，j個のどんぐりを消費するときの価値の最大値 dpa[i,j]
    dpa = np.zeros([len(gsb_a)+1,n+1], dtype=int)
    for i in range(len(gsb_a)):
        for j in range(n+1):
            if j >= gsb_a[i]:
                dpa[i+1,j] = max(dpa[i+1,j-gsb_a[i]]+gsb_b[i], dpa[i,j]+1)
            else:
                dpa[i+1,j] = dpa[i,j]

    m = max(n, dpa[len(gsb_a),n])

    dpb = np.zeros([len(gsb_b)+1,m+1], dtype=int)
    for i in range(len(gsb_b)):
        for j in range(m+1):
            if j >= gsb_b[i]:
                dpb[i+1,j] = max(dpb[i+1,j-gsb_b[i]]+gsb_a[i], dpb[i,j]+1)
            else:
                dpb[i+1,j] = dpb[i,j]

    ## print(dpa, dpb)
    print(max(n, m, dpb[len(gsb_b),m]))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from itertools import product

# 入力
N = int(input())
gA, sA, bA = map(int, input().split())
gB, sB, bB = map(int, input().split())

A = (gA, sA, bA)
B = (gB, sB, bB)


# 各貴金属について、買う／売る可能性のある取引所は一意に定まることを用いて
# 全探索により解を求める
def f(n, r):
    R = len(r)
    return (
        n if R == 0 else
        n + (r[-1][1] - r[-1][0]) * n // r[-1][0] if R == 1 else
        max(
            n + (r[0][1] - r[0][0]) * x +
            (r[-1][1] - r[-1][0]) * (n - r[0][0] * x) // r[-1][0]
            for x in range(n // r[0][0] + 1)
        ) if R == 2 else
        max(
            n + (r[0][1] - r[0][0]) * x + (r[1][1] - r[1][0]) * y +
            (r[-1][1] - r[-1][0]) * (n - r[0][0] * x - r[1][0] * y) // r[-1][0]
            for x in range(n // r[0][0] + 1)
            for y in range((n - x * r[0][0]) // r[1][0] + 1)
        )
    )


ans = f(
    f(
        N,
        [
            (a, b)
            for a, b in zip(A, B)
            if a < b
        ]
    ),
    [
        (b, a)
        for b, a in zip(B, A)
        if a > b
    ]
)

# 出力
print(ans)

Submission status: Wrong Answer

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

def change(a,list_):
  rate=[0]*3
  rate[0]=[0,list_[1][0]/list_[0][0]]
  rate[1]=[1,list_[1][1]/list_[0][1]]
  rate[2]=[2,list_[1][2]/list_[0][2]]
  rate=sorted(rate,key=lambda x:x[1],reverse=True)
  
  result=0
  if rate[0][1]>1:
    gold=a//list_[0][rate[0][0]]
    a-=gold*list_[0][rate[0][0]]
    result+=gold*list_[1][rate[0][0]]
  
  if rate[1][1]>1:
    gold=a//list_[0][rate[1][0]]
    a-=gold*list_[0][rate[1][0]]
    result+=gold*list_[1][rate[1][0]]
  
  if rate[2][1]>1:
    gold=a//list_[0][rate[2][0]]
    a-=gold*list_[0][rate[2][0]]
    result+=gold*list_[1][rate[2][0]]
  
  return result+a

n=change(n,li)
a=li[0]
li[0]=li[1]
li[1]=a
n=change(n,li)
print(n)

Submission status: Wrong Answer

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

def change(a,list_):
  rate=[0]*3
  rate[0]=[0,list_[1][0]/list_[0][0]]
  rate[1]=[1,list_[1][1]/list_[0][1]]
  rate[2]=[2,list_[1][2]/list_[0][2]]
  rate=sorted(rate,key=lambda x:x[1],reverse=True)
  
  result=0
  gold=a//list_[0][rate[0][0]]
  a-=gold*list_[0][rate[0][0]]
  result+=gold*list_[1][rate[0][0]]
  
  gold=a//list_[0][rate[1][0]]
  a-=gold*list_[0][rate[1][0]]
  result+=gold*list_[1][rate[1][0]]
  
  gold=a//list_[0][rate[2][0]]
  a-=gold*list_[0][rate[2][0]]
  result+=gold*list_[1][rate[2][0]]
  
  if result<n:
    return n
  return result+a

n=change(n,li)
a=li[0]
li[0]=li[1]
li[1]=a
n=change(n,li)
print(n)

Submission status: Wrong Answer

Solution code:
import numpy as np
import math

def main():
    n = int(input())
    gsb_a = list(map(int, input().split(' ')))
    gsb_b = list(map(int, input().split(' ')))

    ## gsbをi番目まで利用して，j個のどんぐりを消費するときの価値の最大値 dpa[i,j]
    dpa = np.zeros([len(gsb_a)+1,n+1], dtype=int)
    for i in range(len(gsb_a)):
        for j in range(n+1):
            dpa[i+1,j] = dpa[i,j]
            if j >= gsb_a[i]:
                dpa[i+1,j] = max(dpa[i+1,j-gsb_a[i]]+gsb_b[i], dpa[i+1,j]+1)

    m = max(n, dpa[len(gsb_a),n])

    dpb = np.zeros([len(gsb_b)+1,m+1], dtype=int)
    for i in range(len(gsb_b)):
        for j in range(m+1):
            dpb[i+1,j] = dpb[i,j]
            if j >= gsb_b[i]:
                dpb[i+1,j] = max(dpb[i+1,j-gsb_b[i]]+gsb_a[i], dpb[i+1,j]+1)

    ## print(dpa, dpb)
    print(max(n, m, dpb[len(gsb_b),m]))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

def change(a,list_):
  rate=[0]*3
  rate[0]=[0,list_[1][0]/list_[0][0]]
  rate[1]=[1,list_[1][1]/list_[0][1]]
  rate[2]=[2,list_[1][2]/list_[0][2]]
  rate=sorted(rate,key=lambda x:x[1],reverse=True)
  
  result=0
  gold=a//list_[0][rate[0][0]]
  a-=gold*list_[0][rate[0][0]]
  result+=gold*list_[1][rate[0][0]]
  
  gold=a//list_[0][rate[1][0]]
  a-=gold*list_[0][rate[1][0]]
  result+=gold*list_[1][rate[1][0]]
  
  gold=a//list_[0][rate[2][0]]
  a-=gold*list_[0][rate[2][0]]
  result+=gold*list_[1][rate[2][0]]
  
  if result<n:
    return n
  return result

n=change(n,li)
a=li[0]
li[0]=li[1]
li[1]=a
n=change(n,li)
print(n)

Submission status: Wrong Answer

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

def change(a,list_):
  rate=[0]*3
  rate[0]=[0,list_[1][0]//list_[0][0]]
  rate[1]=[1,list_[1][1]//list_[0][1]]
  rate[2]=[2,list_[1][2]//list_[0][2]]
  rate=sorted(rate,key=lambda x:x[1],reverse=True)
  
  result=0
  gold=a//list_[0][rate[0][0]]
  a-=gold*list_[0][rate[0][0]]
  result+=gold*list_[1][rate[0][0]]
  
  gold=a//list_[0][rate[1][0]]
  a-=gold*list_[0][rate[1][0]]
  result+=gold*list_[1][rate[1][0]]
  
  gold=a//list_[0][rate[2][0]]
  a-=gold*list_[0][rate[2][0]]
  result+=gold*list_[1][rate[2][0]]
  
  if result<n:
    return n
  return result

n=change(n,li)
a=li[0]
li[0]=li[1]
li[1]=a
n=change(n,li)
print(n)

Submission status: Wrong Answer

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

def change(a,list_):
  rate=[0]*3
  rate[0]=[0,list_[1][0]//list_[0][0]]
  rate[1]=[1,list_[1][1]//list_[0][1]]
  rate[2]=[2,list_[1][2]//list_[0][2]]
  rate=sorted(rate,key=lambda x:x[1],reverse=True)
  
  result=0
  gold=a//list_[0][rate[0][0]]
  a-=gold*list_[0][rate[0][0]]
  result+=gold*list_[1][rate[0][0]]
  
  gold=a//list_[0][rate[1][0]]
  a-=gold*list_[0][rate[1][0]]
  result+=gold*list_[1][rate[1][0]]
  
  gold=a//list_[0][rate[2][0]]
  a-=gold*list_[0][rate[2][0]]
  result+=gold*list_[1][rate[2][0]]
  
  return result

n=change(n,li)
a=li[0]
li[0]=li[1]
li[1]=a
n=change(n,li)
print(n)

Submission status: Wrong Answer

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


def count(n, a, b):
    a0, a1, a2 = a
    b0, b1, b2 = b
    dp = [0] * (n + 1)
    for i in range(n + 1):
        c0 = i - a0 + b0 if i - a0 >= 0 else i
        c1 = i - a1 + b1 if i - a1 >= 0 else i
        c2 = i - a2 + b2 if i - a2 >= 0 else i
        dp[i] = max([i, c0, c1, c2])
    return dp[n]


print(count(count(N, A, B), B, A))

Submission status: Wrong Answer

Solution code:
## A→Bでどんぐりが最大になるように調整し、
## B→Aでどんぐりが最大になるように調整する。
## 各遷移では、どんぐりの数が増えるような金属種しか選ばない

## 重さと価値の情報を持つN個の荷物を重さがW以下になるように詰め込む時の価値の和を最大化
def knapsack(N,W,weight,value):
  ## 初期化
  inf=float("inf")
  dp=[[-inf for i in range(W+1)] for j in range(N+1)]
  for i in range(W+1): dp[0][i]=0
  ## DP
  for i in range(N):
    for w in range(W+1):
      if weight[i] <= w: ## dp[i][w-weight[i]の状態にi番目の荷物が入る可能性がある
        dp[i+1][w] = max(dp[i][w], dp[i][w-weight[i]]+value[i])
      else: ## 入る可能性はない
        dp[i+1][w] = dp[i][w]
  return dp[N][W]

## 同じ荷物を重複を許して選んで良い場合のナップサック
## dp[i][j]: i種類の荷物を用いて大きさjのカバンに入れる場合の最大価値     
def knapsack_repeated(N,W,weight,value):
  ## 初期化
  inf=float("inf")
  dp = [[-inf for i in range(W+1)] for j in range(N+1)]
  for i in range(W+1): dp[0][i] = 0
  ## DP
  for i in range(N): 
    for w in range(W+1): 
      if weight[i] <= w: ## dp[i][w-weight[i]の状態にi番目の荷物が入る可能性がある
        dp[i+1][w] = max(dp[i][w], dp[i][w-weight[i]]+value[i], dp[i+1][w-weight[i]]+value[i])
      else: ## 入る可能性はない
        dp[i+1][w] = dp[i][w]
  print(dp)
  return dp[N][W]

## ナップサックでの重さ→pre_donguriとして計算
def DivCon2D(pre_don, exchange_pre, exchange_post):
  lst_pre = []
  lst_profit = []
  for i in range(3): ## 得になる交換だけを考える
    if exchange_pre[i] < exchange_post[i]:
      lst_pre.append(exchange_pre[i])
      lst_profit.append(exchange_post[i] - exchange_pre[i])
  if len(lst_pre) == 0: return pre_don
  profit = knapsack_repeated(len(lst_pre), pre_don, lst_pre, lst_profit)
  #print('aaa!!!')
  return pre_don + profit
  

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

don_AB = int(DivCon2D(N, (ga, sa, ba), (gb, sb, bb)))
don_BA = int(DivCon2D(don_AB, (gb, sb, bb), (ga, sa, ba)))
print(don_BA)

Submission status: Wrong Answer

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

result=0
max_=[max(li[0][i],li[1][i]) for i in range(3)]
min_=[min(li[0][i],li[1][i]) for i in range(3)]
para=[[i,max_[i]//min_[i]] for i in range(3)]
para=sorted(para,key=lambda x:x[1],reverse=True)

gold=n//min_[para[0][0]]
n-=gold*min_[para[0][0]]
result+=gold*max_[para[0][0]]

gold=n//min_[para[1][0]]
n-=gold*min_[para[1][0]]
result+=gold*max_[para[1][0]]

gold=n//min_[para[2][0]]
n-=gold*min_[para[2][0]]
result+=gold*max_[para[2][0]]

print(result)

Submission status: Wrong Answer

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

result=0
max_=[max(li[0][i],li[1][i]) for i in range(3)]
min_=[min(li[0][i],li[1][i]) for i in range(3)]
para=[[i,max_[i]//min_[i]] for i in range(3)]
para=sorted(para,key=lambda x:x[1],reverse=True)

print(para)

gold=n//min_[para[0][0]]
n-=gold*min_[para[0][0]]
result+=gold*max_[para[0][0]]

gold=n//min_[para[1][0]]
n-=gold*min_[para[1][0]]
result+=gold*max_[para[1][0]]

gold=n//min_[para[2][0]]
n-=gold*min_[para[2][0]]
result+=gold*max_[para[2][0]]

print(result)

Submission status: Wrong Answer

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

result=0
max_=[max(li[0][i],li[1][i]) for i in range(3)]
min_=[min(li[0][i],li[1][i]) for i in range(3)]
para=[[i,max_[i]//min_[i]] for i in range(3)]
para=sorted(para,key=lambda x:x[1],reverse=True)

gold=n//min_[para[0][0]]
n-=gold
result+=gold*para[0][1]

gold=n//min_[para[1][0]]
n-=gold
result+=gold*para[1][1]

gold=n//min_[para[2][0]]
n-=gold
result+=gold*para[2][1]

print(result)

Submission status: Wrong Answer

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

DP = [0] * (n + 5001)
DP[0] = n

for i in range(n):
    for j in range(3):
        DP[i + A[j]] = max(DP[i + A[j]], DP[i] + B[j] - A[j])

ans1 = max(DP[:n + 1])

DP = [0] * (ans1 + 5001)
DP[0] = ans1

for i in range(ans1):
    DP[i + B[j]] = max(DP[i + B[j]], max(DP[i] + A[j] - B[j] for j in range(3)))

print(max(DP[:ans1 + 1]))

Submission status: Wrong Answer

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

ans2 = n

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

DP = [0] * (ans1 + 5001)
DP[0] = ans1

for i in range(ans1):
    for j in range(3):
        DP[i + B[j]] = max(DP[i + B[j]], DP[i] + A[j] - B[j])

print(max(DP[:ans1 + 1]))

Submission status: Wrong Answer

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

ans2 = n

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

DP = [0] * (ans1 + 5001)
DP[0] = ans1

for i in range(ans1):
    for j in range(3):
        DP[i + B[j]] = max(DP[i + B[j]], DP[i] + A[j] - B[j])

print(max(DP))

Submission status: Wrong Answer

Solution code:
import numpy as np
import math

def main():
    n = int(input())
    gsb_a = list(map(int, input().split(' ')))
    gsb_b = list(map(int, input().split(' ')))

    ## gsbをi番目まで利用して，j個のどんぐりを消費するときの価値の最大値 dpa[i,j]
    dpa = np.zeros([len(gsb_a)+1,n+1], dtype=int)
    for i in range(len(gsb_a)):
        for j in range(n+1):
            dpa[i+1,j] = dpa[i,j]
            if j >= gsb_a[i]:
                dpa[i+1,j] = max(dpa[i+1,j-gsb_a[i]]+gsb_b[i], dpa[i+1,j])

    m = max(n, dpa[len(gsb_a),n])

    dpb = np.zeros([len(gsb_b)+1,m+1], dtype=int)
    for i in range(len(gsb_b)):
        for j in range(m+1):
            dpb[i+1,j] = dpb[i,j]
            if j >= gsb_b[i]:
                dpb[i+1,j] = max(dpb[i+1,j-gsb_b[i]]+gsb_a[i], dpb[i+1,j])

    # print(dpa, dpb)
    print(max(n, m, dpb[len(gsb_b),m]))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)

def main():
    n = I()
    a = LI()
    b = LI()
    if sum(a) > sum(b):
        a,b = b,a
    c = sorted([(ai,bi) for ai,bi in zip(a,b)], key=lambda x: x[0]-x[1])

    if c[2][0] < c[2][1]:
        m = n
        for i in range(0,n+1,a[0]):
            k = n - i
            u = i // a[0] * b[0]
            for j in range(0,k+1,a[1]):
                l = k - j
                t = u + j // a[1] * b[1] + l % a[2] + l // a[2] * b[2]
                if m < t:
                    m = t
        return m

    m = n
    if c[1][0] < c[1][1]:
        for i in range(0,n+1,c[1][0]):
            k = n - i
            t = i // c[1][0] * c[1][1]
            t += k % c[0][0]
            t += k // c[0][0] * c[0][1]
            if m < t:
                m = t
    else:
        t = n % c[0][0]
        t += n // c[0][0] * c[0][1]
        if m < t:
            m = t

    r = m
    if c[1][0] > c[1][1]:
        for i in range(0,m+1,c[1][1]):
            k = m - i
            t = i // c[1][1] * c[1][0]
            t += k % c[2][1]
            t += k // c[2][1] * c[2][0]
            if r < t:
                r = t
    else:
        t = m % c[2][1]
        t += m // c[2][1] * c[2][0]
        if r < t:
            r = t

    return r


print(main())

Submission status: Wrong Answer

Solution code:
import numpy as np
import math

def main():
    n = int(input())
    gsb_a = list(map(int, input().split(' ')))
    gsb_b = list(map(int, input().split(' ')))

    ## gsbをi番目まで利用して，j個のどんぐりを消費するときの価値の最大値 dpa[i,j]
    dpa = np.zeros([len(gsb_a)+1,n+1], dtype=int)
    for i in range(len(gsb_a)):
        for j in range(n+1):
            dpa[i+1,j] = dpa[i,j]
            if j >= gsb_a[i]:
                dpa[i+1,j] = max(dpa[i+1,j-gsb_a[i]]+gsb_b[i], dpa[i+1,j])

    m = dpa[len(gsb_a),n]
    if m < n: m = n

    dpb = np.zeros([len(gsb_b)+1,m+1], dtype=int)
    for i in range(len(gsb_b)):
        for j in range(m+1):
            dpb[i+1,j] = dpb[i,j]
            if j >= gsb_b[i]:
                dpb[i+1,j] = max(dpb[i+1,j-gsb_b[i]]+gsb_a[i], dpb[i+1,j])

    # print(dpa, dpb)
    print(dpb[len(gsb_b),m])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-



N = int(input())
gsb = [[None,None] for _ in range(3)]
g1,s1,b1 = map(int, input().split())
g2,s2,b2 = map(int, input().split())
gsb = [sorted([g1,g2]),sorted([s1,s2]),sorted([b1,b2])]
gsb = sorted(gsb,key=lambda x:x[0]-x[1])

## 下位2パターンの最適解
ans = 0
for d1 in range(N//gsb[0][0]+1):
  for d2 in range((N-d1)//gsb[1][0]+1):
    d3 = (N-d1-d2)//gsb[2][0] * gsb[2][0]
    num = d1*gsb[0][1] + d2*gsb[1][1] + d3*gsb[2][1] + (N-d1-d2-d3)
    ans = max(ans,num)
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-



N = int(input())
gsb = [[None,None] for _ in range(3)]
g1,s1,b1 = map(int, input().split())
g2,s2,b2 = map(int, input().split())
gsb = [sorted([g1,g2]),sorted([s1,s2]),sorted([b1,b2])]
gsb = sorted(gsb,key=lambda x:x[0]-x[1])

## 下位2パターンの最適解
pat1 = [0 for _ in range(N+1)]
for d1 in range(N+1):
  for d2 in range(d1+1):
    ans = (N-d1)//gsb[0][0] * gsb[0][1]
    ans += (d1-d2)//gsb[1][0] * gsb[1][1]
    ans += d2//gsb[2][0] * gsb[2][1]
    pat1[d1] = max(pat1[d1],ans)
print(max(pat1))