Santa is going to pack gifts into a bag for a family. There are N kinds of gifts. The size and the price of the i-th gift (1≤i≤N) are s 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
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
In addition, if a child did not receive a gift that the other children in the same family receive, he/she will complain about that. Hence Santa must distribute gifts fairly to all the children of a family, by giving the same set of gifts to each child. In other words, for a family with k children, Santa must pack zero or k items for each kind of gifts. Santa gives one bag to one family, therefore, the total size of the gifts for each family does not exceed C.
Santa wants to maximize the total price of packed items for a family but does not know the number of children in the family he is going to visit yet. The number seems at most M. To prepare all the possible cases, calculate the maximum total price of items for a family with k children for each 1≤k≤M.
Input
The input consists of a single test case in the following format.
CNMs1p1...sNpN
The first line contains three integers C, N and M, where C (1≤C≤104) is the size of the bag, N (1≤N≤104) is the number of kinds of the gifts, and M (1≤M≤104) is the maximum number of children in the family. The i-th line of the following N lines contains two integers si and pi (1≤si,pi≤104), where si and pi are the size and the price of the i-th gift, respectively.
Output
The output should consist of M lines. In the k-th line, print the maximum total price of gifts for a family with k children.
Examples
Input
6 3 2
1 2
2 10
3 5
Output
17
24
Input
200 5 5
31 41
59 26
53 58
97 93
23 84
Output
235
284
375
336
420
Input
1 1 2
1 1
Output
1
0
Input
2 2 2
1 1
2 100
Output
100
2
To solve this problem, we need to determine the maximum total price of gifts that Santa can pack for families with varying numbers of children, given the constraints on gift sizes and ensuring that each child receives the same set of gifts. The solution involves a dynamic programming approach to efficiently compute the maximum value for all possible family sizes up to a given limit.
Approach
Problem Analysis: Each family must receive the same set of gifts, meaning either 0 or k items of each type (where k is the number of children). The goal is to maximize the total price while ensuring the total size does not exceed the bag's capacity C.
Dynamic Programming (0-1 Knapsack):
We use a 0-1 knapsack approach to precompute the maximum value achievable for all possible capacities up to C. This allows us to quickly determine the best gift set for any family size k.
Precompute Maximum Values:
Compute a dp array where dp[i] is the maximum value achievable with a total size of exactly i.
Transform this into a max_val array where max_val[q] is the maximum value achievable with a total size ≤ q.
Query Handling:
For each possible number of children k, compute the maximum achievable capacity Q = floor(C/k) and use the precomputed max_val array to get the corresponding value, then multiply by k to get the result for that family size.
Solution Code
cpp
#include <iostream>
#include <vector>
int main() {
int C, N, M;
std::cin >> C >> N >> M;
std::vector<std::pair<int, int>> items;
for (int i = 0; i < N; ++i) {
int s, p;
std::cin >> s >> p;
if (s <= C) {
items.emplace_back(s, p);
}
}
std::vector<int> dp(C + 1, 0);
for (const auto& pr : items) {
int s = pr.first, p = pr.second;
for (int c = C; c >= s; --c) {
if (dp[c - s] + p > dp[c]) {
dp[c] = dp[c - s] + p;
}
}
}
std::vector<int> max_val(C + 1);
int current_max = 0;
for (int q = 0; q <= C; ++q) {
if (dp[q] > current_max) {
current_max = dp[q];
}
max_val[q] = current_max;
}
for (int k = 1; k <= M; ++k) {
int q = C / k;
if (q < 0) q = 0;
if (q > C) q = C;
int val = max_val[q] * k;
std::cout << val << '\n';
}
return 0;
}
Explanation
Reading Input: We read the capacity C, number of gift types N, and maximum family size M. We also filter out gifts with sizes larger than C since they cannot be part of any valid selection.
0-1 Knapsack DP Array Initialization: The dp array is initialized to store the maximum value achievable for each possible total size up to C.
DP Array Population: For each gift, update the dp array in reverse to ensure each gift is considered only once per iteration. This ensures the solution remains optimal for the 0-1 knapsack problem constraints.
Max Value Array: The max_val array is derived from the dp array to store the maximum value achievable for any capacity up to each index. This allows quick lookups for any required capacity.
Query Processing: For each possible family size k, compute the allowable capacity Q = floor(C/k) and use the precomputed max_val array to quickly determine the maximum value for that capacity, then scale by k to get the result for that family size.
This approach efficiently handles the problem constraints using dynamic programming, ensuring optimal performance even for large inputs.