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:
def solve(n, ga, sa, ba, gb, sb, bb):
    gd, sd, bd = gb - ga, sb - sa, bb - ba

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


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

Submission status: Wrong Answer

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

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


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

Submission status: Wrong Answer

Solution code:
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):
    lcm=a[0]*a[1]//gcd(a[0],a[1])
    lcm=lcm*a[2]//gcd(a[2],lcm)

    F=[[a[i],b[i]] for i in range(3)]
    F.sort(reverse=True)
    a=sorted([F[i][0] for i in range(3)])
    b=sorted([F[i][1] for i in range(3)])
    

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

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

import sys
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 queue

"""
N = int(input())
#S = input()
# (N,M) = (int(i) for i in input().split(" "))
# A = [int(i) for i in input().split()]
A = []
for i in range(N):
	A.append(int(input()))

print(A)

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

C = [[B[i]/A[i], A[i], B[i]] for i in range(3)]
C = sorted(C)[::-1]

max_num = N
if C[0][0] > 1:
	max_F = int(N / C[0][1])
	max_num = max_F*C[0][2]
	cur_num = max_num
	lasts = N - max_F * C[0][1]
	if C[1][0] > 1:
		while lasts*C[1][0]+cur_num >= max_num and cur_num >= 0:
			max_S = int(lasts / C[1][1])
			max_num_S = max_S*C[1][2]
			cur_num_S = max_num_S
			max_num_T = 0
			lasts_S = lasts - max_S * C[1][1]
			if C[2][0] > 1:
				while lasts_S*C[2][0]+cur_num_S > max_num_S and cur_num_S >= 0:
					if cur_num_S + int(lasts_S / C[2][1]) * C[2][2] > max_num_S:
						max_num_S = cur_num_S + int(lasts_S / C[2][1]) * C[2][2]
					lasts_S += C[1][1]
					cur_num_S -= C[1][2]
			if max_num_S + cur_num > max_num:
				max_num = cur_num + max_num_S
			lasts += C[0][1]
			cur_num -= C[0][2]


C = [[A[i]/B[i], B[i], A[i]] for i in range(3)]
C = sorted(C)[::-1]
N = max_num
if C[0][0] > 1:
	max_F = int(N / C[0][1])
	max_num = max_F*C[0][2]
	cur_num = max_num
	lasts = N - max_F * C[0][1]
	if C[1][0] > 1:
		while lasts*C[1][0]+cur_num >= max_num and cur_num >= 0:
			max_S = int(lasts / C[1][1])
			max_num_S = max_S*C[1][2]
			cur_num_S = max_num_S
			max_num_T = 0
			lasts_S = lasts - max_S * C[1][1]
			if C[2][0] > 1:
				while lasts_S*C[2][0]+cur_num_S > max_num_S and cur_num_S >= 0:
					if cur_num_S + int(lasts_S / C[2][1]) * C[2][2] > max_num_S:
						max_num_S = cur_num_S + int(lasts_S / C[2][1]) * C[2][2]
					lasts_S += C[1][1]
					cur_num_S -= C[1][2]
			if max_num_S + cur_num > max_num:
				max_num = cur_num + max_num_S
			lasts += C[0][1]
			cur_num -= C[0][2]
print(max_num)

Submission status: Wrong Answer

Solution code:
import queue

"""
N = int(input())
#S = input()
# (N,M) = (int(i) for i in input().split(" "))
# A = [int(i) for i in input().split()]
A = []
for i in range(N):
	A.append(int(input()))

print(A)

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

C = [[B[i]/A[i], A[i], B[i]] for i in range(3)]
C = sorted(C)[::-1]

max_num = N
if C[0][0] > 1:
	max_F = int(N / C[0][1])
	max_num = max_F*C[0][2]
	cur_num = max_num
	lasts = N - max_F * C[0][1]
	while lasts*C[1][0]+cur_num >= max_num and cur_num >= 0:
		max_S = int(lasts / C[1][1])
		max_num_S = max_S*C[1][2]
		cur_num_S = max_num_S
		max_num_T = 0
		lasts_S = lasts - max_S * C[1][1]
		while lasts_S*C[2][0]+cur_num_S > max_num_S and cur_num_S >= 0:
			if cur_num_S + int(lasts_S / C[2][1]) * C[2][2] > max_num_S:
				max_num_S = cur_num_S + int(lasts_S / C[2][1]) * C[2][2]
			lasts_S += C[1][1]
			cur_num_S -= C[1][2]
		if max_num_S + cur_num > max_num:
			max_num = cur_num + max_num_S
		lasts += C[0][1]
		cur_num -= C[0][2]


