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())
dp = [0] * (n + 1)
for i in range(n):
    if i + ga <= n:
        dp[i + ga] = max(dp[i + ga], dp[i] + gb)
    if i + sa <= n:
        dp[i + sa] = max(dp[i + sa], dp[i] + sb)
    if i + ba <= n:
        dp[i + ba] = max(dp[i + ba], dp[i] + bb)
ans = 0
for i in range(n + 1):
    ans = max(ans, dp[i] + n - i)
dp = [0] * 25000025
for i in range(25000025):
    if i + gb <= n:
        dp[i + gb] = max(dp[i + gb], dp[i] + ga)
    if i + sb <= n:
        dp[i + sb] = max(dp[i + sb], dp[i] + sa)
    if i + bb <= n:
        dp[i + bb] = max(dp[i + bb], dp[i] + ba)
res = 0
for i in range(n + 1):
    res = max(ans, dp[i] + ans - i)
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())
dp = [0] * (n + 1)
for i in range(n):
    if i + ga <= n:
        dp[i + ga] = max(dp[i + ga], dp[i] + gb)
    if i + sa <= n:
        dp[i + sa] = max(dp[i + sa], dp[i] + sb)
    if i + ba <= n:
        dp[i + ba] = max(dp[i + ba], dp[i] + bb)
for i in range(n):
    if i + gb <= n:
        dp[i + gb] = max(dp[i + gb], dp[i] + ga)
    if i + sb <= n:
        dp[i + sb] = max(dp[i + sb], dp[i] + sa)
    if i + bb <= n:
        dp[i + bb] = max(dp[i + bb], dp[i] + ba)
ans = 0
for i in range(n + 1):
    ans = max(ans, dp[i] + n - i)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from fractions import Fraction

sys.setrecursionlimit(10 ** 8)
ni = lambda: int(sys.stdin.readline())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
ns = lambda: sys.stdin.readline().rstrip()

N = ni()
g1, s1, b1 = nm()
g2, s2, b2 = nm()


def solve():
    gr, sr, br = (g2, g1), (s2, s1), (b2, b1)

    rp = sorted(
        [(Fraction(n, d), n, d) for (n, d) in [gr, sr, br] if n > d], reverse=True
    )
    rn = sorted([(Fraction(n, d), n, d) for (n, d) in [gr, sr, br] if n < d])

    # Start
    don, dont = N, 0
    # A
    for _, m2, m1 in rp:
        dd = don // m1
        don %= m1
        dont += dd * m2
    # B
    don += dont
    dont = 0
    for _, m2, m1 in rn:
        dd = don // m2
        don %= m2
        dont += dd * m1
    # A
    don += dont
    dont = 0
    return don


print(solve())

Submission status: Wrong Answer

Solution code:
# greedyに交換するとWA？

N = int(input())
coin = [0, 0, 0, 0]
A = list(map(int, input().split())) + [1]
B = list(map(int, input().split())) + [1]

r = [a/b for a, b in zip(A, B)]
idx1 = r.index(max(r))
idx2 = r.index(min(r))

coin[idx1] = N * A[idx1]
N = coin[idx1] // B[idx1]
coin[idx1] %= B[idx1]
coin[idx2] += N * B[idx2]
N = 0
for a, c in zip(A, coin):
  N += c // a

  
print(N)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ab = []
for i, (j, k) in enumerate(zip(a, b)):
    ab.append([j / k, i])
ab.sort()
gsb = [0, 0, 0]
for i, j in ab:
    if i >= 1:
        break
    gsb[j] += n // a[j]
    n %= a[j]

for i in range(3):
    n += gsb[i] * b[i]
    gsb[i] = 0
for i, j in ab[::-1]:
    if i <= 1:
        continue
    gsb[j] += n // b[j]
    n %= b[j]
    
for i in range(3):
    n += gsb[i] * a[i]
    gsb[i] = 0
print(n)

Submission status: Wrong Answer

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

N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
A = [10**18] * 3
B = [10**18] * 3
dg = abs(ga - gb)
ds = abs(sa - sb)
db = abs(ba - bb)
if ga < gb:
    A[0] = ga
elif ga > gb:
    B[0] = gb
if sa < sb:
    A[1] = sa
elif sa > sb:
    B[1] = sb
