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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef struct ss
{
    int a,b;
    double c;
};
bool f(ss a,ss b)
{
    return a.c>b.c;
}
int main ()
{
    int n;
    cin>>n;
    int a[4],b[4];
    ss a_b[4],b_a[4];
    cin>>a[1]>>a[2]>>a[3];
    cin>>b[1]>>b[2]>>b[3];
    for(int i=1; i<4; i++)
    {
        a_b[i].a=b[i]-a[i];
        a_b[i].c=b[i]/a[i]*1.0;
        a_b[i].b=a[i];
        b_a[i].a=a[i]-b[i];
        b_a[i].b=b[i];
        b_a[i].c=a[i]/b[i]*1.0;
    }
    sort(a_b+1,a_b+4,f);
    sort(b_a+1,b_a+4,f);
    int y,y1,y2;
    if(a_b[1].c>1)
        y=n%a_b[1].b;
    if(y&&a_b[2].c>1)
        y1=y%a_b[2].b;
    if(y1&&a_b[3].c>1)
        y2=y1%a_b[3].b;
    int i;
    for(i=1; i<4; i++)
        if(a_b[i].c<=1)
            break;
    i--;
    if(i==1)
        n=n/a_b[1].b*(a_b[1].a+a_b[1].b)+y;
    if(i==2)
        n=n/a_b[1].b*(a_b[1].a+a_b[1].b)+y/a_b[2].b*(a_b[2].a+a_b[2].b)+y1;
    if(i==3)
        n=n/a_b[1].b*(a_b[1].a+a_b[1].b)+y/a_b[2].b*(a_b[2].a+a_b[2].b)+y1/a_b[3].b*(a_b[3].a+a_b[3].b)+y2;


    if(b_a[1].c>1)
        y=n%b_a[1].b;
    if(y&&b_a[2].c>1)
        y1=y%b_a[2].b;
    if(y1&&b_a[3].c>1)
        y2=y1%b_a[3].b;
    for(i=1; i<4; i++)
        if(b_a[i].c<=1)
            break;
            i--;
    if(i==1)
        n=n/b_a[1].b*(b_a[1].a+b_a[1].b)+y;
    if(i==2)
        n=n/b_a[1].b*(b_a[1].a+b_a[1].b)+y/b_a[2].b*(b_a[2].a+b_a[2].b)+y1;
    if(i==3)
        n=n/b_a[1].b*(b_a[1].a+b_a[1].b)+y/b_a[2].b*(b_a[2].a+b_a[2].b)+y1/b_a[3].b*(b_a[3].a+b_a[3].b)+y2;
    cout<<n;
    return 0;
}