C = [[A[i]/B[i], B[i], A[i]] for i in range(3)]
C = sorted(C)[::-1]
N = max_num
if C[0][0] > 1:
	max_F = int(N / C[0][1])
	max_num = max_F*C[0][2]
	cur_num = max_num
	lasts = N - max_F * C[0][1]
	while lasts*C[1][0]+cur_num >= max_num and cur_num >= 0:
		max_S = int(lasts / C[1][1])
		max_num_S = max_S*C[1][2]
		cur_num_S = max_num_S
		max_num_T = 0
		lasts_S = lasts - max_S * C[1][1]
		while lasts_S*C[2][0]+cur_num_S > max_num_S and cur_num_S >= 0:
			if cur_num_S + int(lasts_S / C[2][1]) * C[2][2] > max_num_S:
				max_num_S = cur_num_S + int(lasts_S / C[2][1]) * C[2][2]
			lasts_S += C[1][1]
			cur_num_S -= C[1][2]
		if max_num_S + cur_num > max_num:
			max_num = cur_num + max_num_S
		lasts += C[0][1]
		cur_num -= C[0][2]
print(max_num)

Submission status: Wrong Answer

Solution code:
# coding:utf-8

import math

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

    a = {
        "g": [ga,gb],
        "s": [sa,sb],
        "b": [ba,bb]
    }
    mx = n
    for i in a:
        for j in a:
            if i == j:
                continue
            for k in a:
                if k == i or k == j:
                    continue
                tmpN = n
                c1 = 0
                c2 = 0
                c3 = 0
                if float(a[i][1]) / a[i][0] >= 1:
                    c1 = tmpN / a[i][0]
                    tmpN = tmpN % a[i][0]
                if float(a[j][1]) / a[j][0] >= 1:
                    c2 = tmpN / a[j][0]
                    tmpN = tmpN % a[j][0]
                if float(a[k][1]) / a[k][0] >= 1:
                    c3 = tmpN / a[k][0]
                    tmpN = tmpN % a[k][0]
                tmpT = tmpN + c1 * a[i][1] + c2 * a[j][1] + c3 * a[k][1]
                mx = max(mx,tmpT)
    n = mx
    mx = n
    for i in a:
        for j in a:
            if i == j:
                continue
            for k in a:
                if k == i or k == j:
                    continue
                tmpN = n
                c1 = 0
                c2 = 0
                c3 = 0
                if float(a[i][0]) / a[i][1] >= 1:
                    c1 = tmpN / a[i][1]
                    tmpN = tmpN % a[i][1]
                if float(a[j][0]) / a[j][1] >= 1:
                    c2 = tmpN / a[j][1]
                    tmpN = tmpN % a[j][1]
                if float(a[k][0]) / a[k][1] >= 1:
                    c3 = tmpN / a[k][1]
                    tmpN = tmpN % a[k][1]
                tmpT = tmpN + c1 * a[i][0] + c2 * a[j][0] + c3 * a[k][0]
                mx = max(mx,tmpT)
    print mx

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

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

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

#D
def D():
    n = I()
    a = LI()
    b = LI()
    v = list(map(lambda x : x[1] - x[0], zip(a,b)))
    dp = [[n for i in range(n+1)] for j in range(3)]
    for i in range(3):
        for w in range(n):
            if w+a[i] <= n:
                dp[i][w+a[i]] = max(dp[i-1][w+a[i]],dp[i][w]+v[i])
    n = dp[2][-1]
    a,b = b,a
    v = list(map(lambda x : x[1] - x[0], zip(a,b)))
    dp = [[n for i in range(n+1)] for j in range(3)]
    for i in range(3):
        for w in range(n):
            if w+a[i] <= n:
                dp[i][w+a[i]] = max(dp[i-1][w+a[i]],dp[i][w]+v[i])
    n = dp[2][-1]
    print(n)
    return

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

    return

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

    return

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

    return

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

    return

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

    return

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

    return

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

Submission status: Wrong Answer

Solution code:
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):
    lcm=a[0]*a[1]//gcd(a[0],a[1])
    lcm=lcm*a[2]//gcd(a[2],lcm)

    F=[[a[i],b[i]] for i in range(3)]
    F.sort()
    a=sorted([F[i][0] for i in range(3)],reverse=True)
    b=sorted([F[i][1] for i in range(3)],reverse=True)
    

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

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

import sys
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:
def solve(n, ga, sa, ba, gb, sb, bb):
    gd, sd, bd = gb - ga, sb - sa, bb - ba

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


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

Submission status: Wrong Answer

Solution code:
N = int(input())

A = [0] * N

Submission status: Wrong Answer

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

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


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

Submission status: Wrong Answer