if ba < bb:
    A[2] = ba
elif ba > bb:
    B[2] = bb

def calc(X):
    res = -1
    for i in range(N+1):
        if X[0] * i > N:
            break
        M = N - X[0] * i
        jj = M // X[1]
        x = 125
        for j in range(max(jj - x, 0), jj+1):
            k = (M - X[1] * j) // X[2]
            res = max(res, dg * i + ds * j + db * k)
        kk = M // X[2]
        for k in range(max(kk - x, 0), kk+1):
            j = (M - X[2] * k) // X[1]
            res = max(res, dg * i + ds * j + db * k)
    return res

N += calc(A)
N += calc(B)
print(N)

Submission status: Wrong Answer

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

N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
A = [10**18] * 3
B = [10**18] * 3
dg = abs(ga - gb)
ds = abs(sa - sb)
db = abs(ba - bb)
if ga < gb:
    A[0] = ga
elif ga > gb:
    B[0] = gb
if sa < sb:
    A[1] = sa
elif sa > sb:
    B[1] = sb
if ba < bb:
    A[2] = ba
elif ba > bb:
    B[2] = bb

def calc(X):
    res = -1
    for i in range(N+1):
        if X[0] * i > N:
            break
        M = N - X[0] * i
        jj = M // X[1]
        x = 500 if N < 1000000 else 100
        for j in range(max(jj - x, 0), jj+1):
            k = (M - X[1] * j) // X[2]
            res = max(res, dg * i + ds * j + db * k)
        kk = M // X[2]
        for k in range(max(kk - x, 0), kk+1):
            j = (M - X[2] * k) // X[1]
            res = max(res, dg * i + ds * j + db * k)
    return res

N += calc(A)
N += calc(B)
print(N)

Submission status: Wrong Answer

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

N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
A = [10**18] * 3
B = [10**18] * 3
dg = abs(ga - gb)
ds = abs(sa - sb)
db = abs(ba - bb)
if ga < gb:
    A[0] = ga
elif ga > gb:
    B[0] = gb
if sa < sb:
    A[1] = sa
elif sa > sb:
    B[1] = sb
if ba < bb:
    A[2] = ba
elif ba > bb:
    B[2] = bb

def calc(X):
    res = -1
    for i in range(N+1):
        if X[0] * i > N:
            break
        M = N - X[0] * i
        jj = M // X[1]
        x = 500 if N < 100000 else 100
        for j in range(max(jj - x, 0), jj+1):
            k = (M - X[1] * j) // X[2]
            res = max(res, dg * i + ds * j + db * k)
        kk = M // X[2]
        for k in range(max(kk - x, 0), kk+1):
            j = (M - X[2] * k) // X[1]
            res = max(res, dg * i + ds * j + db * k)
    return res

N += calc(A)
N += calc(B)
print(N)

Submission status: Wrong Answer

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

N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
A = [10**18] * 3
B = [10**18] * 3
dg = abs(ga - gb)
ds = abs(sa - sb)
db = abs(ba - bb)
if ga < gb:
    A[0] = ga
elif ga > gb:
    B[0] = gb
if sa < sb:
    A[1] = sa
elif sa > sb:
    B[1] = sb
if ba < bb:
    A[2] = ba
elif ba > bb:
    B[2] = bb

def calc(X):
    res = -1
    for i in range(N+1):
        if X[0] * i > N:
            break
        M = N - X[0] * i
        jj = M // X[1]
        x = 500 if N < 10000 else 100
        for j in range(max(jj - x, 0), jj+1):
            k = (M - X[1] * j) // X[2]
            res = max(res, dg * i + ds * j + db * k)
        kk = M // X[2]
        for k in range(max(kk - x, 0), kk+1):
            j = (M - X[2] * k) // X[1]
            res = max(res, dg * i + ds * j + db * k)
    return res

N += calc(A)
N += calc(B)
print(N)

Submission status: Wrong Answer

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

N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
A = [10**18] * 3
B = [10**18] * 3
dg = abs(ga - gb)
ds = abs(sa - sb)
db = abs(ba - bb)
if ga < gb:
    A[0] = ga
elif ga > gb:
    B[0] = gb
if sa < sb:
    A[1] = sa
elif sa > sb:
    B[1] = sb
