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:
#D
N = int(input())
Agsb = list(map(int, input().split()))
Bgsb = list(map(int, input().split()))

divgsb = [Agsb[0]/Bgsb[0],
          Agsb[1]/Bgsb[1],
          Agsb[2]/Bgsb[2]]
vidgsb = [Bgsb[0]/Agsb[0],
          Bgsb[1]/Agsb[1],
          Bgsb[2]/Agsb[2]]
haveg = 0
haves = 0
haveb = 0

if max(vidgsb) == vidgsb[0] :
    if max(vidgsb[1:]) == vidgsb[1] :
        if vidgsb[0] > 1 :
            haveg = N//Agsb[0]
            N %= Agsb[0]
            if vidgsb[1] > 1 :
                haves = N//Agsb[1]
                N %= Agsb[1]
                if vidgsb[2] > 1 :
                    haveb = N//Agsb[2]
                    N %= Agsb[2]
    else :
        if vidgsb[0] > 1 :
            haveg = N//Agsb[0]
            N %= Agsb[0]
            if vidgsb[2] > 1 :
                haves = N//Agsb[2]
                N %= Agsb[2]
                if vidgsb[1] > 1 :
                    haveb = N//Agsb[1]
                    N %= Agsb[1]
elif max(vidgsb) == vidgsb[1] :
    if max(vidgsb[0], vidgsb[2]) == vidgsb[0] :
        if vidgsb[1] > 1 :
            haveg = N//Agsb[1]
            N %= Agsb[1]
            if vidgsb[0] > 1 :
                haves = N//Agsb[0]
                N %= Agsb[0]
                if vidgsb[2] > 1 :
                    haveb = N//Agsb[2]
                    N %= Agsb[2]
    else :
        if vidgsb[1] > 1 :
            haveg = N//Agsb[1]
            N %= Agsb[1]
            if vidgsb[2] > 1 :
                haves = N//Agsb[2]
                N %= Agsb[2]
                if vidgsb[0] > 1 :
                    haveb = N//Agsb[0]
                    N %= Agsb[0]
else :
    if max(vidgsb[:2]) == vidgsb[1] :
        if vidgsb[2] > 1 :
            haveg = N//Agsb[2]
            N %= Agsb[2]
            if vidgsb[1] > 1 :
                haves = N//Agsb[1]
                N %= Agsb[1]
                if vidgsb[0] > 1 :
                    haveb = N//Agsb[0]
                    N %= Agsb[0]
    else :
        if vidgsb[2] > 1 :
            haveg = N//Agsb[2]
            N %= Agsb[2]
            if vidgsb[0] > 1 :
                haves = N//Agsb[0]
                N %= Agsb[0]
                if vidgsb[1] > 1 :
                    haveb = N//Agsb[1]
                    N %= Agsb[1]
N += haveg*Bgsb[0] + haves*Bgsb[1] + haveb*Bgsb[2]        
haveg, haves, haveb = 0, 0, 0


if max(divgsb) == divgsb[0] :
    if max(divgsb[1:]) == divgsb[1] :
        if divgsb[0] > 1 :
            haveg = N//Bgsb[0]
            N %= Bgsb[0]
            if divgsb[1] > 1 :
                haves = N//Bgsb[1]
                N %= Bgsb[1]
                if divgsb[2] > 1 :
                    haveb = N//Bgsb[2]
                    N %= Bgsb[2]
    else :
        if divgsb[0] > 1 :
            haveg = N//Bgsb[0]
            N %= Bgsb[0]
            if divgsb[2] > 1 :
                haves = N//Bgsb[2]
                N %= Bgsb[2]
                if divgsb[1] > 1 :
                    haveb = N//Bgsb[1]
                    N %= Bgsb[1]
