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:
# -*- coding: utf-8 -*-
from itertools import product
from heapq import heappop, heappush
import numpy as np

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    for C in ((A,B), (B,A)):
        *X, = filter(lambda c: int.__sub__(*c)<0, zip(*C))
        dp = np.zeros((len(X), N+1), dtype=int)
        if X:
            dp[0] = X[0][1]*np.repeat(np.arange(N//X[0][0]+1), X[0][0])[:N+1]
            for i,w in product(range(1,len(X)), range(1,N+1)):
                dp[i][w] = (dp[i-1, w-X[i][0]*np.arange(w//X[i][0]+1)]+X[i][1]*np.arange(w//X[i][0]+1)).max()
            N = dp[-1][N]
    res = N

    return res

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

Submission status: Wrong Answer

Solution code:
def knapsack_123(values,weights,capacity):
    work = [0 for i in range(capacity+1)]
    for v,w in zip(values,weights):
        for c in range(w, capacity+1):
            work[c]=max(work[c],work[c-w]+v)
    return work[-1]
def sol(inp=input):
    n=int(inp())
    prices_a=list(map(int,inp().split()))+[1]
    prices_b=list(map(int,inp().split()))+[1]
    
    valA=knapsack_123(values=prices_b,weights=prices_a,capacity=n)
    print(valA)
    valB=knapsack_123(values=prices_a,weights=prices_b,capacity=valA)
    return '{}'.format(valB)
print(sol())

Submission status: Wrong Answer

Solution code:
N = int(input())
A_lis = list(map(int, input().split()))
B_lis = list(map(int, input().split()))

f = [(B_lis[_] - A_lis[_], A_lis[_], B_lis[_]) for _ in range(3)]

f.sort()
f.reverse()

rest = N
gain = 0

for j in range(3):
  if f[j][0] > 0:
    g = rest // f[j][1]
    rest -= g * f[j][1]
    gain += g * f[j][2]

rest = rest + gain
gain = 0
    
f = [(A_lis[_] - B_lis[_], A_lis[_], B_lis[_]) for _ in range(3)]

f.sort()
f.reverse()

for j in range(3):
  if f[j][0] > 0:
    g = rest // f[j][2]
    rest -= g * f[j][2]
    gain += g * f[j][1]

print(gain+rest)

Submission status: Wrong Answer

Solution code:
N=int(input())
gsbA = list(map(int,input().split()))
gsbB = list(map(int,input().split()))
ans = N
for g in range(N+1):
    for s in range(N-g+1):
        b = N-g-s
        ans =max(ans,N+sum(max(0,gsbB[i]-gsbA[i])*[g,s,b][i] for i in range(3)))
M = ans
for g in range(M+1):
    for s in range(M-g+1):
        b = M-g-s
        ans =max(ans,M+sum(max(0,gsbA[i]-gsbB[i])*[g,s,b][i] for i in range(3)))
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
for g in range(N+1):
    for s in range(N-g+1):
        b = N-g-s
        ans =max(ans,N+sum(max(0,gsbB[i]-gsbA[i])*[g,s,b][i] for i in range(3)))
M = ans
for g in range(M):
    for s in range(M-g):
        b = M-g-s
        ans =max(ans,M+sum(max(0,gsbA[i]-gsbB[i])*[g,s,b][i] for i in range(3)))
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, GSB):
    def f(N, GSB):
        ans = N
        for i in range(N//GSB[0][0]+1):
            M = N - i*GSB[0][0]
            g = i*GSB[1][0]
            for j in range(M//GSB[0][1]+1):
                L = M - j*GSB[0][1]
                s = j*GSB[1][1]
                if GSB[0][2] < GSB[1][2]:
                    b = L//GSB[0][2] * GSB[1][2]
                else:
                    b = L
                ans = max(ans, g+s+b)
        return ans

    ans = f(N, GSB)
    GSB[0], GSB[1] = GSB[1], GSB[0]
    ans = f(ans, GSB)
    return ans


def main():
    N = read_int()
    GSB = [read_int_n() for _ in range(2)]
    print(slv(N, GSB))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    w = list(map(int,input().split()))
    b = list(map(int,input().split()))
    v = [0,0,0]
    for i in range(3):
        v[i] = b[i] - w[i]
    dp = [0] * (n+1)
    for i in range(n):
        p = 0
        q = 0
        r = 0
        if i+1 - w[0] >= 0:
            p = dp[i+1 - w[0]] + v[0]
        if i+1 - w[1] >= 0:
            q = dp[i+1 - w[1]] + v[1]
        if i+1 - w[2] >= 0:
            r = dp[i+1 - w[2]] + v[2]
        dp[i+1] = max(p,q,r,dp[i])
    print(n + dp[-1])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import math


def main():
    N = int(input())
    ga, sa, ba = [int(a) for a in input().split()]
    gb, sb, bb = [int(a) for a in input().split()]

    g1 = gb / ga, "g"
    s1 = sb / sa, "s"
    b1 = bb / ba, "b"

    n = N
    gkoukan = 0
    skoukan = 0
    bkoukan = 0
    for s, t in sorted([g1, s1, b1], key=lambda a: a[0], reverse=True):
        if s < 0:
            continue
        if t == "g":
            gkoukan = n // ga
            n -= gkoukan * ga
        elif t == "s":
            skoukan = n // sa
            n -= skoukan * sa
        elif t == "b":
            bkoukan = n // ba
            n -= bkoukan * ba
    # print(gkoukan, skoukan, bkoukan)
    n += gkoukan * gb
    n += skoukan * sb
    n += bkoukan * bb

    g2 = ga / gb, "g"
    s2 = sa / sb, "s"
    b2 = ba / bb, "b"

    # print(n)

    gkoukan = 0
    skoukan = 0
    bkoukan = 0
    for s, t in sorted([g2, s2, b2], key=lambda a: a[0], reverse=True):
        if s < 0:
            continue
        if t == "g":
            gkoukan = n // gb
            n -= gkoukan * gb
        elif t == "s":
            skoukan = n // sb
            n -= skoukan * sb
        elif t == "b":
            bkoukan = n // bb
            n -= bkoukan * bb
    # print(gkoukan, skoukan, bkoukan)
    n += gkoukan * ga
    n += skoukan * sa
    n += bkoukan * ba
    print(n)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math


def main():
    N = int(input())
    ga, sa, ba = [int(a) for a in input().split()]
    gb, sb, bb = [int(a) for a in input().split()]

    g1 = gb - ga, "g"
    s1 = sb - sa, "s"
    b1 = bb - ba, "b"

    n = N
    gkoukan = 0
    skoukan = 0
    bkoukan = 0
    for s, t in sorted([g1, s1, b1], key=lambda a: a[0], reverse=True):
        if s < 0:
            continue
        if t == "g":
            gkoukan = n // ga
            n -= gkoukan * ga
        elif t == "s":
            skoukan = n // sa
            n -= skoukan * sa
        elif t == "b":
            bkoukan = n // ba
            n -= bkoukan * ba
    # print(gkoukan, skoukan, bkoukan)
    n += gkoukan * gb
    n += skoukan * sb
    n += bkoukan * bb

    g2 = ga - gb, "g"
    s2 = sa - sb, "s"
    b2 = ba - bb, "b"

    gkoukan = 0
    skoukan = 0
    bkoukan = 0
    for s, t in sorted([g2, s2, b2], key=lambda a: a[0], reverse=True):
        if s < 0:
            continue
        if t == "g":
            gkoukan = n // gb
            n -= gkoukan * gb
        elif t == "s":
            skoukan = n // sb
            n -= skoukan * sb
        elif t == "b":
            bkoukan = n // bb
            n -= bkoukan * bb
    # print(gkoukan, skoukan, bkoukan)
    n += gkoukan * gb
    n += skoukan * sb
    n += bkoukan * bb
    print(n)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, GSB):
    def f(N, GSB):
        ans = N
        for i in range(N//GSB[0][0]+1):
            M = N - (i*GSB[0][0])
            g = i*GSB[1][0]
            for j in range(M//GSB[0][1]+1):
                L = M - (j*GSB[0][1])
                s = j*GSB[1][1]
                if GSB[0][2] < GSB[1][2]:
                    b = L//GSB[0][2] * GSB[1][2]
                else:
                    b = L
                ans = max(ans, g+s+b)
        return ans

    ans = f(N, GSB)
    GSB[0], GSB[1] = GSB[1], GSB[0]
    ans = f(ans, GSB)
    return ans


def main():
    N = read_int()
    GSB = [read_int_n() for _ in range(2)]
    print(slv(N, GSB))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, GSB):
    def f(N, GSB):
        ans = N
        for i in range(N//GSB[0][0]+1):
            M = N - (i*GSB[0][0])
            g = i*GSB[1][0]
            for j in range(M//GSB[0][1]+1):
                L = M - (j*GSB[0][1])
                s = j*GSB[1][1]
                if GSB[0][2] < GSB[1][2]:
                    b = L//GSB[0][2] * GSB[1][2]
                else:
                    b = 0
                ans = max(ans, g+s+b)
        return ans

    ans = f(N, GSB)
    GSB[0], GSB[1] = GSB[1], GSB[0]
    ans = f(ans, GSB)
    return ans


def main():
    N = read_int()
    GSB = [read_int_n() for _ in range(2)]
    print(slv(N, GSB))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, GSB):
    def f(N, GSB):
        ans = N
        for i in range(N//GSB[0][0]+1):
            M = N - (i*GSB[0][0])
            g = i*GSB[1][0]
            for j in range(M//GSB[0][1]+1):
                L = M - (j*GSB[1][1])
                s = j*GSB[1][1]
                if GSB[0][2] < GSB[1][2]:
                    b = L//GSB[0][2] * GSB[1][2]
                else:
                    b = 0
                ans = max(ans, g+s+b)
        return ans

    ans = f(N, GSB)
    GSB[0], GSB[1] = GSB[1], GSB[0]
    ans = f(ans, GSB)
    return ans


def main():
    N = read_int()
    GSB = [read_int_n() for _ in range(2)]
    print(slv(N, GSB))


if __name__ == '__main__':
    main()

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:
    bbans = N + (N//ups[0][0])*(ups[0][1]-ups[0][0])
    if len(downs) == 2:
        tmp_ans = bbans
        gold_diff = downs[0][0]-downs[0][1]
        silver_diff = downs[1][0]-downs[1][1]
        for gold in range(bbans//downs[0][1]+1):
            left = bbans - gold*downs[0][1]
            silver = left // downs[1][1]
            tmp_ans = (bbans
                       + gold*gold_diff
                       + silver*silver_diff)
            ans = max(ans, tmp_ans)
    elif len(downs) == 1:
        ans =bbans + (bbans//downs[0][1])*(downs[0][0]-downs[0][1])
    else:
        ans = bbans
    print(ans)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    n,k = LI()
    print(n%k)
    return

#B
def B():
    n = I()
    po = LIR(n)
    if n == 1:
        print(1)
        quit()
    ans = float("inf")
    for i in range(n):
        for j in range(n):
            if i == j:continue
            x,y = po[i]
            s,t = po[j]
            p,q = s-x,t-y
            m = n
            for k in range(n):
                for l in range(n):
                    if (po[k][0]-po[l][0],po[k][1]-po[l][1]) == (p,q):m -= 1
            if m < ans:
                ans = m
    print(ans)
    return

#C
def C():
    n = I()
    a = LI()
    a.sort()
    if a[0] >= 0:
        print(sum(a)-2*a[0])
        for i in range(1,n-1):
            print(a[0],a[i])
            a[0] -= a[i]
        print(a[n-1],a[0])
    else:
        if a[-1] < 0:
            print(2*a[-1]-sum(a))
            for i in range(n-1):
                print(a[-1],a[i])
                a[-1] -= a[i]
        else:
            m = bisect.bisect_left(a,0)
            p = n-m
            ans = -sum(a[:m])+sum(a[m:])
            print(ans)
            if p > m:
                l = a[-1]
                a = a[:-1]
                a = a[::-1]
                p -= 1
                p,m = m,p
                k = a[m]-a[m-1]
                print(a[m],a[m-1])
                for i in range(p-1):
                    print(a[m-i-2],k)
                    k = a[m-i-2]-k
                    print(a[m+i+1],k)
                    k = a[m+i+1]-k
                for i in range(m-p):
                    print(k,a[i])
                    k -= a[i]
                print(l,k)
            else:
                k = a[m]-a[m-1]
                print(a[m],a[m-1])
                for i in range(p-1):
                    print(a[m-i-2],k)
                    k = a[m-i-2]-k
                    print(a[m+i+1],k)
                    k = a[m+i+1]-k
                for i in range(m-p):
                    print(k,a[i])
                    k -= a[i]
    return

#D
def D():
    n = I()
    a = LI()
    b = LI()
    dp = defaultdict(int)
    for w in range(n+1):
        x0 = dp[w-a[0]]+b[0] if w-a[0] >= 0 else w
        x1 = dp[w-a[1]]+b[1] if w-a[1] >= 0 else w
        x2 = dp[w-a[2]]+b[2] if w-a[2] >= 0 else w
        dp[w] = max(x0,x1,x2)
    n = dp[n]
    dp = defaultdict(int)
    for w in range(n+1):
        x0 = dp[w-b[0]]+a[0] if w-b[0] >= 0 else w
        x1 = dp[w-b[1]]+a[1] if w-b[1] >= 0 else w
        x2 = dp[w-b[2]]+a[2] if w-b[2] >= 0 else w
        dp[w] = max(x0,x1,x2)
    print(dp[n])
    return

#E
def E():
    n = I()

    return

#F
def F():
    n = I()

    return

#G
def G():
    n = I()

    return

#H
def H():
    n = I()

    return

#I
def I_():
    n = I()

    return

#J
def J():
    n = I()

    return

#Solve
if __name__ == "__main__":
    D()

Submission status: Wrong Answer

Solution code:
from sys import stdin
import numpy as np

N = int(stdin.readline())
A = [int(x) for x in stdin.readline().rstrip().split()]
B = [int(x) for x in stdin.readline().rstrip().split()]
C = list()
D = list()

for i in range(3):
    C.append(B[i] / A[i])
    D.append(A[i] / B[i])

c = C.index(max(C))
d = D.index(max(D))

sum = N
a1 = 0
b1 = 0
c1 = 0

# A での取引
if C[c] > 0:
    a1 = sum // A[c]
    sum = sum % A[c]

# B での取引
sum = sum + B[c] * a1

if D[d] > 0:
    b1 = sum // B[d]
    sum = sum % B[d]

# Aでの取引
sum = sum + A[d] * b1
print(sum)

Submission status: Wrong Answer

Solution code:
n = int(input())
kane = [0,0,0]
l = [list(map(int, input().split())) for i in range(2)]
u = []
for i in range(3):
    u.append(l[1][i] / l[0][i])
l.append(u)
uu = sorted(u, reverse=True)
if max(u) <= 1:
    print(n)
else :
    kane[u.index(uu[0])] += n // l[0][u.index(uu[0])]
    n = n % l[0][u.index(uu[0])]
    if uu[1] > 1:
        kane[u.index(uu[1])] += n // l[0][u.index(uu[1])]
        n = n % l[0][u.index(uu[1])]
        if uu[2] > 1:
            kane[u.index(uu[2])] += n // l[0][u.index(uu[2])]
            n = n % l[0][u.index(uu[2])]
    n += kane[u.index(max(u))] * l[1][u.index(max(u))]
    n += kane[u.index(uu[1])] * l[1][u.index(uu[1])]
    n += kane[u.index(uu[2])] * l[1][u.index(uu[2])]
    #print(kane)
    #print(l)
    #print(n)
    kane = [0,0,0]
    u = []
    for i in range(3):
        u.append(l[0][i] / l[1][i])
    uu = sorted(u, reverse=True)
    l[0],l[1] = l[1],l[0]
    if uu[0]> 1:
        kane[u.index(uu[0])] += n // l[0][u.index(uu[0])]
        n = n % l[0][u.index(uu[0])]
        if uu[1] > 1:
            kane[u.index(uu[1])] += n // l[0][u.index(uu[1])]
            n = n % l[0][u.index(uu[1])]
            if uu[2] > 1:
                kane[u.index(uu[2])] += n // l[0][u.index(uu[2])]
                n = n % l[0][u.index(uu[2])]
        n += kane[u.index(max(u))] * l[1][u.index(max(u))]
        n += kane[u.index(uu[1])] * l[1][u.index(uu[1])]
        n += kane[u.index(uu[2])] * l[1][u.index(uu[2])]
    print(n)

Submission status: Wrong Answer

Solution code:
def gcd(a,b):
    if b>a:
        a,b=b,a
    while b:
        a,b=b, a%b
    return a

def f(a,b,N):
    l=[]
    for i in range(3):
        if a[i]<b[i]: l.append(i)

    if len(l)==0: return N
    if len(l)==1:
        i=l[0]
        return (N//a[i])*b[i]+N%a[i]
    elif len(l)==2:
        if a[l[0]]>a[l[1]]: x,y=l[0],l[1]
        else: x,y=l[1],l[0]

        lcm=a[x]*a[y]//gcd(a[x],a[y])

        n1,n2=N-N%lcm,N%lcm
        ans1=max(n1,(n1*b[x])//a[x],(n1*b[y])//a[y])

        i=0
        ans2=n2
        while i*a[x]<=n2:
            j=(n2-i*a[x])//a[y]
            ans2=max(ans2, i*b[x]+j*b[y] + (n2-i*a[x]-j*a[y]) )
            i+=1
        return ans1+ans2
        
    else:
        lcm=(a[0]*a[1])//gcd(a[0],a[1])
        lcm=(lcm*a[2])//gcd(lcm,a[2])

        n1,n2=N-N%lcm,N%lcm
        ans1=max(n1,(n1*b[0])//a[0],(n1*b[1])//a[1],(n1*b[2])//a[2])

        i=0
        ans2=n2
        while i*a[0]<=n2:
            j=0
            while  i*a[0]+j*a[1]<=n2:
                k=(n2-i*a[0]-j*a[1])//a[2]
                ans2=max(ans2, i*b[0]+j*b[1]+k*b[2] + (n2-i*a[0]-j*a[1]-k*a[2]) )
                j+=1
            i+=1
        return ans1+ans2

import sys
M=int(input())

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

M=f(A,B,M)
M=f(B,A,M)
print(int(M))

Submission status: Wrong Answer

Solution code:
N = int(input())
A_lis = list(map(int, input().split()))
B_lis = list(map(int, input().split()))

f = [(B_lis[_]/A_lis[_], A_lis[_], B_lis[_]) for _ in range(3)]

f.sort()
f.reverse()

rest = N
gain = 0

for j in range(3):
  if f[j][0] > 1.0:
    g = rest // f[j][1]
    rest -= g * f[j][1]
    gain += g * f[j][2]

rest = rest + gain
gain = 0
    
f = [(A_lis[_]/B_lis[_], A_lis[_], B_lis[_]) for _ in range(3)]

f.sort()
f.reverse()

for j in range(3):
  if f[j][0] > 1.0:
    g = rest // f[j][2]
    rest -= g * f[j][2]
    gain += g * f[j][1]

print(gain+rest)

Submission status: Wrong Answer

Solution code:
def gcd(a,b):
    if b>a:
        a,b=b,a
    while b:
        a,b=b, a%b
    return a

def f(a,b,N):
    l=[]
    for i in range(3):
        if a[i]<b[i]: l.append(i)

    if len(l)==0: return N
    if len(l)==1:
        i=l[0]
        return (N//a[i])*b[i]+N%a[i]
    elif len(l)==2:
        if a[l[0]]>a[l[1]]: x,y=l[0],l[1]
        else: x,y=l[1],l[0]

        lcm=a[x]*a[y]//gcd(a[x],a[y])

        n1,n2=N-N%lcm,N%lcm
        ans1=max((n1*b[x])//a[x],(n1*b[y])//a[y])

        i=0
        ans2=n2
        while i*a[x]<=n2:
            j=(n2-i*a[x])//a[y]
            ans2=max(ans2, i*b[x]+j*b[y]+ (n2-i*a[x]-j*a[y]) )
            i+=1
        return ans1+ans2
        
    else:
        lcm=(a[0]*a[1])//gcd(a[0],a[1])
        lcm=(lcm*a[2])//gcd(a[2],lcm)

        n1,n2=N-N%lcm,N%lcm
        ans1=max( (n1*b[i])//a[i] for i in range(3))

        i=0
        ans2=n2
        while i*a[0]<=n2:
            j=0
            while  i*a[0]+j*a[1]<=n2:
                k=(n2-i*a[0]-j*a[1])//a[2]
                ans2=max(ans2, i*b[0]+j*b[1]+k*b[2] + (n2-i*a[0]-j*a[1]-k*a[2]) )
                j+=1
            i+=1
        return ans1+ans2

import sys
M=int(input())

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

M=f(A,B,M)
M=f(B,A,M)
print(int(M))

Submission status: Wrong Answer

Solution code:
def solve(n, ga, sa, ba, gb, sb, bb):
    gd, sd, bd = gb - ga, sb - sa, bb - ba

    if bd <= 0:
        if sd <= 0:
            if gd <= 0:
                return n
            return n + n // ga * gd
        if gd <= 0:
            return n + n // sa * sd
        return max(n + g * gd + (n - g * ga) // sa * sd for g in range(n // ga + 1))
    if sd <= 0:
        if gd <= 0:
            return n + n // ba * bd
        return max(n + g * gd + (n - g * ga) // ba * bd for g in range(n // ga + 1))
    if gd <= 0:
        return max(n + s * sd + (n - s * sa) // ba * bd for s in range(n // sa + 1))
    (_, ga, gd), (_, sa, sd), (_, ba, bd) = \
        sorted([(gb / ga, -ga, gd), (sb / sa, -sa, sd), (bb / ba, -ba, bd)], reverse=True)
    ga, sa, ba = -ga, -sa, -ba
    sga = 0 if n // ga > 1000 else n // ga // 2
    return max(n + g * gd + s * sd + (n - g * ga - s * sa) // ba * bd
               for g in range(sga, n // ga + 1)
               for s in range((n - g * ga) // sa + 1))


n = int(input())
ga, sa, ba = list(map(int, input().split()))
gb, sb, bb = list(map(int, input().split()))
n2 = solve(n, ga, sa, ba, gb, sb, bb)
n3 = solve(n2, gb, sb, bb, ga, sa, ba)
print(n3)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import numpy as np

N = int(stdin.readline())
A = [int(x) for x in stdin.readline().rstrip().split()]
B = [int(x) for x in stdin.readline().rstrip().split()]
C = list()

for i in range(3):
    C.append(A[i] / B[i])

b = C.index(max(C))
a = C.index(min(C))

sum = N
a1 = 0
b1 = 0
c1 = 0

# A での取引
if B[a] > A[a]:
    a1 = sum * A[a]
    sum = 0
    
# B での取引
sum = sum + B[a] * a1

if A[b] > B[b]:
    b1 = sum * B[b]
    sum = 0

# Aでの取引
sum = sum + A[b] * b1

print(sum)

Submission status: Wrong Answer

Solution code:
N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
dp = [0 for i in range(N+1)]
for i in range(1, N+1):
    koho = max(i, dp[i-1] + 1)
    if gb > ga:
        if i >=  ga:
            koho = max(koho, dp[i-ga] + gb)
    elif ga > gb:
        if i >= gb:
            koho = max(koho, dp[i-gb] + ga)
    if sb > sa:
        if i >= sa:
            koho = max(koho, dp[i-sa] + sb)
    elif sa > sb:
        if i >= sb:
            koho = max(koho, dp[i-sb] + sa)
    if bb > ba:
        if i >= ba:
            koho = max(koho, dp[i-ba] + bb)
    elif ba > bb:
        if i >= bb:
            koho = max(koho, dp[i-bb] + ba)
    dp[i] = koho
print(dp[N])

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
A=tuple(map(int,input().split()))
B=tuple(map(int,input().split()))

def exchange(n,A,B):
    R=[]
    for a,b in zip(A,B):
        if a<b:
            R.append((a,b))
    if len(R)==0:
        return n
    if len(R)==1:
        a,b=R[0]
        return n+n//a*(b-a)
    if len(R)==2:
        a0,b0=R[0]; a1,b1=R[1]
        ans=n
        s=n//a0
        for i in range(s+1):
            p=b0*i
            q=n-a0*i
            r=q//a1
            ans=max(ans,a+q+r*(b1-a1))
        return ans
    a0,b0=R[0]; a1,b1=R[1]; a2,b2=R[2]
    ans=n
    s=n//a0
    for i in range(s+1):
        p=n-a0*i
        t=p//a1
        for j in range(t+1):
            q=p-a1*j
            ans=max(ans,b0*i+b1*j+q+q//a2*(b2-a2))
    return ans

n=exchange(n,A,B)
print(exchange(n,B,A))

Submission status: Wrong Answer

Solution code:
N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
dp = [0 for i in range(N+1)]
for i in range(1, N+1):
    koho = max(i, dp[i-1] + 1)
    if gb > ga:
        if i >=  ga:
            koho = max(koho, dp[i-ga] + gb)
    elif ga > gb:
        if i >= gb:
            koho = max(koho, dp[i-gb] + ga)
    if sb > sa:
        if i >= sa:
            koho = max(koho, dp[i-sa] + sb)
    elif sa > sb:
        if i >= sb:
            koho = max(koho, dp[i-sb] + sa)
    if bb > ba:
        if i >= ba:
            koho = max(koho, dp[i-ba] + bb)
    elif ba > bb:
        if i >= bb:
            koho = max(koho, dp[i-bb] + ba)
    dp[i] = koho
print(dp)

Submission status: Wrong Answer

Solution code:
def getlist():
	return list(map(int, input().split()))

N = int(input())
A = getlist()
B = getlist()


L = [0 for i in range(N + 1)]
l1 = []
l2 = []
for i in range(3):
	if B[i] - A[i] > 0:
		l1.append([A[i], B[i]])
	elif B[i] - A[i] < 0:
		l2.append([B[i], A[i]])

c = 0
n = 0
for i in range(N + 1):
	for j in range(len(l1)):
		x = l1[j][0]
		if i + x <= N:
			if L[i] + l1[j][1] > L[i + x]:
				L[i + x] = L[i] + l1[j][1]
		if L[i] > c:
			c = L[i]
			n = i

N += c - n
c = 0
n = 0
L = [0 for i in range(N + 1)]
for i in range(N + 1):
	for j in range(len(l2)):
		x = l2[j][0]
		y = L[i]
		if i + x <= N:
			z = L[i + x]
			if y + l2[j][1] > z:
				z = y + l2[j][1]
		if y > c:
			c = y
			n = i

N += c - n
print(N)