#include using namespace std; using ll = long long; using vi = vector; using vb = vector; using vd = vector; using vl = vector; using vvi = vector; using vvb = vector; using vvd = vector; using vvl = vector; #define REP(i,n) for(ll i=0; i<(n); ++i) #define FOR(i,b,n) for(ll i=(b); i<(n); ++i) int main() { ll t, n; cin >> t >> n; vl _c(n); REP(i, n) cin >> _c[i]; vl _v(n); REP(i, n) cin >> _v[i]; vl c, v; REP(i, n) while(_v[i]>0){ c.push_back(_c[i]); v.push_back(_v[i]); _v[i] /= 2; } vvl dp(t + 1, vl(v.size(), -1)); // dp[t+1][v.size()] dp[0][0] = 0; if (c[0] <= t) dp[c[0]][0] = v[0]; FOR(i, 0, v.size()-1) REP(j, t + 1) if(dp[j][i]!=-1){ if (t >= j + c[i + 1]) dp[j + c[i + 1]][i + 1] = dp[j][i] + v[i + 1]; dp[j][i + 1] = max(dp[j][i+1], dp[j][i]); } ll ans = 0; REP(i, t + 1) ans = max(ans, dp[i][v.size() - 1]); cout << ans << endl; return 0; }