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:
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in input().split()]
def II(): return int(input())


N=II()
RA=LI()
RB=LI()
GAIN1=[]
GAIN2=[]
for i in range(3):
    if RA[i] < RB[i]:
        GAIN1.append((RA[i], RB[i]-RA[i]))
    if RA[i] > RB[i]:
        GAIN2.append((RB[i], RA[i]-RB[i]))

memo1 = {}
def dp1(n):
    if n <= 0:
        return 0
    if n in memo1:
        return memo1[n]
    sol = 0
    for cost, g in GAIN1:
        sol = max(dp1(n-cost)+g, sol)
    memo1[n] = sol
    return sol

memo2 = {}
def dp2(n):
    if n <= 0:
        return 0
    if n in memo2:
        return memo2[n]
    sol = 0
    for cost, g in GAIN2:
        sol = max(dp2(n-cost)+g, sol)
    memo2[n] = sol
    return sol

med = N+dp1(N)
print(med+dp2(med))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in input().split()]
def II(): return int(input())


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

memo1 = {}
def dp1(n):
    if n <= 0:
        return 0
    if n in memo1:
        return memo1[n]
    sol = max(dp1(n-GA)+GB-GA,dp1(n-SA)+SB-SA, dp1(n-BA)+BB-BA, 0)
    memo1[n] = sol
    return sol
memo2 = {}
def dp2(n):
    if n <= 0:
        return 0
    if n in memo2:
        return memo2[n]
    sol = max(dp2(n-GB)+GA-GB,dp2(n-SB)+SA-SB, dp2(n-BB)+BA-BB, 0)
    memo2[n] = sol
    return sol

med = N+dp1(N)
print(med+dp2(med))

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

memo1 = {}
def dp1(n):
    if n <= 0:
        return 0
    if n in memo1:
        return memo1[n]
    sol = max(dp1(n-GA)+GB-GA,dp1(n-SA)+SB-SA, dp1(n-BA)+BB-BA)
    memo1[n] = sol
    return sol
memo2 = {}
def dp2(n):
    if n <= 0:
        return 0
    if n in memo2:
        return memo2[n]
    sol = max(dp2(n-GB)+GA-GB,dp2(n-SB)+SA-SB, dp2(n-BB)+BA-BB)
    memo2[n] = sol
    return sol

med = N+dp1(N)
print(med+dp2(med))

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

memo1 = {}
def dp1(n):
    if n <= 0:
        return 0
    if n in memo1:
        return memo1[n]
    sol = max(dp1(n-GA)+GB-GA,dp1(n-SA)+SB-SA, dp1(n-BA)+BB-BA)
    memo1[n] = sol
    return sol
memo2 = {}
def dp2(n):
    if n <= 0:
        return 0
    if n in memo2:
        return memo2[n]
    sol = max(dp2(n-GB)+GA-GB,dp2(n-SB)+SA-SB, dp2(n-BB)+BA-SB)
    memo2[n] = sol
    return sol

med = N+dp1(N)
print(med+dp2(med))

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

memo1 = {}
def dp1(n):
    if n <= 0:
        return 0
    if n in memo1:
        return memo1[n]
    sol = max(dp1(n-GA)+GB,dp1(n-SA)+SB, dp1(n-BA)+BB)
    memo1[n] = sol
    return sol
memo2 = {}
def dp2(n):
    if n <= 0:
        return 0
    if n in memo2:
        return memo2[n]
    sol = max(dp2(n-GB)+GA,dp2(n-SB)+SA, dp2(n-BB)+BA)
    memo2[n] = sol
    return sol

print(dp2(dp1(N)))

Submission status: Wrong Answer

Solution code:
def transaction(N, r):
    s = N
    for k, v in sorted(r.items(), key=lambda x: -x[1]):
        n = int(N / k[0])
        s += n * k[1]
        N -= n * k[0]
        if N == 0:
            break
    return s

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

r = {}
for i in range(3):
    (a, b) = (A[i], B[i])
    if b > a:
        r[(a, b - a)] = b / a
        
N = transaction(N, r)
r = {}
for i in range(3):
    (a, b) = (A[i], B[i])
    if a > b:
        r[(b, a - b)] = a / b
        
print(transaction(N, r))

Submission status: Wrong Answer

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

