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:
#!venv/bin/python
 
import math
import numpy as np
import random
import copy
import statistics 
import bisect
import heapq
from collections import defaultdict 
import time
import sys
from pprint import pprint
import itertools

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

dealdA = []
for order in itertools.permutations(range(4)):
    status = [0,0,0,N]
    for type in order:
        if type == 3:
            break
        status[type] += status[3] // A[type]
        status[3] = status[3] % A[type]
    dealdA.append(status)
dealdB = []
for f_status in dealdA:
    for type in range(3):
        f_status[3] += B[type] * f_status[type]
        f_status[type] = 0
    for order in itertools.permutations(range(4)):
        status = f_status.copy()
        for type in order:
            if type == 3:
                break
            status[type] += status[3] // B[type]
            status[3] = status[3] % B[type]
        dealdB.append(status)

count = []
for f_status in dealdB:
    status = f_status
    for type in range(3):
        status[3] += A[type] * status[type]
        status[type] = 0
    count.append(status[3])

print(max(count))

Submission status: Wrong Answer

Solution code:
###############################################################################

from sys import stdout
from bisect import bisect_left as binl
from copy import copy, deepcopy
from collections import defaultdict
import math


mod = 1


def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return tuple(map(int, input_tuple))


def intina():
    return [int(i) for i in input().split()]


def intinl(count):
    return [intin() for _ in range(count)]


def modadd(x, y):
    global mod
    return (x + y) % mod


def modmlt(x, y):
    global mod
    return (x * y) % mod


def mod_inv(x):
    # available only when mod is prime
    global mod
    return pow(x, mod - 2, mod)


def gcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x


def combination(x, y):
    assert(x >= y)

    ret = math.factorial(x)
    ret = ret // (math.factorial(x - y) * math.factorial(y))

    return ret


