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++ (GCC 9.2.1) (judge verdict: Runtime Error)

#include <bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
//namespace mp = boost::multiprecision;

using namespace std;

const double PI = 3.14159265358979323846;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<ll, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

double equal(double a, double b) {
    return fabs(a - b) < DBL_EPSILON;
}

std::istream &operator>>(std::istream &in, set<int> &o) {
    int a;
    in >> a;
    o.insert(a);
    return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
    ll a;
    in >> a;
    o.push(a);
    return in;
}

bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }

typedef priority_queue<ll, vector<ll>, greater<ll> > PQ_ASK;
//  a <  b < c < || a > b > c >

ll solve_dp(ll n, ll ga, ll sa, ll ba, ll gb, ll sb, ll bb) {
    vector<vector<ll>> dp_b(n + 1, vector<ll>(n + 1, -1));
    vector<vector<ll>> dp_d(n + 1, vector<ll>(n + 1, -1));

    for (ll g = 0; g <= n; g++) {
        for (ll s = 0; s <= n; s++) {
            ll use = g * ga + s * sa;
            ll rem = n - use;

            if (use > n) continue;
            if (ba > bb) {
                dp_b[g][s] = 0;
                dp_d[g][s] = rem;
            } else {
                ll b = rem / ba;
                dp_b[g][s] = b;
                dp_d[g][s] = rem - (rem / ba * ba);
            }
        }
    }

    ll ans = 0;
    for (ll g = 0; g <= n; g++) {
        for (ll s = 0; s <= n; s++) {
            if (dp_b[g][s] == -1) continue;
            ll now = g * gb + s * sb + dp_b[g][s] * bb + dp_d[g][s];
            cmax(ans, now);
        }
    }
    return ans;
}

void all(ll n, ll ga, ll sa, ll ba, ll gb, ll sb, ll bb) {
    ll ans = solve_dp(n, ga, sa, ba, gb, sb, bb);
    cout << ans << endl;
}

void one(ll n, ll ga, ll sa, ll gb, ll sb, ll ba, ll bb) {
    ll ab = solve_dp(n, ga, sa, ba, gb, sb, bb);
    ll p = ab / bb;
    ll q = p * bb;
    ll t = ab - q;
    ll ans = p * ba + t;
    cout << ans << endl;
}

void tow(ll n, ll ba, ll bb, ll ga, ll sa, ll gb, ll sb) {
    ll p = n / ba;
    ll r = p * ba;
    ll q = p * bb;

    ll nn = n - r + q;
    // sa >= sb && ga >= gb

    ll ans = 0;
    for (ll g = 0; g * gb <= nn; g++) {
        ll gp = g * gb;
        ll rem = nn - gp;
        ll s = rem / sb;
        ll sp = s * sb;
        ll t = nn - gp - sp;

        ll now = g * ga + s * sa + t;
        cmax(ans, now);
    }
    cout << ans << endl;
}


int main() {
    ll n;
    ll ga, sa, ba;
    ll gb, sb, bb;
    cin >> n;
    cin >> ga >> sa >> ba;
    cin >> gb >> sb >> bb;

    if (ga <= gb && sa <= sb && ba <= bb) {
        all(n, ga, sa, ba, gb, sb, bb);
        ret();
    }

    if (ga >= gb && sa >= sb && ba >= bb) {
        all(n, gb, sb, bb, ga, sa, ba);
        ret();
    }

    if (ga <= gb && sa <= sb && ba >= bb) {
        one(n, ga, sa, gb, sb, ba, bb);
        ret();
    }
    if (ga <= gb && sa >= sb && ba <= bb) {
        one(n, ga, ba, gb, bb, sa, sb);
        ret();
    }
    if (ga >= gb && sa <= sb && ba <= bb) {
        one(n, sa, ba, sb, bb, ga, gb);
        ret();
    }

    if (ga <= gb && sa >= sb && ba >= bb) {
        tow(n, ga, gb, sa, ba, sb, bb);
        ret();
    }

    __throw_runtime_error("mada");
}