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 <iostream> #include <vector> using namespace std; int main() { int C, N, M; cin >> C >> N >> M; vector<pair<int, int>> gifts; for (int i = 0; i < N; ++i) { int s, p; cin >> s >> p; if (s <= C) { gifts.emplace_back(s, p); } } N = gifts.size(); vector<int> dp(C + 1, -1); dp[0] = 0; for (const auto& p : gifts) { int s = p.first; int val = p.second; for (int w = C; w >= s; --w) { if (dp[w - s] != -1) { if (dp[w] < dp[w - s] + val) { dp[w] = dp[w - s] + val; } } } } vector<int> dp_max(C + 1); dp_max[0] = dp[0]; for (int s = 1; s <= C; ++s) { dp_max[s] = dp_max[s - 1]; if (dp[s] != -1 && dp[s] > dp_max[s]) { dp_max[s] = dp[s]; } } for (int k = 1; k <= M; ++k) { int s_max = C / k; if (s_max < 0) s_max = 0; if (s_max > C) { s_max = C; } if (s_max == 0) { cout << 0 << endl; } else { cout << (s_max <= C ? dp_max[s_max] * k : 0) << 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.