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

gA, sA, bA = map(int, input().split())
gB, sB, bB = map(int, input().split())

def perm_rec(items, p, perms):
	if items:
		p = p + [None]
		for i in range(len(items)):
			tmp_items = list(items)
			p[-1] = tmp_items.pop(i)
			perm_rec(tmp_items, p, perms)
	else:
		perms.append(p)

def perm():
	perms_tmp = []
	perm_rec(["g", "s", "b"], [], perms_tmp)
	perms = []
	for perm in perms_tmp:
		for i in range(0, 3 ** 3):
			new_perm = list(perm)
			for j in range(3):
				if i % 3 == 2:
					new_perm[j] = new_perm[j].upper()
				elif i % 3 == 1:
					new_perm[j] = ' '
				i //= 3
			perms.append(new_perm)
	return perms

perms = perm()

def convert(items, perm, g, s, b):
	items = items.copy()
	for p in perm:
		if p == "g":
			items["d"] += g * items["g"]
			items["g"] = 0
		elif p == "s":
			items["d"] += s * items["s"]
			items["s"] = 0
		elif p == "b":
			items["d"] += b * items["b"]
			items["b"] = 0
		elif p == "G":
			c = items["d"] // g
			items["g"] += c
			items["d"] -= c * g
		elif p == "S":
			c = items["d"] // s
			items["s"] += c
			items["d"] -= c * s
		elif p == "B":
			c = items["d"] // b
			items["b"] += c
			items["d"] -= c * b
	return items
max_d = 0
for p1 in perms:
	items = convert({"d": N, "g": 0, "s": 0, "b": 0}, p1, gA, sA, bA)
	for p2 in perms:
		mid_items = convert(items, p2, gB, sB, bB)
#		print(items, p1, mid_items)
		last_items = convert(mid_items, ["g", "s", "b"], gA, sA, bA)
		d = last_items["d"]
		if d > max_d:
			max_d = d

print(max_d)

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]
        k=n
        fa=n
        for i in range(n+1):
            if(ga*i>n):
                break
            tmp=i*(gb-ga)+k%ba+(k//ba)*bb
            k-=1
            fa=max(tmp,fa)
    elif(len(ea)==3):
        ga,gb=ea[0]
        sa,sb=ea[1]
        ba,bb=ea[2]
        fa=n
        if(ga==sa):
            return main([ea[0],ea[2]],n)
        if(sa==sb):
            return main([ea[0],ea[1]],n)
        for i in range(n+1):
            if(ga*i>n):
                break
            for j in range(n+1):
                if(ga*i+sa*i>n):
                    break
                k=n-i-j
                tmp=i*(gb-ga)+j*(sb-sa)+k%ba+k//ba*bb
                fa=max(tmp,fa)
    else:
        fa=n
    return fa
n=main(ea,n)
n=main(eb,n)
print(n)

Submission status: Wrong Answer

Solution code:
n=int(input())
ga,sa,ba=map(int,input().split())
gb,sb,bb=map(int,input().split())
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 and s1s[2]!=s1s[1]:
  ll[ind]+=n//la[ind]
  n=n%la[ind]
ind=s1.index(s1s[1])
if n>=la[ind] and s1s[1]>1 and s1s[1]!=s1s[0]:
  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 and s1s[0]!=s1s[1]:
  ll[ind]+=n//lb[ind]
  n=n%lb[ind]
ind=s1.index(s1s[1])
if n>=lb[ind] and s1s[1]<1 and s1s[2]!=s1s[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:
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],a[i],b[i]])
    d.append([a[i]-b[i],a[i],b[i]])
c.sort()
d.sort()
if 1 <= c[2][0]:
    if c[2][1] <= n:
        n += n*c[2][0]
    elif 1 <= c[1][0]:
        if c[1][1] <= n:
            n += n*c[1][0]
    elif 1 <= c[0][0]:
        if c[0][1] <= n:
            n += n*c[0][0]
