#include #include #include #include using namespace std; #define REP(i, n) for(int(i)=0;(i)<(n);++(i)) int T, N, C[16], V; int dp[200][10010]; int main(){ cin >> T >> N; REP(i,N) cin >> C[i]; vector >v; REP(i,N){ cin >> V; while(V){ v.push_back(make_pair(V,C[i])); V /= 2; } } memset(dp, -1, sizeof(dp)); dp[0][0] = 0; int s = v.size(); REP(i,s){ int k = v[i].first, c = v[i].second; REP(j,T){ if(dp[i][j] < 0) continue; if(j + c <= T){ dp[i+1][j+c] = max(dp[i+1][j+c], dp[i][j] + k); } dp[i+1][j] = max(dp[i+1][j], dp[i][j]); } } int maxv = 0; REP(j,T+1) maxv = max(maxv, dp[s][j]); cout << maxv << endl; return 0; }