To solve this problem, we need to determine the maximum total price of gifts Santa can pack for families with varying numbers of children, ensuring that each child receives the same set of gifts such that the total size does not exceed the bag's capacity. The solution involves dynamic programming (DP) to efficiently compute the maximum prices for each possible number of children.
Approach
-
Problem Analysis:
- For a family with
k children, each gift is either not chosen or chosen for all children. Thus, the total size of chosen gifts must be exactly k times the sum of the sizes of the selected gifts.
- The goal is to compute the maximum price for each possible
k from 1 to M.
-
Dynamic Programming Setup:
- Use a DP array
dp where dp[i] represents the maximum price achievable by selecting a subset of gifts with a total size of i.
- The maximum price for a subset of gifts with total size up to
i can be derived using the cumulative maximum of the DP array.
-
Optimization with NumPy:
- Utilize NumPy to handle large arrays efficiently by vectorized operations, reducing computation time significantly compared to traditional loops.
-
Steps:
- Filter gifts: Exclude gifts larger than the bag's capacity as they can't be part of any valid subset.
- DP Array Initialization: Initialize a DP array of size
C+1 where dp[0] = 0 and others are set to negative infinity.
- Update DP Array: For each gift, update the DP array in reverse to ensure each gift is considered only once.
- Compute Cumulative Maximum: Use
numpy.maximum.accumulate to compute the maximum price achievable for any size up to C.
- Result Calculation: For each number of children
k, determine the maximum possible size of gifts and compute the total price.
Solution Code
import sys
import numpy as np
def main():
C, N, M = map(int, sys.stdin.readline().split())
gifts = []
for _ in range(N):
s, p = map(int, sys.stdin.readline().split())
if s <= C:
gifts.append((s, p))
# Initialize DP array
dp = np.full((C + 1,), -np.inf)
dp[0] = 0.0 # Base case: 0 cost for 0 items
for s, p in gifts:
if s > C:
continue
candidate_end = C + 1 - s
candidate = dp[:candidate_end] + p
dp[s:] = np.maximum(dp[s:], candidate)
# Compute best_p as cumulative maximum
best_p = np.maximum.accumulate(dp)
for k in range(1, M + 1):
s_max = C // k
if s_max < 0:
s_max = 0
if s_max > C:
s_max = C
max_p = best_p[s_max]
print(int(max_p * k))
if __name__ == "__main__":
main()
Explanation
-
Reading Input and Filtering Gifts:
- We read the input values and filter out gifts that exceed the bag's capacity as they can't contribute to any valid subset.
-
DP Array Initialization:
- The DP array is initialized to handle all possible subset sizes from 0 to
C. The base case dp[0] = 0 is set since no gifts yield a price of 0.
-
Updating the DP Array:
- For each gift, we compute potential new maximum prices by considering the gift in reverse to ensure each gift is only added once per iteration. This is optimized using NumPy for efficient vector operations.
-
Cumulative Maximum Calculation:
- The cumulative maximum array is derived from the DP array to quickly find the best possible price for any upper size limit, which is necessary for each family size
k.
-
Result Computation:
- For each
k, compute the maximum allowable size (s_max) and use the precomputed cumulative maximum array to find the highest price. The total price for k children is this price multiplied by k.
This approach efficiently combines dynamic programming with NumPy optimizations to handle large input sizes within feasible time limits.