Solution code:
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((b[i],a[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]
        fa=n
        for i in range(n+1):
            if(ga*i>n):
                break
            k=n-ga*i
            tmp=i*gb+k%ba+(k//ba)*bb
            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==ba):
            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-ga*i-sa*j
                tmp=i*gb+j*sb+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:
def solve(n, ga, sa, ba, gb, sb, bb):
    gd, sd, bd = gb - ga, sb - sa, bb - ba

    if bd <= 0:
        if sd <= 0:
            if gd <= 0:
                return n
            return n + n // ga * gd
        if gd <= 0:
            return n + n // sa * sd
        return max(n + g * gd + (n - g * ga) // sa * sd for g in range(n // ga + 1))
    if sd <= 0:
        if gd <= 0:
            return n + n // ba * bd
        return max(n + g * gd + (n - g * ga) // ba * bd for g in range(n // ga + 1))
    if gd <= 0:
        return max(n + s * sd + (n - s * sa) // ba * bd for s in range(n // sa + 1))
    (gd, ga), (sd, sa), (bd, ba) = sorted([(gd, -ga), (sd, -sa), (bd, -ba)], reverse=True)
    ga, sa, ba = -ga, -sa, -ba
    return max(n + g * gd + s * sd + (n - g * ga - s * sa) // ba * bd
               for g in range(n // ga // 2, n // ga + 1)
               for s in range((n - g * ga) // sa + 1))


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

Submission status: Wrong Answer

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

def arbitrage(g1, s1, b1, g2, s2, b2, n):
    before = [g1, s1, b1]
    after = [g2, s2, b2]
    dp = []
    for i in range(4):
        dp_plus = [0] * (n + 1)
        dp.append(dp_plus)
    for i in range(3):
        for j in range(n + 1):
            if j < before[i]:
                dp[i + 1][j] = dp[i][j]
            else:
                dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - before[i]] + after[i])
    return max(n, dp[3][n])

trade1 = arbitrage(ga, sa, ba, gb, sb, bb, n)
trade2 = arbitrage(gb, sb, bb, ga, sa, ba, trade1)
print(trade2)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import numpy as np

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

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

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

sum = N
a1 = 0
b1 = 0

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

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

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

print(sum)

Submission status: Wrong Answer

Solution code:
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):
    lcm=a[0]*a[1]//gcd(a[0],a[1])
    lcm=lcm*a[2]//gcd(a[2],lcm)

    F=[[a[i],b[i]] for i in range(3)]
    F.sort()
    a=[F[i][0] for i in range(3)]
    b=[F[i][1] for i in range(3)]
    

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

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

import sys
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 sys
import numpy as np

def main():
    input = sys.stdin.readline
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    dp = np.zeros(N+1, dtype=np.int)
    for i in range(3):
        a = A[i]
        n = N // a
        for k in range(1, n+1):
            dp[k*a:] = np.maximum(dp[k*a:], dp[:N-k*a+1] + k*B[i])

    M = dp[N]
    dp = np.zeros(M+1, dtype=np.int)
    for i in range(3):
        a = B[i]
        n = M // a
        for k in range(1, n+1):
            dp[k*a:] = np.maximum(dp[k*a:], dp[:M-k*a+1] + k*A[i])

    return dp[M]


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

Submission status: Wrong Answer

Solution code:
n = int(input())
ra = [int(v) for v in input().split()]
rb = [int(v) for v in input().split()]
p = [n, 0, 0, 0]

rab = []
rba = []
for i in range(3):
  rab.append((i, rb[i] / ra[i]))
  rba.append((i, ra[i] / rb[i]))

rab.sort(key=lambda x:x[1], reverse=True)
rba.sort(key=lambda x:x[1], reverse=True)

#print("巣", p)

#Aで金銀銅に交換
for r in rab:
  if r[1] >= 1:
    i = r[0]
    d = p[0]
    p[0] = d % ra[i]
    p[i + 1] = d // ra[i]

#print("Aで金銀銅に交換", p)
#print("rate", rab)

#Bでドングリに交換
for i in range(3):
  p[0] += p[i + 1] * rb[i]
  p[i + 1] = 0
#print("Bでドングリに交換", p)
#print("rate", rb)

#Bで金銀銅に交換
for r in rba:
  if r[1] >= 1:
    i = r[0]
    d = p[0]
    p[0] = d % rb[i]
    p[i + 1] = d // rb[i]

#print("Bで金銀銅に交換", p)

#Aでドングリに交換
for i in range(3):
  p[0] += p[i + 1] * ra[i]
  p[i + 1] = 0
#print("Aでドングリに交換", p)
print(p[0])

Submission status: Wrong Answer

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

    if bd <= 0:
        if sd <= 0:
            if gd <= 0:
                return n
            return n + n // ga * gd
        if gd <= 0:
            return n + n // sa * sd
        return max(n + g * gd + (n - g * ga) // sa * sd for g in range(n // ga + 1))
    if sd <= 0:
        if gd <= 0:
            return n + n // ba * bd
        return max(n + g * gd + (n - g * ga) // ba * bd for g in range(n // ga + 1))
    if gd <= 0:
        return max(n + s * sd + (n - s * sa) // ba * bd for s in range(n // sa + 1))
    if (gd, -ga) < (bd, -ba):
        ga, gd, ba, bd = ba, bd, ga, gd
    return max(n + g * gd + s * sd + (n - g * ga - s * sa) // ba * bd
               for g in range(n // ga // 2, n // ga + 1)
               for s in range((n - g * ga) // sa // 2, (n - g * ga) // sa + 1))


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

Submission status: Wrong Answer

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

    if bd <= 0:
        if sd <= 0:
            if gd <= 0:
                return n
            return n + n // ga * gd
        if gd <= 0:
            return n + n // sa * sd
        return max(n + g * gd + (n - g * ga) // sa * sd for g in range(n // ga + 1))
    if sd <= 0:
        if gd <= 0:
            return n + n // ba * bd
        return max(n + g * gd + (n - g * ga) // ba * bd for g in range(n // ga + 1))
    if gd <= 0:
        return max(n + s * sd + (n - s * sa) // ba * bd for s in range(n // sa + 1))
    if gd < bd:
        ga, gd, ba, bd = ba, bd, ga, gd
    return max(n + g * gd + s * sd + (n - g * ga - s * sa) // ba * bd
               for g in range(n // ga // 2, n // ga + 1)
               for s in range((n - g * ga) // sa // 2, (n - g * ga) // sa + 1))


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

Submission status: Wrong Answer

Solution code:
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):
    lcm=a[0]*a[1]//gcd(a[0],a[1])
    lcm=lcm*a[2]//gcd(a[2],lcm)

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

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

import sys
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 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], i) for i in range(3) if B[i] > A[i]]
    AB = [(A[i]/B[i], i) for i in range(3) if A[i] > B[i]]
    BA.sort(reverse=True)
    AB.sort(reverse=True)
    now = N

    money = [0, 0, 0]
    for (_, i) in BA:
        money[i] = now//A[i]
        now = now % A[i]
    for i in range(3):
        now += money[i]*B[i]

    money = [0, 0, 0]
    for (_, i) in AB:
        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:
from sys import stdin
import numpy as np

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

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

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

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

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

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

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

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

print(sum)

Submission status: Wrong Answer

Solution code:
import queue

"""
N = int(input())
#S = input()
# (N,M) = (int(i) for i in input().split(" "))
# A = [int(i) for i in input().split()]
A = []
for i in range(N):
	A.append(int(input()))

print(A)

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

C = [[B[i]/A[i], A[i], B[i]] for i in range(3)]
C = sorted(C)[::-1]

max_num = N
if C[0][0] > 1:
	max_F = int(N / C[0][1])
	max_num = max_F*C[0][2]
	cur_num = max_num
	lasts = N - max_F * C[0][1]
	if C[1][0] > 1:
		while lasts*C[1][0]+cur_num >= max_num and cur_num >= 0:
			max_S = int(lasts / C[1][1])
			max_num_S = max_S*C[1][2]
			cur_num_S = max_num_S
			max_num_T = 0
			lasts_S = lasts - max_S * C[1][1]
			while lasts_S*C[2][0]+cur_num_S > max_num_S and cur_num_S >= 0:
				if cur_num_S + int(lasts_S / C[2][1]) * C[2][2] > max_num_S:
					max_num_S = cur_num_S + int(lasts_S / C[2][1]) * C[2][2]
				lasts_S += C[0][1]
				cur_num_S -= C[1][2]
			if max_num_S + cur_num > max_num:
				max_num = cur_num + max_num_S
			lasts += C[0][1]
			cur_num -= C[0][2]


C = [[A[i]/B[i], B[i], A[i]] for i in range(3)]
C = sorted(C)[::-1]
N = max_num
if C[0][0] > 1:
	max_F = int( N/ C[0][1])
	max_num = max_F*C[0][2]
	cur_num = max_num
	lasts = N - max_F * C[0][1]
	if C[1][0] > 1:
		while lasts*C[1][0]+cur_num > max_num and cur_num >= 0:
			max_S = int(lasts / C[1][1])
			max_num_S = max_S*C[1][2]
			cur_num_S = max_num_S
			max_num_T = 0
			lasts_S = lasts - max_S * C[1][1]
			while lasts_S*C[2][0]+cur_num_S > max_num_S and cur_num_S >= 0:
				if cur_num_S + int(lasts_S / C[2][1]) * C[2][2] > max_num_S:
					max_num_S = cur_num_S + int(lasts_S / C[2][1]) * C[2][2]
				lasts_S += C[0][1]
				cur_num_S -= C[1][2]
			if max_num_S + cur_num > max_num:
				max_num = cur_num + max_num_S
			lasts += C[0][1]
			cur_num -= C[0][2]
print(max_num)

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

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

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

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

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

import sys
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))