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:
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]

    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:
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]:
        return n
        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[0][0]):
            k = n - i
            t = i // c[0][0] * c[0][1]
            t += k % c[1][0]
            t += k // c[1][0] * c[1][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 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
            tm = 0
            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[0][0]):
            k = n - i
            t = i // c[0][0] * c[0][1]
            t += k % c[1][0]
            t += k // c[1][0] * c[1][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:
N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
def solve(N, P):
    V = []; W = []
    for w, v in P:
        if w < v:
            V.append(v); W.append(w)
    if len(V) == 1:
        v = V[0]; w = W[0]
        k = N // w
        return N - k*w + k*v
    if len(V) == 2:
        w1, w2 = W
        M = w1 * w2
        dp = [i for i in range(M+1)]
        for v, w in zip(V, W):
            if v <= w:
                continue
            for k in range(w, M+1):
                dp[k] = max(dp[k], dp[k-w] + v)
        ans = 0
        k = N // M
        for i in range(M):
            dp[i+1] = max(dp[i+1], dp[i])
        return dp[M] * k + dp[N - M*k]
    dp = [i for i in range(N+1)]
    for v, w in zip(V, W):
        if v <= w:
            continue
        for k in range(w, N+1):
            dp[k] = max(dp[k], dp[k-w] + v)
    return max(dp)

A = solve(N, [[ga, gb], [sa, sb], [ba, bb]])
B = solve(A, [[gb, ga], [sb, sa], [bb, ba]])
print(B)

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
            tm = 0
            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[0][0]):
            k = n - i
            t = i // c[0][0] * c[0][1]
            t += k % c[1][0]
            t += k // c[1][0] * c[1][1]
            if m < t:
                m = t
    else:
        m = n % c[0][0]
        m += n // c[0][0] * c[0][1]

    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:
        r = m % c[2][1]
        r += m // c[2][1] * c[2][0]

    return r


print(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(n+1):
            k = n - i
            tm = 0
            u = i % a[0] + i // a[0] * b[0]
            for j in range(k):
                l = k - j
                t = u + j % a[1] + 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(n):
            k = n - i
            t = i % c[0][0]
            t += i // c[0][0] * c[0][1]
            t += k % c[1][0]
            t += k // c[1][0] * c[1][1]
            if m < t:
                m = t
    else:
        m = n % c[0][0]
        m += n // c[0][0] * c[0][1]

    r = m
    if c[1][0] > c[1][1]:
        for i in range(m):
            k = m - i
            t = i % c[1][1]
            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:
        r = m % c[2][1]
        r += m // c[2][1] * c[2][0]

    return r


print(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])
    print(c)

    if c[2][0] < c[2][1]:
        m = n
        for i in range(n+1):
            k = n - i
            tm = 0
            u = i % a[0] + i // a[0] * b[0]
            for j in range(k):
                l = k - j
                t = u + j % a[1] + 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(n):
            k = n - i
            t = i % c[0][0]
            t += i // c[0][0] * c[0][1]
            t += k % c[1][0]
            t += k // c[1][0] * c[1][1]
            if m < t:
                m = t
    else:
        m = n % c[0][0]
        m += n // c[0][0] * c[0][1]

    r = m
    if c[1][0] > c[1][1]:
        for i in range(m):
            k = m - i
            t = i % c[1][1]
            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:
        r = m % c[2][1]
        r += m // c[2][1] * c[2][0]

    return r


print(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])
    print(c)

    if c[2][0] < c[2][1]:
        m = n
        for i in range(n+1):
            k = n - i
            tm = 0
            u = i % a[0] + i // a[0] * b[0]
            for j in range(k):
                l = k - j
                t = u + j % a[1] + 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(n):
            k = n - i
            t = i % c[0][0]
            t += i // c[0][0] * c[0][1]
            t += k % c[1][0]
            t += k // c[1][0] * c[1][1]
            if m < t:
                m = t
    else:
        m = n % c[0][0]
        m += n // c[0][0] * c[0][1]

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

    return r


