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:
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 + 1, 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 + 1, 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:
# Enter your code here. Read input from STDIN. Print output to STDOUT

def find3(N,a,b,c,A,B,C):
    MAX=N
    for x in xrange(5001):
        if N-a*x<0:
            break
        for y in xrange(5001):
            temp=N-(a*x+b*y)
            if temp>=0:
                z=temp//c
                MAX=max(MAX,N-(a*x+b*y+c*z)+(A*x+B*y+C*z))
            else:
                break
    return MAX

def find2(N,a,b,A,B):
    MAX=N
    
    for x in xrange(N+1):
        temp=N-a*x
        if temp>=0:
            y=temp//b
            MAX=max(MAX,N-(a*x+b*y)+(A*x+B*y))
        else:
            break
   
    return max(MAX,N+(A-a)*x+(B-b)*y)
    

def find1(N,a,A):
    MAX=N
    num=N//a
    return N-num*a+num*A

N=int(raw_input())
X=list(map(int,raw_input().strip().split(' ')))
Y=list(map(int,raw_input().strip().split(' ')))

##stage1
index=[0]*3

for i in xrange(3):
    if X[i]<Y[i]:
        index[i]=1
post_1=[]
post_2=[]
for i in xrange(len(index)):
    if index[i]>0:
        post_1+=[i]    
    else:
        post_2+=[i]
number=sum(index)
if number==0:
    print(find3(N,Y[0],Y[1],Y[2],X[0],X[1],X[2]))
elif number==1:
    N=find1(N,X[post_1[0]],Y[post_1[0]])
    N=find2(N,Y[post_2[0]],Y[post_2[1]],X[post_2[0]],X[post_2[1]])
    print(N)
elif number==2:
    N=find2(N,X[post_1[0]],X[post_1[1]],Y[post_1[0]],Y[post_1[1]])
    N=find1(N,Y[post_2[0]],X[post_2[0]])
    print(N)
else:
    N=find3(N,X[0],X[1],X[2],Y[0],Y[1],Y[2])
    print(N)

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):
        n=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
        for i in range(n+1):
            if(ga*i>n):
                break
            tmp=i*(gb-ga)+k%ba+(k//ba)*bb
            k-=1
            n=max(tmp,n)
    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)
    return n
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())
List_atob = sorted([gb/ga,  sb/sa, bb/ba])
List_btoa =sorted([ga/gb,sa/sb,ba/bb])
res = N
for i in range(5000):
    for j in range(5000-i):
        if ga*i+sa*j<=N:
            if ba<bb:
                
                k = (N-ga*i-sa*j)//ba
                res = max(res, gb*i+sb*j+bb*k+(N-ga*i-sa*j)%ba)
            else:
                res = max(res, gb*i+sb*j+N-ga*i-sa*j)
N=res  
for i in range(5000):
    for j in range(5000-i):
        if gb*i+sb*j<=N:
            if bb<ba:
                k = (N-gb*i-sb*j)//bb
                res = max(res, ga*i+sa*j+ba*k+(N-gb*i-sb*j)%bb)
            else:
                res = max(res, ga*i+sa*j+N-gb*i-sb*j)
print(res)

Submission status: Wrong Answer

Solution code:
#D問題
N = int(input())
GSB1 = list(map(int,input().split()))
GSB2 = list(map(int,input().split()))

X = [N for i in range(7)]

x = X[0]
n1,n2,n3 = 0,0,0
if GSB1[0] < GSB2[0]:
    n1 = x//GSB1[0]
    x-=n1
if GSB1[1] < GSB2[1]:
    n2 = x//GSB1[1]
    x-=n2
if GSB1[2] < GSB2[2]:
    n3 = x//GSB1[2]
    x-=n3
x+=n3*GSB2[2]
x+=n2*GSB2[1]
x+=n1*GSB2[0]
X[0] = x

x = X[1]
n1,n2,n3 = 0,0,0
if GSB1[0] < GSB2[0]:
    n1 = x//GSB1[0]
    x-=n1
if GSB1[2] < GSB2[2]:
    n2 = x//GSB1[2]
    x-=n2
if GSB1[1] < GSB2[1]:
    n3 = x//GSB1[1]
    x-=n3
x+=n3*GSB2[1]
x+=n2*GSB2[2]
x+=n1*GSB2[0]
X[1] = x

x = X[2]
n1,n2,n3 = 0,0,0
if GSB1[1] < GSB2[1]:
    n1 = x//GSB1[1]
    x-=n1