if 1 <= d[2][0]:
    if d[2][2] <= n:
        n += n*d[2][0]
    elif 1 <= d[1][0]:
        if d[1][2] <= n:
            n += n*d[1][0]
    elif 1 <= d[0][0]:
        if d[0][2] <= n:
            n += n*d[0][0]
print(n)

Submission status: Wrong Answer

Solution code:
inpl = lambda: list(map(int,input().split()))
N = int(input())
rate1 = []
rate1.append(inpl())
rate1.append(inpl())
rate = []
for i in range(3):
    rate.append([rate1[0][i],rate1[1][i]])
    rate[-1].sort()
    rate[-1][1] = rate[-1][1] - rate[-1][0]

ans = 0
dp = [0]
q = [0]*3
for i in range(1,N+1):
    for k in range(3):
        if i >= rate[k][0]:
            q[k] = dp[i-rate[k][0]] + rate[k][1]
        else:
            q[k] = 0
    dp.append(max(dp[i-1], max(q)))
print(dp[N])

Submission status: Wrong Answer

Solution code:
def getN():
    return int(input())

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

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



n = getN()
pra = getlist()
prb = getlist()

rita = []
ritb = []
for a, b in zip(pra, prb):
    rita.append(a/b)
    ritb.append(b/a)

if max(rita) < max(ritb):
    # こちらですべて金に帰ろ(問題文と同じ)

    idx = sorted(range(len(ritb)), key=lambda k: -ritb[k])
    pra = [pra[k] for k in idx]
    prb = [prb[k] for k in idx]
    # print(idx, pra, prb)
    # print(rita, ritb)
    kin, gin, dou = 0, 0, 0
    kin = n // pra[0]
    n -= kin * pra[0]

    if ritb[idx[1]] > 1:
        gin = n // pra[1]
        n -= gin * pra[1]

    if ritb[idx[2]] > 1:
        dou = n // pra[2]
        n -= dou * pra[2]


    print(n + kin*prb[0] + gin*prb[1] + dou*prb[2])
    import sys; sys.exit()

else:
    # 向こうで仕入れ
    idx = sorted(range(len(rita)), key=lambda k: -rita[k])
    pra = [pra[k] for k in idx]
    prb = [prb[k] for k in idx]
    # print(idx, pra, prb)
    # print(rita, ritb)
    if rita[idx[2]] > 1:
        # こちらで買うものなし
        kin = n // prb[0]
        n -= kin * prb[0]
        n += kin * pra[0]
        print(n)

        import sys;sys.exit()

    else:
        #銅かっとけ
        dou = n // pra[2]
        n -= dou * pra[2]

        if rita[idx[1]] < 1:
            #銀も買っとけ
            gin = n // pra[1]
            n -= gin * pra[1]

            n += gin * prb[1]

        n += dou * prb[2]

        kin = n // prb[0]
        n -= kin * prb[0]
        n += kin * pra[0]

        print(n)

Submission status: Wrong Answer

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

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

ans = 0
    
for d,_ in dic:
    if d == 0:
        if gy > N:
            continue
        n1 = N//gy
        N -= n1 * gy
        ans += n1 * gx
    elif d == 1:
        if sy > N:
            continue
        n1 = N//sy
        N -= n1 * sy
        ans += n1*sx
    elif d == 2:
        if by > N:
            continue

        n1 = N//by
        N -= n1 * by
        ans += n1*bx
    elif d == 3:
        if gx > N:
            continue

        n1 = N//gx
        N -= n1 * gx
        ans += n1 * gy
    elif d == 4:
        if sx > N:
            continue

        n1 = N//sx
        N -= n1*sx
        ans += n1*sy
    elif d == 5:
        if bx > N:
            continue

        n1 = N//bx
        N -= n1*bx
        ans += n1*by

print(N+int(ans))

Submission status: Wrong Answer

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

dp = [0]*(N*2)

G = N//ga

for g in range(G):
    g += 1
    dp[g] = max(dp[g], g*gb)
    rest = N - g
    S = rest//sa
    for s in range(S):
        s += 1
        dp[g+s] = max(dp[g+s], dp[g]+s*sb)
        rest = rest - s
        B = rest//ba
        for b in range(B):
            b += 1
            dp[g+s+b] = max(dp[g+s+b], dp[g+s]+b*bb)

