#include #include void solve() { int t, n; std::cin >> t >> n; std::vector cs(n), vs(n); for (auto& c : cs) std::cin >> c; for (auto& v : vs) std::cin >> v; std::vector> ps; for (int i = 0; i < n; ++i) { int c = cs[i], v = vs[i]; while (v > 0) { ps.emplace_back(c, v); v /= 2; } } std::vector dp(t + 1, 0); for (auto [c, v] : ps) { for (int x = t; x >= c; --x) { dp[x] = std::max(dp[x], dp[x - c] + v); } } std::cout << dp[t] << "\n"; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); solve(); return 0; }