#include using namespace std; int main() { long long N; 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-1; i++ ) { for( int j = i+1; j < 3; j++ ) { if( A[i]*B[j] > A[j]*B[i] ) { swap(A[i], A[j]); swap(B[i], B[j]); } } } int K = 1<<20; long long ans = 0; vector dp(K); for( int i = 0; i < K; i++ ) { for( int j = 0; j < 3; j++ ) { if( i >= A[j] ) dp[i] = max(dp[i], dp[i-A[j]]+B[j]); } if( i <= N ) ans = max(ans, dp[i]+B[0]*((N-i)/A[0])); } cout << ans << endl; }