if ba < bb:
    A[2] = ba
elif ba > bb:
    B[2] = bb

def calc(X):
    res = -1
    for i in range(N+1):
        if X[0] * i > N:
            break
        M = N - X[0] * i
        jj = M // X[1]
        for j in range(max(jj - 100, 0), jj+1):
            k = (M - X[1] * j) // X[2]
            res = max(res, dg * i + ds * j + db * k)
        kk = M // X[2]
        for k in range(max(kk - 100, 0), kk+1):
            j = (M - X[2] * k) // X[1]
            res = max(res, dg * i + ds * j + db * k)
    return res

N += calc(A)
N += calc(B)
print(N)

Submission status: Wrong Answer

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

N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
A = [10**18] * 3
B = [10**18] * 3
dg = abs(ga - gb)
ds = abs(sa - sb)
db = abs(ba - bb)
if ga < gb:
    A[0] = ga
elif ga > gb:
    B[0] = gb
if sa < sb:
    A[1] = sa
elif sa > sb:
    B[1] = sb
if ba < bb:
    A[2] = ba
elif ba > bb:
    B[2] = bb

def calc(X):
    res = -1
    for i in range(N+1):
        if X[0] * i > N:
            break
        M = N - X[0] * i
        jj = M // X[1]
        for j in range(max(jj - 10, 0), jj+1):
            k = (M - X[1] * j) // X[2]
            res = max(res, dg * i + ds * j + db * k)
        kk = M // X[2]
        for k in range(max(kk - 10, 0), kk+1):
            j = (M - X[2] * k) // X[1]
            res = max(res, dg * i + ds * j + db * k)
    return res

N += calc(A)
N += calc(B)
print(N)

Submission status: Wrong Answer

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

N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
A = [10**18] * 3
B = [10**18] * 3
dg = abs(ga - gb)
ds = abs(sa - sb)
db = abs(ba - bb)
if ga < gb:
    A[0] = ga
elif ga > gb:
    B[0] = gb
if sa < sb:
    A[1] = sa
elif sa > sb:
    B[1] = sb
if ba < bb:
    A[2] = ba
elif ba > bb:
    B[2] = bb

def calc(X):
    res = -1
    for i in range(N+1):
        if X[0] * i > N:
            break
        M = N - X[0] * i
        j = M // X[1]
        k = (M - X[1] * j) // X[2]
        res = max(res, dg * i + ds * j + db * k)
        k = M // X[2]
        j = (M - X[2] * k) // X[1]
        res = max(res, dg * i + ds * j + db * k)
    return res

N += calc(A)
N += calc(B)
print(N)

Submission status: Wrong Answer

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

N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
A = [10**18] * 3
B = [10**18] * 3
dg = abs(ga - gb)
ds = abs(sa - sb)
db = abs(ba - bb)
if ga < gb:
    A[0] = ga
elif ga > gb:
    B[0] = gb
if sa < sb:
    A[1] = sa
elif sa > sb:
    B[1] = sb
if ba < bb:
    A[2] = ba
elif ba > bb:
    B[2] = bb