def get_divisors(x):
    retlist = []
    for i in range(1, int(x**0.5) + 3):
        if x % i == 0:
            retlist.append(i)
            retlist.append(x // i)
    return retlist


def get_factors(x):
    retlist = []
    for i in range(2, int(x**0.5) + 3):
        while x % i == 0:
            retlist.append(i)
            x = x // i
    retlist.append(x)
    return retlist


def make_linklist(xylist):
    linklist = {}
    for a, b in xylist:
        linklist.setdefault(a, [])
        linklist.setdefault(b, [])
        linklist[a].append(b)
        linklist[b].append(a)
    return linklist


def calc_longest_distance(linklist, v=1):
    distance_list = {}
    distance_count = 0
    distance = 0
    vlist_previous = []
    vlist = [v]
    nodecount = len(linklist)

    while distance_count < nodecount:
        vlist_next = []
        for v in vlist:
            distance_list[v] = distance
            distance_count += 1
            vlist_next.extend(linklist[v])
        distance += 1
        vlist_to_del = vlist_previous
        vlist_previous = vlist
        vlist = list(set(vlist_next) - set(vlist_to_del))

    max_distance = -1
    max_v = None
    for v, distance in distance_list.items():
        if distance > max_distance:
            max_distance = distance
            max_v = v

    return (max_distance, max_v)


def calc_tree_diameter(linklist, v=1):
    _, u = calc_longest_distance(linklist, v)
    distance, _ = calc_longest_distance(linklist, u)
    return distance


class UnionFind:

    def __init__(self, n):
        self.parent = [i for i in range(n)]

    def root(self, i):
        if self.parent[i] == i:
            return i
        self.parent[i] = self.root(self.parent[i])
        return self.parent[i]

    def unite(self, i, j):
        rooti = self.root(i)
        rootj = self.root(j)
        if rooti == rootj:
            return
        if rooti < rootj:
            self.parent[rootj] = rooti
        else:
            self.parent[rooti] = rootj

    def same(self, i, j):
        return self.root(i) == self.root(j)


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


def main():
    n = intin()
    gsb_a = intina()
    gsb_b = intina()

    ga = gsb_a[0]
    sa = gsb_a[1]
    ba = gsb_a[2]

    gb = gsb_b[0]
    sb = gsb_b[1]
    bb = gsb_b[2]

    g = s = b = 0

    # a: buy
    g_ratio = gb / ga
    s_ratio = sb / sa
    b_ratio = bb / ba
    ratios = [(g_ratio, 'g'), (s_ratio, 's'), (b_ratio, 'b')]
    ratios.sort(reverse=True)

    for ratio, t in ratios:
        if ratio < 1:
            break
        if t == 'g':
            nn = n
            n -= ga * (nn // ga)
            g += nn // ga
        if t == 's':
            nn = n
            n -= sa * (nn // sa)
            s += nn // sa
        if t == 'b':
            nn = n
            n -= ba * (nn // ba)
            b += nn // ba

    # b: sell
    n += g * gb
    n += s * sb
    n += b * bb
    g = s = b = 0

    # b: buy
    g_ratio = ga / gb
    s_ratio = sa / sb
    b_ratio = ba / bb
    ratios = [(g_ratio, 'g'), (s_ratio, 's'), (b_ratio, 'b')]
    ratios.sort(reverse=True)

    for ratio, t in ratios:
        if ratio < 1:
            break
        if t == 'g':
            nn = n
            n -= gb * (nn // gb)
            g += nn // gb
        if t == 's':
            nn = n
            n -= sb * (nn // sb)
            s += nn // sb
        if t == 'b':
            nn = n
            n -= bb * (nn // bb)
            b += nn // bb

    # a: sell
    n += g * ga
    n += s * sa
    n += b * ba
    g = s = b = 0

    print(n)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

# 最低N個
res = N

for g in range(N // ga + 1):
    rest1 = N - g
    if ga > gb:
        if g > 0:
            break
    for s in range(rest1 // sa + 1):
        rest2 = rest1 - s
        if sa > sb:
            if s > 0:
                break
        for b in range(rest2 // ba + 1):
            if ba > bb:
                if b > 0:
                    break
            rest3 = rest2 - b
            res = max(res, g * gb + s * sb + b * bb + rest3)
            # print(res, g, s, b)

M = res
for g in range(M // gb + 1):
    rest1 = M - g
    if ga < gb:
        if g > 0:
            break
    for s in range(rest1 // sb + 1):
        rest2 = rest1 - s
        if sa < sb:
            if s > 0:
                break
        for b in range(rest2 // bb + 1):
            if ba < bb:
                if b > 0:
                    break
            rest3 = rest2 - b
            res = max(res, g * ga + s * sa + b * ba + rest3)
            # print(res, g, s, b)

print(res)

Submission status: Wrong Answer

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

g_ratio = gb / ga
s_ratio = sb / sa
b_ratio = bb / ba

lst = []
lst.append([0, ga, g_ratio])
lst.append([1, sa, s_ratio])
lst.append([2, ba, b_ratio])

lst2 = sorted(lst, key=lambda x:-x[2])
outs = [0, 0, 0]
n = N
out= 0
for n1 in range(N+1):
    for n2 in range(N-n1+1):
        for n3 in range(N-n1-n2+1):
            nn = [n1, n2, n3]
            for i in range(3):
                #print(lst2[i])
                a0 = lst2[i][1]
                ratio0 = lst2[i][2]
                if ratio0 <= 1:
                    continue
                n0 = nn[i]
                #n0 = (n // a0) * a0
                #print(n0, a0)
                #n -= n0
                #n0s[i] = n0
                outs[i] = int(n0 * ratio0)
            out = max(out, sum(outs) + (N - n1 - n2 - n3))
print(out)

Submission status: Wrong Answer

Solution code:
from itertools import*
from math import*
from collections import*
from heapq import*
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf = float("inf")
mod = 10**9+7
from functools import reduce
import sys
sys.setrecursionlimit(10**7)


N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
AB = [(A[i]/B[i],i) for i in range(3)]
AB.sort()
AB1 = deepcopy(AB)
ans = 0
M = N
for i in range(3):
    ab,j = AB1.pop(0)
    if ab >= 1:
        break
    else:
        ans += (M//A[j])*(B[j])
        M = M % A[j]
        ans += M
M = ans
ans = 0
for i in range(3):
    ab,j = AB.pop()
    if ab < 1:
        break
    else:
        ans += (M//B[j])*(A[j])
        M = M % B[j]
        ans += M
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ea=[]
eb=[]
for i in range(3):
    if(a[i]<b[i]):
        ea.append((a[i],b[i]))
    elif(a[i]>b[i]):
        eb.append((a[i],b[i]))
ea.sort(reverse=True)
eb.sort(reverse=True)
def main(ea,n):
    if(len(ea)==1):
        fa=n%ea[0][0]+n//ea[0][0]*ea[0][1]
    elif(len(ea)==2):
        ga,gb=ea[0]
        ba,bb=ea[1]
        k=n
        fa=n
        for i in range(n+1):
            if(ga*i>n):
                break
            tmp=i*(gb-ga)+k%ba+(k//ba)*bb
            k-=1
            fa=max(tmp,fa)
    elif(len(ea)==3):
        ga,gb=ea[0]
        sa,sb=ea[1]
        ba,bb=ea[2]
        fa=n
        if(ga==sa):
            return main([ea[0],ea[2]],n)
        if(sa==sb):
            return main([ea[0],ea[1]],n)
        for i in range(n+1):
            if(ga*i>n):
                break
            for j in range(n+1):
                if(ga*i+sa*i>n):
                    break
                k=n-i-j
                tmp=i*(gb-ga)+j*(sb-sa)+k%ba+k//ba*bb
                fa=max(tmp,fa)
    else:
      fa=n
    return fa
n=main(ea,n)
n=main(eb,n)
print(n)

Submission status: Wrong Answer

Solution code:
N = int(input())
ga,sa,ba = map(int,input().split())
gb,sb,bb = map(int,input().split())
A = max([ga-gb,sa-sb,ba-bb])
B = max([gb-ga,sb-sa,bb-ba])
max = N
for a in range(N):
    for b in range(N-a):
        for c in range(N-a-b):
            M = a + b * A + c * B
            if max < M:
                max = M
print(max)

Submission status: Wrong Answer

Solution code:
acorn = int(input())
gA, sA, bA = map(int, input().split())
gB, sB, bB = map(int, input().split())
a = [gA < gB, sA < sB, bA < bB]
s = sum(a)
gold = 0
silver = 0
bronze = 0
totalma = 0
if s == 1:
    if a[0]:
        gold += acorn // gA
        acorn %= gA
        acorn += gold * gB
        totalma = max(acorn, totalma)
        for silveracorns in range(0, acorn + 1, sB):
            bronzeacorns = acorn - silveracorns
            acorn = (silveracorns // sB) * sA + (bronzeacorns // bB) * bA + bronzeacorns % bB
            totalma = max(acorn, totalma)
    elif a[1]:
        silver += acorn // sA
        acorn %= sA
        acorn += silver * gB
        totalma = max(acorn, totalma)
        for goldacorns in range(0, acorn + 1, gB):
            bronzeacorns = acorn - goldacorns
            acorn = (goldacorns // gB) * gA + (bronzeacorns // bB) * bA + bronzeacorns % bB
            totalma = max(acorn, totalma)
    else:
        bronze += acorn // gA
        acorn %= bA
        acorn += bronze * gB
        totalma = max(acorn, totalma)
        for goldacorns in range(0, acorn + 1, gB):
            silveracorns = acorn - goldacorns
            acorn = (goldacorns // gB) * gA + (silveracorns // sB) * sA + silveracorns % sB
            totalma = max(acorn, totalma)
elif s == 2:
    if not a[0]:
        for silveracorns in range(0, acorn + 1, sA):
            bronzeacorns = acorn - silveracorns
            acorn = (silveracorns // sA) * sB + (bronzeacorns // bA) * bB + bronzeacorns % bA
            totalma = max(acorn, totalma)
        gold += totalma // gB
        totalma %= gB
        totalma += gold * gA
    elif not a[1]:
        for goldacorns in range(0, acorn + 1, gA):
            bronzeacorns = acorn - goldacorns
            acorn = (goldacorns // gA) * gB + (bronzeacorns // bA) * bB + bronzeacorns % bA
            totalma = max(acorn, totalma)
        silver += totalma // sB
        totalma %= sB
        totalma += gold * sA
    else:
        for silveracorns in range(0, acorn + 1, sA):
            goldacorns = acorn - silveracorns
            acorn = (silveracorns // sA) * sB + (goldacorns // gA) * gB + goldacorns % gA
            totalma = max(acorn, totalma)
        bronze += totalma // bB
        totalma %= bB
        totalma += gold * bA
elif s == 3:
    for goldacorns in range(0, acorn + 1, gA):
        for silveracorns in range(0, acorn - goldacorns, sA):
            bronzeacorns = acorn - silveracorns - goldacorns
            totalma = max(totalma, goldacorns // gA * gB + silveracorns // sA * sB + bronzeacorns // bA * bB + bronzeacorns % bA)
else:
    for goldacorns in range(0, acorn + 1, gB):
        for silveracorns in range(0, acorn - goldacorns, sB):
            bronzeacorns = acorn - silveracorns - goldacorns
            totalma = max(totalma, goldacorns // gB * gA + silveracorns // sB * sA + bronzeacorns // bB * bA + bronzeacorns % bB)
print(totalma)

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 += (ans//downs[0][1])*(downs[0][0]-downs[0][1])
    print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
gx, sx, bx = map(int, input().split())
gy, sy, by = map(int, input().split())

ns = [gx/gy, sx/sy, bx/by, gy/gx, sy/sx, by/bx]
dic = {i:n for i,n in enumerate(ns) if n > 1}
dic = sorted(dic.items(), key=lambda x:x[1],reverse=True)

ans = 0
for d,_ in dic:
    if d == 0:
        n1 = N//gy
        N -= n1 * gy
        ans += n1 * gx
    elif d == 1:
        n1 = N//sy
        N -= n1 * sy
        ans += n1*sx
    elif d == 2:
        n1 = N//by
        N -= n1 * by
        ans += n1*bx
    elif d == 3:
        n1 = N//gx
        N -= n1 * gx
        ans += n1 * gy
    elif d == 4:
        n1 = N//sx
        N -= n1*sx
        ans += n1*sy
    elif d == 5:
        n1 = N//bx
        N -= n1*bx
        ans += n1*by
print(N+int(ans))

Submission status: Wrong Answer

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


if __name__ == "__main__":
    n = int(input())
    [ga, sa, ba] = li()
    [gb, sb, bb] = li()

    # A->Bでのトレード成果
    # ga, sa, baの中で最大効率を探して、割り切れる範囲で先に進めてしまう
    # gaが最大効率なら(n//ga - 1)*gaまではgを買ったことにすればいい
    r_g = gb / ga
    r_s = sb / sa
    r_b = bb / ba
    # if (n // ga - 5000) > 0 and r_g >= 1 and r_g >= r_s and r_g >= r_b:
    #     skip = (n // ga - 5000) * ga
    #     dp = [gb * skip] * (n + 1 - skip)
    # elif (n // sa - 5000) > 0 and r_s >= 1 and r_s >= r_g and r_s >= r_b:
    #     skip = (n // sa - 5000) * sa
    #     dp = [sb * skip] * (n + 1 - skip)
    # elif (n // ba - 5000) > 0 and r_b >= 1 and r_b >= r_g and r_b >= r_s:
    #     skip = (n // ba - 5000) * ba
    #     dp = [bb * skip] * (n + 1 - skip)
    # else:
    #     skip = n - 1
    #     dp = [n - 1] * 2
    dp = [0] * (n + 1)

    for i in range(1, len(dp)):
        g_trade = dp[i - ga] + gb if i - ga >= 0 else dp[i - 1]
        s_trade = dp[i - sa] + sb if i - sa >= 0 else dp[i - 1]
        b_trade = dp[i - ba] + bb if i - ba >= 0 else dp[i - 1]
        dp[i] = max(dp[i - 1] + 1, g_trade, s_trade, b_trade)
    # print(dp)
    a_result = dp[-1]

    # B->Aでのトレード成果
    r_g = ga / gb
    r_s = sa / sb
    r_b = ba / bb
    skip_num = 5000
    if (a_result // gb - 1) > 0 and r_g >= 1 and r_g >= r_s and r_g >= r_b:
        # skip_num = max(sb // gb + 1, bb // gb + 1)
        skip = max((a_result // gb - skip_num), 0) * gb
        dp = [ga * skip] * (a_result + 1 - skip)
    elif (a_result // sb - 1) > 0 and r_s >= 1 and r_s >= r_g and r_s >= r_b:
        # skip_num = max(gb // sb + 1, bb // sb + 1)
        skip = max((a_result // sb - skip_num), 0) * sb
        dp = [sa * skip] * (a_result + 1 - skip)
    elif (a_result // bb - 1) > 0 and r_b >= 1 and r_b >= r_g and r_b >= r_s:
        # skip_num = max(sb // bb + 1, gb // bb + 1)
        skip = max((a_result // bb - skip_num), 0) * bb
        dp = [ba * skip] * (a_result + 1 - skip)
    else:
        skip = a_result - 1
        dp = [a_result - 1] * 2

    for i in range(1, len(dp)):
        g_trade = dp[i - gb] + ga if i - gb >= 0 else dp[i - 1]
        s_trade = dp[i - sb] + sa if i - sb >= 0 else dp[i - 1]
        b_trade = dp[i - bb] + ba if i - bb >= 0 else dp[i - 1]
        dp[i] = max(dp[i - 1] + 1, g_trade, s_trade, b_trade)
    # print(dp)
    print(dp[-1])

# n+1個のドングリを取引にかけたときに得られるドングリポテンシャルは
# n個までの最適+1, n-gA個までの最適+gB, n-sA個までの最適+sB, n-bA個までの最適+bBで一番大きくなる値

# AでBより安く金属を手に入れる
# Bでドングリに戻して、Aより安く金属を手に入れる
# Aでドングリに戻して帰る

Submission status: Wrong Answer

Solution code:
from functools import lru_cache

metals = []


class Metal:
    def __init__(self, buy, sell):
        self.buy = buy
        self.sell = sell

    @property
    def gain(self):
        return self.sell - self.buy


@lru_cache(None)
def values(dngr_count, metals_count):
    if dngr_count <= 0:
        return 0
    if metals_count <= 0:
        return 0
    metal = metals[metals_count-1]
    if metals_count == 1:
        return (dngr_count // metal.buy) * metal.gain
    cands = []
    for k in range(1, dngr_count // metal.buy):
        nnn = values(dngr_count-metal.buy*k, metals_count) + metal.gain*k
        cands.append(nnn)
    cands.append(values(dngr_count, metals_count-1))
    return max(cands)


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

    global metals
    if gb > ga:
        metals.append(Metal(ga, gb))
    if sb > sa:
        metals.append(Metal(sa, sb))
    if bb > ba:
        metals.append(Metal(ba, bb))
    for i in range(len(metals)+1):
        for j in range(N):
            values(j, i)
    a = values(N, len(metals))

    values.cache_clear()
    N += a
    metals = []
    if ga > gb:
        metals.append(Metal(gb, ga))
    if sa > sb:
        metals.append(Metal(sb, sa))
    if ba > bb:
        metals.append(Metal(bb, ba))
    for i in range(len(metals)+1):
        for j in range(N):
            values(j, i)
    c = values(N, len(metals))
    print(c+N)


m()

Submission status: Wrong Answer

Solution code:
N = int(input())
gsba = list(map(int, input().split()))
gsbb = list(map(int, input().split()))


ans = 0
for _ in range(6):
    diff1 = [[(N // gsba[i]) * gsbb[i], 1, i] if gsbb[i] - gsba[i] > 0 else [0, 1, i] for i in range(3)]
    diff2 = [[(N // gsbb[i]) * gsba[i], 2, i] if gsba[i] - gsbb[i] > 0 else [0, 2, i] for i in range(3)]

    diff = diff1 + diff2
    diff.sort(key = lambda x:-x[0])
    for k, i, j in diff:
        if k == 0:
            continue
        if i == 1:
            ans += (N // gsba[j]) * gsbb[j]
            N = N % gsba[j]
        else:
            ans += (N // gsbb[j]) * gsba[j]
            N = N % gsbb[j]
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import permutations
n = int(input())
ga, sa, ba = [int(item) for item in input().split()]
gb, sb, bb = [int(item) for item in input().split()]
 
p = [n]
for perm in permutations(["g", "s", "b"]):
    val = n
    after = 0
    for c in perm:
        if c == "g":
            cnt = val // ga
            val %= ga 
            after += cnt * gb
            p.append(after + val)
        if c == "s":
            cnt = val // sa
            val %= sa 
            after += cnt * sb
            p.append(after + val)
        if c == "b":
            cnt = val // ba
            val %= ba 
            after += cnt * bb
            p.append(after + val)
p = [max(p)]
for perm in permutations(["g", "s", "b"]):
    val = p[0]
    after = 0
    for c in perm:
        if c == "g":
            cnt = val // gb
            val %= gb 
            after += cnt * ga
            p.append(after + val)
        if c == "s":
            cnt = val // sb
            val %= sb 
            after += cnt * sa
            p.append(after + val)
        if c == "b":
            cnt = val // bb
            val %= bb 
            after += cnt * ba
            p.append(after + val)
print(max(p))

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

    r = np.zeros([3], dtype=int)
    maxx = np.zeros([n])
    for i in range(len(r)):
        r[i] = get_don(n, i, gsb_a, gsb_b)
    print(np.max(r))

def get_don(dnum, t, gsb_a, gsb_b):
    can = dnum // gsb_a[t]
    if can == 0: return 0
    am = dnum % gsb_a[t]
    rate = gsb_b[t] / gsb_a[t]
    treaded = can * rate
    g = treaded + get_don(am, 0, gsb_a, gsb_b)
    s = treaded + get_don(am, 1, gsb_a, gsb_b)
    b = treaded + get_don(am, 2, gsb_a, gsb_b)
    return max(g, s, b)

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

Achange = []
Bchange = []

for i in range(3):
  if A[i] > B[i]:
    Bchange.append(i)
  elif A[i] < B[i]:
    Achange.append(i)

#print(Achange, Bchange)

# 最初のA交換所→B交換所
Dmax1 = N
if Achange:
  if len(Achange)==3:
    i = Achange[0]
    j = Achange[1]
    k = Achange[2]
    for x in range(N+1):
      for y in range(N+1):
        if A[i]*x+A[j]*y <= N:
          Dmax1 = max(Dmax1, B[i]*x + B[j]*y + (N-A[i]*x-A[j]*y)%A[k] + (N-A[i]*x-B[j]*y)//A[k] * B[k])
    
  if len(Achange)==2:
    i = Achange[0]
    j = Achange[1]
    for x in range(N+1):
      if A[i]*x <= N:
          Dmax1 = max(Dmax1, B[i]*x + (N-A[i]*x)%A[j] + (N-A[i]*x)//A[j] * B[j])
    
  if len(Achange)==1:
    i = Achange[0]
    Dmax1 = max(Dmax1, N%A[i] + (N//A[i] * B[i]))
    
#print(Dmax1)
#print(Achange, Bchange)
#次のB交換所→A交換所
Achange = Bchange
A,B=B,A
N = Dmax1
#print(Achange, Bchange)

if Achange:
  if len(Achange)==3:
    i = Achange[0]
    j = Achange[1]
    k = Achange[2]
    for x in range(N+1):
      for y in range(N+1):
        if A[i]*x+A[j]*y <= N:
          Dmax1 = max(Dmax1, B[i]*x + B[j]*y + (N-A[i]*x-A[j]*y)%A[k] + (N-A[i]*x-B[j]*y)//A[k] * B[k])
    
  if len(Achange)==2:
    i = Achange[0]
    j = Achange[1]
    for x in range(N+1):
      if A[i]*x <= N:
          Dmax1 = max(Dmax1, B[i]*x + (N-A[i]*x)%A[j] + (N-A[i]*x)//A[j] * B[j])
    
  if len(Achange)==1:
    i = Achange[0]
    Dmax1 = max(Dmax1, N%A[i] + (N//A[i] * B[i]))
    
print(Dmax1)

Submission status: Wrong Answer

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

    changes = [(ga, gb), (sa, sb), (ba, bb)]
    changes.sort(key=lambda x: x[1] / x[0], reverse=True)
    new_n = 0
    if changes[0][1] < changes[0][0]:
        pass
    elif changes[1][1] < changes[1][0]:
        new_n += (n // changes[0][0]) * changes[0][1]
        n %= changes[0][0]
    else:
        x1, y1 = changes[0]
        x2, y2 = changes[1]
        x3, y3 = changes[2]
        new_curr_n = 0
        for i in range(1 + n // x1):
            curr = i * y1
            curr_n = n - x1 * i
            plus = (curr_n // x2) * y2
            curr_n %= x2

            if y3 > x3:
                plus += (curr_n // x3) * y3
                curr_n %= x3

                if curr_n:
                    curr_n += 1

            if curr + plus > new_n:
                new_n = curr + plus
                new_curr_n = curr_n
            elif curr + plus == new_n:
                new_curr_n = max(new_curr_n, curr_n)

        if new_n:
            n = new_curr_n
    n += new_n

    changes = [(gb, ga), (sb, sa), (bb, ba)]
    changes.sort(key=lambda x: x[1] / x[0], reverse=True)
    new_n = 0
    if changes[0][1] < changes[0][0]:
        pass
    elif changes[1][1] < changes[1][0]:
        new_n += (n // changes[0][0]) * changes[0][1]
        n %= changes[0][0]
    else:
        x1, y1 = changes[0]
        x2, y2 = changes[1]
        x3, y3 = changes[2]
        new_curr_n = 0
        for i in range(1 + n // x1):
            curr = i * y1
            curr_n = n - x1 * i
            plus = (curr_n // x2) * y2
            curr_n %= x2

            if y3 > x3:
                plus += (curr_n // x3) * y3
                curr_n %= x3

                if curr_n:
                    curr_n += 1

            if curr + plus > new_n:
                new_n = curr + plus
                new_curr_n = curr_n
            elif curr + plus == new_n:
                new_curr_n = max(new_curr_n, curr_n)
        if new_n:
            n = new_curr_n
    n += new_n
    print(n)


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(tmp_ans//downs[0][1]+1):
            left = tmp_ans - gold*downs[0][1]
            silver = left // downs[1][1]
            tmp_ans = (tmp_ans
                       + gold*gold_diff
                       + silver*silver_diff)
            ans = max(ans, tmp_ans)
    elif len(downs) == 1:
        ans += (ans//downs[0][1])*(downs[0][0]-downs[0][1])
    print(ans)