#include using namespace std; const int INF = 1 << 29; int dp[10001]; int main(){ cin.tie(nullptr)->sync_with_stdio(false); int W; cin >> W; int N; cin >> N; vector wt(N), val(N); for(int i = 0; i < N; ++i){ cin >> wt[i]; } for(int i = 0; i < N; ++i){ cin >> val[i]; } memset(dp, -INF, sizeof(dp)); dp[0] = 0; for(int i = 0; i < N; ++i){ int vl = val[i]; while(vl){ for(int x = W; x >= wt[i]; --x){ dp[x] = max(dp[x], dp[x - wt[i]] + vl); } vl >>= 1; } } cout << *max_element(dp, dp + W + 1) << endl; return 0; }