#include #define show(x) cout << #x << " = " << x << endl using namespace std; using ll = long long; using pii = pair; using vi = vector; template ostream& operator<<(ostream& os, const vector& v) { os << "sz=" << v.size() << "\n["; for (const auto& p : v) { os << p << ","; } os << "]\n"; return os; } template ostream& operator<<(ostream& os, const pair& p) { os << "(" << p.first << "," << p.second << ")"; return os; } constexpr ll MOD = 1e9 + 7; template constexpr T INF = numeric_limits::max() / 100; int main() { int T, N; cin >> T >> N; vector cost(N); vector val(N); for (ll i = 0; i < N; i++) { cin >> cost[i]; } for (ll i = 0; i < N; i++) { cin >> val[i]; } for (ll i = 0; i < N; i++) { int v = val[i] / 2; while (v > 0) { cost.push_back(cost[i]); val.push_back(v); v /= 2; } } // show(val); // show(cost); const int size = cost.size(); vector> dp(size + 1, vector(T + 1, -1)); // i番目のアトラクションまでチェックして残り時間t の最大幸福 dp[0][T] = 0; for (ll i = 1; i <= size; i++) { // show(dp); const int v = val[i - 1]; const int tim = cost[i - 1]; for (ll t = 0; t <= T; t++) { if (t + tim > T) { if (dp[i - 1][t] != -1) { dp[i][t] = dp[i - 1][t]; } } else { if (dp[i - 1][t + tim] != -1) { dp[i][t] = max(dp[i][t], dp[i - 1][t + tim] + v); } if (dp[i - 1][t] != -1) { dp[i][t] = max(dp[i][t], dp[i - 1][t]); } } } } // show(dp); int ans = 0; for (ll i = 0; i <= T; i++) { if (dp[size][i] != -1) { ans = max(ans, dp[size][i]); } } cout << ans << endl; return 0; }