elif max(divgsb) == divgsb[1] :
    if max(divgsb[0], divgsb[2]) == divgsb[0] :
        if divgsb[1] > 1 :
            haveg = N//Bgsb[1]
            N %= Bgsb[1]
            if divgsb[0] > 1 :
                haves = N//Bgsb[0]
                N %= Bgsb[0]
                if divgsb[2] > 1 :
                    haveb = N//Bgsb[2]
                    N %= Bgsb[2]
    else :
        if divgsb[1] > 1 :
            haveg = N//Bgsb[1]
            N %= Bgsb[1]
            if divgsb[2] > 1 :
                haves = N//Bgsb[2]
                N %= Bgsb[2]
                if divgsb[0] > 1 :
                    haveb = N//Bgsb[0]
                    N %= Bgsb[0]
else :
    if max(divgsb[:2]) == divgsb[1] :
        if divgsb[2] > 1 :
            haveg = N//Bgsb[2]
            N %= Bgsb[2]
            if divgsb[1] > 1 :
                haves = N//Bgsb[1]
                N %= Bgsb[1]
                if divgsb[0] > 1 :
                    haveb = N//Bgsb[0]
                    N %= Bgsb[0]
    else :
        if divgsb[2] > 1 :
            haveg = N//Bgsb[2]
            N %= Bgsb[2]
            if divgsb[0] > 1 :
                haves = N//Bgsb[0]
                N %= Bgsb[0]
                if divgsb[1] > 1 :
                    haveb = N//Bgsb[1]
                    N %= Bgsb[1]
                    
N += haveg*Agsb[0] + haves*Agsb[1] + haveb*Agsb[2]
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())

