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())
ans=n
def f(k,a1,a2,a3,b1,b2,b3):
  A=[(1,b1/a1,b1,a1),(2,b2/a2,b2,a2),(3,b3/a3,b3,a3)]
  A.sort(key=lambda x:x[1],reverse=True)
  if A[0][1]<=1:
    return k
  else:
    ret=(k//A[0][3])*A[0][2]
    res=k%A[0][3]
    if A[1][1]<=1:
      return ret+res
    else:
      ret+=(res//A[1][3])*A[1][2]
      res=res%A[1][3]
      if A[2][1]<=1:
        return ret+res
      else:
        ret+=(res//A[2][3])*A[2][2]
        res=res%A[2][3]
        return ret+res
n=f(n,ga,sa,ba,gb,sb,bb)
print(f(n,gb,sb,bb,ga,sa,ba))

Submission status: Wrong Answer

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

diff = [(B[i]-A[i],A[i],B[i]) for i in range(3)]
diff.sort()
diff = diff[::-1]

x = N
y = N
for i in range(3):
    if diff[i][0] < 0:
        break
    a = diff[i][1]
    b = diff[i][2]
    y += x//a*(b-a)
    x -= x//a*a
diff = diff[::-1]
x = y
for i in range(3):
    if diff[i][0] > 0:
        break
    a = diff[i][1]
    b = diff[i][2]
    y += x//b*(a-b)
    x -= x//b*b
print(y)

Submission status: Wrong Answer

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

def calc(num,rate1,rate2):
    return [(num//rate1)*rate2 + num%rate1, num%rate1] if rate2-rate1 > 0 else [num,0]
lst = [N]
for i in range(3):
    tmplst = []
    for j in range(3):
        if i == j:
            continue
        first,remainder = calc(N,A_rate[i],B_rate[i])
        second,remainder = calc(remainder,A_rate[j],B_rate[j])
        third,remainder = calc(remainder,A_rate[3-i-j],B_rate[3-i-j])
        tmplst.append(first+second+third+remainder)
    lst.append(max(tmplst))

N = max(lst)

lst.clear()
lst.append(N)
for i in range(3):
    tmplst = []
    for j in range(3):
        if i == j:
            continue
        first,remainder = calc(N,B_rate[i],A_rate[i])
        second,remainder = calc(remainder,B_rate[j],A_rate[j])
        third,remainder = calc(remainder,B_rate[3-i-j],A_rate[3-i-j])
        tmplst.append(first+second+third+remainder)
    lst.append(max(tmplst))

N = max(lst)

Submission status: Wrong Answer

Solution code:
def change(N, A, B):
    ans = N
    for i in range(N//A[0]+1):
        if B[0] <= A[0] and i >= 1:
            break
        rem = N - A[0] * i
        for j in range(rem//A[1]+1):
            if B[1] <= A[1] and j >= 1:
                break
            remm = rem - A[1] * j
            if B[2] <= A[2]:
                k = 0
            else:
                k = remm // A[2]
            remmm = remm - A[2] * k
            ans = max(N, remmm + i*B[0] + j*B[1] + k*B[2])
    return ans

def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    N1 = change(N, A, B)
    N2 = change(N1, B, A)
    print(N2)


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())
ABrate = [(gb/ga, 'g'), (sb/sa,'s'), (bb/ba,'b')]
ABrate.sort(key= lambda x:x[0], reverse=True)
g = 0
s = 0
b = 0
for rate, item in ABrate:
    if rate < 1:
        continue
    if item == 'g':
        g = N//ga
        N -= g*ga
    elif item == 's':
        s = N//sa
        N -= s*sa
    elif item == 'b':
        b = N//ba
        N -= b*ba

N += g*gb + s*sb + b*bb

g = 0
s = 0
b = 0
ABrate.sort(key= lambda x:x[0])
for rate, item in ABrate:
    if 1 < rate:
        continue
    if item == 'g':
        g = N//gb
        N -= g*gb
    elif item == 's':
        s = N//sb
        N -= s*sb
    elif item == 'b':
        b = N//bb
        N -= b*bb

N += g*ga + s*sa + b*ba
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:
    ans += (ans//ups[0][0])*(ups[0][1]-ups[0][0])
    if len(downs) == 2:
        gold_diff = downs[0][0]-downs[0][1]
        silver_diff = downs[1][0]-downs[1][1]
        for gold in range(ans//downs[0][1]+1):
            left = ans - gold*downs[0][1]
            silver = left // downs[1][1]
            tmp_ans = (ans
                       + gold*gold_diff
                       + silver*silver_diff)
            ans = max(ans, tmp_ans)
    elif len(downs) == 1:
        ans += (ans//downs[0][1])*(downs[0][0]-downs[0][1])
    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())
para = [gb/(ga+1), sb/(sa+1), bb/(ba+1)]
pm = sorted(para, reverse=True)
ans = 0
i = 0
while(N):
    pi = para.index(pm[i])
    pp = para[pi]
    if pp >= 1:
        if pi == 0:
            ans += N // ga * gb
            N = N % ga
        elif pi == 1:
            ans += N // sa * sb
            N = N % sa
        elif pi == 2:
            ans += N // ba * bb
            N = N % ba
        if (N < ga) and (N < sa) and (N < ba):
            break
    else:
        break
    i += 1
N += ans
i = 0
para = [ga/(gb+1), sa/(sb+1), ba/(bb+1)]
pm = sorted(para, reverse=True)
while(N):
    pi = para.index(pm[i])
    pp = para[pi]
    if pp >= 1:
        if pi == 0:
            ans += N // gb * ga
            N = N % gb
        elif pi == 1:
            ans += N // sb * sa
            N = N % sb
        elif pi == 2:
            ans += N // bb * ba
            N = N % bb
        if (N < gb) and (N < sb) and (N < bb):
            break
    else:
        break
    i += 1
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())
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:
  ll[ind]+=n//la[ind]
  n=n%la[ind]
ind=s1.index(s1s[1])
if n>=la[ind] and s1s[1]>1:
  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:
  ll[ind]+=n//lb[ind]
  n=n%lb[ind]
ind=s1.index(s1s[1])
if n>=lb[ind] and s1s[1]<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(n)

Submission status: Wrong Answer

Solution code:
N = int(input())
ca = list(map(int,input().split()))
cb = list(map(int,input().split()))

ab = [a-b for a,b in zip(ca,cb)]
ba = [b-a for a,b in zip(ca,cb)] 

def cal(array,x):
    global N
    init = N
    a,b,c = array
    gx, sx, bx = x
    if a <= 0:
        r0 = 0
    else:
        r0 = init // gx
        init -= gx*r0
    if b <= 0:
        r1 = 0
    else:
        r1 = init // sx
        init -= sx*r1
    if c <= 0:
        r2 = 0
    else:
        r2 = init // bx
        init -= bx*r2
    res = 0
    for k in range(r0+1):
        for l in range(r1+1):
            for m in range(r2+1):
                loss = k*gx + l*sx + m*bx
                if  loss > N:
                    continue
                score = k*a + l*b + m*c
                if score > res:
                    res = score
    N += res

cal(ba,ca)
cal(ab,cb)

print(N)

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys

s = sys.stdin.read()
ss = s.split(maxsplit=1)
N = int(ss[0])
a = np.fromstring(ss[1], dtype='i4', sep=' ')
a = a.reshape((2, 3))
a.sort(axis=0)
d = a[1] / a[0]
ix = np.argsort(d)
b = a[0]
c = a[1]
b = b[ix]
c = c[ix]
d = d[ix]

ans = 0
for i in range(2, -1, -1):
    x = N // b[i]
    ans += x * c[i]
    N = N - x * b[i]
ans += N
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

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


# case
# すべて a > b
# すべて b < a
# 2個  a > b
# 1個  a > b

# weight:ga price:gb どんぐり weight:1 price:1


vmax = 0
for g in range(N+1):
    for s in range(N+1-g):
        v = (g//ga)*gb + (s//sa)*sb
        amari = N - g -s
        if amari < 0:
            break
        if ba < bb:
            v += amari//ba * bb
            amari %= ba
        v += amari

        vmax = max(vmax,v)

D = vmax
vmax = D
for g in range(D + 1):
    if ga < gb and g  > 0:
        break
    for s in range(D + 1 - g):
        if sa < sb and s > 0:
            break
        v = (g//gb)*ga + (s//sa)*sb
        amari = D - g - s
        if amari < 0:
            break
        if ba > bb:
            v += amari // bb * ba
            amari %= bb
        v += amari

        vmax = max(vmax, v)

print(vmax)

Submission status: Wrong Answer

Solution code:
def solve():
    N = int(input())
    As = list(map(int, input().split()))
    Bs = list(map(int, input().split()))

    C1, C2, C3 = [min(A, B) for A, B in zip(As, Bs)]
    D1, D2, D3 = [max(A, B) for A, B in zip(As, Bs)]

    K = 5000**2
    dp = [0] * (K+1)
    for i in range(K):
        if i+C1 <= K:
            dp[i+C1] = max(dp[i+C1], dp[i]+D1)
        if i+C2 <= K:
            dp[i+C2] = max(dp[i+C2], dp[i]+D2)
        if i+C3 <= K:
            dp[i+C3] = max(dp[i+C3], dp[i]+D3)
        if i+1 <= K:
            dp[i+1] = max(dp[i+1], dp[i]+1)

    print(dp[N])

solve()

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:
        n1 = N//gy
        N -= n1 * gy
        ans += n1*gx
    elif d == 1:
        n1 = N//sy
        N -= n1*sy
        ans += n1*sx
    elif d == 2:
        n1 = N//by
        N -= n1*by
        ans += n1*bx
    elif d == 3:
        n1 = N//gx
        N -= n1 * gx
        ans += n1*gy
    elif d == 4:
        n1 = N//sx
        N -= n1*sx
        ans += n1*sy
    elif d == 5:
        n1 = N//bx
        N -= n1*bx
        ans += n1*by
print(ans)

Submission status: Wrong Answer

Solution code:
###############################################################################

from sys import stdout
from bisect import bisect_left as binl
from copy import copy, deepcopy
from collections import defaultdict
import math


mod = 1


def intin():
    input_tuple = input().split()
    if len(input_tuple) <= 1:
        return int(input_tuple[0])
    return tuple(map(int, input_tuple))


def intina():
    return [int(i) for i in input().split()]


def intinl(count):
    return [intin() for _ in range(count)]


def modadd(x, y):
    global mod
    return (x + y) % mod


def modmlt(x, y):
    global mod
    return (x * y) % mod


def mod_inv(x):
    # available only when mod is prime
    global mod
    return pow(x, mod - 2, mod)


def gcm(x, y):
    while y != 0:
        z = x % y
        x = y
        y = z
    return x


def combination(x, y):
    assert(x >= y)

    ret = math.factorial(x)
    ret = ret // (math.factorial(x - y) * math.factorial(y))

    return ret


def get_divisors(x):
    retlist = []
    for i in range(1, int(x**0.5) + 3):
        if x % i == 0:
            retlist.append(i)
            retlist.append(x // i)
    return retlist


def get_factors(x):
    retlist = []
    for i in range(2, int(x**0.5) + 3):
        while x % i == 0:
            retlist.append(i)
            x = x // i
    retlist.append(x)
    return retlist


def make_linklist(xylist):
    linklist = {}
    for a, b in xylist:
        linklist.setdefault(a, [])
        linklist.setdefault(b, [])
        linklist[a].append(b)
        linklist[b].append(a)
    return linklist


def calc_longest_distance(linklist, v=1):
    distance_list = {}
    distance_count = 0
    distance = 0
    vlist_previous = []
    vlist = [v]
    nodecount = len(linklist)

    while distance_count < nodecount:
        vlist_next = []
        for v in vlist:
            distance_list[v] = distance
            distance_count += 1
            vlist_next.extend(linklist[v])
        distance += 1
        vlist_to_del = vlist_previous
        vlist_previous = vlist
        vlist = list(set(vlist_next) - set(vlist_to_del))

    max_distance = -1
    max_v = None
    for v, distance in distance_list.items():
        if distance > max_distance:
            max_distance = distance
            max_v = v

    return (max_distance, max_v)


def calc_tree_diameter(linklist, v=1):
    _, u = calc_longest_distance(linklist, v)
    distance, _ = calc_longest_distance(linklist, u)
    return distance


class UnionFind:

    def __init__(self, n):
        self.parent = [i for i in range(n)]

    def root(self, i):
        if self.parent[i] == i:
            return i
        self.parent[i] = self.root(self.parent[i])
        return self.parent[i]

    def unite(self, i, j):
        rooti = self.root(i)
        rootj = self.root(j)
        if rooti == rootj:
            return
        if rooti < rootj:
            self.parent[rootj] = rooti
        else:
            self.parent[rooti] = rootj

    def same(self, i, j):
        return self.root(i) == self.root(j)


###############################################################################


def main():
    n = intin()
    gsb_a = intina()
    gsb_b = intina()

    ga = gsb_a[0]
    sa = gsb_a[1]
    ba = gsb_a[2]

    gb = gsb_b[0]
    sb = gsb_b[1]
    bb = gsb_b[2]

    g = s = b = 0

    # a: buy
    g_ratio = gb / ga
    s_ratio = sb / sa
    b_ratio = bb / ba
    ratios = [(g_ratio, 'g'), (s_ratio, 's'), (b_ratio, 'b')]
    ratios.sort(reverse=True)

    for ratio, t in ratios:
        if ratio <= 1:
            break
        if t == 'g':
            nn = n
            n -= ga * (nn // ga)
            g += nn // ga
        if t == 's':
            nn = n
            n -= sa * (nn // sa)
            s += nn // sa
        if t == 'b':
            nn = n
            n -= ba * (nn // ba)
            b += nn // ba

    # b: sell
    n += g * gb
    n += s * sb
    n += b * bb
    g = s = b = 0

    # b: buy
    g_ratio = ga / gb
    s_ratio = sa / sb
    b_ratio = ba / bb
    ratios = [(g_ratio, 'g'), (s_ratio, 's'), (b_ratio, 'b')]
    ratios.sort(reverse=True)

    for ratio, t in ratios:
        if ratio <= 1:
            break
        if t == 'g':
            nn = n
            n -= gb * (nn // gb)
            g += nn // gb
        if t == 's':
            nn = n
            n -= sb * (nn // sb)
            s += nn // sb
        if t == 'b':
            nn = n
            n -= bb * (nn // bb)
            b += nn // bb

    # a: sell
    n += g * ga
    n += s * sa
    n += b * ba
    g = s = b = 0

    print(n)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
MOD = 1000000007
def POW(x, y): return pow(x, y, MOD)
def INV(x, m=MOD): return pow(x, m - 2, m)
def DIV(x, y, m=MOD): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())


N=II()
GA,SA,BA=LI()
GB,SB,BB=LI()

GM,SM,BM = max(GA,GB), max(SA,SB), max(BA,BB)
Gm,Sm,Bm = min(GA,GB), min(SA,SB), min(BA,BB)

res = N
for i in range(N+1):
    num_g = i//Gm
    rest_g = i % Gm
    for j in range(0, N-i+1):
        k = N-j-i

        num_s = j//Sm
        rest_s = j % Sm

        num_b = k // Bm
        rest_b = k % Bm

        res = max(res, rest_g + rest_s + rest_b + num_g*GM + num_s*SM + num_b*BM)

print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
List_atob = sorted([gb/ga,  sb/sa, bb/ba])
List_btoa =sorted([ga/gb,sa/sb,ba/bb])
res = N
for i in range(5000):
    for j in range(5000-i):
        if ga*i+sa*j<=N:
            k = (N-ga*i-sa*j)//ba
            res = max(res, gb*i+sb*j+bb*k+(N-ga*i-sa*j)%ba)
N=res  
for i in range(5000):
    for j in range(5000-i):
        if gb*i+sb*j<=N:
            k = (N-gb*i-sb*j)//bb
            res = max(res, ga*i+sa*j+ba*k+(N-gb*i-sb*j)%bb)
print(res)

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:
    ans += (ans//ups[0][0])*(ups[0][1]-ups[0][0])
    if len(downs) == 2:
        gold_diff = downs[0][0]-downs[0][1]
        silver_diff = downs[1][0]-downs[1][1]
        for gold in range(ans//downs[0][1]+1):
            left = N - gold*downs[0][1]
            silver = left // downs[1][1]
            tmp_ans = (ans
                       + gold*gold_diff
                       + silver*silver_diff)
            ans = max(ans, tmp_ans)
    elif len(downs) == 1:
        ans += (ans//downs[0][1])*(downs[0][0]-downs[0][1])
    print(ans)

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]
    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]

    if rita[idx[2]] > 1:
        # こちらで買うものなし
        kin = n // prb[0]
        n -= kin * prb[0]
        n += kin * pra[1]
        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:
# Enter your code here. Read input from STDIN. Print output to STDOUT

def find3(N,a,b,c,A,B,C):
    MAX=N
    for x in xrange(5001):
        for y in xrange(5001):
            temp=N-(a*x+b*y)
            if temp>=0:
                z=temp//c
                MAX=max(MAX,N-(a*x+b*y+c*z)+(A*x+B*y+C*z))
            else:
                break
    return MAX

def find2(N,a,b,A,B):
    MAX=N
    for x in xrange(N+1):
        temp=N-a*x
        if temp>=0:
            y=temp//b
            MAX=max(MAX,N-(a*x+b*y)+(A*x+B*y))
        else:
            break
   
    return max(MAX,N+(A-a)*x+(B-b)*y)
    

def find1(N,a,A):
    MAX=N
    num=N//a
    return N-num*a+num*A

N=int(raw_input())
X=list(map(int,raw_input().strip().split(' ')))
Y=list(map(int,raw_input().strip().split(' ')))

##stage1
index=[0]*3

for i in xrange(3):
    if X[i]<Y[i]:
        index[i]=1
post_1=[]
post_2=[]
for i in xrange(len(index)):
    if index[i]>0:
        post_1+=[i]    
    else:
        post_2+=[i]
number=sum(index)
if number==0:
    print(find3(N,Y[0],Y[1],Y[2],X[0],X[1],X[2]))
elif number==1:
    N=find1(N,X[post_1[0]],Y[post_1[0]])
    N=find2(N,Y[post_2[0]],Y[post_2[1]],X[post_2[0]],X[post_2[1]])
    print(N)
elif number==2:
    N=find2(N,X[post_1[0]],X[post_1[1]],Y[post_1[0]],Y[post_1[1]])
    N=find1(N,Y[post_2[0]],X[post_2[0]])
    print(N)
else:
    N=find3(N,X[0],X[1],X[2],Y[0],Y[1],Y[2])
    print(N)

Submission status: Wrong Answer

Solution code:
N = int(input())
Ag, As, Ab = map(int, input().split())
Bg, Bs, Bb = map(int, input().split())

ABg = N % Ag + (N // Ag) * Bg
ABb = N % Ab + (N // Ab) * Bb
ABs = N % As + (N // As) * Bs

N = max(ABg, ABb, ABs, N)

ABAg = N % Bg + (N // Bg) * Ag
ABAb = N % Bb + (N // Bb) * Ab
ABAs = N % Bs + (N // Bs) * As

ans = max(ABAg, ABAb, ABAs, N)

print(ans)

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:
    ans += (ans//ups[0][0])*(ups[0][1]-ups[0][0])
    if len(downs) == 2:
        gold_diff = downs[0][0]-downs[0][1]
        silver_diff = downs[1][0]-downs[1][1]
        for gold in range(N//downs[0][1]+1):
            left = N - gold*downs[0][1]
            silver = left // downs[1][1]
            tmp_ans = (ans
                       + gold*gold_diff
                       + silver*silver_diff)
            ans = max(ans, tmp_ans)
    elif len(downs) == 1:
        ans += (ans//downs[0][1])*(downs[0][0]-downs[0][1])
    print(ans)

Submission status: Wrong Answer

Solution code:
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)


main()

Submission status: Wrong Answer

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

gmin = min(ga,gb)
gmax = max(ga,gb)
smin = min(sa,sb)
smax = max(sa,sb)
bmin = min(ba,bb)
bmax = max(ba,bb)

if min(gmin,smin,bmin) > n:
    print(n)
else:
    ans = 0
    for i in range( (n//gmin) + 1):
        k = n - gmin*i
        for s in range( (k//smin) +1 ):
            ba = k - s*smin
            ja = ba//bmin
            #print(i,s,ja)
            if k >= 0 and s >= 0 and ja >= 0:
                #print(i,s,ja)
                ans = max(ans, (i*gmax + s*smax + ja* bmax) + n-(i*gmin+s*smin+ja*bmin))
    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())
gmax = max(gb,ga)
gmin = min(gb,ga)
smax = max(sb,sa)
smin = min(sb,sa)
bmax = max(bb,ba)
bmin = min(bb,ba)
count = 0
a = 0

if max(gmax/gmin,smax/smin,bmax/bmin)==gmax/gmin:
    count = (n//gmin)*gmax
    a = n%gmin
    if smax/smin >=bmax/bmin:
        if a>=smin:
            count = count + (a//smin)*smax
            a = a%smin
        if a >= bmin:
            count = count + (a//bmin)*bmax
    else:
        if a >= bmin:
            count = count + (a//bmin)*bmax
            a = a%bmin
        if a>=smin:
            count = count + (a//smin)*smax
elif max(gmax/gmin,smax/smin,bmax/bmin)==smax/smin:
    count = (n//smin)*smax
    a = n%smin
    if gmax/gmin >=bmax/bmin:
        if a>=gmin:
            count = count + (a//gmin)*gmax
            a = a%gmin
        if a >= bmin:
            count = count + (a//bmin)*bmax
    else:
        if a >= bmin:
            count = count + (a//bmin)*bmax
            a = a%bmin
        if a>=gmin:
            count = count + (a//gmin)*gmax
else:
    count = (n//bmin)*bmax
    a = n%bmin
    if gmax/gmin >=smax/smin:
        if a>=gmin:
            count = count + (a//gmin)*gmax
            a = a%gmin
        if a >= smin:
            count = count + (a//smin)*smax
    else:
        if a >= smin:
            count = count + (a//smin)*smax
            a = a%smin
        if a>=gmin:
            count = count + (a//gmin)*gmax
    
print(count)