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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    int n;
    long ga, sa, ba;
    long gb, sb, bb;
    ArrayList<P> pab = new ArrayList<>();
    ArrayList<P> pba = new ArrayList<>();

    void run() {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        ga = sc.nextLong();
        sa = sc.nextLong();
        ba = sc.nextLong();
        gb = sc.nextLong();
        sb = sc.nextLong();
        bb = sc.nextLong();

        if (ga < gb) {
            pab.add(new P(ga, gb));
        } else {
            pba.add(new P(ga, gb));
        }

        if (sa < sb) {
            pab.add(new P(sa, sb));
        } else {
            pba.add(new P(sa, sb));
        }

        if (ba < bb) {
            pab.add(new P(ba, bb));
        } else {
            pba.add(new P(ba, bb));
        }

        if (pab.size() == 3) {
            long max = n;
            for (long gcnt = 0; gcnt <= n; gcnt++) for (long scnt = 0; scnt <= n - gcnt; scnt++) {
                long bcnt = n - gcnt - scnt;
                long gd = gcnt + (gcnt / ga) * (gb - ga);
                long sd = scnt + (scnt / sa) * (sb - sb);
                long bd = bcnt + (bcnt / ba) * (bb - ba);
                max = Math.max(max, gd + sd + bd);
            }

            System.out.println(max);
        } else if (pab.size() == 2) {
            long max = n;
            for (long cnt1 = 0; cnt1 <= n; cnt1++) {
                long cnt2 = n - cnt1;
                long d1 = cnt1 + (cnt1 / pab.get(0).a) * (pab.get(0).b - pab.get(0).a);
                long d2 = cnt2 + (cnt2 / pab.get(1).a) * (pab.get(1).b - pab.get(1).a);
                max = Math.max(max, d1 + d2);
            }

            System.out.println(max + (max / pba.get(0).b) * (pba.get(0).a - pba.get(0).b));
        } else if (pab.size() == 1) {
            long max = n + (n / pab.get(0).a) * (pab.get(0).b - pab.get(0).a);
            long ans = max;
            for (long cnt1 = 0; cnt1 <= max; cnt1++) {
                long cnt2 = max - cnt1;
                long d1 = cnt1 + (cnt1 / pba.get(0).b) * (pba.get(0).a - pba.get(0).b);
                long d2 = cnt2 + (cnt2 / pba.get(1).b) * (pba.get(1).a - pba.get(0).b);
                ans = Math.max(ans, d1 + d2);
            }

            System.out.println(ans);
        } else {
            long max = n;
            for (long gcnt = 0; gcnt <= n; gcnt++) for (long scnt = 0; scnt <= n - gcnt; scnt++) {
                long bcnt = n - gcnt - scnt;
                long gd = gcnt  + (gcnt / gb) * (ga - gb);
                long sd = scnt  + (scnt / sb) * (sa - sb);
                long bd = bcnt  + (bcnt / bb) * (ba - bb);
                max = Math.max(max, gd + sd + bd);
            }

            System.out.println(max);
        }
    }

    class P implements Comparable<P> {
        long a, b, d;
        P(long a, long b) {
            this.a = a;
            this.b = b;
            d = b - a;
        }

        @Override
        public int compareTo(P o) {
            return (o.d - d) > 0 ? 1 : -1;
        }

        @Override
        public String toString() {
            return a + " " + b;
        }
    }

    void debug(Object...os) {
        System.err.println(Arrays.deepToString(os));
    }

    public static void main(String[] args) {
        new Main().run();
    }
}