r = {}
for i in range(3):
    (a, b) = (A[i], B[i])
    if a > b:
        r[(b, a - b)] = a / b
    elif b > a:
        r[(a, b - a)] = b / a

s = N
for k, v in sorted(r.items(), key=lambda x: -x[1]):
    n = int(N / k[0])
    s += n * k[1]
    N -= n * k[0]
    if N == 0:
        break

print(s)

Submission status: Wrong Answer

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

r = {}
for i in range(3):
    (a, b) = (A[i], B[i])
    if a > b:
        r[(b, a)] = a / b
    elif b > a:
        r[(a, b)] = b / a

s = 0
for k, v in sorted(r.items(), key=lambda x: -x[1]):
    n = int(N / k[0])
    s += n * k[1]
    N -= n * k[0]
    if N == 0:
        break

print(s)

Submission status: Wrong Answer

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

r = {}
for i in range(3):
    (a, b) = (A[i], B[i])
    if a > b:
        r[(b, a)] = a / b
    elif b > a:
        r[(a, b)] = b / a

s = 0
for k, v in sorted(r.items(), key=lambda x: -x[1]):
    n = int(N / k[0]) * k[1]
    s += n
    N -= n
    if N == 0:
        break

print(s)

Submission status: Wrong Answer

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

r = {}
for i in range(3):
    (a, b) = (A[i], B[i])
    if a > b:
        r[a / b] = b
    elif b > a:
        r[b / a] = a

s = 0
for k, v in sorted(r.items(), key=lambda x: -x[0]):
    n = int(N / v) * k
    s += n
    N -= n
    if N == 0:
        break

print(s)

Submission status: Wrong Answer

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

gl = max(ga, gb)
gs = min(ga, gb)
sl = max(sa, sb)
ss = min(sa, sb)
bl = max(ba, bb)
bs = min(ba, bb)

r_list = [[gl, gs], [sl, ss], [bl, bs]]
r_list.sort(key=lambda x: x[0]/x[1], reverse=True)

n = N
ans = 0
for r in r_list:
    if n > r[1]:
        c = n // r[1]
        n -= c*r[1]
        ans += c*r[0]

print(ans+n)

Submission status: Wrong Answer

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

gl = max(ga, gb)
gs = min(ga, gb)
sl = max(sa, sb)
ss = min(sa, sb)
bl = max(ba, bb)
bs = min(ba, bb)

r_list = [[gl, gs], [sl, ss], [bl, bs]]
r_list.sort(key=lambda x: x[0]/x[1], reverse=True)

n = N
ans = 0
for r in r_list:
    if n > r[1]:
        c = n // r[1]
        n -= c*r[1]
        ans += c*r[0]

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
aa=list(map(int,input().split()))
bb=list(map(int,input().split()))
dm=[]
for a,b in zip(aa,bb):
    if a==b:continue
    elif a>b:a,b=b,a
    d=b/a
    dm.append([d,a,b])
dm.sort(key=lambda x:(-x[0],x[1]))
ans=0
for d,m,M in dm:
    s,n=divmod(n,m)
    ans+=s*M
ans+=n
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
dm=[]
for ak,bk in zip(a,b):
    d=abs(ak-bk)
    if d==0:continue
    if ak>bk:
        ak,bk=bk,ak
    dm.append([d,ak,bk])
dm.sort(reverse=True)
ans=0
for d,m,M in dm:
    s,n=divmod(n,m)
    ans+=s*M
ans+=n
print(ans)

Submission status: Wrong Answer

Solution code:
def exchange(n, s, d):
  v = [n] * (n + 1)
  r = [(1, 1)] + [(a, b) for a, b in zip(s, d) if s < d]

  for i in range(len(v) - 1):
    for a, b in r:
      if i + a < len(v):
        v[i + a] = max(v[i] - a + b, v[i + a])

  return v[-1]


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

  n = exchange(n, a, b)
  n = exchange(n, b, a)

  print(n)


if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
def exchange(n, s, d):
  v = [n] * n
  s = [1] + s
  d = [1] + d

  for i in range(n - 1):
    for a, b in zip(s, d):
      if i + a < n:
        v[i + a] = max(v[i] - a + b, v[i + a])

  return v[-1]


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

  n = exchange(n, a, b)
  n = exchange(n, b, a)

  print(n)


if __name__ == '__main__':
  main()

Submission status: Wrong Answer

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

