#include using namespace std; using ll = long long; int main(){ ll N, ans=0, k, l, cnt; cin >> N; vector A(3), B(3); for (int i=0; i<3; i++) cin >> A[i] >> B[i]; for (int i=0; i<3; i++){ for (int j=i+1; j<3; j++){ k = 3-i-j; for (int n=0; n<=2000; n++){ for (int m=0; m<=2000; m++){ l = N-A[i]*n-A[j]*m; if (l < 0) continue; cnt = B[i]*n+B[j]*m+l/A[k]*B[k]; //cout << A[i] << " " << A[j] << " " << A[k] << " " << n << " " << m << " " << l << " " << cnt << endl; ans = max(ans, cnt); } } } } cout << ans << endl; return 0; }