if GSB1[0] < GSB2[0]:
    n2 = x//GSB1[0]
    x-=n2
if GSB1[2] < GSB2[2]:
    n3 = x//GSB1[2]
    x-=n3
x+=n3*GSB2[2]
x+=n2*GSB2[0]
x+=n1*GSB2[1]
X[2] = x

x = X[3]
n1,n2,n3 = 0,0,0
if GSB1[1] < GSB2[1]:
    n1 = x//GSB1[1]
    x-=n1
if GSB1[2] < GSB2[2]:
    n2 = x//GSB1[2]
    x-=n2
if GSB1[0] < GSB2[0]:
    n3 = x//GSB1[0]
    x-=n3
x+=n3*GSB2[0]
x+=n2*GSB2[2]
x+=n1*GSB2[1]
X[3] = x

x = X[4]
n1,n2,n3 = 0,0,0
if GSB1[2] < GSB2[2]:
    n1 = x//GSB1[2]
    x-=n1
if GSB1[0] < GSB2[0]:
    n2 = x//GSB1[0]
    x-=n2
if GSB1[1] < GSB2[1]:
    n3 = x//GSB1[1]
    x-=n3
x+=n3*GSB2[1]
x+=n2*GSB2[0]
x+=n1*GSB2[2]
X[4] = x

x = X[5]
n1,n2,n3 = 0,0,0
if GSB1[2] < GSB2[2]:
    n1 = x//GSB1[2]
    x-=n1
if GSB1[1] < GSB2[1]:
    n2 = x//GSB1[1]
    x-=n2
if GSB1[0] < GSB2[0]:
    n3 = x//GSB1[0]
    x-=n3
x+=n3*GSB2[0]
x+=n2*GSB2[1]
x+=n1*GSB2[2]
X[5] = x

N = max(X)

X = [N for i in range(7)]

n1,n2,n3 = 0,0,0
if GSB2[0] < GSB1[0]:
    n1 = x//GSB2[0]
    x-=n1
if GSB2[2] < GSB1[2]:
    n2 = x//GSB2[2]
    x-=n2
if GSB2[1] < GSB1[1]:
    n3 = x//GSB2[1]
    x-=n3
x+=n3*GSB1[1]
x+=n2*GSB1[2]
x+=n1*GSB1[0]
X[1] = x

x = X[2]
n1,n2,n3 = 0,0,0
if GSB2[1] < GSB1[1]:
    n1 = x//GSB2[1]
    x-=n1
if GSB2[0] < GSB1[0]:
    n2 = x//GSB2[0]
    x-=n2
if GSB2[2] < GSB1[2]:
    n3 = x//GSB2[2]
    x-=n3
x+=n3*GSB1[2]
x+=n2*GSB1[0]
x+=n1*GSB1[1]
X[2] = x

x = X[3]
n1,n2,n3 = 0,0,0
if GSB2[1] < GSB1[1]:
    n1 = x//GSB2[1]
    x-=n1
if GSB2[2] < GSB1[2]:
    n2 = x//GSB2[2]
    x-=n2
if GSB2[0] < GSB1[0]:
    n3 = x//GSB2[0]
    x-=n3
x+=n3*GSB1[0]
x+=n2*GSB1[2]
x+=n1*GSB1[1]
X[3] = x

x = X[4]
n1,n2,n3 = 0,0,0
if GSB2[2] < GSB1[2]:
    n1 = x//GSB2[2]
    x-=n1
if GSB2[0] < GSB1[0]:
    n2 = x//GSB2[0]
    x-=n2
if GSB2[1] < GSB1[1]:
    n3 = x//GSB2[1]
    x-=n3
x+=n3*GSB1[1]
x+=n2*GSB1[0]
x+=n1*GSB1[2]
X[4] = x

x = X[5]
n1,n2,n3 = 0,0,0
if GSB2[2] < GSB1[2]:
    n1 = x//GSB2[2]
    x-=n1
if GSB2[1] < GSB1[1]:
    n2 = x//GSB2[1]
    x-=n2
if GSB2[0] < GSB1[0]:
    n3 = x//GSB2[0]
    x-=n3
x+=n3*GSB1[0]
x+=n2*GSB1[1]
x+=n1*GSB1[2]
X[5] = x

print(max(X))

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
    gain += g * f[j][2]

print(gain+rest)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


n = ni()
ga, sa, ba = li()
gb, sb, bb = li()

go = [(ga, gb), (sa, sb), (ba, bb)]
go.sort(key=lambda x: x[1]/x[0], reverse=True)

