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] = 0
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
    if 0 < dp[i] then
      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
  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
    if 0 < dp[i] then
      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
  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])
end
n = newn

-- B to A
dp = {}
for i = 1, n do
  dp[i] = 0
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
    if 0 < dp[i] then
      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
  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
    if 0 < dp[i] then
      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
  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])
end
print(newn)