print(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(n+1):
            k = n - i
            tm = 0
            u = i // a[0] * b[0]
            for j in range(k):
                l = k - j
                t = u + j // a[1] * b[1] + 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(n):
            k = n - i
            t = i % c[0][0]
            t += i // c[0][0] * c[0][1]
            t += k % c[1][0]
            t += k // c[1][0] * c[1][1]
            if m < t:
                m = t
    else:
        m = n % c[0][0]
        m += n // c[0][0] * c[0][1]

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

    return r


print(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()

    def f(a,b,n):
        t = n // a
        return n - t + t * b

    m = n
    for i in range(3):
        t = f(a[i],b[i],n)
        if m < t:
            m = t

    r = m
    for i in range(3):
        t = f(b[i],a[i],n)
        if r < t:
            r = t


    return r


print(main())

Submission status: Wrong Answer

Solution code:
def napsack(num,getlis):
    DPlis = [0]
    for i in range(1,num+1):
        nowgetsum =[DPlis[-1]+1]
        for j,k in getlis:
            if i > j:
                nowgetsum.append(DPlis[i-j]+k)
        DPlis.append(max(nowgetsum))
        #print(DPlis)
    return DPlis[-1]

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

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

N2 = N



if len(ABget)==1:
    i,j =ABget[0]
    N2 = N + (N//i)*(j-i)
elif len(ABget)>1:
    N2 = napsack(N,ABget)

print(N2)
if len(BAget)==1:
    i,j = BAget[0]
    N2 += (N2//i)*(j-i)
elif len(BAget)>1:
    N2 = napsack(N2,BAget)
print(N2)

Submission status: Wrong Answer

Solution code:

def napsack(num,getlis):
    DPlis = [0]
    for i in range(1,N+1):
        nowgetsum =[DPlis[-1]+1]
        for j,k in getlis:
            if i > j:
                nowgetsum.append(DPlis[i-j]+k)
        DPlis.append(max(nowgetsum))
        print(DPlis)
    return DPlis[-1]

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

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

N2 = N



if len(ABget)==1:
    i,j =ABget[0]
    N2 = N + (N//i)*(j-i)
elif len(ABget)>1:
    N2 = napsack(N,ABget)

if len(BAget)==1:
    i,j = BAget[0]
    N2 += (N2//i)*(j-i)
elif len(BAget)>1:
    N2 = napsack(N2,BAget)
print(N2)

Submission status: Wrong Answer

Solution code:
def napsack(num,getlis):
    DPlis = [0]
    for i in range(N):
        i += 1
        nowgetsum =[DPlis[i-1]]
        for j,k in getlis:
            if i > j:
                nowgetsum.append(DPlis[i-j]+k)
        DPlis.append(max(nowgetsum))
    return DPlis[-1]

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

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

N2 = N



if len(ABget)==1:
    i,j =ABget[0]
    N2 = N + (N//i)*(j-i)
elif len(ABget)>1:
    N2 = napsack(N,ABget)

if len(BAget)==1:
    i,j = BAget[0]
    N2 += (N2//i)*(j-i)
elif len(BAget)>1:
    N2 = napsack(N2,BAget)
print(N2)

Submission status: Wrong Answer

Solution code:
N=int(input())
gsbA = list(map(int,input().split()))
gsbB = list(map(int,input().split()))
ans = N
gsbAB = sorted(((b-a,a,b) for a,b in zip(gsbA,gsbB)),reverse=True)
if gsbAB[2][0]>=0:
    #全部A->B
    gsbAB.sort(key=lambda x:-x[1])
    for i in range((N+1)//gsbAB[0][1]):
        for j in range((N-i*gsbAB[0][1]+1)//gsbAB[1][1]):
            k = (N-i*gsbAB[0][1] - j*gsbAB[1][1])//gsbAB[2][1]
            ans = max(ans,
                     N + gsbAB[0][0]*i + gsbAB[1][0]*j + gsbAB[2][0]*k)
elif gsbAB[0][0]<=0:
    #全部B->A
    gsbAB.sort(key=lambda x:-x[2])
    for i in range((N+1)//gsbAB[0][2]):
        for j in range((N-i*gsbAB[0][2]+1)//gsbAB[1][2]):
            k = (N-i*gsbAB[0][2] - j*gsbAB[1][2])//gsbAB[2][2]
            ans = max(ans,
                     N - gsbAB[0][0]*i - gsbAB[1][0]*j - gsbAB[2][0]*k)
elif gsbAB[1][0]>=0:
    #g,s を A->B, bをB->A
    for i in range((N+1)//gsbAB[0][1]):
        j = (N-i*gsbAB[0][1])//gsbAB[1][1]
        if j<0:break
        ans = max(ans,
                  N + gsbAB[0][0]*i + gsbAB[1][0]*j)
    M = ans
    k = M//gsbAB[2][2]
    ans += max(0,-gsbAB[2][0] * k)
else:
    #gを A->B、s,bをB->A
    i = N//gsbAB[0][1]
    ans += max(0,gsbAB[0][0]*i)
    M=ans
    if gsbAB[1][2]<gsbAB[2][2]:
        gsbAB[1],gsbAB[2] = gsbAB[2],gsbAB[1]
    for j in range((M+1)//gsbAB[1][2]):
        k = (M-j*gsbAB[1][2])//gsbAB[2][2]
        if k<0:break
        ans = max(ans,
                 M - gsbAB[1][0]*j - gsbAB[2][0]*k)
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
gsbA = list(map(int,input().split()))
gsbB = list(map(int,input().split()))
ans = N
gsbAB = sorted(((b-a,a,b) for a,b in zip(gsbA,gsbB)),reverse=True)
if gsbAB[2][0]>=0:
    #全部A->B
    gsbAB.sort(key=lambda x:-x[1])
    for i in range((N+1)//gsbAB[0][1]):
        for j in range((N-i*gsbAB[0][1]+1)//gsbAB[1][1]):
            k = (N-i*gsbAB[0][1] - j*gsbAB[1][1])//gsbAB[2][1]
            ans = max(ans,
                     N + gsbAB[0][0]*i + gsbAB[1][0]*j + gsbAB[2][0]*k)
elif gsbAB[0][0]<=0:
    #全部B->A
    gsbAB.sort(key=lambda x:-x[2])
    for i in range((N+1)//gsbAB[0][2]):
        for j in range((N-i*gsbAB[0][2]+1)//gsbAB[1][2]):
            k = (N-i*gsbAB[0][2] - j*gsbAB[1][2])//gsbAB[2][2]
            ans = max(ans,
                     N - gsbAB[0][0]*i - gsbAB[1][0]*j - gsbAB[2][0]*k)
elif gsbAB[1][0]>=0:
    #g,s を A->B, bをB->A
    for i in range((N+1)//gsbAB[0][1]):
        j = (N-i*gsbAB[0][1])//gsbAB[1][1]
        if j<0:break
        ans = max(ans,
                  N + sum(max(0,gsbAB[l][0])*[i,j][l] for l in range(2)))
    M = ans
    k = M//gsbAB[2][2]
    ans += max(0,-gsbAB[2][0] * k)
else:
    #gを A->B、s,bをB->A
    i = N//gsbAB[0][1]
    ans += max(0,gsbAB[0][0]*i)
    M=ans
    if gsbAB[1][2]<gsbAB[2][2]:
        gsbAB[1],gsbAB[2] = gsbAB[2],gsbAB[1]
    for j in range((M+1)//gsbAB[1][2]):
        k = (M-j*gsbAB[1][2])//gsbAB[2][2]
        if k<0:break
        ans = max(ans,
                 M + sum(max(0,-gsbAB[l][0])*[0,j,k][l] for l in (1,2)))
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
gsbA = list(map(int,input().split()))
gsbB = list(map(int,input().split()))
ans = N
gsbAB = sorted(((b-a,a,b) for a,b in zip(gsbA,gsbB)),reverse=True)
if gsbAB[2][0]>=0:
    #全部A->B
    gsbAB.sort(key=lambda x:-x[1])
    for i in range((N+1)//gsbAB[0][1]):
        for j in range((N-i*gsbAB[0][1]+1)//gsbAB[1][1]):
            k = (N-i*gsbAB[0][1] - j*gsbAB[1][1])//gsbAB[2][1]
            if k<0:break
            ans = max(ans,
                     N + sum(max(0,gsbAB[l][0])*[i,j,k][l] for l in range(3)))
elif gsbAB[0][0]<=0:
    #全部B->A
    gsbAB.sort(key=lambda x:-x[2])
    for i in range((N+1)//gsbAB[0][2]):
        for j in range((N-i*gsbAB[0][2]+1)//gsbAB[1][2]):
            k = (N-i*gsbAB[0][2] - j*gsbAB[1][2])//gsbAB[2][2]
            if k<0:break
            ans = max(ans,
                     N + sum(max(0,-gsbAB[l][0])*[i,j,k][l] for l in range(3)))
elif gsbAB[1][0]>=0:
    #g,s を A->B, bをB->A
    for i in range((N+1)//gsbAB[0][1]):
        j = (N-i*gsbAB[0][1])//gsbAB[1][1]
        if j<0:break
        ans = max(ans,
                  N + sum(max(0,gsbAB[l][0])*[i,j][l] for l in range(2)))
    M = ans
    k = M//gsbAB[2][2]
    ans += max(0,-gsbAB[2][0] * k)
else:
    #gを A->B、s,bをB->A
    i = N//gsbAB[0][1]
    ans += max(0,gsbAB[0][0]*i)
    M=ans
    if gsbAB[1][2]<gsbAB[2][2]:
        gsbAB[1],gsbAB[2] = gsbAB[2],gsbAB[1]
    for j in range((M+1)//gsbAB[1][2]):
        k = (M-j*gsbAB[1][2])//gsbAB[2][2]
        if k<0:break
        ans = max(ans,
                 M + sum(max(0,-gsbAB[l][0])*[0,j,k][l] for l in (1,2)))
print(ans)

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


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


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

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

# 出力
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
gsbA = list(map(int,input().split()))
gsbB = list(map(int,input().split()))
ans = N
gsbAB = sorted(((b-a,a,b) for a,b in zip(gsbA,gsbB)),reverse=True)
if gsbAB[2][0]>=0:
    #全部A->B
    for i in range((N+1)//gsbAB[0][1]):
        for j in range((N-i*gsbAB[0][1]+1)//gsbAB[1][1]):
            k = (N-i*gsbAB[0][1] - j*gsbAB[1][1])//gsbAB[2][1]
            if k<0:break
            ans = max(ans,
                     N + sum(max(0,gsbAB[l][0])*[i,j,k][l] for l in range(3)))
elif gsbAB[0][0]<=0:
    #全部B->A
    for i in range((N+1)//gsbAB[0][2]):
        for j in range((N-i*gsbAB[0][2]+1)//gsbAB[1][2]):
            k = (N-i*gsbAB[0][2] - j*gsbAB[1][2])//gsbAB[2][2]
            if k<0:break
            ans = max(ans,
                     N + sum(max(0,-gsbAB[l][0])*[i,j,k][l] for l in range(3)))
elif gsbAB[1][0]>=0:
    #g,s を A->B, bをB->A
    for i in range((N+1)//gsbAB[0][1]):
        j = (N-i*gsbAB[0][1])//gsbAB[1][1]
        if j<0:break
        ans = max(ans,
                  N + sum(max(0,gsbAB[l][0])*[i,j][l] for l in range(2)))
    M = ans
    k = M//gsbAB[2][2]
    ans += max(0,-gsbAB[2][0] * k)
else:
    #gを A->B、s,bをB->A
    """
    i = N//gsbAB[0][1]
    ans += max(0,gsbAB[0][0]*i)
    M=ans
    if gsbAB[1][2]<gsbAB[2][2]:
        gsbAB[1],gsbAB[2] = gsbAB[2],gsbAB[1]
    for j in range((M+1)//gsbAB[1][2]):
        k = (M-j*gsbAB[1][2])//gsbAB[2][2]
        if k<0:break
        ans = max(ans,
                 M + sum(max(0,-gsbAB[l][0])*[0,j,k][l] for l in (1,2)))
    """
    pass
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
gsbA = list(map(int,input().split()))
gsbB = list(map(int,input().split()))
ans = N
gsbAB = sorted(((b-a,a,b) for a,b in zip(gsbA,gsbB)),reverse=True)
if gsbAB[2][0]>=0:
    #全部A->B
    for i in range((N+1)//gsbAB[0][1]):
        for j in range((N-i*gsbAB[0][1]+1)//gsbAB[1][1]):
            k = (N-i*gsbAB[0][1] - j*gsbAB[1][1])//gsbAB[2][1]
            if k<0:break
            ans = max(ans,
                     N + sum(max(0,gsbAB[l][0])*[i,j,k][l] for l in range(3)))
elif gsbAB[0][0]<=0:
    #全部B->A
    for i in range((N+1)//gsbAB[0][2]):
        for j in range((N-i*gsbAB[0][2]+1)//gsbAB[1][2]):
            k = (N-i*gsbAB[0][2] - j*gsbAB[1][2])//gsbAB[2][2]
            if k<0:break
            ans = max(ans,
                     N + sum(max(0,-gsbAB[l][0])*[i,j,k][l] for l in range(3)))
elif gsbAB[1][0]>=0:
    #g,s を A->B, bをB->A
    for i in range((N+1)//gsbAB[0][1]):
        j = (N-i*gsbAB[0][1])//gsbAB[1][1]
        if j<0:break
        ans = max(ans,
                  N + sum(max(0,gsbAB[l][0])*[i,j][l] for l in range(2)))
    M = ans
    k = M//gsbAB[2][2]
    ans += max(0,-gsbAB[2][0] * k)
else:
    #gを A->B、s,bをB->A
    i = N//gsbAB[0][1]
    ans += max(0,gsbAB[0][0]*i)
    M=ans
    if gsbAB[1][2]<gsbAB[2][2]:
        gsbAB[1],gsbAB[2] = gsbAB[2],gsbAB[1]
    for j in range((M+1)//gsbAB[1][2]):
        k = (M-j*gsbAB[1][2])//gsbAB[2][2]
        if k<0:break
        ans = max(ans,
                 M + sum(max(0,-gsbAB[l][0])*[0,j,k][l] for l in (1,2)))
print(ans)