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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Linq;

namespace D
{
    class Program
    {
        static void Main(string[] args)
        {
            // 入力を取得
            var n = int.Parse(Console.ReadLine());
            var inputsA = Console.ReadLine().Split().Select(int.Parse).ToArray();
            var inputsB = Console.ReadLine().Split().Select(int.Parse).ToArray();

            var storeA = new Store(inputsA);
            var storeB = new Store(inputsB);

            var maxAcornAtoB = OneWayResale(n, storeA, storeB);
            var maxAcorn = OneWayResale(maxAcornAtoB, storeB, storeA);

            // 解答の出力
            Console.WriteLine(maxAcorn);
        }

        static int OneWayResale(int acorn, Store seller, Store buyer)
        {
            var higherG = seller.Gold < buyer.Gold;
            var higherS = seller.Silver < buyer.Silver;
            var higherB = seller.Bronze < buyer.Bronze;
            var pseudoSeller = seller.GeneratePseudoStore(higherG, higherS, higherB);
            var pseudoBuyer = buyer.GeneratePseudoStore(higherG, higherS, higherB);

            var gold = new JewelPrice(pseudoSeller.Gold, pseudoBuyer.Gold);
            var silver = new JewelPrice(pseudoSeller.Silver, pseudoBuyer.Silver);
            var bronze = new JewelPrice(pseudoSeller.Bronze, pseudoBuyer.Bronze);
            var jewelPrices = new[] { gold, silver, bronze };
            jewelPrices = jewelPrices.OrderByDescending(p => p.InterestRate).ToArray();

            var high = jewelPrices[0];
            var middle = jewelPrices[1];
            var low = jewelPrices[2];

            if (acorn % high.SellerPrice == 0)
            {
                return acorn + high.Profit * (acorn / high.SellerPrice);
            }

            var maxAcorns = acorn;
            for (var i = acorn / high.SellerPrice; 0 <= i; i--)
            {
                var remain = acorn - high.SellerPrice * i;
                if (remain % middle.SellerPrice == 0)
                {
                    var acornCount = acorn + high.Profit * i + middle.Profit * (remain / middle.SellerPrice);
                    return maxAcorns < acornCount ? acornCount : maxAcorns;
                }
                for (var j = remain / middle.SellerPrice; 0 <= j; j--)
                {
                    var lowCount = (remain - middle.SellerPrice * j) / low.SellerPrice;
                    var acornCount = acorn + high.Profit * i + middle.Profit * j + low.Profit * lowCount;
                    if (acornCount > maxAcorns) maxAcorns = acornCount;
                }
            }
            return maxAcorns;
        }

        struct Store
        {
            public readonly int Gold, Silver, Bronze;

            public Store(int[] prices)
            {
                Gold = prices[0];
                Silver = prices[1];
                Bronze = prices[2];
            }

            Store(int gold, int silver, int bronze)
            {
                Gold = gold;
                Silver = silver;
                Bronze = bronze;
            }

            public Store GeneratePseudoStore(bool stayGold, bool staySilver, bool stayBronze)
            {
                var gold = stayGold ? Gold : int.MaxValue;
                var silver = staySilver ? Silver : int.MaxValue;
                var bronze = stayBronze ? Bronze : int.MaxValue;
                return new Store(gold, silver, bronze);
            }
        }

        struct JewelPrice
        {
            public readonly int SellerPrice, BuyerPrice;

            public JewelPrice(int seller, int buyer)
            {
                SellerPrice = seller;
                BuyerPrice = buyer;
            }

            public float InterestRate
            {
                get { return (float)Profit / SellerPrice; }
            }

            public int Profit
            {
                get { return BuyerPrice - SellerPrice; }
            }
        }
    }
}