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

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.NoSuchElementException;

public class Main {
    public static void main(String[] args) {
        BasicScanner sc = new BasicScanner(System.in);
        long n = sc.nextInt();
        int[][] ab = new int[3][2];
        ab[0][0] = sc.nextInt(); ab[1][0] = sc.nextInt(); ab[2][0] = sc.nextInt();
        ab[0][1] = sc.nextInt(); ab[1][1] = sc.nextInt(); ab[2][1] = sc.nextInt();
        Arrays.sort(ab, (u, v) -> (v[1] - v[0]) - (u[1] - u[0]));
        long ga = ab[0][0], sa = ab[1][0], ba = ab[2][0];
        long gb = ab[0][1], sb = ab[1][1], bb = ab[2][1];
        long c = ba <= bb ? 3 : sa <= sb ? 2 : ga <= gb ? 1 : 0;
        long max = n;
        if (c == 0) {
            for (long i = 0; i <= n / gb; i++) {
                for (long j = 0; j <= (n - i * gb) / sb; j++) {
                    long k = (n - i * gb - j * sb) / bb;
                    long s = n + i * (ga - gb) + j * (sa - sb) + k * (ba - bb);
                    max = Math.max(max, s);
                }
            }
        } else if (c == 1) {
            long i = n / ga;
            n += i * (gb - ga);
            for (long j = 0; j <= n / sb; j++) {
                long k = (n - j * sb) / bb;
                long s = n + j * (sa - sb) + k * (ba - bb);
                max = Math.max(max, s);
            }
        } else if (c == 2) {
            for (long i = 0; i <= n / sa; i++) {
                long j = (n - i * ga) / sa;
                long s = n + i * (gb - ga) + j * (sb - sa);
                long k = s / bb;
                s += k * (ba - bb);
                max = Math.max(max, s);
            }
        } else {
            for (long i = 0; i <= n / ga; i++) {
                for (long j = 0; j <= (n - i * ga) / sa; j++) {
                    long k = (n - i * ga - j * sa) / ba;
                    long s = n + i * (gb - ga) + j * (sb - sa) + k * (bb - ba);
                    max = Math.max(max, s);
                }
            }
        }
        System.out.println(max);
    }
}


/**
 * @author https://atcoder.jp/users/suisen
 */
class BasicScanner {
    private final InputStream in;
    private final byte[] buf = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    public BasicScanner(InputStream in) {this.in = in;}
    private boolean hasNextByte() {
        if (ptr < buflen) return true;
        ptr = 0;
        try {buflen = in.read(buf);}
        catch (final IOException e) {e.printStackTrace();}
        return buflen > 0;
    }
    private int readByte() {return hasNextByte() ? buf[ptr++] : -1;}
    public boolean hasNext() {
        while (hasNextByte() && !(33 <= buf[ptr] && buf[ptr] <= 126)) ptr++;
        return hasNextByte();
    }
    private StringBuilder nextSequence() {
        if (!hasNext()) throw new NoSuchElementException();
        final StringBuilder sb = new StringBuilder();
        int b = readByte();
        while (33 <= b && b <= 126) {sb.appendCodePoint(b); b = readByte();}
        return sb;
    }
    public char nextChar() {
        return (char) readByte();
    }
    public String next() {
        return nextSequence().toString();
    }
    public String next(int len) {
        return new String(nextChars(len));
    }
    public char[] nextChars() {
        final StringBuilder sb = nextSequence();
        int l = sb.length();
        char[] dst = new char[l];
        sb.getChars(0, l, dst, 0);
        return dst;
    }
    public char[] nextChars(int len) {
        if (!hasNext()) throw new NoSuchElementException();
        char[] s = new char[len];
        int i = 0;
        int b = readByte();
        while (33 <= b && b <= 126 && i < len) {s[i++] = (char) b; b = readByte();}
        if (i != len) throw new NoSuchElementException(String.format("length %d is longer than the sequence.", len));
        return s;
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {minus = true; b = readByte();}
        if (b < '0' || '9' < b) throw new NumberFormatException();
        while (true) {
            if ('0' <= b && b <= '9') n = n * 10 + b - '0';
            else if (b == -1 || !(33 <= b && b <= 126)) return minus ? -n : n;
            else throw new NumberFormatException();
            b = readByte();
        }
    }
    public int nextInt() {return Math.toIntExact(nextLong());}
    public double nextDouble() {return Double.parseDouble(next());}
}