Santa is going to pack gifts into a bag for a family. There are $N$ kinds of gifts. The…
- User
Santa is going to pack gifts into a bag for a family. There are kinds of gifts. The size and the price of the -th gift () are and , respectively. The size of the bag is , thus Santa can pack gifts so that the total size of the gifts does not exceed . Children are unhappy if they are given multiple items of the same kind gift, so Santa has to choose at most one gift of the same kind per child.
Assistant
#include <bits/stdc++.h> using namespace std; int main() { int C, N, M; cin >> C >> N >> M; vector<int> s(N), p(N); for (int i = 0; i < N; ++i) { cin >> s[i] >> p[i]; } const int INF = -1e9; vector<int> dp(C + 1, INF); dp[0] = 0; for (int i = 0; i < N; ++i) { int si = s[i], pi = p[i]; for (int c = C; c >= si; --c) { if (dp[c - si] != INF && dp[c - si] + pi > dp[c]) { dp[c] = dp[c - si] + pi; } } } vector<int> max_p(C + 1); max_p[0] = dp[0]; for (int c = 1; c <= C; ++c) { max_p[c] = max(max_p[c - 1], (dp[c] == INF ? -INF : dp[c])); } for (int k = 1; k <= M; ++k) { int Q = C / k; if (Q < 0) Q = 0; if (Q > C) Q = C; int val = max_p[Q]; cout << max(val, 0) * k << endl; } return 0; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.