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.io.IOException;
import java.io.InputStream;
import java.util.NoSuchElementException;
import java.util.Random;

public class Main {
    private static FastScanner sc = new FastScanner();
    private static boolean DEBUG_FLG = false;

    public static void main(String[] args) {
    	long N = sc.nextLong();
    	long ga = sc.nextLong();
    	long sa = sc.nextLong();
    	long ba = sc.nextLong();
    	long gb = sc.nextLong();
    	long sb = sc.nextLong();
    	long bb = sc.nextLong();

    	long max = N;
    	for(long i=0; i<=N/ga; i++) {
    		for(long j=0; j<=(N - (i*ga))/sa; j++) {
    			if(i*gb + j*sb > max) {
    				max = i*gb + j*sb;
    			}
    			long k = (N - (i*ga) - (j*sa)) / ba;
    			if(i*gb + j*sb + k*bb > max) {
    				max = i*gb + j*sb + k*bb;
    			}
    		}
    	}

    	long max2 = max;
    	if(ga < gb) {
    		for(long j=0; j<=max/sb; j++) {
    			if(j*sa > max2) {
    				max2 = j*sa;
    			}
        		long k = (max - j*sb) / bb;
        		if(j*sa + k*ba > max2) {
    				max2 = j*sa + k*ba;
    			}
        	}
    	} else if(sa < sb) {
    		for(long i=0; i<=max/gb; i++) {
    			if(i*ga > max2) {
    				max2 = i*ga;
    			}
        		long k = (max - i*gb) / bb;
        		if(i*ga + k*ba > max2) {
    				max2 = i*ga + k*ba;
    			}
        	}
    	} else if(ba < bb) {
    		for(long i=0; i<=max/gb; i++) {
    			if(i*ga > max2) {
    				max2 = i*ga;
    			}
        		long j = (max - i*gb) / sb;
        		if(i*ga + j*sa > max2) {
    				max2 = i*ga + j*sa;
    			}
        	}
    	} else {
        	for(long i=0; i<=max/gb; i++) {
        		for(long j=0; j<=(max - (i*gb))/sb; j++) {
        			if(i*ga + j*sa > max2) {
        				max2 = i*ga + j*sa;
        			}
        			long k = (max - (i*gb) - (j*sb)) / bb;
        			if(i*ga + j*sa + k*ba > max2) {
        				max2 = i*ga + j*sa + k*ba;
        			}
        		}
        	}
    	}

    	System.out.println(max2);
    }

    static void debug(long... args) {
    	if(!DEBUG_FLG) return;
    	boolean flg = false;
    	for(long s : args) {
    		if(flg) System.out.print(" ");
    		flg = true;
    		System.out.print(s);
    	}
    	System.out.println();
    }

    static class FastScanner {
        private final InputStream in = System.in;
        private final byte[] buffer = new byte[1024];
        private int ptr = 0;
        private int buflen = 0;
        private boolean hasNextByte() {
            if(ptr < buflen) {
                return true;
            } else {
                ptr = 0;
                try {
                    buflen = in.read(buffer);
                } catch(IOException e) {
                    e.printStackTrace();
                }
                if(buflen <= 0) {
                    return false;
                }
            }
            return true;
        }
        private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
        private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
        private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
        public boolean hasNext() { skipUnprintable(); return hasNextByte();}
        public String next() {
            if (!hasNext()) throw new NoSuchElementException();
            StringBuilder sb = new StringBuilder();
            int b = readByte();
            while(isPrintableChar(b)) {
                sb.appendCodePoint(b);
                b = readByte();
            }
            return sb.toString();
        }
        public long nextLong() {
            return Long.parseLong(next());
        }
        public int nextInt(){
            return Integer.parseInt(next());
        }
        public double nextDouble(){
            return Double.parseDouble(next());
        }
    }

    static void shuffleArray(long[] arr){
        int n = arr.length;
        Random rnd = new Random();
        for(int i=0; i<n; ++i){
            long tmp = arr[i];
            int randomPos = i + rnd.nextInt(n-i);
            arr[i] = arr[randomPos];
            arr[randomPos] = tmp;
        }
    }
}