#include #include #include using namespace std; int t, n; vector c, v; vector< vector > dp; int solve(int _n, int _t) { if (_n < 0) return 0; if (dp[_n][_t] != -1) return dp[_n][_t]; int ret = -1; if (_t - c[_n] >= 0) ret = max(ret, solve(_n - 1, _t - c[_n]) + v[_n]); ret = max(ret, solve(_n - 1, _t)); return dp[_n][_t] = ret; } int main(void) { cin >> t >> n; c.assign(n, 0); v.assign(n, 0); for (int i = 0; i < n; i++) cin >> c[i]; for (int i = 0; i < n; i++) cin >> v[i]; for (int i = 0; i < n; i++) { int _v = v[i]; while (true) { _v = _v / 2; if (_v == 0) break; v.push_back(_v); c.push_back(c[i]); } } dp.assign(c.size(), vector(t + 1, -1)); cout << solve(c.size() - 1, t) << endl; return 0; }