Problem:


Score : 600 points

Problem Statement

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

His plan is as follows:

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

Go to Exchange A and do some trades.

Go to Exchange B and do some trades.

Go to Exchange A and do some trades.

Go back to the nest.

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

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

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

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

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

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

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

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

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

Constraints

1 \leq N \leq 5000

1 \leq g_{X} \leq 5000

1 \leq s_{X} \leq 5000

1 \leq b_{X} \leq 5000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
g_A s_A b_A
g_B s_B b_B

Output

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

Sample Input 1

23
1 1 1
2 1 1

Sample Output 1

46

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

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

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

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

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



Sample input:
23
1 1 1
2 1 1


Sample output:
46

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
aHIKUb=[[i,a[i]-b[i]] for i in range(3)]
aHIKUb.sort(key=lambda x:x[1])
changeCnt=[0,0,0]
changeCnt[aHIKUb[0][0]]=n//a[aHIKUb[0][0]]
for i,aibi in aHIKUb[1:]:
    remain=n
    for j in range(3):
        remain-=a[j]*changeCnt[j]
    if aibi<0 and remain>a[i]:
        changeCnt[i]=remain//a[i]
for i in range(3):
    n+=(-a[i]+b[i])*changeCnt[i]

changeCnt=[0,0,0]
changeCnt[aHIKUb[2][0]]=n//b[aHIKUb[2][0]]
for i,aibi in aHIKUb[:2]:
    remain=n
    for j in range(3):
        remain-=a[j]*changeCnt[j]
    if aibi>0 and remain>a[i]:
        changeCnt[i]=remain//a[i]
for i in range(3):
    n+=(a[i]-b[i])*changeCnt[i]
print(n)

Submission status: Wrong Answer

Solution code:
n = int(input())

rate = []

for i in range(2):
    rate.append(list(map(int, input().split(' '))))

g_ab = 1 if rate[0][0] // rate[1][0] == 0 else rate[0][0] // rate[1][0]
s_ab = 1 if rate[0][1] // rate[1][1] == 0 else rate[0][1] // rate[1][1]
b_ab = 1 if rate[0][2] // rate[1][2] == 0 else rate[0][2] // rate[1][2]

g_ba = 1 if rate[1][0] // rate[0][0] == 0 else rate[1][0] // rate[0][0]
s_ba = 1 if rate[1][1] // rate[0][1] == 0 else rate[1][1] // rate[0][1]
b_ba = 1 if rate[1][2] // rate[0][2] == 0 else rate[1][2] // rate[0][2]

rate_k = [g_ab * g_ba, s_ab * s_ba, b_ab * b_ba]

rate_k.sort()

print(rate_k[-1] * rate_k[-2] * n)

Submission status: Wrong Answer

Solution code:
N = int(input())
gA, sA, bA = map(int, input().split())
gB, sB, bB = map(int, input().split())

lst = [[gB-gA, -gA, gB, 'G'], [sB-sA, -sA, sB, 'S'], [bB-bA, -bA, bB, 'B'],]

lst.sort(reverse = True)

