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.

Solution in LuaJIT (2.0.4) (judge verdict: Wrong Answer)

local mce, mfl, msq, mmi, mma = math.ceil, math.floor, math.sqrt, math.min, math.max

local n = io.read("*n")
local ga, sa, ba = io.read("*n", "*n", "*n")
local gb, sb, bb = io.read("*n", "*n", "*n")

local dp = {}
for i = 1, n do
  dp[i] = i
end
if ga < gb then
  local max = mfl(n / ga)
  for i = 1, max do
    dp[i * ga] = i * gb
  end
end
if sa < sb then
  for i = n - 1, 1, -1 do
    local addable = mfl((n - i) / sa)
    for j = 1, addable do
      dp[i + j * sa] = mma(dp[i + j * sa], dp[i] + j * sb)
    end
  end
  local max = mfl(n / sa)
  for i = 1, max do
    dp[i * sa] = mma(dp[i * sa], i * sb)
  end
end
if ba < bb then
  for i = n - 1, 1, -1 do
    local addable = mfl((n - i) / ba)
    for j = 1, addable do
      dp[i + j * ba] = mma(dp[i + j * ba], dp[i] + j * bb)
    end
  end
  local max = mfl(n / ba)
  for i = 1, max do
    dp[i * ba] = mma(dp[i * ba], i * bb)
  end
end
local newn = n
for i = 1, n do
  newn = mma(newn, dp[i] + n - i)
end

local changed = n ~= newn
n = newn

local function remchange(xb, xa, yb, ya)
  if xa * yb < ya * xb then
    xb, xa, yb, ya = yb, ya, xb, xa
  end
  local maxmul = mfl(n / xb)
  local maxrem = n % xb
  local maxrem_mul = mfl(maxrem / yb)
  local maxrem_rem = maxrem % yb
  local cand = maxrem_rem + maxrem_mul * ya + maxmul * xa
  if maxrem_rem == 0 then print(cand)
  else
    for i = maxmul, 0, -1 do
      local base = i * xa
      local rem_mul = mfl((n - base) / yb)
      local rem_rem = (n - base) % yb
      if maxrem_rem == rem_rem then break end
      cand = mma(cand, base + rem_mul * ya + rem_rem)
    end
    print(cand)
  end
end

-- B to A
if changed then
  if gb < ga then
    if sb < sa then
      remchange(gb, ga, sb, sa)
    elseif bb < ba then
      remchange(gb, ga, bb, ba)
    else
      local rem, mul = n % gb, mfl(n / gb)
      print(rem + mul * ga)
    end
  elseif sb < sa then
    if bb < ba then
      remchange(sb, sa, bb, ba)
    else
      local rem, mul = n % sb, mfl(n / sb)
      print(rem + mul * sa)
    end
  elseif bb < ba then
    local rem, mul = n % bb, mfl(n / bb)
    print(rem + mul * ba)
  else
    print(newn)
  end
else
  for i = 1, n do
    dp[i] = i
  end
  if gb < ga then
    local max = mfl(n / gb)
    for i = 1, max do
      dp[i * gb] = i * ga
    end
  end
  if sb < sa then
    for i = n - 1, 1, -1 do
      local addable = mfl((n - i) / sb)
      for j = 1, addable do
        dp[i + j * sb] = mma(dp[i + j * sb], dp[i] + j * sa)
      end
    end
    local max = mfl(n / sb)
    for i = 1, max do
      dp[i * sb] = mma(dp[i * sb], i * sa)
    end
  end
  if bb < ba then
    for i = n - 1, 1, -1 do
      local addable = mfl((n - i) / bb)
      for j = 1, addable do
        dp[i + j * bb] = mma(dp[i + j * bb], dp[i] + j * ba)
      end
    end
    local max = mfl(n / bb)
    for i = 1, max do
      dp[i * bb] = mma(dp[i * bb], i * ba)
    end
  end
  local newn = n
  for i = 1, n do
    newn = mma(newn, dp[i] + n - i)
  end
  print(newn)
end