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
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

dp = [0] * (N + 1)
for i in range(N, -1, -1):
  for j in range(3):
    x = a[j]
    y = b[j]
    if i - x >= 0:
      dp[i - x] = max(dp[i - x], dp[i] + y)
for i in range(N + 1): dp[i] += i
#print(dp)
t = max(dp)
e = []
def check(x):
  tt = t - b[e[0]] * x
  return (tt // b[e[1]]) * a[e[1]] + tt % b[e[1]] + a[e[0]] * x

if t > N:
  for i in range(3):
    if b[i] < a[i]: e.append(i)
  if len(e) == 0:
    print(t)
  elif len(e) == 1:
    print(a[e[0]] * (t // b[e[0]]) + t % b[e[0]])
  else:
    if b[e[0]] < b[e[1]]: e[0], e[1] = e[1], e[0]
    x = e[0]
    y = e[1]
    res = check(t // b[x])
    res = max(res, check(max(0, t // b[x] - 1)))
    e[0], e[1] = y, x
    res = max(res, check(max(0, t // b[y])))
    res = max(res, check(max(0, t // b[y] - 1)))
    
    print(res)
else:
  dp = [0] * (N + 1)
  for i in range(N, -1, -1):
    for j in range(3):
      y = a[j]
      x = b[j]
    if i - x >= 0:
      dp[i - x] = max(dp[i - x], dp[i] + y)
  for i in range(N + 1): dp[i] += i
  print(max(dp))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

dp = [0] * (N + 1)
for i in range(N, -1, -1):
  for j in range(3):
    x = a[j]
    y = b[j]
    if i - x >= 0:
      dp[i - x] = max(dp[i - x], dp[i] + y)
for i in range(N + 1): dp[i] += i
#print(dp)
t = max(dp)
e = []
def check(x):
  tt = t - b[e[0]] * x
  return (tt // b[e[1]]) * a[e[1]] + tt % b[e[1]] + a[e[0]] * x
if t > N:
  for i in range(3):
    if b[i] < a[i]: e.append(i)
  if len(e) == 0:
    print(t)
  elif len(e) == 1:
    print(a[e[0]] * (t // b[e[0]]) + t % b[e[0]])
  else:
    dp2 = [0] * (t + 1)
    for i in range(t, -1, -1):
      for j in range(3):
        y = a[j]
        x = b[j]
        if i - x >= 0:
          dp2[i - x] = max(dp2[i - x], dp2[i] + y)
    for i in range(t + 1): dp2[i] += i
    print(max(dp2))
else:
  dp = [0] * (N + 1)
  for i in range(N, -1, -1):
    for j in range(3):
      y = a[j]
      x = b[j]
    if i - x >= 0:
      dp[i - x] = max(dp[i - x], dp[i] + y)
  for i in range(N + 1): dp[i] += i
  print(max(dp))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

dp = [0] * (N + 1)
for i in range(N, -1, -1):
  for j in range(3):
    x = a[j]
    y = b[j]
    if i - x >= 0:
      dp[i - x] = max(dp[i - x], dp[i] + y)
for i in range(N + 1): dp[i] += i
#print(dp)
t = max(dp)
e = []
def check(x):
  tt = t - b[e[0]] * x
  return (tt // b[e[1]]) * a[e[1]] + tt % b[e[1]] + a[e[0]] * x

if t > N:
  for i in range(3):
    if b[i] < a[i]: e.append(i)
  if len(e) == 0:
    print(t)
  elif len(e) == 1:
    print(a[e[0]] * (t // b[e[0]]) + t % b[e[0]])
  else:
    if b[e[0]] < b[e[1]]: e[0], e[1] = e[1], e[0]
    x = e[0]
    res = 0
    for i in range(t // b[x] + 1): res = max(res, check(i))
    print(res)
else:
  dp = [0] * (N + 1)
  for i in range(N, -1, -1):
    for j in range(3):
      y = a[j]
      x = b[j]
    if i - x >= 0:
      dp[i - x] = max(dp[i - x], dp[i] + y)
  for i in range(N + 1): dp[i] += i
  print(max(dp))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

dp = [0] * (N + 1)
for i in range(N, -1, -1):
  for j in range(3):
    x = a[j]
    y = b[j]
    if i - x >= 0:
      dp[i - x] = max(dp[i - x], dp[i] + y)
for i in range(N + 1): dp[i] += i
#print(dp)
t = max(dp)
e = []
def check(x):
  tt = t - b[e[0]] * x
  return (tt // b[e[1]]) * a[e[1]] + tt % b[e[1]] + a[e[0]] * x

if t > N:
  for i in range(3):
    if b[i] < a[i]: e.append(i)
  if len(e) == 0:
    print(t)
  elif len(e) == 1:
    print(a[e[0]] * t // b[e[0]] + t % b[e[0]])
  else:
    if b[e[0]] < b[e[1]]: e[0], e[1] = e[1], e[0]
    x = e[0]
    res = 0
    for i in range(t // b[x] + 1): res = max(res, check(i))
    print(res)
else:
  dp = [0] * (N + 1)
  for i in range(N, -1, -1):
    for j in range(3):
      y = a[j]
      x = b[j]
    if i - x >= 0:
      dp[i - x] = max(dp[i - x], dp[i] + y)
  for i in range(N + 1): dp[i] += i
  print(max(dp))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

dp = [0] * (N + 1)
for i in range(N, -1, -1):
  for j in range(3):
    x = a[j]
    y = b[j]
    if i - x >= 0:
      dp[i - x] = max(dp[i - x], dp[i] + y)
for i in range(N + 1): dp[i] += i
#print(dp)
t = max(dp)
e = []
def check(x):
  tt = t - b[e[0]] * x
  return (tt // b[e[1]]) * a[e[1]] + a[e[0]] * x

if t > N:
  for i in range(3):
    if b[i] < a[i]: e.append(i)
  if len(e) == 0:
    print(t)
  elif len(e) == 1:
    print(a[e[0]] * t // b[e[0]] + t % b[e[0]])
  else:
    if b[e[0]] < b[e[1]]: e[0], e[1] = e[1], e[0]
    l = 0
    r = t // b[e[0]]
    while r - l > 2:
      lx = (l * 2 + r) // 3
      rx = (l + r * 2) // 3
      cl = check(lx)
      cr = check(rx) 
      if cl > cr: r = rx
      else: l = lx
      #print((lx, rx), cl, cr)
    res = 0
    #print(l, r, e)
    for i in range(l, r + 1): res = max(res, check(i) + (t - b[e[0]] * i) % b[e[1]])
    print(res)

else:
  dp = [0] * (N + 1)
  for i in range(N, -1, -1):
    for j in range(3):
      y = a[j]
      x = b[j]
    if i - x >= 0:
      dp[i - x] = max(dp[i - x], dp[i] + y)
  for i in range(N + 1): dp[i] += i
  print(max(dp))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

dp = [0] * (N + 1)
for i in range(N, -1, -1):
  for j in range(3):
    x = a[j]
    y = b[j]
    if i - x >= 0:
      dp[i - x] = max(dp[i - x], dp[i] + y)
for i in range(N + 1): dp[i] += i
#print(dp)
t = max(dp)
e = []
def check(x):
  tt = t - b[e[0]] * x
  return (tt // b[e[1]]) * a[e[1]] + tt % b[e[1]] + a[e[0]] * x

if t > N:
  for i in range(3):
    if b[i] < a[i]: e.append(i)
  if len(e) == 0:
    print(t)
  elif len(e) == 1:
    print(a[e[0]] * t // b[e[0]] + t % b[e[0]])
  else:
    if b[e[0]] < b[e[1]]: e[0], e[1] = e[1], e[0]
    l = 0
    r = t // b[e[0]]
    while r - l > 2:
      lx = (l * 2 + r) // 3
      rx = (l + r * 2) // 3
      cl = check(lx)
      cr = check(rx) 
      if cl > cr: r = rx
      else: l = lx
      #print((lx, rx), cl, cr)
    res = 0
    #print(l, r, e)
    for i in range(l, r + 1): res = max(res, check(i))
    print(res)

else:
  dp = [0] * (N + 1)
  for i in range(N, -1, -1):
    for j in range(3):
      y = a[j]
      x = b[j]
    if i - x >= 0:
      dp[i - x] = max(dp[i - x], dp[i] + y)
  for i in range(N + 1): dp[i] += i
  print(max(dp))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

dp = [0] * (N + 1)
for i in range(N, -1, -1):
  for j in range(3):
    x = a[j]
    y = b[j]
    if i - x >= 0:
      dp[i - x] = max(dp[i - x], dp[i] + y)
for i in range(N + 1): dp[i] += i
#print(dp)
t = max(dp)
e = []
def check(x):
  tt = t - b[e[0]] * x
  return (tt // b[e[1]]) * a[e[1]] + tt % b[e[1]] + a[e[0]] * x

if t > N:
  for i in range(3):
    if b[i] < a[i]: e.append(i)
  if len(e) == 0:
    print(t)
  elif len(e) == 1:
    print(a[e[0]] * t // b[e[0]] + t % b[e[0]])
  else:
    exit(0)


    l = 0
    r = t // b[e[0]]
    while r - l > 2:
      lx = (l * 2 + r) // 3
      rx = (l + r * 2) // 3
      cl = check(lx)
      cr = check(rx) 
      if cl > cr: r = rx
      else: l = lx
      #print((lx, rx), cl, cr)
    res = 0
    #print(l, r, e)
    for i in range(l, r + 1): res = max(res, check(i))
    print(res)

else:
  dp = [0] * (N + 1)
  for i in range(N, -1, -1):
    for j in range(3):
      y = a[j]
      x = b[j]
    if i - x >= 0:
      dp[i - x] = max(dp[i - x], dp[i] + y)
  for i in range(N + 1): dp[i] += i
  print(max(dp))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

dp = [0] * (N + 1)
for i in range(N, -1, -1):
  for j in range(3):
    x = a[j]
    y = b[j]
    if i - x >= 0:
      dp[i - x] = max(dp[i - x], dp[i] + y)
for i in range(N + 1): dp[i] += i
#print(dp)
t = max(dp)
e = []
def check(x):
  tt = t - b[e[0]] * x
  return (tt // b[e[1]]) * a[e[1]] + tt % b[e[1]] + a[e[0]] * x

if t > N:
  for i in range(3):
    if b[i] < a[i]: e.append(i)
  if len(e) == 0:
    print(t)
  elif len(e) == 1:
    print(a[e[0]] * t // b[e[0]] + t % b[e[0]])
  else:
    l = 0
    r = t // b[e[0]]
    while r - l > 2:
      lx = (l * 2 + r) // 3
      rx = (l + r * 2) // 3
      cl = check(lx)
      cr = check(rx) 
      if cl > cr: r = rx
      else: l = lx
      #print((lx, rx), cl, cr)
    res = 0
    #print(l, r, e)
    for i in range(l, r + 1): res = max(res, check(i))
    print(res)

else:
  dp = [0] * (N + 1)
  for i in range(N, -1, -1):
    for j in range(3):
      y = a[j]
      x = b[j]
    if i - x >= 0:
      dp[i - x] = max(dp[i - x], dp[i] + y)
  for i in range(N + 1): dp[i] += i
  print(max(dp))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

dp = [0] * (N + 1)
for i in range(N, -1, -1):
  for j in range(3):
    x = a[j]
    y = b[j]
    if i - x >= 0:
      dp[i - x] = max(dp[i - x], dp[i] + y)
for i in range(N + 1): dp[i] += i
t = max(dp)
dp2 = [0] * (t + 1)
for i in range(N, -1, -1):
  for j in range(3):
    y = a[j]
    x = b[j]
    if i - x >= 0:
      dp2[i - x] = max(dp2[i - x], dp2[i] + y)
for i in range(t + 1): dp2[i] += i
print(max(dp2))

Submission status: Wrong Answer

Solution code:
N = int(input())
ga, sa, ba = map(int, input().split())
gb, sb, bb = map(int, input().split())
metal = [(ga, gb), (sa, sb), (ba, bb)]
metal.sort(reverse = True, key = lambda x:x[1]-x[0])
ga, gb = metal[0]
sa, sb = metal[1]
ba, bb = metal[2]
if ba<=bb:
    res = N
    for i in range(N//ga+1):
        for j in range((N-i*ga)//sa+1):
            k = (N-i*ga-j*sa)//ba
            tmp = k*bb+j*sb+i*gb+(N-i*ga-j*sa-k*ba)
            res = max(res, tmp)
elif sa<=sb:
    res = N
    for i in range(N//ga+1):
        j = (N-i*ga)//sa
        tmp = gb*i+sb*j+(N-i*ga-sa*j)
        res = max(res, tmp)
    k = res//bb
    res += (ba-bb)*k
elif ga<=gb:
    i = N//ga
    N+=i*(gb-ga)
    res = N
    for i in range(N//sb):
        j = (N-i*sb)//bb
        tmp = N+(sa-sb)*i+(ba-bb)*j
        res = max(res, tmp)
else:
    res = N
    ga, gb = gb, ga
    sa, sb = sb, sa
    ba, bb = bb, ba
    for i in range(N//ga+1):
        for j in range((N-i*ga)//sa+1):
            k = (N-i*ga-j*sa)//ba
            tmp = k*bb+j*sb+i*gb+(N-i*ga-j*sa-k*ba)
            res = max(res, tmp)
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())
metal = [(ga, gb), (sa, sb), (ba, bb)]
metal.sort(reverse = True, key = lambda x:x[1]-x[0])
ga, gb = metal[0]
sa, sb = metal[1]
ba, bb = metal[2]
if ba<=bb:
    res = N
    for i in range(N//ga+1):
        for j in range((N-i*ga)//sa+1):
            k = (N-i*ga-j*sa)//ba
            tmp = k*bb+j*sb+i*gb+(N-i*ga-j*sa-k*ba)
            res = max(res, tmp)
elif sa<=sb:
    res = N
    for i in range(N//ga+1):
        j = (N-i*ga)//sa
        tmp = gb*i+sb*j+(N-i*ga-sa*j)
        res = max(res, tmp)
    k = res//bb
    res += (ba-bb)*k
elif ga<=gb:
    i = N//ga
    N+=i*(gb-ga)
    res = N
    for i in range(N//sb):
        j = (N-i*sb)//bb
        tmp = i*sa+j*ba+(N-i*sb-j*bb)
        res = max(res, tmp)
else:
    res = N
    ga, gb = gb, ga
    sa, sb = sb, sa
    ba, bb = bb, ba
    for i in range(N//ga+1):
        for j in range((N-i*ga)//sa+1):
            k = (N-i*ga-j*sa)//ba
            tmp = k*bb+j*sb+i*gb+(N-i*ga-j*sa-k*ba)
            res = max(res, tmp)
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())
metal = [(ga, gb), (sa, sb), (ba, bb)]
metal.sort(reverse = True, key = lambda x:x[1]-x[0])
ga, gb = metal[0]
sa, sb = metal[1]
ba, bb = metal[2]
if ba<=bb:
    res = 0
    for i in range(N//ga+1):
        for j in range((N-i*ga)//sa+1):
            k = (N-i*ga-j*sa)//ba
            tmp = k*bb+j*sb+i*gb+(N-i*ga-j*sa-k*ba)
            res = max(res, tmp)
elif sa<=sb:
    res = 0
    for i in range(N//ga+1):
        j = (N-i*ga)//sa
        tmp = gb*i+sb*j+(N-i*ga-sa*j)
        res = max(res, tmp)
    k = res//bb
    res += (ba-bb)*k
elif ga<=gb:
    i = N//ga
    N+=i*(gb-ga)
    res = 0
    for i in range(N//sb):
        j = (N-i*sb)//bb
        tmp = i*sa+j*ba+(N-i*sb-j*bb)
        res = max(res, tmp)
else:
    res = 0
    ga, gb = gb, ga
    sa, sb = sb, sa
    ba, bb = bb, ba
    for i in range(N//ga+1):
        for j in range((N-i*ga)//sa+1):
            k = (N-i*ga-j*sa)//ba
            tmp = k*bb+j*sb+i*gb+(N-i*ga-j*sa-k*ba)
            res = max(res, tmp)
print(res)

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[] for _ in range(num)]
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

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

N = I()
A = III()
B = III()

first_use = []
second_use = []
for i in range(3):
    if A[i]<B[i]:
        first_use.append(i)
    if A[i]>B[i]:
        second_use.append(i)

# Aでちょうどi個のドングリを交換したときの，Bでのドングリの最大数
dp = [0]*(N+1)
for i in range(N+1):
    for j in first_use:
        if i-A[j]>=0:
            dp[i] = max(dp[i-A[j]]+B[j],dp[i])
for i in range(N+1):
    dp[i] = N-i+dp[i]

# B→A
count = max(dp)
dp = [0]*(count+1)
for i in range(count+1):
    for j in second_use:
        if i-B[j]>=0:
            dp[i] = max(dp[i-B[j]]+A[j],dp[i])
for i in range(N+1):
    dp[i] = count-i+dp[i]

print(max(dp))

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[] for _ in range(num)]
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

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

N = I()
A = III()
B = III()

# Aでちょうどi個のドングリを交換したときの，Bでのドングリの最大数
dp = [0]*(N+1)
for i in range(N+1):
    for j in range(3):
        if i-A[j]>=0:
            dp[i] = max(dp[i-A[j]]+B[j],dp[i])
for i in range(N+1):
    dp[i] = N-i+dp[i]

# B→A
count = max(dp)
dp = [0]*(count+1)
for i in range(count+1):
    for j in range(3):
        if i-B[j]>=0:
            dp[i] = max(dp[i-B[j]]+A[j],dp[i])
for i in range(N+1):
    dp[i] = count-i+dp[i]

print(max(dp))

Submission status: Wrong Answer

Solution code:
n = int(input())
g1, s1, b1 = map(int, input().split())
g2, s2, b2 = map(int, input().split())

go, back = [], []

if g1 > g2:
	back.append([g2, g1-g2])
elif g1 < g2:
	go.append([g1, g2-g1])

if s1 > s2:
	back.append([s2, s1-s2])
elif s1 < s2:
	go.append([s1, s2-s1])

if b1 > b2:
	back.append([b2, b1-b2])
elif b1 < b2:
	go.append([b1, b2-b1])

if len(go) == 0:
	m = n
elif len(go) == 1:
	m = go[0][1] * (n // go[0][0]) + (n // go[0][0])
else:
	dp1 = [n for _ in range(n+1)]
	for cost, value in go:
		for i in range(n-cost+1):
			dp1[i+cost] = max(dp1[i+cost], dp1[i] + value)
	m = dp1[n]

if len(back) == 0:
	ans = m
elif len(back) == 1:
	ans = back[0][1] * (m // back[0][0]) + (m // back[0][0])
else:
	dp2 = [m for _ in range(m+1)]
	for cost, value in back:
		for i in range(m-cost+1):
			dp2[i+cost] = max(dp2[i+cost], dp2[i] + value)
	ans = dp2[m]

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import namedtuple

Ratio = namedtuple('Ratio', 'gold, silver copper')


def convert(n, from_, to_):
    Df = Ratio(*(t - f for f, t in zip(from_, to_)))

    ret = n

    g_max = n // Ra.gold if Df.gold > 0 else 0
    for g in range(g_max + 1):
        dg = Df.gold * g
        s_max = (n - Ra.gold * g) // Ra.silver if Df.silver > 0 else 0
        for s in range(s_max + 1):
            ds = Df.silver * s
            b = (n - Ra.gold * g - Ra.silver * s) // Ra.copper if Df.copper > 0 else 0

            db = Df.copper * b
            ret = max(ret, n + dg + ds + db)

    return ret


n = int(input())
Ra = Ratio(*map(int, input().split()))
Rb = Ratio(*map(int, input().split()))

ret = convert(n=n, from_=Ra, to_=Rb)
ret = convert(n=ret, from_=Rb, to_=Ra)
print(ret)

Submission status: Wrong Answer

Solution code:
from collections import namedtuple

Ratio = namedtuple('Ratio', 'gold, silver copper')

n = int(input())
Ra = Ratio(*map(int, input().split()))
Rb = Ratio(*map(int, input().split()))

Df = Ratio(*(b - a for a, b in zip(Ra, Rb)))

ret = n

g_max = n // Ra.gold if Df.gold > 0 else 0
for g in range(g_max + 1):
    dg = Df.gold * g
    s_max = (n - Ra.gold * g) // Ra.silver if Df.silver > 0 else 0
    for s in range(s_max + 1):
        ds = Df.silver * s
        b = (n - Ra.gold * g - Ra.silver * s) // Ra.copper if Df.copper > 0 else 0

        db = Df.copper * b
        ret = max(ret, n + dg + ds + db)

print(ret)

Submission status: Wrong Answer

Solution code:
from collections import namedtuple

Ratio = namedtuple('Ratio', 'gold, silver copper')

x = int(input())
Ra = Ratio(*map(int, input().split()))
Rb = Ratio(*map(int, input().split()))

ret = x
for g in range(x // Ra.gold + 1):
    y = x - Ra.gold * g
    bg = Rb.gold * g
    for s in range(y // Ra.silver + 1):
        b, r = divmod(y - Ra.silver * s, Ra.copper)

        bs = Rb.silver * s
        bb = Rb.copper * b

        ret = max(ret, bg + bs + bb + r)

print(ret)

Submission status: Wrong Answer

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

ListB = list(map(int, input().split()))

AnswerList = []
for i in range(len(ListA)):
  AnswerList.append(n//ListA[i]*ListB[i]//ListA[i])

print(max(AnswerList))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = [list(map(int,input().split())) for i in range(2)]
D = [[], []]

for i in range(3):
    if A[0][i] > A[1][i]:
        D[0].append([A[1][i], A[0][i]])
    else:
        D[1].append([A[0][i], A[1][i]])

def d1(x, in_0, out_0):
    return out_0 * (x // in_0) + (x % in_0)

def d2(x, in_0, out_0, in_1, out_1):
    ans = 0
    for i in range((x // in_0) + 1):
        ans = max(ans, out_0 * i + out_1 * ((x - i * in_0) // in_1) + ((x - i * in_0) % in_1))
    return ans

def d3(x, in_0, out_0, in_1, out_1, in_2, out_2):
    ans = 0
    for i in range((x // in_0) + 1):
        for j in range(((x - i * in_0) // in_1) + 1):
            ans = max(ans, out_0 * i + out_1 * j + out_2 * ((x - i * in_0 - j * in_1) // in_2) \
                + ((x - i * in_0 - j * in_1) % in_2))
            # print(i,j,ans)
    return ans

# print(D)

ans = 0
l = len(D[0])
if l == 3:
    ans = d3(n, D[0][0][0], D[0][0][1], D[0][1][0], D[0][1][1], D[0][2][0], D[0][2][1])
elif l == 2:
    ans = d1(d2(n, D[0][0][0], D[0][0][1], D[0][1][0], D[0][1][1]), D[1][0][0], D[1][0][1])
elif l == 1:
    ans = d2(d1(n, D[0][0][0], D[0][0][1]), D[1][0][0], D[1][0][1], D[1][1][0], D[1][1][1])
elif l == 0:
    ans = d3(n, D[1][0][0], D[1][0][1], D[1][1][0], D[1][1][1], D[1][2][0], D[1][2][1])

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
from itertools import repeat
N = int(input())
A = tuple(map(int,input().split()))
B = tuple(map(int,input().split()))

def helper(A, B, N):
    res = deque(repeat(0, max(A)+1),max(A)+1)
    for v in range(max(A),N+1):
        res.append(max(0,max(res[-x]+y-x for x,y in zip(A,B) if v >= x)))
    return max(res)+N

M = helper(A,B,N)
O = helper(B,A,M)
print(O)

Submission status: Wrong Answer

Solution code:

N = int(input())
A = tuple(map(int,input().split()))
B = tuple(map(int,input().split()))

def helper(A, B, N):
    res = [0]*(N+1)
    for v in range(1,N+1):
        res[v] = max(res[v-1], max((res[v-x]+y-x for x,y in zip(A,B) if v >= x), default=0))
    return res

X,Y = helper(A,B,N), helper(B,A,N)
print(N+max(x+y for x,y in zip(X,reversed(Y))))

Submission status: Wrong Answer

Solution code:

N = int(input())
A = tuple(map(int,input().split()))
B = tuple(map(int,input().split()))

def helper(A, B, N):
    res = [0]*(N+1)
    for v in range(1,N+1):
        res[v] = max(0, max((res[v-x]+y-x for x,y in zip(A,B) if v >= x), default=0))
    return res

X,Y = helper(A,B,N), helper(B,A,N)
print(N+max(x+y for x,y in zip(X,reversed(Y))))

Submission status: Wrong Answer

Solution code:

N = int(input())
A = tuple(map(int,input().split()))
B = tuple(map(int,input().split()))

def helper(A, B, N):
    res = [0]*(N+1)
    for v in range(1,N+1):
        res[v] = max((res[v-x]+y-x for x,y in zip(A,B) if v >= x), default=0)
    return res

X,Y = helper(A,B,N), helper(B,A,N)
print(N+max(x+y for x,y in zip(X,reversed(Y))))

Submission status: Wrong Answer

Solution code:

N = int(input())
A = tuple(map(int,input().split()))
B = tuple(map(int,input().split()))

def helper(A, B, N):
    res = [0]*(N+1)
    for v in range(1,N+1):
        res[v] = max((res[v-x]+y-x for x,y in zip(A,B) if v >= x), default=0)
    return res

X,Y = helper(A,B,N), helper(B,A,N)
print(X,Y)
print(N+max(x+y for x,y in zip(X,reversed(Y))))

Submission status: Wrong Answer

Solution code:

N = int(input())
A = tuple(map(int,input().split()))
B = tuple(map(int,input().split()))

def helper(A, B, N):
    res = [0]*(N+1)
    res[0] = -float('inf')
    for v in range(N+1):
        res[v] = max(0, max(res[max(0,v-x)]+y-x for x,y in zip(A,B)))
    return res

X,Y = helper(A,B,N), helper(B,A,N)
print(N+max(x+y for x,y in zip(X,reversed(Y))))

Submission status: Wrong Answer

Solution code:

N = int(input())
A = tuple(map(int,input().split()))
B = tuple(map(int,input().split()))

def helper(A, B, N):
    res = [0]*(N+1)
    res[0] = -float('inf')
    for v in range(N+1):
        res[v] = max(0, max(res[max(0,v-x)]+y for x,y in zip(A,B)))
    return res

X,Y = helper(A,B,N), helper(B,A,N)
print(max(x+y for x,y in zip(X,reversed(Y))))

Submission status: Wrong Answer

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

def search3(w,v,n):
    res = 0
    for n1 in range(n//w[0]+1):
        for n2 in range((n-w[0]*n1)//w[1]+1):
            n3 = (n-w[0]*n1-w[1]*n2)//w[2]
            res = max(res,n+(v[0]-w[0])*n1+(v[1]-w[1])*n2+(v[2]-w[2])*n3)
    return res

def search2(w,v,n):
    res = 0
    for n1 in range(n//w[0]+1):
        n2 = (n-w[0]*n1)//w[1]
        res = max(res,n+(v[0]-w[0])*n1+(v[1]-w[1]*n2))
    return res

AB = [[A[0]-B[0],A[0],B[0]],[A[1]-B[1],A[1],B[1]],[A[2]-B[2],A[2],B[2]]]
AB.sort()
A = [AB[i][1] for i in range(3)]
B = [AB[i][2] for i in range(3)]

if AB[0][0]>0:
    ans = search3(B,A,N)
elif AB[1][0]>0:
    n = N+(B[0]-A[0])*(N//A[0])
    ans = search2(B[1:],A[1:],n)
elif AB[2][0]>0:
    n = search2(A[:2],B[:2],N)
    ans = n+(A[2]-B[2])*(n//B[2])
else:
    ans = search3(A,B,N)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
from itertools import product
def resolve():
    n=int(input())
    A=list(map(int,input().split()))
    B=list(map(int,input().split()))
    less=[i for i in range(3) if A[i]<=B[i]]
    greater=[i for i in range(3) if A[i]>B[i]]


    # 全てに大小関係がある場合(A<=Bとする)
    if(all(a>=b for a,b in zip(A,B))): A,B=B,A
    if(all(a<=b for a,b in zip(A,B))):
        ans=0
        for i,j in product(range(n+1),repeat=2):
            if(i+j>n): continue
            k=n-i-j
            score=i%A[0]+(i//A[0])*B[0]+j%A[1]+(j//A[1])*B[1]+k%A[2]+(k//A[2])*B[2]
            ans=max(ans,score)
        print(ans)

    # A <= B なる個数が1つのとき -> 初めに全部買う
    if(len(less)==1):
        ans=0
        i=less[0]
        j,k=greater
        # B で売った後
        m=n%A[i]+(n//A[i])*B[i]
        for s in range(m+1):
            t=m-s
            score=s%B[j]+(s//B[j])*A[j]+t%B[k]+(t//B[k])*B[k]
            ans=max(ans,score)
        print(ans)

    # A <= B なる個数が2つのとき -> 最初を分ける
    if(len(less)==2):
        ans=0
        i,j=less
        k=greater[0]
        for s,t in product(range(n+1),repeat=2):
            if(s+t>n): continue
            m=s%A[i]+(s//A[i])*B[i]+t%A[i]+(t//A[j])*B[j]
            score=m%B[k]+(m//B[k])*A[k]
            ans=max(ans,score)
        print(ans)
resolve()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
from itertools import product
def resolve():
    n=int(input())
    A=list(map(int,input().split()))
    B=list(map(int,input().split()))
    less=[i for i in range(3) if A[i]<=B[i]]
    greater=[i for i in range(3) if A[i]>B[i]]

    # 全てに大小関係がある場合(A<=Bとする)
    if(len(less)==0):
        A,B=B,A
    if(len(less) in [0,3]):
        ans=0
        for i,j in product(range(n+1),repeat=2):
            if(i+j>n): continue
            k=n-i-j
            # A
            m,g,s,b=i%A[0]+j%A[1]+k%A[2],i//A[0],j//A[1],k//A[2]
            # B
            m+=g*B[0]+s*B[1]+b*B[2]
            ans=max(ans,m)
        print(ans)

    # A <= B なる個数が1つのとき -> 初めに全部買う
    if(len(less)==1):
        ans=0
        i=less[0]
        j,k=greater
        # B で売った後
        m=n%A[i]+(n//A[i])*B[i]
        for s in range(m+1):
            t=m-s
            score=s%B[j]+(s//B[j])*A[j]+t%B[k]+(t//B[k])*B[k]
            ans=max(ans,score)
        print(ans)

    # A <= B なる個数が2つのとき -> 最初を分ける
    if(len(less)==2):
        ans=0
        i,j=less
        k=greater[0]
        for s,t in product(range(n),repeat=2):
            if(s+t>n): continue
            m=s%A[i]+(s//A[i])*B[i]+t%A[i]+(t//A[j])*B[j]
            score=m%B[k]+(m//B[k])*A[k]
            ans=max(ans,score)
        print(ans)
resolve()

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())
    
    def calc_1(dg,a0,a1):
        return (dg//a0)*a1+dg%a0

    def calc_2(dg,a0,a1,b0,b1):
        x=dg//a0
        #visit=set()
        temp=0
        for i in range(x,-1,-1):
            amari=(dg-x*a0)%b0
            #if amari in visit:
            #    break
            temp=max(x*a1+((dg-x*a0)//b0)*b1+amari,temp)
            #visit.add(amari)
        return temp

    def calc_3(dg,a0,a1,b0,b1,c0,c1):
        return max([a1*i+calc_2(dg-a0*i,b0,b1,c0,c1) for i in range(dg//a0,-1,-1)])

    gsb=[[ga,gb],[sa,sb],[ba,bb]]
    gsb.sort(key=lambda x:x[1]/x[0],reverse=True)
    temp=[[i,j] for i,j in gsb if i<j]
    if len(temp)==1:
        n=calc_1(n,temp[0][0],temp[0][1])
    elif len(temp)==2:
        n=calc_2(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1])
    elif len(temp)==3:
        n=calc_3(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1],temp[2][0],temp[2][1])

    gsb=reversed(gsb)
    temp=[[j,i] for i,j in gsb if i>j]
    if len(temp)==1:
        n=calc_1(n,temp[0][0],temp[0][1])
    elif len(temp)==2:
        n=calc_2(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1])
    elif len(temp)==3:
        n=calc_3(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1],temp[2][0],temp[2][1])
    print(n)
main()

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())
    
    def calc_1(dg,a0,a1):
        return (dg//a0)*a1+dg%a0

    def calc_2(dg,a0,a1,b0,b1):
        x=dg//a0
        visit=set()
        temp=0
        for i in range(x,-1,-1):
            amari=(dg-x*a0)%b0
            if amari in visit:
                break
            temp=max(x*a1+((dg-x*a0)//b0)*b1+amari,temp)
            visit.add(amari)
        return temp

    def calc_3(dg,a0,a1,b0,b1,c0,c1):
        return max([a1*i+calc_2(dg-a0*i,b0,b1,c0,c1) for i in range(dg//a0,-1,-1)])

    gsb=[[ga,gb],[sa,sb],[ba,bb]]
    gsb.sort(key=lambda x:x[1]/x[0],reverse=True)
    temp=[[i,j] for i,j in gsb if i<j]
    if len(temp)==1:
        n=calc_1(n,temp[0][0],temp[0][1])
    elif len(temp)==2:
        n=calc_2(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1])
    elif len(temp)==3:
        n=calc_3(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1],temp[2][0],temp[2][1])

    gsb=reversed(gsb)
    temp=[[j,i] for i,j in gsb if i>j]
    if len(temp)==1:
        n=calc_1(n,temp[0][0],temp[0][1])
    elif len(temp)==2:
        n=calc_2(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1])
    elif len(temp)==3:
        n=calc_3(n,temp[0][0],temp[0][1],temp[1][0],temp[1][1],temp[2][0],temp[2][1])
    print(n)
main()