array = [(1, b[0] / a[0]), (2, b[1] / a[1]), (3, b[2] / a[2])]
array.sort(key=lambda x: x[1], reverse=True)

debug = [N, 0, 0, 0]
for arr in array:
    if arr[1] > 1:
        debug[arr[0]] += debug[0] // a[arr[0]-1]
        debug[0] -= (debug[0] // a[arr[0]-1]) * a[arr[0]-1]

for arr in array:
    if arr[1] > 1:
        debug[0] += debug[arr[0]] * b[arr[0]-1]
        debug[arr[0]] = 0

for arr in array[::-1]:
    if arr[1] < 1:
        debug[arr[0]] += debug[0] // b[arr[0]-1]
        debug[0] -= (debug[0] // b[arr[0]-1]) * b[arr[0]-1]

for arr in array[::-1]:
    if arr[1] < 1:
        debug[0] += debug[arr[0]] * a[arr[0]-1]
        debug[arr[0]] = 0

print(debug[0])

Submission status: Wrong Answer

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

array = [(1, b[0] / a[0]), (2, b[1] / a[1]), (3, b[2] / a[2])]
array.sort(key=lambda x: x[1], reverse=True)

debug = [N, 0, 0, 0]
for arr in array:
    if arr[1] > 1:
        debug[arr[0]] += debug[0] // a[arr[0]-1]
        debug[0] -= (debug[0] // a[arr[0]-1]) * a[arr[0]-1]

for arr in array:
    if arr[1] > 1:
        debug[0] += debug[arr[0]] * b[arr[0]-1]
        debug[arr[0]] = 0

for arr in array:
    if arr[1] < 1:
        debug[arr[0]] += debug[0] // b[arr[0]-1]
        debug[0] -= (debug[0] // b[arr[0]-1]) * b[arr[0]-1]

for arr in array:
    if arr[1] < 1:
        debug[0] += debug[arr[0]] * a[arr[0]-1]
        debug[arr[0]] = 0

print(debug[0])

Submission status: Wrong Answer

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

array = [(1, b[0] / a[0]), (2, b[1] / a[1]), (3, b[2] / a[2])]
array.sort(key=lambda x: x[1], reverse=True)

debug = [N, 0, 0, 0]
for arr in array:
    if arr[1] > 1:
        debug[arr[0]] += N // a[arr[0]-1]
        debug[0] -= (N // a[arr[0]-1]) * a[arr[0]-1]

for arr in array:
    if arr[1] > 1:
        debug[0] += debug[arr[0]] * b[arr[0]-1]
        debug[arr[0]] = 0

print(debug[0])

Submission status: Wrong Answer

Solution code:
import math
import os
import sys

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

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

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


def knapsack(n, ga, sa, ba, gb, sb, bb):
    values = []
    weights = []
    if ga / gb > 1:
        values.append(ga - gb)
        weights.append(gb)
    if sa / sb > 1:
        values.append(sa - sb)
        weights.append(sb)
    if ba / bb > 1:
        values.append(ba - bb)
        weights.append(bb)
    if len(values) == 3:
        # dp[i]: 重さ i 以下で選ぶのの価値の最大値
        dp = [0] * (n + 1)
        dp[0] = n
        for i in range(1, n + 1):
            x = dp[i - 1]
            for w, v in zip(weights, values):
                if i - w >= 0:
                    x = max(x, dp[i - w] + v)
            dp[i] = x
        return dp[n]
    if len(values) == 2:
        w1, w2 = weights
        v1, v2 = values
        if (v1 + w1) / w1 < (v2 + w2) / w2:
            v1, v2 = v2, v1
            w1, w2 = w2, w1
        c1 = n // w1
        c2 = (n - w1 * c1) // w2
        ret = n + v1 * c1 + v2 * c2
        while c1 >= 0:
            a = n - w1 * c1 - w2 * c2
            c1 -= max(1, math.ceil((w2 - a) / w1))
            c2 = (n - w1 * c1) // w2
            ret = max(ret, n + v1 * c1 + v2 * c2)
            if w1 * c1 + w2 * c2 == n:
                break
        return ret
    if len(values) == 1:
        return n + n // weights[0] * values[0]
    return n


ans = knapsack(N, GA, SA, BA, GB, SB, BB)
ans = knapsack(ans, GB, SB, BB, GA, SA, BA)
print(int(ans))

Submission status: Wrong Answer

Solution code:
import math
import os
import sys

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

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

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

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

values = []
weights = []
if GA / GB > 1:
    values.append(GA - GB)
    weights.append(GB)
if SA / SB > 1:
    values.append(SA - SB)
    weights.append(SB)
if BA / BB > 1:
    values.append(BA - BB)
    weights.append(BB)
# dp[i]: 重さ i 以下で選ぶのの価値の最大値
dp = [0] * (N + 1)
dp[0] = N
if len(values) == 3:
    for i in range(1, N + 1):
        x = dp[i - 1]
        for w, v in zip(weights, values):
            if i - w >= 0:
                x = max(x, dp[i - w] + v)
        dp[i] = x
    N = dp[N]
if len(values) == 2:
    w1, w2 = weights
    v1, v2 = values
    if (v1 + w1) / w1 < (v2 + w2) / w2:
        v1, v2 = v2, v1
        w1, w2 = w2, w1
    c1 = N // w1
    c2 = (N - w1 * c1) // w2
    ret = N + v1 * c1 + v2 * c2
    while c1 >= 0:
        a = (N - w1 * c1) % w2
        c1 -= math.ceil(a / w1)
        c2 = (N - w1 * c1) // w2
        ret = max(ret, N + v1 * c1 + v2 * c2)
        if w1 * c1 + w2 * c2 == N:
            break
    N = ret
if len(values) == 1:
    N += N // weights[0] * values[0]
print(int(N))

Submission status: Wrong Answer

Solution code:
import bisect

n = int(input())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))

rates_list = [[],[]]
for i in range(3):
	if aa[i] > bb[i]:
		rates_list[1].append([bb[i], aa[i]])
	else:
		rates_list[0].append([aa[i], bb[i]])

max_d = n
for j in range(2):
	rates = rates_list[j]
	if len(rates) == 1:
		max_d = (max_d // rates[0][0]) * rates[0][1] + max_d % rates[0][0]
	elif len(rates) == 2:
		val = [rates[0][1] * i for i in range(max_d // rates[0][0] + 1)]
		rest = [max_d - x for x in val]
		val = [val[i] + rates[1][1] * (rates[1][0] // rest[i]) + (rates[1][0] % rest[i]) for i in range(len(val))]
		max_d = max(val)
	elif len(rates) == 3:
		dp = [(d // rates[0][0]) * rates[0][1] + d % rates[0][0] for d in range(max_d + 1)]
		for d in range(1, max_d + 1):
			dp[d] = max([dp[d]] + [dp[d - rates[i][0]] + rates[i][1] if d - rates[i][0] >= 0 else d for i in range(1, len(rates))])
		max_d = dp[-1]
print(max_d)

Submission status: Wrong Answer

Solution code:
n = int(input())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))

rates_list = [[],[]]
for i in range(3):
	if aa[i] > bb[i]:
		rates_list[1].append([bb[i], aa[i]])
	else:
		rates_list[0].append([aa[i], bb[i]])

max_d = n
for j in range(2):
	rates = rates_list[j]
	if len(rates) == 1:
		max_d = (max_d // rates[0][0]) * rates[0][1] + max_d % rates[0][0]
	elif len(rates) == 2:
		dp = [rates[0][1] * (d // rates[0][0]) + rates[1][1] * ((max_d - d + d % rates[0][0]) // rates[1][0]) + ((max_d - d + d % rates[0][0]) % rates[1][0]) for d in range(max_d)]
		max_d = max(dp)
	elif len(rates) == 3:
		dp = [(d // rates[0][0]) * rates[0][1] + d % rates[0][0] for d in range(max_d + 1)]
		for d in range(1, max_d + 1):
			dp[d] = max([dp[d]] + [dp[d - rates[i][0]] + rates[i][1] if d - rates[i][0] >= 0 else d for i in range(1, len(rates))])
		max_d = dp[-1]
print (max_d)

Submission status: Wrong Answer

Solution code:
import random
import bisect
import math

option = 0  # 0:PSO 1:CFA 2:CFArank
max_or_min = 0  # 0:minimize 1:maxmize
filewrite = 0  # 1:writefile 2:benchmark
dimension = 2
iter = 1

N = 200  # 粒子の数
T = 2000  # 世代数(ループの回数)


# --------粒　子　群　最　適　化------------------------------

# 評価関数
def criterion(x, x_min, x_max):
    x_br = n - x[0] - x[1]
    z1 = x[0] // ga * gb + x[1] // sa * sb + x_br // ba * bb
    z2 = x[0] // ga * gb + x[1] // sa * sb
    z = -max(z1, z2)
    if x_br < 0 or x[0] < 0 or x[1] < 0:
        z = float("inf")
    return z


# 粒子の位置の更新を行う関数
def update_position(x, v):
    new_x = [x[i] + v[i] for i in range(dimension)]
    return new_x


#粒子の速度の更新を行う関数
def update_velocity(x, v, p, g, w=1, ro_max=0.5,v_max=10):
    #パラメーターroはランダムに与える
    ro1 =random.uniform(0, ro_max)
    ro2 =random.uniform(0, ro_max)
    #粒子速度の更新を行う
    new_v = [int((w * v[i] + ro1 * (p[i] - x[i]) + ro2 * (g[i] - x[i])))  for i in range(dimension)]
    for i in range(dimension):
        if abs(new_v[i]) > v_max:
            new_v[i] = int(v_max * new_v[i] / abs(new_v[i]))
    return new_v

def main():
    w = 0.5
    w_best, w_worst = 1.25, 0.25
    x_min = [minimum for i in range(dimension)]
    x_max = [maximum for i in range(dimension)]

    # 粒子位置, 速度, パーソナルベスト, グローバルベストの初期化を行う
    ps = [[random.randint(x_min[j], x_max[j]) for j in range(dimension)] for i in range(N)]
    vs = [[0 for j in range(dimension)] for i in range(N)]

    personal_best_positions = list(ps)
    personal_best_scores = [criterion(p, x_min, x_max) for p in ps]
    best_particle = personal_best_scores.index(max(personal_best_scores))
    global_best_position = personal_best_positions[best_particle]

    for t in range(T):
        for n in range(N):
            x = ps[n]
            v = vs[n]
            p = personal_best_positions[n]

            if option >= 2:
                best_list = sorted(personal_best_positions)
                mu = bisect.bisect_left(best_list, p) + 1
                w = w_best - mu * (w_best - w_worst) / (N - 1)

            # 粒子の位置の更新を行う
            new_x = update_position(x, v)
            ps[n] = new_x

            # 粒子の速度の更新を行う
            new_v = update_velocity(new_x, v, p, global_best_position, w)
            vs[n] = new_v

            # 評価値を求め, パーソナルベストの更新を行う
            score = criterion(new_x, x_min, x_max)

            if max_or_min == 1:
                if score > personal_best_scores[n]:
                    personal_best_scores[n] = score
                    personal_best_positions[n] = new_x
            elif max_or_min == 0:
                if score < personal_best_scores[n]:
                    personal_best_scores[n] = score
                    personal_best_positions[n] = new_x

        # グローバルベストの更新を行う
        if max_or_min == 1:
            if filewrite >= 1: f.write(str(max(personal_best_scores)) + "\n")
            best_particle = personal_best_scores.index(max(personal_best_scores))
            global_best_position = personal_best_positions[best_particle]

        elif max_or_min == 0:
            if filewrite >= 1: f.write(str(min(personal_best_scores)) + "\n")
            best_particle = personal_best_scores.index(min(personal_best_scores))
            global_best_position = personal_best_positions[best_particle]


    # 最適解
    if max_or_min == 1:
        return max(personal_best_scores)
    elif max_or_min == 0:
        return min(personal_best_scores)


# --------------------------------------------------------------

n = int(input())
ga, sa, ba = [int(i) for i in input().split()]
gb, sb, bb = [int(i) for i in input().split()]
maximum = n
minimum = 0

if max_or_min == 1:
    best = -float("inf")
    for i in range(iter):
        if filewrite >= 2: option = i
        if filewrite >= 1:
            with open('result_' + str(option) + ".txt", 'w') as f:
                best = max(best, main())
        else:
            best = max(best, main())

elif max_or_min == 0:
    best = float("inf")
    for i in range(iter):
        if filewrite >= 2: option = i
        if filewrite >= 1:
            with open('result_' + str(option) + ".txt", 'w') as f:
                best = min(best, main())
        else:
            best = min(best, main())

#print(best)

n = -best
maximum = n
minimum = 0
ga, gb = gb, ga
sa, sb = sb, sa
ba, bb = bb, ba

if max_or_min == 1:
    best = -float("inf")
    for i in range(iter):
        if filewrite >= 2: option = i
        if filewrite >= 1:
            with open('result_' + str(option) + ".txt", 'w') as f:
                best = max(best, main())
        else:
            best = max(best, main())

elif max_or_min == 0:
    best = float("inf")
    for i in range(iter):
        if filewrite >= 2: option = i
        if filewrite >= 1:
            with open('result_' + str(option) + ".txt", 'w') as f:
                best = min(best, main())
        else:
            best = min(best, main())

print(best)

Submission status: Wrong Answer

Solution code:
import os
import sys

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

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

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

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

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

print(int(N))

Submission status: Wrong Answer

Solution code:
import os
import sys

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

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

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


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


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

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
ga, sa, ba = li()
gb, sb, bb = li()

vg1, vg2 = gb - ga, ga - gb
vs1, vs2 = sb - sa, sa - sb
vb1, vb2 = bb - ba, ba - bb

dp1 = [0]*(n+1)

for i in range(1,n+1):
    dp1[i] = max(dp1[i-1],
                  dp1[i-ga] + gb if i-ga >= 0 else 0,
                  dp1[i-sa] + sb if i-sa >= 0 else 0,
                  dp1[i-ba] + bb if i-ba >= 0 else 0)

m = max(n, dp1[n])
dp2 = [0] * (m+1)

for i in range(1, m+1):
    dp2[i] = max(dp2[i-1],
                 dp2[i-gb] + ga if i-gb >= 0 else 0,
                 dp2[i-sb] + sa if i-sb >= 0 else 0,
                 dp2[i-bb] + ba if i-bb >= 0 else 0)

print(max(m, dp2[m]))

Submission status: Wrong Answer

Solution code:
def f(a,b,N):

    tmpa,tmpb=[],[]
    for i in range(3):
        if a[i]<b[i]:
            tmpa.append(a[i])
            tmpb.append(b[i])
    a,b=tmpa,tmpb

    if len(a)==0: return N
    elif len(a)==1: return (N//a[0])*b[0]+N%a[0]

    elif len(a)==2:
        if a[0]<a[1]:
            a,b=a[::-1],b[::-1]

        i,ans=0,N
        while i*a[0]<=N:
            j=(N-i*a[0])//a[1]
            ans=max(ans,i*b[0]+j*b[1]+(N-i*a[0]-j*a[0]))
            i+=1
        return ans

    else:
        memo=[i for i in range(N+1)]

        for i in range(1,N+1):
            tmp=[]
            for j in range(len(a)):
                if i-a[j]>=0: tmp.append(memo[i-a[j]]+b[j])
            if tmp!=[]: memo[i]=max(tmp)
        return memo[N]

import sys
N=int(input())

A=list(map(int,input().split()))
B=list(map(int,input().split()))

N=f(A,B,N)
N=f(B,A,N)
print(N)

Submission status: Wrong Answer

Solution code:
def f(a,b,N):
    memo=[0]+[-1]*(N)
    for i in range(1,N+1):
        tmp=[]
        for j in range(3):
            x=i-a[j]
            if x>=0 and memo[x]!=-1: tmp.append(memo[x]+b[j])
        if tmp!=[]: memo[i]=max(i,max(tmp))
    return memo[N]

import sys
M=int(input())

A=list(map(int,input().split()))
B=list(map(int,input().split()))

M=f(A,B,M)
M=f(B,A,M)
print(M)

Submission status: Wrong Answer

Solution code:
def f(a,b,N):
    memo=[0]+[-1]*(N)
    for i in range(1,N+1):
        tmp=[]
        for j in range(3):
            x=i-a[j]
            if x>=0 and memo[x]!=-1: tmp.append(memo[x]+b[j])
        if tmp!=[]: memo[i]=max(tmp)

    return memo[N]

import sys
N=int(input())

A=list(map(int,input().split()))
B=list(map(int,input().split()))

N=f(A,B,N)
N=f(B,A,N)
print(N)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N=INT()
ga,sa,ba=MAP()
gb,sb,bb=MAP()

a=ga/gb
b=gb/ga
c=sa/sb
d=sb/sa
e=ba/bb
f=bb/ba
mx=max(a,b,c,d,e,f)

print(int(N*mx))