#include using namespace std; typedef long long ll; typedef pair< int, int > pii; typedef vector< int > vi; typedef vector< vi > vvi; typedef vector< ll > vl; typedef vector< vl > vvl; typedef vector< pii > vp; typedef vector< vp > vvp; typedef vector< string > vs; typedef vector< double > vd; typedef vector< vd > vvd; template< class T1, class T2 > int upmin( T1 &x, T2 v ){ if( x > v ){ x = v; return 1; } return 0; } template< class T1, class T2 > int upmax( T1 &x, T2 v ){ if( x < v ){ x = v; return 1; } return 0; } const int INF = 0x3f3f3f3f; int T; int N; vi C; vi V; void init(){ cin >> T; cin >> N; C = V = vi( N ); for( int i = 0; i < N; ++i ) cin >> C[ i ]; for( int i = 0; i < N; ++i ) cin >> V[ i ]; } int dp[ 15 + 1 ][ 10000 + 1 ][ 8 + 1 ]; void preprocess(){ memset( dp, -1, sizeof( dp ) ); dp[ 0 ][ T ][ 0 ] = 0; for( int i = 0; i < N; ++i ) for( int t = T; t >= 0; --t ) for( int j = 0; j <= 8; ++j ) if( dp[ i ][ t ][ j ] != -1 ){ if( j + 1 <= 8 and t - C[ i ] >= 0 ) upmax( dp[ i ][ t - C[ i ] ][ j + 1 ], dp[ i ][ t ][ j ] + ( int ) ( V[ i ] / pow( 2, j ) ) ); upmax( dp[ i + 1 ][ t ][ 0 ], dp[ i ][ t ][ j ] ); } } void solve(){ int ans = 0; for( int t = 0; t <= 10000; ++t ) for( int j = 0; j <= 8; ++j ) upmax( ans, dp[ N ][ t ][ j ] ); cout << ans << endl; } signed main(){ ios::sync_with_stdio( 0 ); init(); preprocess(); solve(); return 0; }