tmp = 0

for goa, gob in go:
    if gob // goa < 1:
        continue

    else:
        tmp += gob * (n//goa)
        n -= goa * (n//goa)

n += tmp


bk = [(gb, ga), (sb, sa), (bb, ba)]
bk.sort(key=lambda x: x[1]/x[0], reverse=True)

tmp = 0

for bkb, bka in bk:
    if bka // bkb < 1:
        continue

    else:
        tmp += bka * (n//bkb)
        n -= bkb * (n//bkb)

n += tmp

print(n)

Submission status: Wrong Answer

Solution code:
def solve():
    N = int(input())
    As = list(map(int, input().split()))
    Bs = list(map(int, input().split()))

    C1, C2, C3 = [min(A, B) for A, B in zip(As, Bs)]
    D1, D2, D3 = [max(A, B) for A, B in zip(As, Bs)]

    K = 5000**2
    dp = [0] * (K+1)
    for i in range(K):
        if i+C1 <= K:
            dp[i+C1] = max(dp[i+C1], dp[i]+D1)
        if i+C2 <= K:
            dp[i+C2] = max(dp[i+C2], dp[i]+D2)
        if i+C3 <= K:
            dp[i+C3] = max(dp[i+C3], dp[i]+D3)

    ans = max([dp[i]+N-i for i in range(N+1)])
    print(ans)

solve()

Submission status: Wrong Answer

Solution code:
N=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]

rate=[]
for i in range(3):
    rate+=[b[i]/a[i]]

rate.sort()
if rate[-1]<1:
    pass
else:
    N*=rate[-1]

if rate[0]>1:
    pass
else:
    N/=rate[0]

print(int(N))

Submission status: Wrong Answer

Solution code:
n=int(input())
ga,sa,ba=map(int,input().split())
gb,sb,bb=map(int,input().split())
la=[ga,sa,ba]
lb=[gb,sb,bb]
ll=[0,0,0]
s1=[gb/ga,sb/sa,bb/ba]
s1s=sorted(s1)
#step1
ind=s1.index(s1s[2])
if n>=la[ind] and s1s[2]>1:
  ll[ind]+=n//la[ind]
  n=n%la[ind]
ind=s1.index(s1s[1])
if n>=la[ind] and s1s[1]>1:
  ll[ind]+=n//la[ind]
  n=n%la[ind]
ind=s1.index(s1s[0])
if n>=la[ind] and s1s[0]>1:
  ll[ind]+=n//la[ind]
  n=n%la[ind]
#step2kankin
n+=sum([ll[_]*lb[_] for _ in range(3)])
ll=[0,0,0]
#step2buy
ind=s1.index(s1s[0])
if n>=lb[ind] and s1s[0]<1:
  ll[ind]+=n//lb[ind]
  n=n%lb[ind]
ind=s1.index(s1s[1])
if n>=lb[ind] and s1s[1]<1:
  ll[ind]+=n//lb[ind]
  n=n%lb[ind]
ind=s1.index(s1s[2])
if n>=lb[ind] and s1s[2]<1:
  ll[ind]+=n//lb[ind]
  n=n%lb[ind]
#step3kankin
n+=sum([ll[_]*la[_] for _ in range(3)])
print(int(n))

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
gsb_A = LIST()
gsb_B = LIST()
gsb_delta = {}
for i in range(3):
		if min(gsb_A[i], gsb_B[i]) in gsb_delta:
			gsb_delta[min(gsb_A[i], gsb_B[i])] = max(max(gsb_A[i], gsb_B[i]), gsb_delta[min(gsb_A[i], gsb_B[i])])
		else:
			gsb_delta[min(gsb_A[i], gsb_B[i])] = max(gsb_A[i], gsb_B[i])
# print(gsb_delta)
dp = [0] * (N+1)
for i in range(N+1):
	for j in gsb_delta:
		if i-j >= 0:
			dp[i] = max(dp[i], dp[i-j] + gsb_delta[j])
# print(dp)
# print(dp[N])
ans = 0
left = N
for i in range(N, -1, -1):
	if dp[i] != 0 and i <= left:
		ans += dp[i]
		left -= i
ans += left

print(max(N, ans))

Submission status: Wrong Answer

Solution code:
n=int(input())
ga,sa,ba=map(int,input().split())
gb,sb,bb=map(int,input().split())
c=min(ga,gb),min(sa,sb),min(ba,bb)
g=abs(ga-gb),abs(sa-sb),abs(ba-bb)
dp=[-10000 for i in range(5000)]+[0 for i in range(n+1)]
for i in range(5000,n+5001):
  dp[i]=max(dp[i-c[0]]+g[0],dp[i-c[1]]+g[1],dp[i-c[2]]+g[2],0)
print(dp[n+5000]+n)

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 = 1
    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:
#!/usr/bin/env python
from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip


def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    s = 0

    a.append(1)
    b.append(1)

    c = sorted(zip(a, b), key=lambda x: (-abs(x[0] - x[1]), min(x[0], x[1])))

    for ai, bi in c:
        mi = min(ai, bi)
        ma = max(ai, bi)

        s += ma * (n // mi)
        n %= mi

    print(s)



# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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])
ans = 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:
import sys
sys.setrecursionlimit(10 ** 5)


def LI(): return [int(x) for x in sys.stdin.readline().split()]


def main():
    N = int(input())
    A = LI()
    B = LI()
    if A == B:
        print(N)
        return
    BA = [((B[i]-A[i])/A[i], i) for i in range(3)]
    AB = [((A[i]-B[i])/B[i], i) for i in range(3)]
    BA.sort(reverse=True)
    AB.sort(reverse=True)
    now = N
    money = [0, 0, 0]
    for (d, i) in BA:
        if d <= 0:
            break
        money[i] = now//A[i]
        now = now % A[i]
    for i in range(3):
        now += money[i]*B[i]
    money = [0, 0, 0]
    for (d, i) in AB:
        if d <= 0:
            break
        money[i] = now//B[i]
        now = now % B[i]
    for i in range(3):
        now += money[i]*A[i]
    print(now)


main()

Submission status: Wrong Answer

Solution code:
N = int(input())
A_rate = list(map(int, input().split()))
B_rate = list(map(int, input().split()))

def calc(num,rate1,rate2):
    return [(num//rate1)*rate2 + num%rate1, num%rate1] if rate2-rate1 > 0 else [num,0]
lst = [N]
for i in range(3):
    tmplst = []
    for j in range(3):
        if i == j:
            continue
        first,remainder = calc(N,A_rate[i],B_rate[i])
        second,remainder = calc(remainder,A_rate[j],B_rate[j])
        third,remainder = calc(remainder,A_rate[3-i-j],B_rate[3-i-j])
        tmplst.append(first+second+third+remainder)
    lst.append(max(tmplst))

N = max(lst)

lst.clear()
lst.append(N)
for i in range(3):
    tmplst = []
    for j in range(3):
        if i == j:
            continue
        first,remainder = calc(N,B_rate[i],A_rate[i])
        second,remainder = calc(remainder,B_rate[j],A_rate[j])
        third,remainder = calc(remainder,B_rate[3-i-j],A_rate[3-i-j])
        tmplst.append(first+second+third+remainder)
    lst.append(max(tmplst))

N = max(lst)

print(N)

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
    return max(
        values(dngr_count, metals_count-1),
        values(dngr_count-metal.buy, metals_count) + metal.gain
    )


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

Submission status: Wrong Answer

Solution code:
import sys
#input = sys.stdin.readline
from sys import setrecursionlimit
setrecursionlimit(10**9)

def inpl(): return list(map(int, input().split()))
N = int(input())
GA = inpl()
GB = inpl()

typ = [GA[i] <= GB[i] for i in range(3)]

if sum(typ) == 3:
    ans = 0
    for g in range(N//GA[0] + 1):
        D = N - GA[0]*g
        for s in range(D//GA[1] + 1):
            b, D2 = divmod(D - GA[1]*s, GA[2])
            ans = max(ans, D2 + g*GB[0] + s*GB[1] + b*GB[2])

elif sum(typ) == 2:
    ans = 0
    x, y = [i for i in range(3) if typ[i]]
    z = [i for i in range(3) if not typ[i]][0]
    for a in range(N//GA[x] + 1):
        b, D = divmod(N - GA[x]*a, GA[y])
        c, D2 = divmod(D + GB[x]*a + GB[y]*b, GB[z])
        ans = max(ans, D2 + c*GA[z])

elif sum(typ) == 1:
    ans = 0
    x, y = [i for i in range(3) if not typ[i]]
    z = [i for i in range(3) if typ[i]][0]
    c, D = divmod(N, GA[z])
    D += GB[z]*c

    for a in range(D//GB[x] + 1):
        b, D2 = divmod(D-GB[x]*a, GB[y])
        ans = max(ans, D2 + GA[x]*a + GB[y]*b)

    for b in range(D//GB[y] + 1):
        a, D2 = divmod(D-GB[y]*b, GB[x])
        ans = max(ans, D2 + GA[x]*a + GB[y]*b)

else:
    ans = 0
    for g in range(N//GB[0] + 1):
        D = N - GB[0]*g
        for s in range(D//GB[1] + 1):
            b, D2 = divmod(D - GB[1]*s, GB[2])
            ans = max(ans, D2 + g*GA[0] + s*GA[1] + b*GA[2])

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
List_atob = sorted([gb/ga,  sb/sa, bb/ba])
List_btoa =sorted([ga/gb,sa/sb,ba/bb])
res = N
for i in range(5000):
    for j in range(5000-i):
        if ga*i+sa*j<=N:
            if bb<ba:
                
                k = (N-ga*i-sa*j)//ba
                res = max(res, gb*i+sb*j+bb*k+(N-ga*i-sa*j)%ba)
            else:
                res = max(res, gb*i+sb*j+N-ga*i-sa*j)
N=res  
for i in range(5000):
    for j in range(5000-i):
        if gb*i+sb*j<=N:
            if bb<ba:
                k = (N-gb*i-sb*j)//bb
                res = max(res, ga*i+sa*j+ba*k+(N-gb*i-sb*j)%bb)
            else:
                res = max(res, ga*i+sa*j+N-gb*i-sb*j)
print(res)

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:
        l=l[0]
        return (N//a[l])*b[l]+N%a[l]
    elif len(l)==2:
        x,y=l[0],l[1]
        p,q=a[x],a[y]
        lcm=p*q//gcd(p,q)

        n1,n2=N-N%lcm,N%lcm
        ans1=max(n1*b[x]//p,n1*b[y]//q)
        i=0
        ans2=0
        while i*p<=n2:
            ans2=max(ans2,i*b[x]+((n2-i*p)//q)*b[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=0
        while i*a[0]<=n2:
            n3=n2-i*a[0]
            j=0
            while  j*a[1]<=n3:
                ans2=max(ans2,i*b[0]+j*b[1]+(n3-j*a[1])*b[2])
                j+=1
            i+=1
        return ans1+ans2

import sys
N=int(input())

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

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

Submission status: Wrong Answer

Solution code:
import numpy as np

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

d = N
d_new = 0

R = []
for i in range(len(A)):
  a = A[i]
  b = B[i]
  R.append(b / a)
idx = np.argsort(R)
idx = idx[::-1]

for i in idx:
  a = A[i]
  b = B[i]
  if (a < b):
    d_new += ((d // a) * b)
    d -= (d // a)

d = (d + d_new)
d_new = 0
idx = idx[::-1]
for i in idx:
  a = B[i]
  b = A[i]
  if (a < b):
    d_new += ((d // a) * b)
    d -= (d // a)

d = (d + d_new)
print(d)

Submission status: Wrong Answer

Solution code:
import itertools

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

a = [1]+a
b = [1]+b


ans = 0

l = [0,1,2,3]

for v in itertools.permutations(l,4):
    p = [n, 0, 0, 0]
    q1 = [0, 0, 0, 0]
    for x in v:
        q1[x] += p[0]//a[x]
        p[0] = p[0]%a[x]
    s=[p[i]+q1[i] for i in range(4)]

    q2 = [0,0,0,0]
    for i in range(4):
        q2[0] += s[i] * b[i]
    r=[q2[i] for i in range(4)]

    for w in itertools.permutations(l,4):
        t = [r[i] for i in range(4)]
        q3 = [0, 0, 0, 0]
        for y in w:
            q3[y] += t[0] // b[y]
            t[0] = t[0] % b[y]
        u = [t[i] + q3[i] for i in range(4)]


        q4 = [0,0,0,0]
        for i in range(4):
            q4[0] += u[i] * a[i]
        o = [q4[i] for i in range(4)]
        ans = max(ans, o[0])

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = []
d = []
for i in range(3):
    c.append(b[i]-a[i])
    d.append(a[i]-b[i])
c.sort()
d.sort()
if 1 <= c[2] <= n:
    if c[2] <= n:
        n += n*c[2]
    elif 1 <= c[1] <= n:
        n += n*c[1]
    elif 1 <= c[0] <= n:
        n += n*c[0]
if 1 <= d[2] <= n:
    if d[2] <= n:
        n += n*d[2]
    elif 1 <= d[1] <= n:
        n += n*d[1]
    elif 1 <= d[0] <= n:
        n += n*d[0]
print(n)