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: Accepted)

#include<bits/stdc++.h>
#define FOR(i, n, m) for(int i = n; i < (int)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define pb push_back
using namespace std;
using ll = std::int_fast64_t;
using P = pair<ll, ll>;
constexpr ll inf = 1000000000;
constexpr ll mod = 1000000007;
constexpr long double eps = 1e-15;
template<typename T1, typename T2>
ostream& operator<<(ostream& os, pair<T1, T2> p) {
	os << to_string(p.first) << " " << to_string(p.second);
	return os;
}
template<typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
	REP(i, v.size()) {
		if(i) os << " ";
		os << to_string(v[i]);
	}
	return os;
}


//number//
ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}

struct modint {
	ll n;
public:
	modint(const ll n = 0) : n((n % mod + mod) % mod) {}
	static modint pow(modint a, ll m) {
		// static vector<modint> v(1000010, -1);
		// if(m == mod - 2 && v[a.n] != -1) return v[a.n];
		// int tmp = a.n;
		modint r = 1;
		while(m > 0) {
			if(m & 1) { r *= a; }
			a = (a * a); m /= 2;
		}
		// if(m == mod - 2) return v[tmp] = r;
		return r;
	}
	modint &operator++() { *this += 1; return *this; }
	modint &operator--() { *this -= 1; return *this; }
	modint operator++(int) { modint ret = *this; *this += 1; return ret; }
	modint operator--(int) { modint ret = *this; *this -= 1; return ret; }
	modint operator~() const { return (this -> pow(n, mod - 2)); } // inverse
	friend bool operator==(const modint& lhs, const modint& rhs) {
		return lhs.n == rhs.n;
	}
	friend bool operator<(const modint& lhs, const modint& rhs) {
		return lhs.n < rhs.n;
	}
	friend bool operator>(const modint& lhs, const modint& rhs) {
		return lhs.n > rhs.n;
	}
	friend modint &operator+=(modint& lhs, const modint& rhs) {
		lhs.n += rhs.n;
		if (lhs.n >= mod) lhs.n -= mod;
		return lhs;
	}
	friend modint &operator-=(modint& lhs, const modint& rhs) {
		lhs.n -= rhs.n;
		if (lhs.n < 0) lhs.n += mod;
		return lhs;
	}
	friend modint &operator*=(modint& lhs, const modint& rhs) {
		lhs.n = (lhs.n * rhs.n) % mod;
		return lhs;
	}
	friend modint &operator/=(modint& lhs, const modint& rhs) {
		lhs.n = (lhs.n * (~rhs).n) % mod;
		return lhs;
	}
	friend modint operator+(const modint& lhs, const modint& rhs) {
		return modint(lhs.n + rhs.n);
	}
	friend modint operator-(const modint& lhs, const modint& rhs) {
		return modint(lhs.n - rhs.n);
	}
	friend modint operator*(const modint& lhs, const modint& rhs) {
		return modint(lhs.n * rhs.n);
	}
	friend modint operator/(const modint& lhs, const modint& rhs) {
		return modint(lhs.n * (~rhs).n);
	}
};
istream& operator>>(istream& is, modint m) { is >> m.n; return is; }
ostream& operator<<(ostream& os, modint m) { os << m.n; return os; }

struct binomial_coefficient {
private:
	int m;
	vector<modint> fact;
public:
	binomial_coefficient(int m) : m(m) {
		fact.resize(m);
		fact[0] = 1;
		for(int i = 1; i < m; i++) fact[i] = fact[i - 1] * i;
	}
	modint combination(int n, int k) {
		if(n < 0 || k < 0 || n < k) return 0;
		return fact[n] / fact[n - k] / fact[k];
	}
};