print(max(dp))

Submission status: Wrong Answer

Solution code:
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")

N = int(sys.stdin.readline())
GA, SA, BA = list(map(int, sys.stdin.readline().split()))
GB, SB, BB = list(map(int, sys.stdin.readline().split()))

# # TODO: 整数対応
# donguri = N
# rates = np.array([GB / GA, SB / SA, BB / BA])
# if rates.max() >= 1:
#     if rates.argmax() == 0:
#         donguri = donguri // GA * GB
#     if rates.argmax() == 1:
#         donguri = donguri // SA * SB
#     if rates.argmax() == 2:
#         donguri = donguri // BA * BB
#
# rates = np.array([GA / GB, SA / SB, BA / BB])
# if rates.max() >= 1:
#     if rates.argmax() == 0:
#         donguri = donguri // GB * GA
#     if rates.argmax() == 1:
#         donguri = donguri // SB * SA
#     if rates.argmax() == 2:
#         donguri = donguri // BB * BA
#
# print(donguri)


# プラスになるなら換金して持ってく
# 整数だから換金しきれないかも
# ナップサックだ！
values = []
weights = []
if GB / GA >= 1:
    values.append(GB / GA)
    weights.append(GA)
if SB / SA >= 1:
    values.append(SB / SA)
    weights.append(SA)
if BB / BA >= 1:
    values.append(BB / BA)
    weights.append(BA)
# dp[i]: 重さ i 以下で選ぶのの価値の最大値
dp = [0] * (N + 1)
dp[0] = 0
if values:
    for i in range(1, N + 1):
        for w, v in zip(weights, values):
            if i - w >= 0:
                dp[i] = max(dp[i], dp[i - w] + v)
    # 5000->5000*5000 になったら 2回目がだめだ
    N = int(round(dp[N]))

values = []
weights = []
if GA / GB > 1:
    values.append(GA / GB)
    weights.append(GB)
if SA / SB > 1:
    values.append(SA / SB)
    weights.append(SB)
if BA / BB > 1:
    values.append(BA / BB)
    weights.append(BB)
# dp[i]: 重さ i 以下で選ぶのの価値の最大値
dp = [0] * (N + 1)
dp[0] = 0
for i in range(1, N + 1):
    for w, v in zip(weights, values):
        if i - w >= 0:
            dp[i] = max(dp[i], dp[i - w] + v)
N = int(round(dp[N]))
print(N)

Submission status: Wrong Answer

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


ups = []
downs = []
eqs = []
for i in range(3):
    if A[i] < B[i]:
        ups.append((A[i], B[i]))
    elif A[i] == B[i]:
        eqs.append((A[i], B[i]))
    else:
        downs.append((A[i], B[i]))
ans = N

if len(eqs) == 3:
    print(N)
    exit()

if len(ups) == 0:
    tmp = A
    A = B
    B = tmp
    tmp = ups
    ups = downs
    downs = tmp

