#define _USE_MATH_DEFINES #include #include #include #include #include #include //#include #include #include #include #include #include #include ///////// #define REP(i, x, n) for(int i = x; i < n; i++) #define rep(i,n) REP(i,0,n) #define P(p) cout<<(p)< ///////// typedef long long LL; typedef long double LD; ///////// using namespace::std; ///////// const int dpmax = 15000; int dp[dpmax+1]; int main(void){ std::cin.tie(0); std::ios::sync_with_stdio(false); std::cout << std::fixed;// cout << setprecision(16);// int T,N; cin>>T>>N; int c[15],v[15]; vector vec; vec.reserve(15); rep(i,N){cin>>c[i];} rep(i,N){cin>>v[i];} rep(i,N){ vec.push_back(PII(c[i],v[i])); } vector::iterator itr = vec.begin(); int vv = itr->second; int vsum = 0; int tsum = 0; for(;itr != vec.end();++itr ){ for(int i=dpmax;i>=0;--i){ if( i==0 || dp[i] ){ vv = itr->second; vsum = 0; tsum = 0; while( vv ){ tsum += itr->first; vsum += vv; if( dp[i] + tsum > T){ break; } if( dp[i+vsum] == 0){ dp[i+vsum] = dp[i] + tsum; }else{ dp[i+vsum] = min( dp[i+vsum],dp[i] + tsum); } vv /= 2; } } } } int i = dpmax; for(;i>=0;--i){ if( dp[i] ){ break; } } P(i); return 0; }