ans1 = (N//abs(lst[0][1])) * lst[0][2] + N%abs(lst[0][1])
ans2 = (N//abs(lst[1][1])) * lst[1][2] + N%abs(lst[1][1])
ans3 = (N//abs(lst[2][1])) * lst[2][2] + N%abs(lst[2][1])

print (max(ans1, ans2, ans3, N))

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
X = np.zeros((2, 3))
for i in range(2):
    X[i, :] = [int(x) for x in input().split()]

if max((X[1, :] / X[0, :])) > 1:
    N *= max((X[1, :] / X[0, :]))

if max((X[0, :] / X[1, :])) > 1:
    N *= max((X[1, :] / X[0, :]))

print(int(N))

Submission status: Wrong Answer

Solution code:
def solve(n, ga, sa, ba, gb, sb, bb):
    ans = n
    for g in range(1 if ga >= gb else n // ga + 1):
        tmp1 = g * gb
        n2 = n - g * ga
        if ba >= bb:
            ans = max(ans, tmp1, tmp1 + n2 // sa * sb)
        elif sa >= sb:
            ans = max(ans, tmp1, tmp1 + n2 // ba * bb)
        else:
            for s in range(n2 // sa + 1):
                ans = max(ans, tmp1 + s * sb + (n2 - s * sa) // ba * bb)
    return ans


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:
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

K = [0]*3
for i in range(3):
    K[i] = max(B[i]/A[i],A[i]/B[i])

zyunban = []
if K[0] > K[1]:
    if K[0] > K[2]:
        if K[1] > K[2]:
            zyunban = [0,1,2]
        else:
            zyunban = [0,2,1]
    else:
        zyunban = [2,0,1]
else:
    if K[1] > K[2]:
        if K[0] > K[2]:
            zyunban = [1,0,2]
        else:
            zyunban = [1,2,0]
    else:
        zyunban = [2,1,0]

ans = 0
for i in range(3):
    p = zyunban[i]
    z = N//min(A[p],B[p])
    N %= min(A[p],B[p])
    if z != 0:
        ans = max(A[p],B[p])*z

print(ans+N)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
print([A[0]/B[0],A[1]/B[1],A[2]/B[2],B[0]/A[0],B[1]/A[1],B[2]/A[2]])
max_rate = max([A[0]/B[0],A[1]/B[1],A[2]/B[2],B[0]/A[0],B[1]/A[1],B[2]/A[2]])
print(int(N*max_rate))

Submission status: Wrong Answer

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

if A[0] / B[0] >= A[1]/B[1] and A[0] / B[0] >= A[2] / B[2]:
    idx = 0
    if A[1]/B[1] > A[2]/B[2]:
        idx2 = 1
        midx = 2
    else:
        idx2 = 2
        midx = 1
elif A[1] / B[1] >= A[0]/B[0] and A[1] / B[1] >= A[2] / B[2]:
    idx = 1
    if A[0]/B[0] > A[2]/B[2]:
        idx2 = 0
        midx = 2
    else:
        idx2 = 2
        midx = 0

elif A[2] / B[2] > A[0]/B[0] and A[2] / B[2] > A[1] / B[1]:
    idx = 2
    if A[1]/B[1] > A[0]/B[0]:
        idx2 = 1
        midx = 0
    else:
        idx2 = 0
        midx = 1
else:
    print(aaa)
#A -> B
ans = n
money = n // A[idx]
ans1 = money * B[idx]
if n%A[idx] != 0:
    dn = n % A[idx] 
    #残りのドングリ
    money = dn // A[idx2]
    ans1 += money * B[idx2]
    if dn % A[idx] != 0:
        dng = dn % A[idx2]
        money = dng // A[midx]
        ans1 += money * B[midx]
#B -> A
money = n // B[midx]
ans2 = money * A[midx]
if n%B[idx] != 0:
    dn = n % B[midx]
    money = n // B[idx2]
    ans2 += money * A[idx2]
    if dn % B[idx] != 0:
        dng = dn % B[idx2]
        money = dng // B[idx]
        ans2 += money * A[idx]
print(max(ans,ans1,ans2))

Submission status: Wrong Answer

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

num = 0
ab = []
ba = []
for i in range(3):
    if a[i]-b[i] < 0:
        ab.append([a[i],b[i]])  
    else:
        ba.append([a[i],b[i]])

dp = [0]*(n+1)
for i in range(n+1):
    for x,y in ab:
        if i >= x:
            dp[i] = max(dp[i],dp[i-x]+y)

p = dp[n]

dp2 = [0]*(p+1)
for i in range(p+1):
    for y,x in ba:
        if i >= x:
            dp2[i] = max(dp2[i],dp2[i-x]+y)
print(dp2[-1])

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
def make(N,a,b):
    if b-a>0:
        N=N%a+(N//a)*b
        return N
    else:
        return N
first=0
for a,b in zip(A,B):
    first=max(first,make(N,a,b))
N=first
ans=0
for a,b in zip(B,A):
    ans=max(N,make(N,a,b))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
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()
    AB = [(A[i]-B[i], A[i], B[i]) for i in range(3)]
    L = [[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1], [2, 1, 0]]
    ans = []
    for l1 in L:
        for l2 in L:
            now = N
            money = [0, 0, 0]
            for i in l1:
                if AB[i][0] < 0:
                    money[i] = now//AB[i][1]
                    now = now % AB[i][1]
            for i in range(3):
                now += money[i]*AB[i][2]
            for i in l2:
                if AB[i][1] > 0:
                    money[i] = now//AB[i][2]
                    now = now % AB[i][2]
            for i in range(3):
                now += money[i]*AB[i][1]
            ans.append(now)
    print(max(ans))


main()

Submission status: Wrong Answer

Solution code:
N = int(input())
gA, sA, bA = map(int, input().split())
gB, sB, bB = map(int, input().split())

lst = [[gB-gA, -gA, gB, 'G'], [sB-sA, -sA, sB, 'S'], [bB-bA, -bA, bB, 'B'],]

lst.sort(reverse = True)

ans1 = (N//abs(lst[0][1])) * lst[0][2] + N%abs(lst[0][1])
ans2 = (N//abs(lst[1][1])) * lst[1][2] + N%abs(lst[1][1])
ans3 = (N//abs(lst[2][1])) * lst[2][2] + N%abs(lst[2][1])

print (max(ans1, ans2, ans3))

Submission status: Wrong Answer

Solution code:
N = int(input())
Ga, Sa, Ba = map(int, input().split())
Gb, Sb, Bb = map(int, input().split())

rateabs = [(Gb/Ga,Ga,Gb), (Sb/Sa,Sa,Sb), (Bb/Ba,Ba,Bb)]
rateabs.sort(key=lambda x:-x[0])
ratebas = [(Ga/Gb,Gb,Ga), (Sa/Sb,Sb,Sa), (Ba/Bb,Bb,Ba)]
ratebas.sort(key=lambda x:-x[0])

n = N
n2 = 0
for rate, a, b in rateabs:
  if rate < 1:
    continue
  t = n // a
  n2 += t * b
  n -= t * a
n2 += n
n, n2 = n2, 0
for rate, a, b in ratebas:
  if rate < 1:
    continue
  t = n // a
  n2 += t * b
  n -= t * a
n2 += n
print(n2)

Submission status: Wrong Answer

Solution code:
n=int(input())
o=lambda:list(map(int,input().split()))
a=o()
b=o()
r=list(map(lambda x:x[1]/x[0],zip(a,b)))
for i in range(2):
  s=0
  for q,p in sorted(zip(r,zip(a,b)),key=lambda x:x[0])[::-1]:
    if q>1 and i==0:
      i,j=p[0],p[1]
      k=n//i
      s+=k*j
      n-=k*i
    elif q<1 and i==1:
      i,j=p[1],p[0]
      k=n//i
      s+=k*j
      n-=k*i
  n+=s
print(n)

Submission status: Wrong Answer

Solution code:
n = int(input())

rate = []

for i in range(2):
    rate.append(list(map(int, input().split(' '))))

g_ab = 1 if rate[0][0] // rate[1][0] == 0 else rate[0][0] // rate[1][0]
s_ab = 1 if rate[0][1] // rate[1][1] == 0 else rate[0][1] // rate[1][1]
b_ab = 1 if rate[0][2] // rate[1][2] == 0 else rate[0][2] // rate[1][2]

g_ba = 1 if rate[1][0] // rate[0][0] == 0 else rate[1][0] // rate[0][0]
s_ba = 1 if rate[1][1] // rate[0][1] == 0 else rate[1][1] // rate[0][1]
b_ba = 1 if rate[1][2] // rate[0][2] == 0 else rate[1][2] // rate[0][2]

rate_k = [g_ab * g_ba, s_ab * s_ba, b_ab * b_ba]

print(max(rate_k) * n)

Submission status: Wrong Answer

Solution code:
import sys,heapq
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True  and False
def ddprint(x):
  if DBG:
    print(x)

n = inn()
aa = inl()
bb = inl()
mx = n
for i in range(3):
    a = aa[i]
    b = bb[i]
    if a>=b:
        continue
    d = n + (b-a)*(n//a)
    ddprint("i {} d {} a {} b {} nb {}".format(i,d,a,b,n//b))
    mx = max(mx,d)

ddprint(mx)
n = mx
for i in range(3):
    a = aa[i]
    b = bb[i]
    if a<=b:
        continue
    d = n + (a-b)*(n//b)
    ddprint("i {} d {} a {} b {} nb {}".format(i,d,a,b,n//b))
    mx = max(mx,d)

print(mx)

Submission status: Wrong Answer

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

if A[0] / B[0] >= A[1]/B[1] and A[0] / B[0] >= A[2] / B[2]:
    idx = 0
    if A[1]/B[1] > A[2]/B[2]:
        idx2 = 1
        midx = 2
    else:
        idx2 = 2
        midx = 1
elif A[1] / B[1] >= A[0]/B[0] and A[1] / B[1] >= A[2] / B[2]:
    idx = 1
    if A[0]/B[0] > A[2]/B[2]:
        idx2 = 0
        midx = 2
    else:
        idx2 = 2
        midx = 0

elif A[2] / B[2] > A[0]/B[0] and A[2] / B[2] > A[1] / B[1]:
    idx = 2
    if A[1]/B[1] > A[0]/B[0]:
        idx2 = 1
        midx = 0
    else:
        idx2 = 0
        midx = 1
else:
    print(aaa)
#A -> B
ans = n
money = n // A[idx]
ans1 = money * B[idx]
if n//A[idx] != 0:
    dn = n % A[idx] 
    money = dn // A[idx2]
    ans1 += money * B[idx2]
    if dn // A[idx] != 0:
        dng = dn % A[midx]
        money = dng // A[midx]
        ans1 += money * B[midx]
#B -> A
money = n // B[midx]
ans2 = money * A[midx]
if n//B[idx] != 0:
    dn = n % B[idx]
    money = dn // B[idx2]
    ans2 += money * A[idx2]
    if dn // B[idx] != 0:
        dng = dn % B[idx]
        money = dng // B[idx]
        ans2 += money * A[idx]
print(max(ans,ans1,ans2))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

n = int(input())
g_a, s_a, b_a = map(int, input().split())
g_b, s_b, b_b = map(int, input().split())
g_value = max(g_a/g_b, g_b/g_a)
s_value = max(s_a/s_b, s_b/s_a)
b_value = max(b_a/b_b, b_b/b_a)
gsb = [[g_value, max(g_a, g_b), min(g_a, g_b)], [s_value, max(s_a, s_b), min(s_a, s_b)], [b_value, max(b_a, b_b), min(b_a, b_b)]]
gsb.sort(reverse=True)
ans = 0
cnt, mod = divmod(n, gsb[0][2])
ans += cnt*gsb[0][1]
if mod != 0:
    cnt, mod = divmod(mod, gsb[1][2])
    ans += cnt*gsb[1][1]
    if mod != 0:
        cnt, mod = divmod(mod, gsb[2][2])
        ans += cnt*gsb[2][1]
print(ans+mod)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
AtCoder A
"""

import sys, math, random
import numpy as np

# N = int(input())
# A = list(map(int,input().split())) # N row 1 column
# A = [int(input()) for _ in range(N)] # 1 row N column
# S = str(input()) # str(input()) == input() -> 'abc'
# S = list(input()) # abc -> ['a','b','c']
# S.replace('ABC','X') # "testABCABC" -> "testXX"
 
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
 
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def main():
    N=I()
    A=LI()
    B=LI()
    m=0
    n=5
    for i in range(3):
        tmp=int(N/min(A[i],B[i]))*max(A[i],B[i])
        if tmp>m:
            m=tmp
            n=i

    m2=0
    n2=5
    if n==5:
        print(m)
        exit()
    N=N%min(A[n],B[n])
    for i in range(3):
        if i!=n:
            tmp=int(N/min(A[i],B[i]))*max(A[i],B[i])
            if tmp>m:
                m2=tmp
                n2=i

    m3=0
    n3=5
    if n2==5:
        print(m+m2)
        exit()
    N=N%min(A[n2],B[n2])
    for i in range(3):
        if i!=n and i!=n2:
            tmp=int(N/min(A[i],B[i]))*max(A[i],B[i])
            if tmp>m:
                m3=tmp
                n3=i
    print(m+m2+m3)


    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from itertools import permutations as pp
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
fir = []
sec = []
ans = n
for i in range(3):
  if a[i] == b[i]:
    continue
  elif a[i]>b[i]:
    sec.append((a[i],b[i]))
  else:
    fir.append((b[i],a[i]))
for xl in pp(fir):
  for yl in pp(sec):
    gn = n
    en = 0
    for aft,bef in xl:
      use = gn//bef
      gn %= bef
      en += use*aft
    gn += en
    en = 0
    for aft,bef in yl:
      use = gn//bef
      gn %= bef
      en += use*aft
    gn += en
    if ans < gn:
      ans = gn
print(gn)

Submission status: Wrong Answer

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

if A[0] / B[0] >= A[1]/B[1] and A[0] / B[0] >= A[2] / B[2]:
    idx = 0
    if A[1]/B[1] > A[2]/B[2]:
        idx2 = 1
        midx = 2
    else:
        idx2 = 2
        midx = 1
elif A[1] / B[1] >= A[0]/B[0] and A[1] / B[1] >= A[2] / B[2]:
    idx = 1
    if A[0]/B[0] > A[2]/B[2]:
        idx2 = 0
        midx = 2
    else:
        idx2 = 2
        midx = 0

elif A[2] / B[2] > A[0]/B[0] and A[2] / B[2] > A[1] / B[1]:
    idx = 2
    if A[1]/B[1] > A[0]/B[0]:
        idx2 = 1
        midx = 0
    else:
        idx2 = 0
        midx = 1
else:
    print(aaa)
#A -> B
ans = n
money = n // A[idx]
ans1 = money * B[idx]
if n//A[idx] != 0:
    dn = n % A[idx]
    money = n // A[idx2]
    ans1 += money * B[idx2]
    if dn // A[idx] != 0:
        dng = dn % A[midx]
        money = dng // A[midx]
        ans1 += money * B[midx]
#B -> A
money = n // B[midx]
ans2 = money * A[midx]
if n//B[idx] != 0:
    dn = n % B[idx]
    money = n // B[idx2]
    ans2 += money * A[idx2]
    if dn // B[idx] != 0:
        dng = dn % B[idx]
        money = dng // B[idx]
        ans2 += money * A[idx]
print(max(ans,ans1,ans2))

Submission status: Wrong Answer

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

ans = N*1
for i in range(3):
    d, m = divmod(N, GA[i])
    ans = max(m+GB[i]*d, ans)

for i in range(3):
    d, m = divmod(N, GB[i])
    ans = max(m+GA[i]*d, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))

def calc_profit(n, a, b):
    profit = 0
    for i in sorted(range(3), reverse=True, key=lambda i: (b[i] - a[i])/a[i]):
        if b[i] - a[i] <= 0:
            continue
        num = min(n // a[i], n)
        profit += num * (b[i] - a[i])
        n -= num * a[i]
    return profit

n += calc_profit(n, a, b)
n += calc_profit(n, b, a)
print(n)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def YesNo(x): return 'Yes' if x else 'No'
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

def main():
    N = II()
    ga, sa, ba = LI()
    gb, sb, bb = LI()
    rates = [[ga / gb, ga, gb], [sa / sb, sa, sb], [ba / bb, ba, bb]]
    rates.sort()
    metals = [0, 0, 0]
    ans = N
    # a
    for i in range(3):
        r, a, b = rates[i]
        if r >= 1:
            break
        metals[i] += ans // a
        ans = ans % a
    # b:
    ans += sum(metals[i] * rates[i][2] for i in range(3))
    metals = [0, 0, 0]
    for i in range(-1, -3, -1):
        r, a, b = rates[i]
        if r <= 1:
            break
        metals[i] += ans // b
        ans = ans % b
    # a
    ans += sum(metals[i] * rates[i][1] for i in range(3))
    return ans

print(main())

Submission status: Wrong Answer

Solution code:
n=int(input())
o=lambda:list(map(int,input().split()))
a=o()
b=o()
r=list(map(lambda x:x[1]/x[0],zip(a,b)))
s=0
for i in range(2):
  for q,p in sorted(zip(r,zip(a,b)),key=lambda x:x[0])[::-1]:
    if q>1 and i==0:
      i,j=p[0],p[1]
      k=n//i
      s+=k*j
      n-=k*i
    elif q<1 and i==1:
      i,j=p[1],p[0]
      k=n//i
      s+=k*j
      n-=k*i
print(s+n)

Submission status: Wrong Answer

Solution code:
n=int(input())
la=list(map(int,input().split()))
lb=list(map(int,input().split()))
ab=max(lb[0]-la[0],lb[1]-la[1],lb[2]-la[2],0)
ba=max(la[0]-lb[0],la[1]-lb[1],la[2]-lb[2],0)
print(max(n*(ab+1),n*(ba+1)))

Submission status: Wrong Answer

Solution code:
from itertools import permutations as pp
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
fir = []
sec = []
ans = n
for i in range(3):
  if a[i] == b[i]:
    continue
  elif a[i]>b[i]:
    sec.append((a[i],b[i]))
  else:
    fir.append((b[i],a[i]))
for xl in pp(fir):
  for yl in pp(sec):
    gn = n
    for aft,bef in xl:
      gn = (gn%bef) + (gn//bef)*aft
    for aft,bef in yl:
      gn = (gn%bef) + (gn//bef)*aft
    if ans < gn:
      ans = gn
print(gn)

Submission status: Wrong Answer

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

f = [[0, 0],[0, 1],[0, 2]]
for i in range(3):
  k = 1
  for j in range(3):
    if i != j:
      k *= A[i]
  f[i][0] = (B[i] - A[i]) * k

f.sort(reverse = True)
D = 0
for i in range(3):
  if f[i][0] > 0:
    D += (N // A[f[i][1]]) * B[f[i][1]]
    N -= N // A[f[i][1]]
  else:
    D += N
    break

N = D
D = 0
for i in range(2, -1, -1):
  if f[i][0] < 0:
    D += (N // B[f[i][1]]) * A[f[i][1]]
    N -= N // B[f[i][1]]
  else:
    D += N
    break

print(D)

Submission status: Wrong Answer

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

f = [[0, 0],[0, 1],[0, 2]]
for i in range(3):
  k = 1
  for j in range(3):
    if i != j:
      k *= A[i]
  f[i][0] = (B[i] - A[i]) * k

f.sort(reverse = True)
D = 0
for i in range(3):
  if f[i][0] > 0:
    D += (N // A[f[i][1]]) * B[f[i][1]]
    N -= N // A[f[i][1]]
  else:
    D += N
    break

N = D
D = 0
for i in range(2, -1, -1):
  if f[i][0] < 0:
    D += (N // B[f[i][1]]) * A[f[i][1]]
    N -= N // A[f[i][1]]
  else:
    D += N
    break

print(D)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
max_rate = max([A[0]/B[0],A[1]/B[1],A[2]/B[2],B[0]/A[0],B[1]/A[1],B[2]/A[2]])
new_N = N*max_rate
print(int(new_N))

Submission status: Wrong Answer

Solution code:
N=int(input())
ga,sa,ba=map(int, input().split())
gb,sb,bb=map(int, input().split())
print(max(N,N//ga*gb+N%ga,N//gb*ga+N%gb,N//sa*sb+N%sa,N//sb*sa+N%sb,N//ba*bb+N%ba,N//bb*ba+N%bb))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))

def calc_profit(n, a, b):
    profit = 0
    for i in sorted(range(3), reverse=True, key=lambda i: (b[i] - a[i])/a[i]):
        if b[i] - a[i] <= 0:
            continue
        num = n // a[i]
        profit += num * (b[i] - a[i])
        n -= num * a[i]
    return profit

n += calc_profit(n, a, b)
n += calc_profit(n, b, a)
print(n)

Submission status: Wrong Answer

Solution code:
def solve(n, ga, sa, ba, gb, sb, bb):
    rate = [(gb / ga, -ga, gb), (sb / sa, -sa, sb), (bb / ba, -ba, bb)]
    rate.sort(reverse=True)

    ans = n
    pr, pa, pb = rate[0]
    qr, qa, qb = rate[1]
    rr, ra, rb = rate[2]
    pa, qa, ra = -pa, -qa, -ra
    if pr <= 1:
        return n
    for p in range(n // pa * pa, -1, -pa):
        tmp1 = p // pa * pb
        if qr <= 1:
            ans = max(ans, tmp1)
            break
        for q in range((n - p) // qa * qa, -1, -qa):
            tmp2 = tmp1 + q // qa * qb
            if rr <= 1:
                ans = max(ans, tmp2)
                break
            # print(pa, qa, ra)
            # print(p, q, (n - p - q) // ra * ra)
            # print(tmp2 + (n - p - q) // ra * rb)
            ans = max(ans, tmp2 + (n - p - q) // ra * rb)
    return ans


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:
n=int(input())
la=list(map(int,input().split()))
lb=list(map(int,input().split()))
ab=max(lb[0]-la[0],lb[1]-la[1],lb[2]-la[2],0)
ba=max(la[0]-lb[0],la[1]-lb[1],la[2]-lb[2],0)
print(n*(ab+1)*(ba+1))

Submission status: Wrong Answer

Solution code:
import sys,heapq
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True # and False
def ddprint(x):
  if DBG:
    print(x)

n = inn()
aa = inl()
bb = inl()
mx = n
for i in range(3):
    a = aa[i]
    b = bb[i]
    if a<b:
        a,b = b,a
    d = n + (a-b)*(n//b)
    mx = max(mx,d)

print(mx)

Submission status: Wrong Answer

Solution code:
def solve(n, ga, sa, ba, gb, sb, bb):
    rate = [(gb / ga, -ga, gb), (sb / sa, -sa, sb), (bb / ba, -ba, bb)]
    rate.sort(reverse=True)

    ans = n
    pr, pa, pb = rate[0]
    qr, qa, qb = rate[1]
    rr, ra, rb = rate[2]
    if pr <= 1:
        return n
    for p in range(n // pa * pa, -1, pa):
        tmp1 = p // -pa * pb
        if qr <= 1:
            ans = max(ans, tmp1)
            break
        for q in range((n - p) // qa * qa, -1, qa):
            tmp2 = tmp1 + q // -qa * qb
            if rr <= 1:
                ans = max(ans, tmp2)
                break
            ans = max(ans, tmp2 + (n - p - q) // -ra * rb)
    return ans


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:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
fir = []
sec = []
for i in range(3):
  sec.append((a[i]/b[i],a[i],b[i]))
  fir.append((b[i]/a[i],b[i],a[i]))
fir.sort(reverse=True)
sec.sort(reverse=True)
en = 0
for rate,aft,bef in fir:
  if rate <= 1:
    continue
  use = n//bef
  n %= bef
  en += use*aft
n += en
en = 0
for rate,aft,bef in sec:
  if rate < 1:
    continue
  use = n//bef
  n %= bef
  en += use*aft
print(n+en)

Submission status: Wrong Answer

Solution code:
n=int(input())
o=lambda:list(map(int,input().split()))
a=o()
b=o()
r=list(map(lambda x:max(x)/min(x),zip(a,b)))
s=0
for q,p in sorted(zip(r,zip(a,b)),key=lambda x:x[0])[::-1]:
  i,j=min(p),max(p)
  k=n//i
  s+=k*j
  n-=k*i
print(s+n)

Submission status: Wrong Answer

Solution code:
from sys import stdin

def increase(dongri, b, bank_idx):
    _max = 0
    _idx = 0
    for i in range(3):
        diff = b[bank_idx][i] - b[1-bank_idx][i]
        if diff > _max:
            _max = diff
            _idx = i

    if _max > 0:
        d,r = divmod(dongri, b[1 - bank_idx][_idx])
        return r + d * b[bank_idx][_idx]
    else:
        return dongri

def main():
    dongri = int(input())
    b = [tuple(map(int,input().split())) for x in range(2)]

    ab = increase(dongri, b, 1)
    ba = increase(ab, b, 0)
    print(ba)

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()))
l = []
for i in range(3):
  l.append((a[i]/b[i],a[i],b[i]))
  l.append((b[i]/a[i],b[i],a[i]))
l.sort(reverse=True)
en = 0
for rate,aft,bef in l:
  if rate < 1:
    continue
  use = n//bef
  n %= bef
  en += use*aft
print(en+n)

Submission status: Wrong Answer

Solution code:
# 金 -> ドン は1種までしかないので3つ解く
# Aを2回使う意味はない
# A -> B or B -> A
N = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]

def F(N,a,b):
  # A -> B
  x = N//a
  y = N - a*x + b*x
  return y

answer = N
for i in range(3):
  a,b = A[i], B[i]
  for x in [F(N,a,b), F(N,b,a)]:
    answer = max(answer, x)

print(answer)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
l = []
for i in range(3):
  l.append((a[i]/b[i],a[i],b[i]))
  l.append((b[i]/a[i],b[i],a[i]))
l.sort(reverse=True)
en = 0
for rate,aft,bef in l:
  if rate < 1:
    continue
  use = n//bef
  n %= bef
  en += use*aft
print(en)

Submission status: Wrong Answer

Solution code:
from sys import stdin

def main():
    dongri = int(input())
    b = [tuple(map(int,input().split())) for x in range(2)]

    _max = -1
    _idx = ()

    for i in range(3):
        diff = abs(b[1][i] - b[0][i])
        if diff > _max:
            _max = diff
            _idx = (0 if b[1][i] > b[0][i] else 1, i)

    d,r = divmod(dongri, b[_idx[0]][_idx[1]])
    ans = r + d * b[1 - _idx[0]][_idx[1]]
    print(ans)

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()))
aHIKUb=[a[i]-b[i] for i in range(3)]
n=n*(abs(min(aHIKUb))+1)
n=n*(max(aHIKUb)+1)
print(n)

Submission status: Wrong Answer

Solution code:
n=int(input())
o=lambda:list(map(int,input().split()))
a=o()
b=o()
d=list(map(lambda x:abs(x[0]-x[1]),zip(a,b)))
for i,v in zip(zip(a,b),d):
  m,k=max(i),min(i)
  if v==max(d):
    print(int(m/k*n))
    exit(0)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

def main():
    N = int(input())
    gsba = list(map(int, input().split()))
    gsbb = list(map(int, input().split()))
    
    # ナップザック問題として解く
    w = gsba
    v = [gsbb[i]-gsba[i] for i in range(3)]
    dp = [0]*(N+1)

    for i in range(3):
        for j in range(N+1):
            if j >= w[i]:
                dp[j] = max(dp[j],dp[j-w[i]]+v[i])

    M = dp[-1] + N
    w = gsbb
    v = [gsba[i]-gsbb[i] for i in range(3)]
    dp = [0]*(M+1)

    for i in range(3):
        for j in range(M+1):
            if j >= w[i]:
                dp[j] = max(dp[j],dp[j-w[i]]+v[i])

    print(dp[-1] + M)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

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

    ans = 0
    flags = [ga >= gb, sa >= sb, ba >= bb]

    for x in [range(N//ga+1), [0]][flags[0]]:
        for y in [range((N-ga*x)//sa+1), [0]][flags[1]]:
            for z in [range((N-ga*x-sa*y)//ba+1), [0]][flags[2]]:
                M = N + (gb-ga)*x + (sb-sa)*y + (bb-ba)*z# 取引所Bでのどんぐりの数
                for p in [[0], range(M//gb+1)][flags[0]]:
                    for q in [[0], range((M-gb*p)//sb+1)][flags[1]]:
                        for r in [[0], range((M-gb*p-sb*q)//bb+1)][flags[2]]:
                            ans = max(ans, M+(ga-gb)*p+(sa-sb)*q+(ba-bb)*r)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

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

    ans = 0
    flags = [ga >= gb, sa >= sb, ba >= bb]

    for x in [range(N//ga+1), [0]][flags[0]]:
        # yl = [range((N-ga*x)//sa+1), [0]][flags[1]]
        for y in [range((N-ga*x)//sa+1), [0]][flags[1]]:
            for z in [range((N-ga*x-sa*y)//ba+1), [0]][flags[2]]:
                M = N + (gb-ga)*x + (sb-sa)*y + (bb-ba)*z# 取引所Bでのどんぐりの数
                for p in [[0], range(M//gb+1)][flags[0]]:
                    for q in [[0], range((M-gb*p)//sb+1)][flags[1]]:
                        for r in [[0], range((M-gb*p-sb*q)//bb+1)][flags[2]]:
                            ans = max(ans, M+(ga-gb)*p+(sa-sb)*q+(ba-bb)*r)
    print(ans)

if __name__ == "__main__":
    main()