def calc(X):
    res = -1
    for i in range(N+1):
        if X[0] * i > N:
            break
        M = N - X[0] * i
        res = max(res, dg * i + ds * (M // X[1]), dg * i + db * (M // X[2]))
    return res

N += calc(A)
N += calc(B)
print(N)

Submission status: Wrong Answer

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

N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
A = [10**18] * 3
B = [10**18] * 3
dg = abs(ga - gb)
ds = abs(sa - sb)
db = abs(ba - bb)
if ga < gb:
    A[0] = ga
elif ga > gb:
    B[0] = gb
if sa < sb:
    A[1] = sa
elif sa > sb:
    B[1] = sb
if ba < bb:
    A[2] = ba
elif ba > bb:
    B[2] = bb
m = -1
for i in range(N+1):
    if A[0] * i > N:
        break
    for j in range(N+1):
        if A[0] * i + A[1] * j > N:
            break
        k = (N - A[0] * i - A[1] * j) // A[2]
        m = max(m, dg * i + ds * j + db * k)
        print(i, j, k, dg * i + ds * j + db * k)
N += m
m = -1
for i in range(N+1):
    if B[0] * i > N:
        break
    for j in range(N+1):
        if B[0] * i + B[1] * j > N:
            break
        k = (N - B[0] * i - B[1] * j) // B[2]
        m = max(m, dg * i + ds * j + db * k)
N += m
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())

check=(gb>ga)+(sb>sa)+(bb>ba)
if check==3:
    donguri=N

    inc=0
    for i in range(0,donguri//ga+1):
        x=(ga<gb)*i
        for j in range(0,donguri//sa+1):
            y=(sa<sb)*j
            if donguri>=x*ga+y*sa:
                z=((donguri-x*ga-y*sa)//ba)*(bb>ba)
                inc=max(inc,x*(gb-ga)+y*(sb-sa)+z*(bb-ba))

    donguri+=inc
    print(donguri)
elif check==0:
    donguri=N
    inc=0
    for i in range(0,donguri//gb+1):
        x=(ga>gb)*i
        for j in range(0,donguri//sb+1):
            y=(sa>sb)*j
            if donguri>=x*gb+y*sb:
                z=((donguri-x*gb-y*sb)//bb)*(ba>bb)
                inc=max(inc,-x*(gb-ga)-y*(sb-sa)-z*(bb-ba))

    donguri+=inc
    print(donguri)

elif check==2:
    donguri=N
    inc=0
    for i in range(0,donguri//ga+1):
        x=(ga<gb)*i
        for j in range(0,donguri//sa+1):
            y=(sa<sb)*j
            if donguri>=x*ga+y*sa:
                z=((donguri-x*ga-y*sa)//ba)*(bb>ba)
                inc=max(inc,x*(gb-ga)+y*(sb-sa)+z*(bb-ba))

    donguri+=inc
    if ga>gb:
        plus=(donguri//gb)*(ga-gb)
        print(donguri+plus)
    elif sa>sb:
        plus=(donguri//sb)*(sa-sb)
        print(donguri+plus)
    elif ba>bb:
        plus=(donguri//bb)*(ba-bb)
        print(donguri+plus)
    else:
        print(donguri)

else:
    donguri=N
    inc=0
    if ga<gb:
        plus=(donguri//ga)*(gb-ga)
        donguri+=plus
        if sa>sb:
            for i in range(0,donguri//sb+1):
                j=(donguri-i*sb)//bb
                inc=max(inc,i*(sa-sb)+j*(ba-bb))
            print(donguri+inc)
        else:
            inc=(donguri//bb)*(ba-bb)
            print(donguri+inc)

    elif sa<sb:
        plus=(donguri//sa)*(sb-sa)
        donguri+=plus
        if ga>gb:
            for i in range(0,donguri//gb+1):
                j=(donguri-i*gb)//bb
                inc=max(inc,i*(ga-gb)+j*(ba-bb))
            print(donguri+inc)
        else:
            inc=(donguri//bb)*(ba-bb)
            print(donguri+inc)
    else:
        plus=(donguri//ba)*(bb-ba)
        donguri+plus
        if ga>gb:
            for i in range(0,donguri//gb+1):
                j=(donguri-i*gb)//sb
                inc=max(inc,i*(ga-gb)+j*(sa-sb))
            print(donguri+inc)
        else:
            inc=(donguri//sb)*(sa-sb)
            print(donguri+inc)

Submission status: Wrong Answer

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

check=(gb>ga)+(sb>sa)+(bb>ba)
if check==3:
    donguri=N

    inc=0
    for i in range(0,donguri//ga+1):
        x=(ga<gb)*i
        for j in range(0,donguri//sa+1):
            y=(sa<sb)*j
            if donguri>=x*ga+y*sa:
                z=((donguri-x*ga-y*sa)//ba)*(bb>ba)
                inc=max(inc,x*(gb-ga)+y*(sb-sa)+z*(bb-ba))

    donguri+=inc
    print(donguri)
elif check==0:
    donguri=N
    inc=0
    for i in range(0,donguri//gb+1):
        x=(ga>gb)*i
        for j in range(0,donguri//sb+1):
            y=(sa>sb)*j
            if donguri>=x*gb+y*sb:
                z=((donguri-x*gb-y*sb)//bb)*(ba>bb)
                inc=max(inc,-x*(gb-ga)-y*(sb-sa)-z*(bb-ba))

    donguri+=inc
    print(donguri)

elif check==2:
    donguri=N
    inc=0
    for i in range(0,donguri//ga+1):
        x=(ga<gb)*i
        for j in range(0,donguri//sa+1):
            y=(sa<sb)*j
            if donguri>=x*ga+y*sa:
                z=((donguri-x*ga-y*sa)//ba)*(bb>ba)
                inc=max(inc,x*(gb-ga)+y*(sb-sa)+z*(bb-ba))

    donguri+=inc
    if ga>gb:
        plus=(donguri//gb)*(ga-gb)
        print(donguri+plus)
    elif sa>sb:
        plus=(donguri//sb)*(sa-sb)
        print(donguri+plus)
    else:
        plus=(donguri//bb)*(ba-bb)
        print(donguri+plus)

else:
    donguri=N
    inc=0
    if ga<gb:
        plus=(donguri//ga)*(gb-ga)
        donguri+=plus
        if sa>sb:
            for i in range(0,donguri//sb+1):
                j=(donguri-i*sb)//bb
                inc=max(inc,i*(sa-sb)+j*(ba-bb))
            print(donguri+inc)
        else:
            inc=(donguri//bb)*(ba-bb)
            print(donguri+inc)

    elif sa<sb:
        plus=(donguri//sa)*(sb-sa)
        donguri+=plus
        if ga>gb:
            for i in range(0,donguri//gb+1):
                j=(donguri-i*gb)//bb
                inc=max(inc,i*(ga-gb)+j*(ba-bb))
            print(donguri+inc)
        else:
            inc=(donguri//bb)*(ba-bb)
            print(donguri+inc)
    else:
        plus=(donguri//ba)*(bb-ba)
        donguri+plus
        if ga>gb:
            for i in range(0,donguri//gb+1):
                j=(donguri-i*gb)//sb
                inc=max(inc,i*(ga-gb)+j*(sa-sb))
            print(donguri+inc)
        else:
            inc=(donguri//sb)*(sa-sb)
            print(donguri+inc)

Submission status: Wrong Answer

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

check=(gb>ga)+(sb>sa)+(bb>ba)
if check==3:
    donguri=N

    inc=0
    for i in range(0,donguri//ga+1):
        x=(ga<gb)*i
        for j in range(0,donguri//sa+1):
            y=(sa<sb)*j
            if donguri>=x*ga+y*sa:
                z=((donguri-x*ga-y*sa)//ba)*(bb>ba)
                inc=max(inc,x*(gb-ga)+y*(sb-sa)+z*(bb-ba))

    donguri+=inc
    print(donguri)
elif check==0:
    donguri=N
    inc=0
    for i in range(0,donguri//gb+1):
        x=(ga>gb)*i
        for j in range(0,donguri//sb+1):
            y=(sa>sb)*j
            if donguri>=x*gb+y*sb:
                z=((donguri-x*gb-y*sb)//bb)*(ba>bb)
                inc=max(inc,-x*(gb-ga)-y*(sb-sa)-z*(bb-ba))

    donguri+=inc
    print(donguri)

elif check==2:
    donguri=N
    inc=0
    for i in range(0,donguri//ga+1):
        x=(ga<gb)*i
        for j in range(0,donguri//sa+1):
            y=(sa<sb)*j
            if donguri>=x*ga+y*sa:
                z=((donguri-x*ga-y*sa)//ba)*(bb>ba)
                inc=max(inc,x*(gb-ga)+y*(sb-sa)+z*(bb-ba))

    donguri+=inc
    if ga>gb:
        plus=(donguri//gb)*(ga-gb)
        print(donguri+plus)
    elif sa>sb:
        plus=(donguri//sb)*(sa-sb)
        print(donguri+plus)
    else:
        plus=(donguri//bb)*(ba-bb)
        print(donguri+plus)

else:
    donguri=N
    inc=0
    if ga<gb:
        plus=(donguri//ga)*(gb-ga)
        donguri+=plus
        for i in range(0,donguri//sb+1):
            j=(donguri-i*sb)//bb
            inc=max(inc,i*(sa-sb)+j*(ba-bb))
        print(donguri+inc)

    elif sa<sb:
        plus=(donguri//sa)*(sb-sa)
        donguri+=plus
        for i in range(0,donguri//gb+1):
            j=(donguri-i*gb)//bb
            inc=max(inc,i*(ga-gb)+j*(ba-bb))
        print(donguri+inc)
    else:
        plus=(donguri//ba)*(bb-ba)
        donguri+plus
        for i in range(0,donguri//gb+1):
            j=(donguri-i*gb)//sb
            inc=max(inc,i*(ga-gb)+j*(sa-sb))
        print(donguri+inc)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

best=1
best = max(best,ga//gb,sa//sb,ba//bb,gb//ga,sb//sa,bb//ba)

# best = max(best, ga//gb*gb//ga)
best = max(best, ga//gb*sb//sa)
best = max(best, ga//gb*bb//ba)

best = max(best, sa//sb*gb//ga)
# best = max(best, sa//sb*sb//sa)
best = max(best, sa//sb*bb//ba)

best = max(best, ba//bb*gb//ga)
# best = max(best, ba//bb*bb//ba)
best = max(best, ba//bb*sb//sa)

print(n*best)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
change_a = [i for i in range(3) if a[i] < b[i]]
change_b = [i for i in range(3) if a[i] > b[i]]
if len(change_a) == 0:
  if len(change_b) == 0:
    print(n)
  elif len(change_b) == 1:
    i = n // b[change_b[0]]
    n = n % b[change_b[0]]
    n += i * a[change_b[0]]
    print(n)
  elif len(change_b) == 2:
    tmp = n
    for i in range(5001):
      m = n
      m -= i * b[change_b[0]]
      if m < 0:
        break
      j = m // b[change_b[1]]
      m = m % b[change_b[1]]
      m += i * a[change_b[0]] + j * a[change_b[1]]
      tmp = max(tmp, m)
    print(tmp)
  else:      
    tmp = n
    for i in range(5001):
      for j in range(5001):
        m = n
        m -= i * b[change_b[0]]
        if m < 0:
          break
        m -= j * b[change_b[1]]
        if m < 0:
          break
        k = m // b[change_b[2]]
        m = m % b[change_b[2]]
        m += i * a[change_b[0]] + j * a[change_b[1]] + k * a[change_b[2]]
        tmp = max(tmp, m)
    print(tmp)
elif len(change_a) == 1:
  i = n // a[change_a[0]]
  n = n % a[change_a[0]]
  n += i * b[change_a[0]]
  if len(change_b) == 0:
    print(n)
  elif len(change_b) == 1:
    i = n // b[change_b[0]]
    n = n % b[change_b[0]]
    n += i * a[change_b[0]]
    print(n)
  elif len(change_b) == 2:
    tmp = n
    for i in range(5001):
      m = n
      m -= i * b[change_b[0]]
      if m < 0:
        break
      j = m // b[change_b[1]]
      m = m % b[change_b[1]]
      m += i * a[change_b[0]] + j * a[change_b[1]]
      tmp = max(tmp, m)
    print(tmp)
elif len(change_a) == 2:
  tmp = n
  for i in range(5001):
    m = n
    m -= i * a[change_a[0]]
    if m < 0:
      break
    j = m // a[change_a[1]]
    m = m % a[change_a[1]]
    m += i * b[change_a[0]] + j * b[change_a[1]]
    tmp = max(tmp, m)
  n = tmp
  if len(change_b) == 0:
    print(n)
  elif len(change_b) == 1:
    i = n // b[change_b[0]]
    n = n % b[change_b[0]]
    n += i * a[change_b[0]]
    print(n)
else:    
  tmp = n
  for i in range(5001):
    for j in range(5001):
      m = n
      m -= i * a[change_a[0]]
      if m < 0:
        break
      m -= j * a[change_a[1]]
      if m < 0:
        break
      k = m // a[change_a[2]]
      m = m % a[change_a[2]]
      m += i * b[change_a[0]] + j * b[change_a[1]] + k * b[change_a[2]]
      tmp = max(tmp, m)
  print(tmp)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
change_a = [i for i in range(3) if a[i] < b[i]]
change_b = [i for i in range(3) if a[i] > b[i]]
if len(change_a) == 0:
  if len(change_b) == 0:
    print(n)
  elif len(change_b) == 1:
    i = n // b[change_b[0]]
    n = n % b[change_b[0]]
    n += i * a[change_b[0]]
    print(n)
  elif len(change_b) == 2:
    tmp = n
    for i in range(5001):
      m = n
      m -= i * b[change_b[0]]
      if m < 0:
        break
      j = m // b[change_b[1]]
      m = m % b[change_b[1]]
      m += i * a[change_b[0]] + j * a[change_b[1]]
      tmp = max(tmp, m)
    print(tmp)
  else:      
    tmp = n
    for i in range(5001):
      for j in range(5001):
        m = n
        m -= i * b[change_b[0]]
        if m < 0:
          break
        m -= j * b[change_b[1]]
        if m < 0:
          break
        k = m // b[change_b[2]]
        m = m % b[change_b[2]]
        m += i * a[change_b[0]] + j * a[change_b[1]] + k * a[change_b[2]]
        tmp = max(tmp, m)
elif len(change_a) == 1:
  i = n // a[change_a[0]]
  n = n % a[change_a[0]]
  n += i * b[change_a[0]]
  if len(change_b) == 0:
    print(n)
  elif len(change_b) == 1:
    i = n // b[change_b[0]]
    n = n % b[change_b[0]]
    n += i * a[change_b[0]]
    print(n)
  elif len(change_b) == 2:
    tmp = n
    for i in range(5001):
      m = n
      m -= i * b[change_b[0]]
      if m < 0:
        break
      j = m // b[change_b[1]]
      m = m % b[change_b[1]]
      m += i * a[change_b[0]] + j * a[change_b[1]]
      tmp = max(tmp, m)
    print(tmp)
elif len(change_a) == 2:
  tmp = n
  for i in range(5001):
    m = n
    m -= i * a[change_a[0]]
    if m < 0:
      break
    j = m // a[change_a[1]]
    m = m % a[change_a[1]]
    m += i * b[change_a[0]] + j * b[change_a[1]]
    tmp = max(tmp, m)
  n = tmp
  if len(change_b) == 0:
    print(n)
  elif len(change_b) == 1:
    i = n // b[change_b[0]]
    n = n % b[change_b[0]]
    n += i * a[change_b[0]]
    print(n)
else:    
  tmp = n
  for i in range(5001):
    for j in range(5001):
      m = n
      m -= i * a[change_a[0]]
      if m < 0:
        break
      m -= j * a[change_a[1]]
      if m < 0:
        break
      k = m // a[change_a[2]]
      m = m % a[change_a[2]]
      m += i * b[change_a[0]] + j * b[change_a[1]] + k * b[change_a[2]]
      tmp = max(tmp, m)
  print(tmp)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
change_a = [i for i in range(3) if a[i] < b[i]]
change_b = [i for i in range(3) if a[i] > b[i]]
if len(change_a) == 0:
  if len(change_b) == 0:
    print(n)
  elif len(change_b) == 1:
    i = n // b[change_b[0]]
    n = n % b[change_b[0]]
    n += i * a[change_b[0]]
    print(n)
  elif len(change_b) == 2:
    tmp = 0
    for i in range(5001):
      m = n
      m -= i * b[change_b[0]]
      if m < 0:
        break
      j = m // b[change_b[1]]
      m = m % b[change_b[1]]
      m += i * a[change_b[0]] + j * a[change_b[1]]
      tmp = max(tmp, m)
    print(tmp)
  else:      
    tmp = 0
    for i in range(5001):
      for j in range(5001):
        m = n
        m -= i * b[change_b[0]]
        if m < 0:
          break
        m -= j * b[change_b[1]]
        if m < 0:
          break
        k = m // b[change_b[2]]
        m = m % b[change_b[2]]
        m += i * a[change_b[0]] + j * a[change_b[1]] + k * a[change_b[2]]
        tmp = max(tmp, m)
    print(tmp)
elif len(change_a) == 1:
  i = n // a[change_a[0]]
  n = n % a[change_a[0]]
  n += i * b[change_a[0]]
  if len(change_b) == 0:
    print(n)
  elif len(change_b) == 1:
    i = n // b[change_b[0]]
    n = n % b[change_b[0]]
    n += i * a[change_b[0]]
    print(n)
  elif len(change_b) == 2:
    tmp = 0
    for i in range(5001):
      m = n
      m -= i * b[change_b[0]]
      if m < 0:
        break
      j = m // b[change_b[1]]
      m = m % b[change_b[1]]
      m += i * a[change_b[0]] + j * a[change_b[1]]
      tmp = max(tmp, m)
    print(tmp)
elif len(change_a) == 2:
  tmp = 0
  for i in range(5001):
    m = n
    m -= i * a[change_a[0]]
    if m < 0:
      break
    j = m // a[change_a[1]]
    m = m % a[change_a[1]]
    m += i * b[change_a[0]] + j * b[change_a[1]]
    tmp = max(tmp, m)
  n = tmp
  if len(change_b) == 0:
    print(n)
  elif len(change_b) == 1:
    i = n // b[change_b[0]]
    n = n % b[change_b[0]]
    n += i * a[change_b[0]]
    print(n)
else:    
  tmp = 0
  for i in range(5001):
    for j in range(5001):
      m = n
      m -= i * a[change_a[0]]
      if m < 0:
        break
      m -= j * a[change_a[1]]
      if m < 0:
        break
      k = m // a[change_a[2]]
      m = m % a[change_a[2]]
      m += i * b[change_a[0]] + j * b[change_a[1]] + k * b[change_a[2]]
      tmp = max(tmp, m)
  print(tmp)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
change_a = [i for i in range(3) if a[i] < b[i]]
change_b = [i for i in range(3) if a[i] > b[i]]
if len(change_a) == 0:
  if len(change_b) == 0:
    print(n)
  elif len(change_b) == 1:
    i = n // b[change_b[0]]
    n = n % b[change_b[0]]
    n += i * a[change_b[0]]
    print(n)
  elif len(change_b) == 2:
    tmp = 0
    for i in range(5001):
      m = n
      m -= i * b[change_b[0]]
      if m < 0:
        break
      j = m // b[change_b[1]]
      m = m % b[change_b[1]]
      m += i * a[change_b[0]] + j * a[change_b[1]]
      tmp = max(tmp, m)
    print(tmp)
  else:      
    tmp = 0
    for i in range(5001):
      for j in range(5001):
        m = n
        m -= i * b[change_b[0]]
        if m < 0:
          break
        m -= j * b[change_b[1]]
        if m < 0:
          break
        k = m // b[change_b[2]]
        m = m % b[change_b[2]]
        m += i * a[change_b[0]] + j * a[change_b[1]] + k * a[change_b[2]]
        tmp = max(tmp, m)
elif len(change_a) == 1:
  gsb = n // a[change_a[0]]
  n = n % a[change_a[0]]
  n += gsb * b[change_a[0]]
  if len(change_b) == 0:
    print(n)
  elif len(change_b) == 1:
    gsb = n // b[change_b[0]]
    n = n % b[change_b[0]]
    n += gsb * a[change_b[0]]
    print(n)
  elif len(change_b) == 2:
    tmp = 0
    for i in range(5001):
      m = n
      m -= i * b[change_b[0]]
      if m < 0:
        break
      j = m // b[change_b[1]]
      m = m % b[change_b[1]]
      m += i * a[change_b[0]] + j * a[change_b[1]]
      tmp = max(tmp, m)
    print(tmp)
elif len(change_a) == 2:
  tmp = 0
  for i in range(5001):
    m = n
    m -= i * a[change_a[0]]
    if m < 0:
      break
    j = m // a[change_a[1]]
    m = m % a[change_a[1]]
    m += i * b[change_a[0]] + j * b[change_a[1]]
    tmp = max(tmp, m)
  n = tmp
  if len(change_b) == 0:
    print(n)
  elif len(change_b) == 1:
    gsb = n // b[change_b[0]]
    n = n % b[change_b[0]]
    n += gsb * a[change_b[0]]
    print(n)
else:    
  tmp = 0
  for i in range(5001):
    for j in range(5001):
      m = n
      m -= i * a[change_a[0]]
      if m < 0:
        break
      m -= j * a[change_a[1]]
      if m < 0:
        break
      k = m // a[change_a[2]]
      m = m % a[change_a[2]]
      m += i * b[change_a[0]] + j * b[change_a[1]] + k * b[change_a[2]]
      tmp = max(tmp, m)
  print(tmp)