To solve this problem, we need to determine the maximum total price of gifts Santa can pack for families with varying numbers of children, from 1 to M children. The constraints require that each child receives the same set of gifts, and the total size of the gifts must not exceed the bag's capacity, C.
Approach
-
Understanding the Problem: For a family with k children, Santa must choose either 0 or k copies of each gift type. The total size of the chosen gifts multiplied by k must not exceed C, and the goal is to maximize the total price of these gifts multiplied by k.
-
Dynamic Programming (DP) Setup:
- We use a dynamic programming array
dp where dp[c] represents the maximum total price achievable with a total size exactly c.
- We iterate through each gift, updating the DP array in reverse to ensure that each gift is considered only once.
-
Efficient Calculation:
- For each gift, update the DP array for all possible capacities up to
C. This uses vectorized operations with NumPy for efficiency.
- After filling the DP array, compute the result for each k from 1 to M by determining the maximum possible price for the corresponding capacity derived from
C // k.
Solution Code
import numpy as np
def main():
import sys
input = sys.stdin.read().split()
idx = 0
C = int(input[idx]); idx +=1
N = int(input[idx]); idx +=1
M = int(input[idx]); idx +=1
gifts = []
for _ in range(N):
s = int(input[idx]); idx +=1
p = int(input[idx]); idx +=1
gifts.append( (s, p) )
max_capacity = C
MIN = -10**18
dtype = np.int64
dp = np.full(max_capacity + 1, MIN, dtype=dtype)
dp[0] = 0 # Base case: empty subset has price 0 and size 0
for s, p in gifts:
if s > max_capacity:
continue # cannot contribute to any capacity up to C
prev_dp = dp.copy()
possible_end = max_capacity - s
possible = prev_dp[0 : possible_end + 1] + p
# Update positions from s to max_capacity
start = s
end = max_capacity + 1
dp_segment = dp[start:end]
new_values = np.maximum(dp_segment, possible)
dp[start:end] = new_values
for k in range(1, M + 1):
c_val = C // k
if c_val < 0:
val_p = 0
else:
val_p = dp[c_val]
if val_p <= MIN:
val_p = 0 # No subset could be chosen except empty
print(val_p * k)
if __name__ == "__main__":
main()
Explanation
-
Initialization:
- The DP array
dp is initialized to a very small value (representing negative infinity) except for dp[0], which is 0 since an empty subset has 0 price and size.
-
Gift Processing:
- For each gift, compute the maximum prices achievable for each capacity using the current gift. This is done using vectorized operations to efficiently update contributions to the DP array.
-
Query Handling:
- For each possible number of children
k, compute the maximum capacity C//k. Use the precomputed DP array to find the best price for this capacity, then compute the total price for k children by multiplying the price by k.
This approach efficiently leverages dynamic programming with vectorized operations to handle large input constraints within reasonable time, ensuring that we can compute results for all required family sizes up to M efficiently.