// graph //
using weight = long long;
struct edge { int to; weight w; };
template <typename T> struct vedge : edge { T v; };
struct graph {
private:
	int n;
	bool weighted;
	bool tree;
	vector<vector<edge>> g;
public:
	graph(int n, bool tree = false) : n(n), weighted(false), tree(tree), g(vector<vector<edge>>(n)) { }
	vector<edge>& operator[] (int i) { return g[i]; }
	int size() { return n; }
	bool isweighted() { return weighted; }
	bool istree() { return tree; }
	void add_edge(int s, int t, weight w = 1) {
		g[s].push_back({t, w});
		g[t].push_back({s, w});
		if(w != 1) weighted = true;
	}
	void add_dedge(int s, int t, weight w = 1) {
		g[s].push_back({t, w});
		if(w != 1) weighted = true;
	}
};
template <typename T> struct vgraph {
public:
private:
	int n;
	bool weighted;
	bool tree;
	vector<vector<vedge<T>>> g;
	vgraph(int n, bool tree = false) : n(n), weighted(false), tree(tree), g(vector<vector<vedge<T>>>(n)) { }
	vector<edge>& operator[] (int i) { return g[i]; }
	int size() { return n; }
	bool isweighted() { return weighted; }
	bool istree() { return tree; }
	void add_edge(int s, int t, T v, weight w = 1) {
		g[s].push_back({t, w, v});
		g[t].push_back({s, w, v});
		if(w != 1) weighted = true;
	}
	void add_dedge(int s, int t, T v, weight w = 1) {
		g[s].push_back({t, w, v});
		if(w != 1) weighted = true;
	}
	graph build_graph() {
		graph ret(n);
		for(auto v: g) for(auto e: g[v]) {
			ret.add_dedge(v, e.to, e.w);
		}
		return ret;
	}
};

vector<weight> shortest_path(graph& g, int s) {
	int n = g.size();
	vector<weight> path(n, -1);
	if(g.isweighted() && !g.istree()) { // dijkstra
		priority_queue<pair<weight, int>, vector<pair<weight, int>>, greater<pair<weight, int>>> q;
		path[s] = 0;
		q.push({0, s});
		while(!q.empty()) {
			weight cost = q.top().first;
			int p = q.top().second;
			q.pop();
			if(path[p] != cost) continue;
			for(auto e: g[p]) {
				if(path[e.to] == -1 || path[e.to] > cost + e.w) {
					path[e.to] = cost + e.w;
					q.push({path[e.to], e.to});
				}
			}
		}
	} else { // bfs
		queue<int> q;
		path[s] = 0;
		q.push(s);
		while(!q.empty()) {
			int p = q.front();
			q.pop();
			for(auto e: g[p]) {
				if(path[e.to] == -1) {
					path[e.to] = path[p] + 1;
					q.push(e.to);
				}
			}
		}
	}
	return path;
}


using mi = modint;

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	ll n;
	ll ga, sa, ba, gb, sb, bb;
	cin >> n;
	cin >> ga >> sa >> ba >> gb >> sb >> bb;
	ll dong2 = n;
	ll ans = n;
	REP(i, n + 1) REP(j, n + 1) {
		if(i * ga + j * sa > n) continue;
		ll k = 0;
		if(ba < bb) k = ((n - i * ga - j * sa) / ba);
		dong2 = max(dong2, n + i * (gb - ga) + j * (sb - sa) + k * (bb - ba));
	}
	ans = dong2;
	if(ga > gb && sa > sb && ba > bb) {
		REP(i, dong2 + 1) REP(j, dong2 + 1) {
			if(i * gb + j * sb > dong2) continue;
			ll k = 0;
			if(bb < ba) k = ((dong2 - i * gb - j * sb) / bb);
			ans = max(ans, dong2 + i * (ga - gb) + j * (sa - sb) + k * (ba - bb));
		}
	} else if(ga > gb && sa > sb) {
		REP(i, dong2 + 1) {
			if(i * gb > dong2) continue;
			ll j = ((dong2 - i * gb) / sb);
			ans = max(ans, dong2 + i * (ga - gb) + j * (sa - sb));
		}
	} else if(sa > sb && ba > bb) {
		REP(i, dong2 + 1) {
			if(i * sb > dong2) continue;
			ll j = ((dong2 - i * sb) / bb);
			ans = max(ans, dong2 + i * (sa - sb) + j * (ba - bb));
		}
	} else if(ba > bb && ga > gb) {
		REP(i, dong2 + 1) {
			if(i * bb > dong2) continue;
			ll j = ((dong2 - i * bb) / gb);
			ans = max(ans, dong2 + i * (ba - bb) + j * (ga - gb));
		}
	} else if(ga > gb) {
		ll i = (dong2 / gb);
		ans = max(ans, dong2 + i * (ga - gb));
	} else if(sa > sb) {
		ll i = (dong2 / sb);
		ans = max(ans, dong2 + i * (sa - sb));
	} else if(ba > bb) {
		ll i = (dong2 / bb);
		ans = max(ans, dong2 + i * (ba - bb));
	}
	cout << ans << endl;
	return 0;
}

// ---------------------------------------