To solve this problem, we need to determine the maximum total price of gifts Santa can pack for families with varying numbers of children, given the constraints on gift sizes and the bag's capacity. The key insight is that for each number of children ( k ), we need to maximize the total price while ensuring the total size does not exceed ( \frac{C}{k} ).
Approach
- Input Filtering: First, filter out gifts that exceed the bag's capacity ( C ), as they cannot be included in any valid subset.
- Knapsack DP Array: Compute a dynamic programming (DP) array
dp where dp[s] represents the maximum price achievable using a total size exactly s.
- Max Upto Array: Create an array
max_upto where max_upto[s] represents the maximum price achievable using any subset with total size ≤ s.
- Compute Results for Each k: For each number of children ( k ) from 1 to ( M ), determine the maximum size allowed (( \text{floor}(C/k) )), then use the
max_upto array to find the maximum price for that size and multiply by ( k ) to get the result for that ( k ).
Solution Code
import sys
def main():
C, N, M = map(int, sys.stdin.readline().split())
items = []
for _ in range(N):
s, p = map(int, sys.stdin.readline().split())
if s <= C:
items.append((s, p))
# Initialize DP array. Since we can't have very large values, using -infinity as impossible.
INF = float('-inf')
dp = [INF] * (C + 1)
dp[0] = 0 # base case: 0 size, 0 price
# Populate the DP table using 0-1 knapsack approach.
for s, p in items:
# Iterate backwards to avoid reusing the same item multiple times
for current_s in range(C, s - 1, -1):
if dp[current_s - s] + p > dp[current_s]:
dp[current_s] = dp[current_s - s] + p
# Precompute max_upto array where max_upto[s] is the maximum price achievable for any subset with total size <= s.
max_upto = [0] * (C + 1)
current_max = dp[0]
max_upto[0] = current_max
for s in range(1, C + 1):
if dp[s] > current_max:
current_max = dp[s]
max_upto[s] = current_max
# Compute the result for each k from 1 to M
results = []
for k in range(1, M + 1):
s_max = C // k
if s_max < 0:
results.append(0)
else:
if s_max == 0:
results.append(0)
else:
if s_max > C:
s_max = C
max_p = max_upto[s_max]
results.append(max_p * k)
# Printing the results line by line
print('\n'.join(map(str, results)))
if __name__ == "__main__":
main()
Explanation
- Input Filtering: We filter out any gifts where the size exceeds ( C ), as they can't contribute to any valid subset.
- Dynamic Programming Array (dp): This array is built using the 0-1 knapsack approach. Each entry
dp[s] is updated by considering the inclusion of each gift, ensuring that we do not reuse items.
- Max Upto Array: This array efficiently tracks the maximum price achievable up to each possible size, allowing quick lookups for any maximum allowed size.
- Result Calculation: For each ( k ), the maximum allowed size is ( \text{floor}(C/k) ). Using the precomputed
max_upto array, we quickly determine the best subset for that size and multiply by ( k ) to get the result for that ( k ).
This approach efficiently handles the problem constraints using dynamic programming and precomputation, ensuring that we can quickly answer for each ( k ) from 1 to (