#include using namespace std; int main() { int T, N; cin >> T >> N; vector C(N), V(N); for (int i = 0; i < N; i++) cin >> C.at(i); for (int i = 0; i < N; i++) cin >> V.at(i); vector> P; for (int i = 0; i < N; i++) { int tmp = V.at(i); do { P.push_back({C.at(i), tmp}); tmp /= 2; } while (tmp); } vector DP(T + 1, -1); DP.at(0) = 0; for (auto [a, b] : P) { for (int i = T; i >= 0; i--) { if (DP.at(i) == -1) continue; if (i + a < T + 1) DP.at(i + a) = max(DP.at(i + a), DP.at(i) + b); } } cout << DP.at(T) << "\n"; }