if len(ups) == 3:
    gold_diff = B[0]-A[0]
    silver_diff = B[1]-A[1]
    cupper_diff = B[2]-A[2]
    for gold in range(N//A[0]+1):
        left = N - gold*A[0]
        for silver in range(left//A[1]+1):
            cupper = (left - silver*A[1])//A[2]
            tmp_ans = (N
                       + gold*gold_diff
                       + silver*silver_diff
                       + cupper*cupper_diff)
            ans = max(ans, tmp_ans)
    print(ans)
elif len(ups) == 2:
    gold_diff = ups[0][1]-ups[0][0]
    silver_diff = ups[1][1]-ups[1][0]
    for gold in range(N//ups[0][0]+1):
        left = N - gold*ups[0][0]
        silver = left // ups[1][0]
        tmp_ans = (N
                   + gold*gold_diff
                   + silver*silver_diff)
        ans = max(ans, tmp_ans)
    if len(downs) == 1:
        diff = downs[0][0]-downs[0][1]
        ans += (ans // downs[0][1]) * diff
    print(ans)
elif len(ups) == 1:
    bbans = N + (N//ups[0][0])*(ups[0][1]-ups[0][0])
    if len(downs) == 2:
        tmp_ans = bbans
        gold_diff = downs[0][0]-downs[0][1]
        silver_diff = downs[1][0]-downs[1][1]
        for gold in range(bbans//downs[0][1]+1):
            left = bbans - gold*downs[0][1]
            silver = left // downs[1][1]
            tmp_ans = (bbans
                       + gold*gold_diff
                       + silver*silver_diff)
            ans = max(ans, tmp_ans)
    elif len(downs) == 1:
        ans =bbans + (bbans//downs[0][1])*(downs[0][0]-downs[0][1])
    print(ans)

Submission status: Wrong Answer

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


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

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

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

    # B->Aでのトレード成果
    r_g = ga / gb
    r_s = sa / sb
    r_b = ba / bb
    skip_num = 5000
    if (a_result // gb - 1) > 0 and r_g >= 1 and r_g >= r_s and r_g >= r_b:
        skip_num = max(sb // gb + 1, bb // gb + 1)*2
        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)*2
        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)*2
        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:
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:
            j=0
            while  j*a[1]<=n2-i*a[0]:
                ans2=max(ans2,i*b[0]+j*b[1]+((n2-i*a[0]-j*a[1])//a[2])*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:
# 2WAでgive up
# 銀と銅の部分が最適化できていないが、2重ループはたぶんTLEする


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

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

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

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

        if new_n:
            n = new_curr_n
    n += new_n

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

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

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


if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
inp = input()
large_n = int(inp)
inp = input()
split_input = inp.split()
a_g = int(split_input[0])
a_s = int(split_input[1])
a_b = int(split_input[2])
inp = input()
split_input = inp.split()
b_g = int(split_input[0])
b_s = int(split_input[1])
b_b = int(split_input[2])

diff_g = b_g - a_g
diff_s = b_s - a_s
diff_b = b_b - a_b


d_count = large_n
if max(diff_g, diff_b, diff_s) > 0:
    d_count = d_count * (max(diff_g, diff_b, diff_s) + 1)

diff_g = a_g - b_g
diff_s = a_s - b_s
diff_b = a_b - b_b

if max(diff_g, diff_b, diff_s) > 0:
    d_count = d_count * (max(diff_g, diff_b, diff_s) + 1)

print(d_count)

Submission status: Wrong Answer

Solution code:
import sys

cin = sys.stdin
N, = map(int, cin.readline().split())
xa = list(map(int, cin.readline().split()))
xb = list(map(int, cin.readline().split()))


from itertools import permutations


def step(xa, xb, ttl):
    a2b = list(filter(lambda i:xb[i] - xa[i]>0, (i for i in range(3))))
    a2b.sort(key=lambda x:xa[x], reverse=True)
    print(a2b)

    n = len(a2b)
    maxN_a2b = ttl
    if len(a2b)==3:
        dp = [[-0x7fff7fff] * (ttl + 1) for _ in range(n + 1)]
        dp[0][0] = 0
        for j in range(n):
            i = a2b[j]
            for k in range(0, ttl+1):
                for li, l in enumerate(range(k, ttl+1, xa[i])):
                    dp[j+1][l] = max(dp[j+1][l], dp[0][k] + li * xb[i])
        maxN_a2b = max(dp[n])
    elif len(a2b)==2 and (xa[a2b[0]] % xa[a2b[1]]) != 0:
        i0 = a2b[0]
        i1 = a2b[1]
        jl = ttl // xa[i0]
        for j0a in range(jl):
            ttl0 = ttl - j0a*xa[i0]
            j1a = ttl0 // xa[i1]
            ttl0 %= xa[i1]
            maxN_a2b = max(maxN_a2b, ttl0 + j0a*xb[i0] + j1a*xb[i1])
    elif len(a2b)==1:
        i = a2b[0]
        j = ttl // xa[i]
        ttl %= xa[i]
        ttl += j * xb[i]
        maxN_a2b = ttl
    return maxN_a2b


N1 = step(xa, xb, N)
N2 = step(xb, xa, N1)
print(N2)

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 = n

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())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())

# 最低N個
res = N

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

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

print(res)

Submission status: Wrong Answer

Solution code:
import itertools
N=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
C=sorted([(a[i],b[i]) for i in range(3)],key=lambda x:x[1]-x[0],reverse=True)
X=[]
Y=[]
for i in range(3):
    if C[i][1]-C[i][0]>0:
        X.append(C[i])
    elif C[i][1]-C[i][0]<0:
        Y.append(C[i])
M=N
Mmax=M
if len(X)==0:
    pass
elif len(X)==1:
    A,B=X[0]
    Mmax=max(Mmax,M+(B-A)*(M//A))
elif len(X)==2:
    C1=X[0][1]-X[0][0]
    C2=X[1][1]-X[1][0]
    for i in range(M+1):
        #j*C2<=N-i*C1
        j=(M-i*X[0][0])//X[1][0]
        Mmax=max(Mmax,M+i*C1+j*C2)
else:
    C1=X[0][1]-X[0][0]
    C2=X[1][1]-X[1][0]
    C3=X[2][1]-X[2][0]
    for i in range(M+1):
        for j in range(M+1):
            k=(M-i*X[0][0]-j*X[1][0])//X[2][0]
            Mmax=max(Mmax,M+i*C1+j*C2+k*C3)
M=Mmax

if len(Y)==0:
    pass
elif len(Y)==1:
    A,B=Y[0]
    M=M+(B-A)*(M//A)
elif len(Y)==2:
    C1=Y[0][1]-Y[0][0]
    C2=Y[1][1]-Y[1][0]
    for i in range(M+1):
        #j*C2<=N-i*C1
        j=(M-i*Y[0][0])//Y[1][0]
        Mmax=max(Mmax,M+i*C1+j*C2)
else:
    C1=Y[0][1]-Y[0][0]
    C2=Y[1][1]-Y[1][0]
    C3=Y[2][1]-Y[2][0]
    for i in range(M+1):
        for j in range(M+1):
            k=(M-i*Y[0][0]-j*Y[1][0])//Y[2][0]
            Mmax=max(Mmax,M+i*C1+j*C2+k*C3)
print(Mmax)

Submission status: Wrong Answer

Solution code:
def func1(rem,arr1,arr2,typ):
	arr11=[]
	for i in range(3):
		arr11.append((arr1[i],i))
	arr22=[]
	for i in range(3):
		arr22.append((arr2[i],i))

	if(typ==0):
		arr11.sort(reverse=True)
		ans=0
		for i in range(3):
			index=arr11[i][1]
			ans+=(rem//arr2[index])*arr1[i]
			rem=rem%arr2[index]
		ans+=rem

	else:
		arr22.sort(reverse=True)
		ans=0
		for i in range(3):
			index=arr22[i][1]
			ans+=(rem//arr1[index])*arr2[i]
			rem=rem%arr1[index]
		ans+=rem
	return ans


def func2(rem,arr1,arr2,bet):
	arr11=[]
	for i in range(3):
		arr11.append((arr1[i],i))
	arr22=[]
	for i in range(3):
		arr22.append((arr2[i],i))

	first=[]
	second=[]
	for i in range(3):
		if(bet[i]==0):
			first.append(arr11[i])
		else:
			second.append(arr22[i])

	first.sort(reverse=True)
	second.sort(reverse=True)
	ans=0
	for i in range(len(second)):
		index=second[i][1]
		ans+=(rem//arr1[index])*arr2[i]
		rem=rem%arr1[index]
	ans+=rem

	rem=ans
	ans=0
	for i in range(len(first)):
		index=first[i][1]
		ans+=(rem//arr2[index])*arr1[i]
		rem=rem%arr2[index]
	ans+=rem

	return ans

n=int(input())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
bet=[-1]*3
if(arr1[0]>=arr2[0]):
	bet[0]=0
elif(arr1[0]<=arr2[0]):
	bet[0]=1

if(arr1[1]>=arr2[1]):
	bet[1]=0
elif(arr1[1]<=arr2[1]):
	bet[1]=1

if(arr1[2]>=arr2[2]):
	bet[2]=0
elif(arr1[2]<=arr2[2]):
	bet[2]=1

ans=0
count1=bet.count(0)
count2=bet.count(1)
if(count2==0):
	ans=func1(n,arr1,arr2,0)
	print(ans)
elif(count1==0):
	ans=func1(n,arr1,arr2,1)
	print(ans)
else:
	ans=func2(n,arr1,arr2,bet)
	print(ans)

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()
ga,sa,ba = inm()
gb,sb,bb = inm()

z = [ (gb/ga,ga,gb), (sb/sa,sa,sb), (bb/ba,ba,bb)]
z.sort(reverse=True)
nn = n
gain = 0
for x in z:
    r,a,b = x
    if a>=b:
        break
    gain += (b-a)*(n//a)
    nn -= a * (nn//a)
    #ddprint("i {} r {} gain {} a {} b {} nb {}".format(i,r,gain,a,b,n//b))

n += gain

z = [ (ga/gb,ga,gb), (sa/sb,sa,sb), (ba/bb,ba,bb)]
z.sort(reverse=True)
nn = n
gain = 0
for x in z:
    r,a,b = x
    if a<=b:
        break
    gain += (a-b)*(n//b)
    nn -= b * (nn//b)
    #ddprint("i {} r {} gain {} a {} b {} nb {}".format(i,r,gain,a,b,n//b))

print(n+gain)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
arr_A = np.array([int(j) for j in input().split()])
arr_B = np.array([int(j) for j in input().split()])

num = N
rate_max1 = np.max(arr_B/arr_A)
if rate_max1 > 0:
    num = rate_max1*N
    
rate_max2 = np.max(arr_A - arr_B)
if rate_max2>0:
    num = rate_max2*N
    
print(int(num))

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)
    if BA[0][0] >= AB[0][0]:
        i = BA[0][1]
        print(N % A[i] + (N//A[i])*B[i])
    else:
        i = AB[0][1]
        print(N % B[i] + (N//B[i])*A[i])


main()

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:
            j=0
            while  j*a[1]<=n2-i*a[0]:
                ans2=max(ans2,i*b[0]+j*b[1]+((n2-i*a[0]-j*a[1])//a[2])*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))
print(max(i for i in range(10)))

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 and s1[2]!=s1[1]:
  ll[ind]+=n//la[ind]
  n=n%la[ind]
ind=s1.index(s1s[1])
if n>=la[ind] and s1s[1]>1 and s1[1]!=s1[0]:
  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 and s1[0]!=s1[1]:
  ll[ind]+=n//lb[ind]
  n=n%lb[ind]
ind=s1.index(s1s[1])
if n>=lb[ind] and s1s[1]<1 and s1[2]!=s1[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:
n = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

ans2 = n

for c in range(3):
    ans1 = 0
    for i in range((n + 1) // A[0]):
        for j in range((n + 1 - i * A[0]) // A[1]):
            d = n
            d += (B[0] - A[0]) * i
            d += (B[1] - A[1]) * j
            k = n - i * A[0] - j * A[1] if c == 1 else 0
            # assert k >= 0
            d += (B[2] - A[2]) * k
        
            ans1 = max(ans1, d)

    for i in range((ans1 + 1) // B[0]):
        for j in range((ans1 + 1 - i * B[0]) // B[1]):
            d = ans1
            d += (A[0] - B[0]) * i
            d += (A[1] - B[1]) * j
            k = ans1 - i * B[0] - j * B[1] if c == 2 else 0
            # assert k >= 0
            d += (A[2] - B[2]) * k
        
            ans2 = max(ans2, d)

print(ans2)