ans = 0
for G in range(5001):
    for S in range(5001):
        if ga * G + sa * S > N:
            continue
        B = (N - (ga * G + sa * S)) // ba

        tmp_ans = N - (ga * G + sa * S + sb * B)
        tmp_ans += (G * gb) + (S * sb) + (B * bb)
        ans = max(ans, tmp_ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
ri = lambda: int(input())
#rl = lambda: [int(x) if x.isdecimal() else x for x in input().split()]
#rl = lambda: list(input().split()))
rl = lambda: map(int,input().split())
rr = lambda N: [list(l) for l in zip(*[rl() for _ in range(N)])]
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')
OE = lambda x: print('Odd') if x%2 else print('Even')
INF = 10**18
MOD = 10**9+7

N = ri()
ga,sa,ba = rl()
gb,sb,bb = rl()
G = int(ga>=gb)
S = int(sa>=sb)
B = int(ba>=bb)
GSB =G+S+B
xa,xb,ya,yb,za,zb=ga,gb,sa,sb,ba,bb
mode = 0
if GSB==3:
    mode = 1
elif GSB==2:
    mode = 1
    if G==0:
        None
    elif S==0:
        xa,xb,ya,yb = sa,sb,ga,gb
    elif B==0:
        xa,xy,za,zb = ba,bb,ga,gb
elif GSB==1:
    mode = 0
    if G==1:
        None
    elif S==1:
        xa,xb,ya,yb = sa,sb,ga,gb
    elif B==1:
        xa,xy,za,zb = ba,bb,ga,gb
elif GSB==0:
    mode = 0

ans = 0
if GSB==0 or GSB==3:
    xa,xb,ya,yb,za,zb=min(xa,xb),max(xa,xb),min(ya,yb),max(ya,yb),min(za,zb),max(za,zb)
    for x in range(N//xa+1):
        n = N - x*xa
        for y in range(n//ya+1):
            n = n - y*ya
            z = n//za
            n = n- z*za
            ans = max(ans,n+x*xb+y*yb+z*zb)
elif GSB==1:
    for y in range(N//ya+1):
        n = N - y*ya
        z = n//za
        n = n - z*za
        n = n + y*yb + z*zb
        x = n//xb
        n = n - x*xb
        ans = max(ans,n+x*xa)
elif GSB==2:
    for x in range(N//xa+1):
        n = N - x*xa + x*xb
        for y in range(n//yb+1):
            n = n - y*yb
            z = n//zb
            n = n - z*zb
            ans = max(ans,n+y*ya+z*za)
print(ans)

Submission status: Wrong Answer

Solution code:
def inverse(a):
    return pow(a, mod - 2, mod)

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


def maxi(n, a, b):
    ans = 0
    for g in range(n//a[0]+1):
        d = n - g * a[0]
        if d < 0:
            continue
        for s in range(d//a[1]+1):
            d = n - g * a[0] - s * a[1]
            if d < 0:
                continue
            for br in [0, d//a[2]]:
                ans = max([ans, n - g - s - br + g * b[0] + s * b[1] + br * b[2]])
    return ans

def usearch(x, a):
    lft = 0
    rgt = len(a) + 1
    while rgt - lft > 1:
        mid = (rgt + lft) // 2
        if a[mid] <= x:
            lft = mid
        else:
            rgt = mid
    return lft


def lsearch(x, a):
    lft = 0
    rgt = len(a) + 1
    while rgt - lft > 1:
        mid = (rgt + lft) // 2
        if a[mid] < x:
            lft = mid
        else:
            rgt = mid
    return lft


main()

Submission status: Wrong Answer

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

g = (N//A_rate[0])*B_rate[0] + N%A_rate[0]
s = (N//A_rate[1])*B_rate[1] + N%A_rate[1]
b = (N//A_rate[2])*B_rate[2] + N%A_rate[2]

N = max(g,s,b)

g = (N//B_rate[0])*A_rate[0] + N%B_rate[0]
s = (N//B_rate[1])*A_rate[1] + N%B_rate[1]
b = (N//B_rate[2])*A_rate[2] + N%B_rate[2]

N = max(g,s,b)

print(N)

Submission status: Wrong Answer

Solution code:
n = int(input())

torihikiA = list(map(int, input().split()))
torihikiB = list(map(int, input().split()))

rate = 1
for i in range(len(torihikiA)):
    if torihikiA[i] > torihikiB[i]:
        rate *= torihikiA[i] / torihikiB[i]
    elif torihikiA[i] < torihikiB[i]:
        rate *= torihikiB[i] / torihikiA[i]
    else:
        rate *= 1

print("%d" % (rate*n))

Submission status: Wrong Answer

Solution code:
n = int(input())
metal_a = [int(x) for x in input().split()]
metal_b = [int(x) for x in input().split()]

sup_a = []
sup_b = []
for i in range(3):
    a_i = metal_a[i]
    b_i = metal_b[i]
    if a_i > b_i:
        sup_a.append((a_i, b_i))
    elif a_i < b_i:
        sup_b.append((a_i, b_i))

len_a = len(sup_a)
len_b = len(sup_b)

if len_a + len_b == 0:
    pass
elif len_a + len_b == 1:
    if len_a == 1:
        a, b = sup_a[0]
        n = int(n / b) * a + n % b
    else:
        a, b = sup_b[0]
        n = int(n / a) * b + n % a
elif len_a + len_b == 2:
    if len_a == 1 and len_b == 1:
        a, b = sup_b[0]
        n = int(n / a) * b + n % a
        a, b = sup_a[0]
        n = int(n / b) * a + n % b
    elif len_a == 2:
        a, b = sup_a[0]
        n1 = int(n / b) * a + n % b
        a, b = sup_a[1]
        n2 = int(n / b) * a + n % b
        n = max(n1, n2)
    else:
        a, b = sup_b[0]
        n1 = int(n / a) * b + n % a
        a, b = sup_b[1]
        n2 = int(n / a) * b + n % a
        n = max(n1, n2)
else:
    if len_a == 0:
        a, b = sup_b[0]
        n1 = int(n / a) * b + n % a
        a, b = sup_b[1]
        n2 = int(n / a) * b + n % a
        a, b = sup_b[2]
        n3 = int(n / a) * b + n % a
        n = max(n1, n2, n3)
    elif len_a == 1:
        a, b = sup_b[0]
        n1 = int(n / a) * b + n % a
        a, b = sup_b[1]
        n2 = int(n / a) * b + n % a
        n = max(n1, n2)
        a, b = sup_a[0]
        n = int(n / b) * a + n % b
    elif len_a == 2:
        a, b = sup_b[0]
        n = int(n / a) * b + n % a
        a, b = sup_a[0]
        n1 = int(n / b) * a + n % b
        a, b = sup_a[1]
        n2 = int(n / b) * a + n % b
        n = max(n1, n2)
    else:
        a, b = sup_a[0]
        n1 = int(n / b) * a + n % b
        a, b = sup_a[1]
        n2 = int(n / b) * a + n % b
        a, b = sup_a[2]
        n3 = int(n / b) * a + n % b
        n = max(n1, n2, n3)

print(n)

Submission status: Wrong Answer

Solution code:
def do_solve1(n, m1, m2):
    res = 0
    for i in xrange(3):
        v1, v2 = m1[i], m2[i]
        s1 = n * v1
        s2 = s1 / v2
        res = max(res, s2)
    return res

def do_solve2(n, m1, m2):
    res = 0
    for i in xrange(3):
        for j in xrange(3):
            money1 = n * m1[i]
            nut1 = money1 / m2[i]

            money2 = nut1 * m2[j]
            nut2 = money2 / m1[j]

            res = max(res, nut2)
    return res

def solve(n, m1, m2):
    res = n
    res = max(res, do_solve1(n, m1, m2))
    res = max(res, do_solve1(n, m2, m1))
    res = max(res, do_solve2(n, m1, m2))
    return res


n = int(raw_input())
m1 = map(int, raw_input().split())
m2 = map(int, raw_input().split())

print solve(n, m1, m2)

Submission status: Wrong Answer

Solution code:
N = int(input())
ca = list(map(int,input().split()))
cb = list(map(int,input().split()))
ans = N
ans += max([abs(a-b) for a,b in zip(ca,cb)]) * N
print(ans)

Submission status: Wrong Answer

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

f = [0, 0, 0]
for i in range(3):
  if A[i] < B[i]:
    f[i] = 1
  elif A[i] > B[i]:
    f[i] = -1

ma = N
for i in range(3):
  if f[i] < 0: continue
  Di = (N // A[i]) * B[i]
  Ni = N - (N // A[i]) * A[i]
  for j in range(3):
    if i == j: continue
    if f[j] < 0:
      ma = max(ma, Ni + Di)
      continue
    Dj = Di + (Ni // A[j]) * B[i]
    Nj = Ni - (Ni // A[j]) * A[j]
    for k in range(3):
      if i == k or j == k: continue
      if f[k]< 0:
        ma = max(ma, Nj + Dj)
        continue
      Dk = Dj + (Nj // A[k]) * B[k]
      Nk = Nj - (Nj // A[k]) * A[k]
      ma = max(ma, Nk + Dk)

N = ma
for i in range(3):
  if f[i] > 0: continue
  Di = (N // B[i]) * A[i]
  Ni = N - (N // B[i]) * B[i]
  for j in range(3):
    if i == j: continue
    if f[j] > 0:
      ma = max(ma, Ni + Di)
      continue
    Dj = Di + (Ni // B[j]) * A[j]
    Nj = Ni - (Ni // B[j]) * B[j]
    for k in range(3):
      if i == k or j == k: continue
      if f[k] > 0:
        ma = max(ma, Nj + Dj)
        continue
      Dk = Dj + (Nj // B[k]) * A[k]
      Nk = Nj - (Nj // B[k]) * B[k]
      ma = max(ma, Nk + Dk)

print(ma)

Submission status: Wrong Answer

Solution code:
n = int(input())

rate = []

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

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

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

rate_k_ab = [g_ab, s_ab, b_ab]
rate_k_ba = [g_ba, s_ba, b_ba]

rate_k_ab.sort()
rate_k_ba.sort()

#print(rate_k,rate_k[-1], rate_k[-2])

print(rate_k_ab[-1] * rate_k_ba[-1] * n)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
N = int(input())
a_list = np.array(list(map(int, input().split())))
b_list = np.array(list(map(int, input().split())))

a_div_b_list = a_list / b_list
a_div_b_dict = {}
for idx, ite in enumerate(a_div_b_list):
    a_div_b_dict[ite] = (a_list[idx], b_list[idx])

for adb, ab_tap in sorted(a_div_b_dict.items()):
    a = ab_tap[0]
    b = ab_tap[1]
    if adb < 1:
        q, mod = divmod(N, a)
        N = N - (q * a) + (q * b / a)
        if mod == 0:
            break
    else:
        break

for adb, ab_tap in sorted(a_div_b_dict.items(), key=lambda x: -x[0]):
    a = ab_tap[0]
    b = ab_tap[1]
    if adb > 1:
        q, mod = divmod(N, b)
        N = N - (q * b) + (q * a / b)
        if mod == 0:
            break
    else:
        break
print(int(N))

Submission status: Wrong Answer

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

def calc(n, X, Y):
    Z = [(X[i], Y[i]) for i in range(3) if Y[i] > X[i]]
    k = len(Z)
    if k == 0:
        return n
    if k == 1:
        a = n//Z[0][0]
        return n + a*(Z[0][1]-Z[0][0])
    if k == 2:
        ma = 0
        for a in range(n+1):
            b = (n-a*Z[0][0])//Z[1][0]
            ma = max(ma, n + a*(Z[0][1]-Z[0][0]) + b*(Z[1][1]-Z[1][0]))
        return ma
    ma = 0
    for a in range(n+1):
        for b in range(n+1-a):
            c = (n-a*Z[0][0]-b*Z[1][0])//Z[2][0]
            if c >= 0:
                ma = max(ma, n + a*(Z[0][1]-Z[0][0]) + b*(Z[1][1]-Z[1][0]) + c*(Z[2][1]-Z[2][0]))
    return ma

print(calc(calc(N, A, B), B, A))

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, N):
    a2b = filter(lambda i:xb[i] - xa[i]>0, (i for i in range(3)))
    maxN_a2b = N
    if a2b:
        for p in permutations(a2b):
            N0 = N
            y = [0] * 3
            for i in p:
                y[i] = N0 // xa[i]
                N0 = N0 % xa[i]
            for i in p:
                N0 += y[i] * xb[i]
            maxN_a2b = max(maxN_a2b, N0)
    return maxN_a2b


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

Submission status: Wrong Answer

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

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

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

p = max(n,dp[n])

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

Submission status: Wrong Answer

Solution code:
#D
N = int(input())
Agsb = list(map(int, input().split()))
Bgsb = list(map(int, input().split()))

divgsb = [Agsb[0]/Bgsb[0],
          Agsb[1]/Bgsb[1],
          Agsb[2]/Bgsb[2]]
vidgsb = [Bgsb[0]/Agsb[0],
          Bgsb[1]/Agsb[1],
          Bgsb[2]/Agsb[2]]
haveg = 0
haves = 0
haveb = 0

if max(vidgsb) == vidgsb[0] :
    if max(vidgsb[1:]) == vidgsb[1] :
        if vidgsb[0] > 1 :
            haveg = N//Agsb[0]
            N %= Agsb[0]
            if vidgsb[1] > 1 :
                haves = N//Agsb[1]
                N %= Agsb[1]
                if vidgsb[2] > 1 :
                    haveb = N//Agsb[2]
                    N %= Agsb[2]
    else :
        if vidgsb[0] > 1 :
            haveg = N//Agsb[0]
            N %= Agsb[0]
            if vidgsb[2] > 1 :
                haves = N//Agsb[2]
                N %= Agsb[2]
                if vidgsb[1] > 1 :
                    haveb = N//Agsb[1]
                    N %= Agsb[1]
elif max(vidgsb) == vidgsb[1] :
    if max(vidgsb[0], vidgsb[2]) == vidgsb[0] :
        if vidgsb[1] > 1 :
            haveg = N//Agsb[1]
            N %= Agsb[1]
            if vidgsb[0] > 1 :
                haves = N//Agsb[0]
                N %= Agsb[0]
                if vidgsb[2] > 1 :
                    haveb = N//Agsb[2]
                    N %= Agsb[2]
    else :
        if vidgsb[1] > 1 :
            haveg = N//Agsb[1]
            N %= Agsb[1]
            if vidgsb[2] > 1 :
                haves = N//Agsb[2]
                N %= Agsb[2]
                if vidgsb[0] > 1 :
                    haveb = N//Agsb[0]
                    N %= Agsb[0]
else :
    if max(vidgsb[:1]) == vidgsb[1] :
        if vidgsb[2] > 1 :
            haveg = N//Agsb[2]
            N %= Agsb[2]
            if vidgsb[1] > 1 :
                haves = N//Agsb[1]
                N %= Agsb[1]
                if vidgsb[0] > 1 :
                    haveb = N//Agsb[0]
                    N %= Agsb[0]
    else :
        if vidgsb[2] > 1 :
            haveg = N//Agsb[2]
            N %= Agsb[2]
            if vidgsb[0] > 1 :
                haves = N//Agsb[0]
                N %= Agsb[0]
                if vidgsb[1] > 1 :
                    haveb = N//Agsb[1]
                    N %= Agsb[1]
N += haveg*Bgsb[0] + haves*Bgsb[1] + haveb*Bgsb[2]        
haveg, haves, haveb = 0, 0, 0


if max(divgsb) == divgsb[0] :
    if max(divgsb[1:]) == divgsb[1] :
        if divgsb[0] > 1 :
            haveg = N//Bgsb[0]
            N %= Bgsb[0]
            if divgsb[1] > 1 :
                haves = N//Bgsb[1]
                N %= Bgsb[1]
                if divgsb[2] > 1 :
                    haveb = N//Bgsb[2]
                    N %= Bgsb[2]
    else :
        if divgsb[0] > 1 :
            haveg = N//Bgsb[0]
            N %= Bgsb[0]
            if divgsb[2] > 1 :
                haves = N//Bgsb[2]
                N %= Bgsb[2]
                if divgsb[1] > 1 :
                    haveb = N//Bgsb[1]
                    N %= Bgsb[1]
elif max(divgsb) == divgsb[1] :
    if max(divgsb[0], divgsb[2]) == divgsb[0] :
        if divgsb[1] > 1 :
            haveg = N//Bgsb[1]
            N %= Bgsb[1]
            if divgsb[0] > 1 :
                haves = N//Bgsb[0]
                N %= Bgsb[0]
                if divgsb[2] > 1 :
                    haveb = N//Bgsb[2]
                    N %= Bgsb[2]
    else :
        if divgsb[1] > 1 :
            haveg = N//Bgsb[1]
            N %= Bgsb[1]
            if divgsb[2] > 1 :
                haves = N//Bgsb[2]
                N %= Bgsb[2]
                if divgsb[0] > 1 :
                    haveb = N//Bgsb[0]
                    N %= Bgsb[0]
else :
    if max(divgsb[:1]) == divgsb[1] :
        if divgsb[2] > 1 :
            haveg = N//Bgsb[2]
            N %= Bgsb[2]
            if divgsb[1] > 1 :
                haves = N//Bgsb[1]
                N %= Bgsb[1]
                if divgsb[0] > 1 :
                    haveb = N//Bgsb[0]
                    N %= Bgsb[0]
    else :
        if divgsb[2] > 1 :
            haveg = N//Bgsb[2]
            N %= Bgsb[2]
            if divgsb[0] > 1 :
                haves = N//Bgsb[0]
                N %= Bgsb[0]
                if divgsb[1] > 1 :
                    haveb = N//Bgsb[1]
                    N %= Bgsb[1]
                    
N += haveg*Agsb[0] + haves*Agsb[1] + haveb*Agsb[2]
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())

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

lst.sort(reverse = True)

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




print (max(ans1, ans2, ans3, ans4, ans5, ans6, N))

Submission status: Wrong Answer

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

num = 0
if abs(ga - gb) > abs(sa - sb) and abs(ga - gb) > abs(ba - bb):
    num += n // min(ga, gb) * max(ga, gb)
    if abs(sa - sb) > abs(ba - bb):
        num += n % min(ga, gb) // min(sa, sb) * max(sa, sb)
        num += n % min(ga, gb) % min(sa, sb) // min(ba, bb) * max(ba, bb)
    else:
        num += n % min(ga, gb) // min(ba, bb) * max(ba, bb)
        num += n % min(ga, gb) % min(ba, bb) // min(sa, sb) * max(sa, sb)

elif abs(sa - sb) > abs(ga - gb) and abs(sa - sb) > abs(ba - bb):
    num += n // min(sa, sb) * max(sa, sb)
    if abs(ga - gb) > abs(ba - bb):
        num += n % min(sa, sb) // min(ga, gb) * max(ga, gb)
        num += n % min(sa, sb) % min(ga, gb) // min(ba, bb) * max(ba, bb)
    else:
        num += n % min(sa, sb) // min(ba, bb) * max(ba, bb)
        num += n % min(sa, sb) % min(ba, bb) // min(ga, gb) * max(ga, gb)

else:
    num += n // min(ba, bb) * max(ba, bb)
    if abs(sa - sb) > abs(ga - gb):
        num += n % min(ba, bb) // min(sa, sb) * max(sa, sb)
        num += n % min(ba, bb) % min(sa, sb) // min(ga, gb) * max(ga, gb)
    else:
        num += n % min(ba, bb) // min(ga, gb) * max(ga, gb)
        num += n % min(ba, bb) % min(ga, gb) // min(sa, sb) * max(sa, sb)

print(num)

Submission status: Wrong Answer

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

def calc(n, X, Y):
    Z = [(X[i], Y[i]) for i in range(3) if Y[i] > X[i]]
    k = len(Z)
    if k == 0:
        return n
    if k == 1:
        a = n//Z[0][0]
        print(a, n + a*(Z[0][1]-Z[0][0]))
        return n + a*(Z[0][1]-Z[0][0])
    if k == 2:
        ma = 0
        for a in range(n+1):
            b = (n-a*Z[0][0])//Z[1][0]
            ma = max(ma, n + a*(Z[0][1]-Z[0][0]) + b*(Z[1][1]-Z[1][0]))
        return ma
    ma = 0
    for a in range(n+1):
        for b in range(n+1-a):
            c = (n-a*Z[0][0]-b*Z[1][0])//Z[2][0]
            if c >= 0:
                ma = max(ma, n + a*(Z[0][1]-Z[0][0]) + b*(Z[1][1]-Z[1][0]) + c*(Z[2][1]-Z[2][0]))
    return ma

print(calc(calc(N, A, B), B, A))

Submission status: Wrong Answer

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


def solve(N: int, ga: int, sa: int, ba: int, gb: int, sb: int, bb: int):
    if ga > gb:
        ga, gb = gb, ga
    if sa > sb:
        sa, sb = sb, sa
    if ba > bb:
        ba, bb = bb, ba
    tmp = [[gb / ga, ga, gb], [sb / sa, sa, sb], [bb / ba, ba, bb]]
    tmp.sort(reverse=True, key=lambda x: x[0])

    ret = N
    rest = N
    for i in range(3):
        if tmp[i][0] > 1.0 and rest >= tmp[i][1]:
            cnt = rest // tmp[i][1]
            ret = ret - cnt + cnt * tmp[i][2]
            rest -= cnt
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    g_A = int(next(tokens))  # type: int
    s_A = int(next(tokens))  # type: int
    b_A = int(next(tokens))  # type: int
    g_B = int(next(tokens))  # type: int
    s_B = int(next(tokens))  # type: int
    b_B = int(next(tokens))  # type: int
    solve(N, g_A, s_A, b_A, g_B, s_B, b_B)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

ans = 0
for G in range(5001):
    for S in range(5001):
        if ga * G + sa * S > N:
            continue
        B = (N - (ga * G + sa * S)) // ba

        tmp_ans = (G * gb) + (S * sb) + (B * bb)
        ans = max(ans, tmp_ans)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
kane = [0,0,0]
l = [list(map(int, input().split())) for i in range(2)]
u = []
for i in range(3):
    u.append(l[1][i] / l[0][i])
l.append(u)
uu = sorted(u, reverse=True)
if max(u) <= 1:
    print(n)
else :
    kane[u.index(max(u))] += n // l[0][u.index(max(u))]
    n = n % l[0][u.index(max(u))]
    if uu[1] > 0:
        kane[u.index(uu[1])] += n // l[0][u.index(uu[1])]
        n = n % l[0][u.index(uu[1])]
        if uu[2] > 0:
            kane[u.index(uu[2])] += n // l[0][u.index(uu[2])]
            n = n % l[0][u.index(uu[2])]
    n += kane[u.index(max(u))] * l[1][u.index(max(u))]
    n += kane[u.index(uu[1])] * l[1][u.index(uu[1])]
    n += kane[u.index(uu[2])] * l[1][u.index(uu[2])]
    print(n)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
o = [0,0,0]
count = 0
b = list(map(int,input().split()))
c = list(map(int,input().split()))
o[0],o[1],o[2] = c[0]-b[0],c[1]-b[1],c[2]-b[2]
k = b[o.index(max(o))]
l = c[o.index(max(o))]
count += n//k*l
o.remove(max(o))
n = n%k
k = b[o.index(max(o))]
l = c[o.index(max(o))]
count += n//k*l
o.remove(max(o))
n = n%k
k = b[o.index(max(o))]
l = c[o.index(max(o))]
count += n//k*l
o.remove(max(o))
n = n%k
print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
def nap(N,goods):
    dp=[-1*float('inf')]*(N+1)
    dp[0]=0
    for i in range(N):
        for w,v in goods:
            if i+w<=N:
                dp[i+w]=max(dp[i+w],dp[i]+v)
    return max(dp)
N=nap(N,[(a,b) for a,b in zip(A,B)])
N=nap(N,[(b,a) for b,a in zip(B,A)])
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())
a,b,c = gb/ga,sb/sa,bb/ba
lst = [a,b,c]
lst.sort()
res = lst[2]*n
q = n/lst[2]
q = n - q*lst[2]
res += q*lst[1]
p = q
q = n/lst[1]
q = n - q*lst[1]
res += q*lst[0]
print(int(res))

Submission status: Wrong Answer

Solution code:
n = int(input())

rate = []

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

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

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

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

rate_k.sort()

#print(rate_k,rate_k[-1], rate_k[-2])

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

Submission status: Wrong Answer

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

f = [0, 0, 0]
for i in range(3):
  if A[i] < B[i]:
    f[i] = 1
  elif A[i] > B[i]:
    f[i] = -1

ma = N
for i in range(3):
  if f[i] < 0: continue
  Di = (N // A[i]) * B[i]
  Ni = N - N // A[i]
  for j in range(3):
    if i == j: continue
    if f[j] < 0:
      ma = max(ma, Ni + Di)
      continue
    Dj = Di + (Ni // A[j]) * B[i]
    Nj = Ni - Ni // A[j]
    for k in range(3):
      if i == k or j == k: continue
      if f[k]< 0:
        ma = max(ma, Nj + Dj)
        continue
        Dk = Dj + (Nj // A[k]) * B[k]
        Nk = Nj - Nj // A[k]
        ma = max(ma, Nk + Dk)

N = ma
for i in range(3):
  if f[i] > 0: continue
  Di = (N // B[i]) * A[i]
  Ni = N - N // B[i]
  for j in range(3):
    if i == j: continue
    if f[j] > 0:
      ma = max(ma, Ni + Di)
      continue
    Dj = Di + (Ni // B[j]) * A[j]
    Nj = Ni - Ni // B[j]
    for k in range(3):
      if i == k or j == k: continue
      if f[k] > 0:
        ma = max(ma, Nj + Dj)
        continue
        Dk = Dj + (Nj // B[k]) * A[k]
        Nk = Nj - Nj // B[k]
        ma = max(ma, Nk + Dk)

print(ma)