We need to understand problem: There are N kinds of gifts. Each gift type i has size s_i and price p_i. For a family with k children, Santa must give the same set of gifts to each child. That means for each gift type, he can either give 0 copies per child, or exactly k copies per child? Wait: "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." Actually careful: The phrase "Santa must pack zero or k items for each kind of gifts." means: For each gift kind i, either he includes 0 items of that kind, or he includes exactly k items (one per child). But also he could include multiple items of same kind? But earlier: "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." So per child, at most one gift of each kind. Since he gives same set to each child, the total per kind across children is either 0 or exactly k (if included). So yes, for each kind i, decision variable x_i ∈ {0,1} indicating whether to include it for the family. Then total items = sum_i x_i * k? Actually total number of items: each included kind gives k items (one per child). So total size = k * sum_i (x_i * s_i). Because each item has size s_i. Similarly total price = k * sum_i (x_i * p_i). But wait: Is that correct? The size of the bag is C, and the total size of gifts for the family cannot exceed C. But the bag is for the whole family: "Santa gives one bag to one family, therefore, the total size of the gifts for each family does not exceed C." So we pack all gifts for the family into one bag. So total size of all items (sum over children) must ≤ C.
Thus for given k, we need to choose a subset S of gift types, such that k * sum_{i in S} s_i ≤ C, maximize k * sum_{i in S} p_i.
Since k is factor, it's equivalent to maximize sum_{i in S} p_i subject to sum_{i in S} s_i ≤ C/k. But note that sum_{i in S} s_i must be ≤ floor(C/k). Also x_i are binary. So it's like a knapsack problem: we have items with sizes s_i and values p_i, we can take at most one of each, capacity = floor(C/k). Then maximum value times k.
But careful: If k > C/min_s? Then capacity may be 0, then we can take nothing, total price=0. Also note that floor(C/k) can be zero, meaning no item fits? But if C/k < min_s, no item can be taken.
Thus we need to answer for each k from 1 to M the maximum total price = k * dp[ floor(C/k) ], where dp[c] is the maximum total value (sum p_i) achievable with capacity c (0-1 knapsack). However, there is a nuance: The same subset of gift types is used for all k, but dp depends on capacity. So we need to compute 0-1 knapsack for capacity up to C (since floor(C/k) ≤ C). Then for each k compute ans[k] = k * best[ floor(C/k) ].
But wait: Is it that simple? Check examples.
Example 1:
C=6, N=3, M=2
Items: (1,2), (2,10), (3,5)
Compute knapsack dp for capacities 0..6.
We'll compute dp[cap] = max total p.
Cap 0:0
Cap1: from item1:2 ->2
Cap2: can take item2:10 or two item1? but only 0-1 each. So max(10,2)=10? Also maybe item1+? Actually only one each. So dp[2]=max(10, dp[1]=2)=10.
Cap3: options: item3:5, or item1+item2? size1+2=3, price2+10=12, so 12. Also dp[2]=10. So dp[3]=12.
Cap4: combos: item1+item3=2+5=7 size4, price7? Actually 2+5=7. item2+item3=2+3=5 >4 not allowed. item1+item2=12 size3 ≤4. So dp[4]=max(12, dp[3]=12?) also could be item2 alone 10, item3+? maybe item1? already. So dp[4]=12.
Cap5: combos: item1+item2=12 size3, also item2+item3? size5, price15, so 15. Also item1+item3=7. So dp[5]=15.
Cap6: combos: all three? size6, price17 -> 17. Also item2+item3=15, item1+item2=12 etc. So dp[6]=17.
Now compute ans:
k=1: floor(6/1)=6, ans=1* dp[6]=17 -> matches output 17.
k=2: floor(6/2)=3, dp[3]=12, ans=2*12=24 -> matches output 24.
Great.
Example 2:
C=200, N=5, M=5
Items: (31,41), (59,26), (53,58), (97,93), (23,84)
Compute knapsack dp for cap up to 200. But we can test quickly with some reasoning? We'll verify later.
Check output:
k=1: 235
k=2: 284
k=3: 375
k=4: 336
k=5: 420
Let's see if formula gives.
Compute dp[200]? Actually for k=1, capacity=200, need max sum p. We can brute think: items: (31,41), (59,26), (53,58), (97,93), (23,84). Might be best to take (23,84)+(97,93)+(31,41) = total size=23+97+31=151, price=84+93+41=218; add (53,58) size 204 >200; add (59,26) size 210; maybe (23,84)+(97,93)+(59,26) size=179 price=84+93+26=203; (23,84)+(53,58)+(59,26) size=135 price=168; (23,84)+(31,41)+(53,58)=107 price=183; (23,84)+(31,41)+(59,26)=113 price=151; (23,84)+(97,93)+(53,58)=173 price=235; that's 23+97+53=173, price=84+93+58=235. So dp[200] >=235. Could there be better? (97,93)+(53,58)+(59,26)+(31,41) size=97+53+59+31=240 >200; (97,93)+(53,58)+(31,41)=181 price=192; (97,93)+(59,26)+(31,41)=208 >200; (97,93)+(23,84)+(59,26)=179 price=203; (53,58)+(59,26)+(23,84)+(31,41)=53+59+23+31=166 price=58+26+84+41=209; So 235 seems max. So k=1 ans 235 matches.
k=2: floor(200/2)=100, dp[100]? Need max p with capacity 100. Which items? Sizes: 23,31,53,59,97. Subset sum ≤100 max value? Possible combos: (97+? 97 alone price93; 53+31=84 price58+41=99; 53+23=76 price58+84=142; 59+31=90 price26+41=67; 59+23=82 price26+84=110; 31+23=54 price41+84=125; 53+31+23=107 >100; 97 can't combine with anything else because 97+23=120>100. So best is 53+23=76 price142, or 31+23=54 price125, or 53+31=84 price99. So dp[100] likely 142? But output for k=2 is 284, which is 2*142=284. So dp[100]=142. Good.
k=3: floor(200/3)=66 (since 200/3=66.66 -> floor 66). Need dp[66]. Items: what subset fits ≤66? 23+31=54 price125; 23+53=76>66; 31+53=84>66; 23+59=82>66; 31+59=90>66; 23+31+? no; single: 97>66, 59>66, 53>66, 31 alone 41, 23 alone 84, 31+23=125 is best? Actually 23 alone 84, 31 alone 41, so best is 84? But 23+31=125 >84 and fits 54. So dp[66]=125? Wait 23+31=54, price125. That seems best. 59 doesn't fit; 53 doesn't fit; 97 no. So dp[66]=125. Then ans=3*125=375 matches output. Good.
k=4: floor(200/4)=50. dp[50]? Items: sizes ≤50: 23,31. 23+31=54>50, so cannot take both. So best is max(84,41)=84. So dp[50]=84. ans=4*84=336 matches output.
k=5: floor(200/5)=40. dp[40]? Items: 23,31 (but 31≤40, 23≤40). 23+31=54>40, so best single: max(84,41)=84. dp[40]=84. ans=5*84=420 matches output.
So formula holds.
Example 3:
C=1, N=1, M=2
Item: size1, price1.
dp[0]=0, dp[1]=1 (since item fits), dp[? higher? but capacity up to1.
k=1: floor(1/1)=1, ans=1*1=1 output 1.
k=2: floor(1/2)=0, dp[0]=0, ans=0 -> output 0. matches.
Example 4:
C=2, N=2, M=2
Items: (1,1), (2,100)
dp[0]=0, dp[1]=1, dp[2]=max(100,1)=100? Also could take both? size 1+2=3 >2 so no. So dp[2]=100.
k=1: floor(2/1)=2, ans=100 -> output 100? Actually output first line 100, second line 2. Wait output says:
100
2
Yes.
k=2: floor(2/2)=1, dp[1]=1, ans=2*1=2 -> matches.
So formula works.
Thus problem reduces to computing 0-1 knapsack dp for capacity up to C, and then for each k from 1 to M compute k * dp[ floor(C/k) ].
Constraints: C up to 1e4, N up to 1e4, M up to 1e4. 0-1 knapsack with N=1e4, C=1e4 is doable with O(NC) = 1e8, which might be borderline but possible in C++ with optimization (e.g., using bitset or DP with 1D array and loops). However, we have to be careful about time limit; but typical contest (maybe ACM) 1e8 operations might be okay in 1 second? Might be tight but with simple loops and using int and maybe small constants, it could pass. But we can do better using the fact that we only need dp up to C, but N is 1e4, so 1e8 is plausible in C++ if optimized (e.g., using array, inner loop decreasing). But we need to ensure time. Could also use "meet in the middle" but unnecessary. Actually 1e41e4 = 100 million. That's okay for 2 seconds in C++. But we also have to answer M queries (1e4) which are O(1) each. So total time ~100M operations, which is manageable.
However, there might be a nuance: The capacity per k is floor(C/k). So we need dp[cap] for many caps. That is fine.
Thus solution: 0-1 knapsack DP.
But wait: is there any twist? The problem statement says: "Santa must pack zero or k items for each kind of gifts." But careful: Actually, is it that for each gift kind, we can either pack 0 or k copies? But what about the condition "at most one gift of the same kind per child"? That implies for each child we can give at most one of each kind. So indeed each child gets a subset of types. Since all children get same set, total count for type i is either 0 or k. However, could we give multiple items of the same kind to a child? No. So the subset model is correct.
But is there any possibility to pack different numbers of items of a kind? Suppose we decide to include type i for only some children? That would be unfair. So no.
Thus the reduction stands.
Now we need to compute dp[cap] for cap=0..C. Use standard 0-1 knapsack:
Initialize dp[0..C] = 0.
For each item (s, p):
for c from C down to s:
dp[c] = max(dp[c], dp[c-s] + p)
After DP, dp[cap] is max value with capacity exactly cap? Actually typical knapsack DP yields the maximum value for capacity at most cap if we allow dp to be the best for exactly that capacity? In the standard 0-1 knapsack with DP over capacities, dp[c] after all items is the maximum value achievable with total size ≤ c. Because we start dp[0]=0, others -∞? But we initialized all 0, and we do dp[c] = max(dp[c], dp[c-s] + p). This yields maximum value for capacity up to c, because we can always not use remaining capacity. Actually if we initialize dp[0..C]=0, then dp[c] will be the maximum value for capacity exactly ≤ c, because we are allowed to leave unused capacity. So it's okay.
But there's nuance: When we compute dp for capacity exactly C, it's the maximum for total size ≤ C. So dp[cap] we need to query for cap = floor(C/k). But what if dp[cap] is not the maximum for capacity cap? It will be because dp array after DP will hold for each capacity c the best value for weight ≤ c. Yes, because we propagate values upward. Actually standard approach: dp[0] = 0, others = 0. For each item, iterate c from C down to s: dp[c] = max(dp[c], dp[c-s] + p). At the end, dp[c] is the maximum value for capacity exactly c? Actually it's for capacity at most c? Let's think: Suppose we have an item of size 5, value 10. Initially dp[0..C] = 0. After processing, dp[5] becomes max(0, dp[0]+10)=10; dp[6] remains 0; So dp[6] is 0, but we could put the item in a bag of capacity 6, using size 5, so the best for capacity 6 is also 10. However dp[6] didn't get updated because we only updated dp[c] when c exactly s? Actually we only update dp[c] using dp[c-s], but if c>s, dp[c] may not get the value from the item if we don't propagate. But we can still use the item and leave leftover space. In our DP, for capacity 6, we check c=6, s=5, dp[6] = max(dp[6], dp[1]+10). dp[1] is 0, so dp[6] becomes max(0,10)=10? Wait dp[1] is 0, so dp[6] would be set to 10? Actually the update uses dp[c-s] which is dp[1]=0, so dp[6] becomes max(0, 0+10) = 10. So it gets updated because we consider c=6, s=5, dp[1] is 0. So dp[6] becomes 10. But originally dp[6] was 0. So indeed, it works: for each item, we update all capacities from C down to s, so dp[c] will consider using the item with exactly c-s remaining capacity, and dp[c-s] already holds the best for capacity c-s (including possibly leaving extra space). So dp[c] ends up being the best value for capacity exactly c? Actually if you can achieve a value with total weight w ≤ c, then it will be represented in dp[c] because dp[c] can use w with leftover space. For example, if best weight 5 fits into capacity 6, we consider c=6: dp[6] = max(dp[6], dp[1]+10). dp[1] is 0, so dp[6] becomes 10. So dp[6] = 10. That is the best value for capacity ≤ 6? But consider capacity 5: dp[5] = 10. So dp[6] is at least as high as dp[5] because you can also do weight 5 in capacity 6. However dp[6] might also have other combinations that use exactly capacity 6. But if we use DP exactly as described, dp[c] is non-decreasing as c increases? Not necessarily because we do updates per item, but at the end, dp is monotonic non-decreasing with c? Usually, after completing DP, dp[c] will be the maximum value with total weight ≤ c. Because for any capacity c, you can always not use some capacity, so best value for capacity c+1 is at least best value for capacity c. But our DP might not enforce monotonicity automatically because we might not propagate the best from lower capacities to higher ones. However, at the end, we can compute prefix maximum to ensure. But is it necessary? Let's test: With items (size 6, value 10), C=10. Process: initially dp[0..10]=0. After processing item: for c from 10 down to 6: dp[6]=10, dp[7]= max(0, dp[1]+10)=10, dp[8]=10, dp[9]=10, dp[10]=10. So dp[7] becomes 10 as well. So it's monotonic because for c>s, dp[c-s] is zero, giving same value. But if we have items with varying sizes, does dp automatically become monotonic? Consider items: size 5, value 10; size 6, value 5. Process first item (5,10): dp[5]=10, dp[6]=10 (since dp[1]=0), dp[7]=10, dp[8]=10, dp[9]=10, dp[10]=10. Process second item (6,5): we iterate c from 10 down to 6. For c=10: dp[10] = max(current 10, dp[4]+5 = 0+5=5) => remains 10. c=9: dp[9]=max(10, dp[3]+5=5) =>10. c=8: dp[8]=max(10, dp[2]+5=5) =>10. c=7: dp[7]=max(10, dp[1]+5=5) =>10. c=6: dp[6]=max(10, dp[0]+5=5) =>10. So dp stays 10 for all capacities≥5. Good. However, consider a case where best value for capacity 4 is high but not for capacity 5. But because you can underfill capacity 5 with the combination from capacity 4, dp[5] should be at least that value. Will our DP ensure that? Suppose we have items: size 4, value 100; size 1, value 1. Process size4 item: dp[4]=100, dp[5]=? When processing size4, we update dp[5] using dp[1] (which is 0) so dp[5] = max(0,0+100)=100. So dp[5] gets 100. So that's fine. But what about after processing all items, if we never explicitly set dp[5] to the best value of capacity 4? Actually when processing items, the DP updates many capacities, and often dp[5] will get value from dp[1]+p if dp[1] had some value; but dp[1] might be zero initially. However, if the best value for capacity 4 is from a combination that doesn't exactly fill capacity 5, it will be caught because to use that combination in capacity 5, we need dp[1] (the best for remaining capacity 1). But the best for capacity 4 is stored at dp[4]; but to use it in capacity 5 we need dp[1] not dp[4]. That doesn't directly propagate the value from dp[4] to dp[5] because the difference is leftover space of 1. However, if there is no item of size 1, then dp[1] is 0, but you can still put the size-4 combination in a bag of capacity 5, leaving 1 empty. That should be allowed. In DP, we represent this by having dp[5] possibly updated when processing the items that form the combination? Actually the combination for weight 4 is comprised of some items. When we process those items, they may update dp[5] as well. Let's simulate: Items: only one item (4,100). Process it: update from c=C down to 4. For c=5, we set dp[5] = max(dp[5], dp[1]+100) = 0+100 = 100. So indeed dp[5] becomes 100. So it's captured because the item itself, when considered, can be placed in capacity 5 with leftover space. So after processing all items, dp[5] will reflect the best combination that fits within 5, which may be the combination that occupies 4. Good.
Now consider multiple items: Items: (4,100), (2,50). Process (4,100): dp[4]=100, dp[5]=100, dp[6]=100? For c=6: dp[6] = max(0, dp[2]+100). dp[2] currently 0, so dp[6]=100. Then process (2,50): for c from C down to 2: For c=6: dp[6] = max( current 100, dp[4]+50 ). dp[4] after first item is 100, so dp[6] becomes max(100, 150)=150. Also c=5: dp[5] = max(100, dp[3]+50) dp[3]=0 =>100. c=4: dp[4] = max(100, dp[2]+50=50) =>100. c=2: dp[2]=50. So final dp: dp[2]=50, dp[3]=? Wait dp[3] not updated because no combination? Actually dp[3] remains 0? But we can achieve value 50? weight 2 item fits in capacity 3, so dp[3] should be 50. But after processing both items, dp[3] =? Did we update dp[3]? When processing (2,50), for c=3: dp[3] = max(current 0, dp[1]+50 = 0+50) = 50. So dp[3]=50. Good. So dp is monotonic? dp[2]=50, dp[3]=50, dp[4]=100, dp[5]=100, dp[6]=150. So it is non-decreasing. So it seems the DP yields dp[c] = max value with total size ≤ c.
Thus after DP, we can directly use dp[cap]. However, to be safe, we might compute prefix maximum: for c from 1 to C, dp[c] = max(dp[c], dp[c-1]) to ensure monotonic. But it's likely already monotonic due to the DP structure? Actually is it guaranteed? Consider scenario where we have an item of size 3 value 5, and another item of size 10 value 100 with capacity C=10. Process (3,5): dp[3]=5, dp[4]=5? c=4: dp[4]=max(0,dp[1]+5)=5, dp[5]=5, dp[6]=5, dp[7]=5, dp[8]=5, dp[9]=5, dp[10]=5. Then process (10,100): update from c=10: dp[10]=max(5, dp[0]+100)=100. So final dp[9]=5, dp[10]=100. That's non-decreasing? 5 <= 100, so okay. But if we have item (10,1) and (9,100)? Let's test: C=10. Process (10,1): dp[10]=1, dp[9]=0? Actually for c=10: dp[10]=max(0, dp[0]+1)=1; c=9: not updated because s=10 >9. So dp[9]=0. Then process (9,100): update c=10: dp[10]=max(1, dp[1]+100=100) =>100; c=9: dp[9]=max(0, dp[0]+100)=100. So dp[9]=100, dp[10]=100. So monotonic.
Could there be a case where dp[c] < dp[c-1] after full DP? Possibly if we never update dp[c] but dp[c-1] got updated by some combination. But consider c: if dp[c-1] is high, that means there is a combination with total weight ≤ c-1. That same combination also fits in capacity c, so dp[c] should be at least that high. But does our DP ensure that? The combination that gives dp[c-1] is built from items. When those items were processed, they would have also updated dp[c] because when considering those items, there would be a step where leftover capacity is c - (sum of sizes). Since the sum of sizes is ≤ c-1, then when processing the last item in that combination, say we have partial sum w before adding item of size s, and w+s ≤ c-1. Then during the loop for that item, for capacity c' = w+s, we set dp[w+s] to some value. But to set dp[c] (where c > w+s) we need to use leftover space. Actually, if the combination's total weight is w_total ≤ c-1, then when processing the items, for each item we update capacities. Specifically, when processing the first item, we may set dp[some capacity]. The DP algorithm ensures that after all items, dp[c] will be at least the value of any combination with total weight ≤ c because the DP builds combinations by considering each item. But is it guaranteed that a combination with total weight w_total ≤ c-1 will be considered for dp[c]? Possibly not if we don't have an item that can fill the extra space? But we don't need an item; we can leave empty space. The DP update for a combination does not necessarily set dp[c] for c > w_total. Let's check a concrete counterexample.
Suppose we have items: (5, 100). C=10. Process (5,100): For c from 10 down to 5:
c=5: dp[5]=100.
c=6: dp[6] = max(0, dp[1]+100) = 100.
c=7: dp[7] = max(0, dp[2]+100) = 100.
c=8: dp[8] = max(0, dp[3]+100) = 100.
c=9: dp[9] = max(0, dp[4]+100) = 100.
c=10: dp[10] = max(0, dp[5]+100) = max(0, 100+100) = 200? Wait dp[5] is 100 after the update? But careful: When we process item, we iterate c from C down to s. At the time we process the item, dp[5] is being set to 100. Then later when c=10, we read dp[5] which may have just been updated in the same item iteration? Because we are iterating downwards, dp[5] is accessed for c=10 (since c-s = 5). And since we go from C down to s, when we reach c=10, we have already processed c=10,9,8,7,6,5? Actually order: from c=C down to s. So for c=10, it's first. At that point, dp[5] hasn't been updated by this item yet (since 5 < 10, but we go downwards, we haven't reached 5). So dp[5] is still 0. So dp[10] = max(0, 0+100) = 100. Then c=9, dp[4]=0 =>100. c=8, dp[3]=0 =>100. c=7, dp[2]=0 =>100. c=6, dp[1]=0 =>100. c=5, dp[0]=0 =>100. So after processing, dp[5]=100, dp[6]=100,...,dp[10]=100. So all capacities >=5 get 100. Good.
Now consider two items: (5,100), (5,1). Process first item (5,100) as above: dp[5..10]=100. Then process second item (5,1): iterate c from 10 down to 5:
c=10: dp[10] = max(current 100, dp[5]+1). dp[5] currently 100, so dp[10] becomes max(100,101)=101.
c=9: dp[9] = max(100, dp[4]+1=0+1=1) =>100.
c=8: dp[8] = max(100, dp[3]+1=1) =>100.
c=7: dp[7] = max(100, dp[2]+1=1) =>100.
c=6: dp[6] = max(100, dp[1]+1=1) =>100.
c=5: dp[5] = max(100, dp[0]+1=1) =>100.
So final dp: dp[5]=100, dp[6]=100, dp[7]=100, dp[8]=100, dp[9]=100, dp[10]=101. So dp[9]=100, dp[10]=101, monotonic.
Now, what about a scenario where a combination yields high value for capacity 4, but capacity 5 gets lower? Could dp[5] be less than dp[4]? Let's try to construct: Suppose we have items: (4,100). Process it: dp[4]=100, dp[5]=100 (as before). So dp[5] >= dp[4]. So that's fine. So likely dp is non-decreasing.
But is there any scenario where dp[5] ends up less than dp[4]? For dp[5] to be less, the DP must not propagate the combination for capacity 4 to capacity 5. However, during processing of the item that makes dp[4] high, it also sets dp[5] high if the item size is less than or equal to 5. In general, if an item size s, then when we process it, for any c >= s, we set dp[c] = max(dp[c], dp[c-s] + p). If the best combination for capacity 4 includes that item, then that combination came from some previous state dp[4-s] which had value v. When processing the item, we set dp[4] and also dp[5] using dp[5-s] etc. But does dp[5] necessarily get the same combination? Not necessarily: if the combination uses exactly size 4, then to get it into capacity 5 we need to consider dp[1] + p, but dp[1] might be 0, so dp[5] becomes at least p? But the combination's value might be sum of multiple items, not just one. For combination with total weight 4 composed of two items: e.g., sizes 3 and 1, values 50 each. Items: (3,50), (1,50). Process (3,50): dp[3]=50, dp[4]=50 (since dp[1]=0), dp[5]=50, dp[6]=50,... Then process (1,50): for c from C down to 1: For c=4: dp[4] = max(50, dp[3]+50=100) ->100. For c=5: dp[5] = max(50, dp[4]+50). But careful: at the time we process (1,50), we iterate c descending. When c=5, dp[4] might have been updated earlier in this same item iteration? Since we go descending, c=5 comes after c=6 but before c=4? Actually descending: c= C, C-1,...,1. So for c=5, we will compute dp[5] = max(current dp[5] (which is 50 from first item), dp[4] + 50). But dp[4] at that moment may not yet be updated by the current item? That depends if we have already processed c=4? Since descending, c=5 > 4, so when we process c=5, we haven't processed c=4 yet. So dp[4] is still the old value (50 from first item). So dp[5] becomes max(50, 50+50=100) =>100. Then later when we process c=4, we update dp[4] to 100. So after processing both items, dp[4]=100, dp[5]=100. So fine.
What about combination that uses weight 4 but does not involve an item of size 1? It could be two items of size 2 each. Items: (2,60), (2,60). Process first (2,60): dp[2]=60, dp[3]=60, dp[4]=60? For c=4: dp[4] = max(0, dp[2]+60) = 120? Wait careful: After first item, dp[2]=60, dp[3]=60 (dp[1]+60), dp[4]=60 (dp[2]+60? Actually dp[2] initially 0, then we update c=2: dp[2]=60; c=3: dp[3]=max(0, dp[1]+60)=60; c=4: dp[4]=max(0, dp[2]+60)=60+60=120. So after first item alone, dp[4]=120. That's because we allowed using same item twice? Wait, we only have one item of that kind, but we used it twice? In the DP, when processing a single item, we iterate from high to low to avoid using it multiple times. But in the update for c=4, we used dp[2] + 60. At that point, dp[2] had been updated to 60 when we processed c=2 earlier? Since we iterate from high to low, when we process c=4, c=2 is less than c and we haven't updated dp[2] in this same item iteration? Actually we iterate from C down to s. So for the first item, we start with c=10 (say C=10) down to 2. At c=10, dp[2] is still 0 (since we haven't updated c=2 yet). At c=9, dp[7] is 0, etc. At c=4, we compute dp[4] = max(0, dp[2] + 60). But at that moment, dp[2] is still 0 because we haven't reached c=2 yet (since descending, c=4 > 2, so dp[2] hasn't been updated). So dp[4] becomes max(0, 0+60) = 60, not 120. So earlier mistaken. Because we go descending, we cannot use the same item twice. So after first item, dp[4]=60. Good. Then second item (another type but same stats) processing: We'll have dp[4] = max(60, dp[2]+60). At that time, dp[2] from previous item is 60, so dp[4] becomes 120. So final dp[4]=120. For capacity 5: after first item, dp[5] = max(0, dp[3]+60) but dp[3] was 0? Actually after first item: dp[3] = max(0, dp[1]+60)=60? Wait, we need to recalc properly.
Let's do step by step: C=10, items: A (2,60), B (2,60). dp initially all 0.
Process A:
c=10: dp[10] = max(0, dp[8]+60) -> dp[8]=0 =>60
c=9: dp[9] = max(0, dp[7]+60)=60
c=8: dp[8]=max(0, dp[6]+60)=60
c=7: dp[7]=max(0, dp[5]+60)=60
c=6: dp[6]=max(0, dp[4]+60)=60
c=5: dp[5]=max(0, dp[3]+60)=60 (since dp[3]=0)
c=4: dp[4]=max(0, dp[2]+60)=60 (dp[2]=0)
c=3: dp[3]=max(0, dp[1]+60)=60
c=2: dp[2]=max(0, dp[0]+60)=60
After A, dp[2]=60, dp[3]=60, dp[4]=60, dp[5]=60, dp[6]=60, etc.
Process B (same size):
c=10: dp[10]=max(60, dp[8]+60). dp[8] currently 60 => 120.
c=9: dp[9]=max(60, dp[7]+60). dp[7]=60 =>120.
c=8: dp[8]=max(60, dp[6]+60=60+60=120) =>120.
c=7: dp[7]=max(60, dp[5]+60=60+60=120) =>120.
c=6: dp[6]=max(60, dp[4]+60=60+60=120) =>120.
c=5: dp[5]=max(60, dp[3]+60=60+60=120) =>120.
c=4: dp[4]=max(60, dp[2]+60=60+60=120) =>120.
c=3: dp[3]=max(60, dp[1]+60=60) =>120? Actually dp[1] is 0, so max(60,0+60)=60? Wait dp[1]=0, so dp[3]=max(60,0+60)=60 remains 60. But careful: For c=3, dp[3] = max(current 60, dp[1]+60). dp[1] is 0, so stays 60. So dp[3] remains 60. That seems odd: But we have two items of size 2, can we achieve weight 3? No, because min size is 2, can't get exactly 3, but can we get weight 3? With items 2 and 2, you cannot get total 3 because each item size 2, so you could take one item giving weight 2, which fits in capacity 3. So dp[3] should be 60. So it's correct.
Now, dp[4]=120, dp[5] should be at least 120 because you can take both items (total weight 4) and fit in capacity 5. So dp[5] should be 120. Indeed after processing B, dp[5] became 120 (since dp[5] = max(60, dp[3]+60) and dp[3] was 60, so 120). So dp[5]=120. So monotonic: dp[4]=120, dp[5]=120. Good.
Thus dp is non-decreasing after full DP? I suspect it is always non-decreasing because if there is a solution for capacity c, the same solution works for capacity c+1, so the maximum for c+1 is at least that for c. The DP should reflect that because when building solutions, you can always leave extra space. In the DP update, when an item is considered, for capacity c+1 we use dp[(c+1)-s] which may be lower than dp[c-s] for the same combination? Not exactly. But maybe it's automatically ensured because the DP fills all capacities, but I'd like to prove or at least trust that it yields correct maximum for ≤ capacity. However, some implementations might require taking prefix max after DP to guarantee. But typical 0-1 knapsack DP (with dp initialized to 0 and using descending loop) yields dp[cap] as the maximum value for capacity exactly cap? Actually no, it yields for at most cap. Let's check known fact: In the standard 0-1 knapsack where we initialize dp[0]=0 and others to 0, and update dp[j] = max(dp[j], dp[j-w[i]] + v[i]) for j from W down to w[i], the final dp[j] is the maximum value for capacity j (i.e., total weight exactly j) if we require exact? No, because we allow leaving space unused: dp[j] can represent a packing with total weight ≤ j because we start from 0 and we can always not use some capacity. But consider that we never use the option to not include an item? Actually we start dp[0..W] = 0, meaning that we can achieve value 0 with any capacity by taking nothing. And when we add items, we only update dp[j] if using the item yields higher value. The resulting dp[j] is the maximum value for a subset of items with total weight ≤ j. This is a standard approach. And it indeed yields non-decreasing values as j increases, because for any j, the best solution for j is also feasible for j+1, but the DP might not explicitly set dp[j+1] to that value if no item can fill that extra 1, but as argued earlier, when processing items, the combination that yields the best for j might also be able to be placed in capacity j+1 due to the update with leftover capacity. However, is it possible that the best combination for j uses a total weight w < j, and the DP never sets dp[j] to that value because to get to capacity j we need to use an item that exactly fills the gap? Actually the DP builds states based on exact weight sums. For a combination with total weight w, it will be represented in dp[w] after processing items. But will it be represented in dp[j] for j > w? Only if during the processing of items, when considering the last item that contributed to that combination, for capacity j, the DP update would use dp[j - s] where j-s = w - s? No, let's think systematically.
We have DP that for each item i, we update dp[j] = max(dp[j], dp[j - s_i] + p_i) for j from C down to s_i. At the end, dp[j] holds the maximum value for a subset of items whose total weight is exactly j? Actually it's the maximum value for a subset whose total weight is at most j? I recall that in the standard "unbounded" or "0-1" knapsack, if we initialize dp[0]=0 and others = -inf, then dp[j] becomes the maximum value for exact weight j. If we initialize others to 0, then dp[j] becomes maximum value for weight at most j. Let's test with simple case: one item size 5, value 10, C=10. Initialize dp[0..10]=0. Process item: for j from 10 down to 5: dp[5]=max(0, dp[0]+10)=10; dp[6]=max(0, dp[1]+10)=10; ... dp[10]=max(0, dp[5]+10). At j=10, dp[5] is still 0? Because we are iterating downwards, at j=10, dp[5] hasn't been updated yet? Actually we start j=10, then j=9,...,j=5. At j=10, dp[5] is the old value, which is 0. So dp[10]=10. At j=9, dp[4]=0 ->10; j=8 ->10; j=7->10; j=6->10; j=5->10. So after processing, dp[5]=10, dp[6]=10,...,dp[10]=10. So dp[6] is 10 even though the only combination has weight 5. So dp[6] is not exact weight; it's at most. So indeed dp[j] is the max value for total weight ≤ j.
But is it guaranteed that dp[j] is non-decreasing? Let's test a scenario where dp[5] becomes 10 and dp[6] remains 0? That would be a problem. In the above, dp[6] became 10 because when processing the item, j=6 used dp[1] which was 0, giving 10. So it got updated. In general, for any combination that gives value V with total weight w, during processing of the items, at some point after all items, there will be a state dp[w] = V. But does that propagate to larger capacities? The combination itself, when built, may not directly set dp[j] for j > w. However, the DP recurrence ensures that for each item in the combination, there is a step that updates capacities. But the combination might be built incrementally. For any capacity j > w, consider the last item added in the combination. Suppose the combination consists of items i1,...,ik with total weight w. When processing the last item, say i_k, we update dp[j] for all j from C down to s_{i_k}. The combination would have set dp[w] = V. To set dp[j] for j>w, we would need to consider dp[j - s_{i_k}] at the time of processing i_k. But j - s_{i_k} > w - s_{i_k}. However, the combination's state before adding i_k corresponds to weight w - s_{i_k}. That state is dp[w - s_{i_k}] at the moment before processing i_k. But when processing i_k, we consider j = (w - s_{i_k}) + s_{i_k} = w. For j larger than w, say j = w + delta, we would need dp[j - s_{i_k}] which is dp[(w - s_{i_k}) + delta]. That state may not correspond to the combination's prefix because the prefix weight is exactly w - s_{i_k}. But could there be a way to get the same value V into dp[w+delta] without adding extra items? Possibly we can use the same combination and leave delta empty space. In DP, leaving empty space is allowed because we can just not use the capacity. The typical way the DP with zeros initialization ensures that the value propagates to larger capacities because when we process items, for any capacity j, we consider adding the item to a solution that fits in j-s. If we have a solution with weight w that fits in j-s, we can add the item to get weight w+s ≤ j? Not exactly; we want to place the solution of weight w into capacity j without adding extra items. That would correspond to setting dp[j] = max(dp[j], dp[w]) but that's not directly done.
But note: In the DP loop for item i, we are only updating dp[j] using dp[j - s_i] + v_i. So to get a solution with total weight w (no extra item) into dp[j] for j > w, we need to find some item i such that adding that item to some state yields exactly j? That would increase weight beyond w. That's not leaving empty space; it's adding an item to fill the space. So that would create a solution with weight exactly j, not weight w. So how does dp[j] ever get the value V from weight w? Actually it might not directly, but dp[j] might get V from a different combination that uses same items but with extra unused capacity? But the DP does not have a mechanism to directly copy dp[w] to dp[j] unless there is an item of size delta that can be added with zero value? Or we can think that after all items are processed, we could take the maximum over all dp[w] for w ≤ j. But the standard DP as described does not automatically take the maximum over w ≤ j; instead, it computes the best achievable value for each exact capacity, but because we initialize all to 0, we can also think of it as dp[j] = max_{subset} { value(subset) | weight(subset) ≤ j } because the DP inherently allows you to "leave space". Let's analyze more formally.
Define dp after processing some items as the function f(j) = maximum value achievable with total weight exactly j? No, because we start with dp[0]=0, others 0, which means we can achieve value 0 with any weight (by taking no items). But as we add items, we only increase dp at certain capacities. In the typical 0-1 knapsack with zero initialization, the recurrence is:
for i in items:
for j = C down to s_i:
dp[j] = max(dp[j], dp[j - s_i] + p_i)
At any point, dp[j] is the maximum value for a subset of processed items with total weight exactly j? Actually after processing some items, dp[j] might represent maximum value for weight exactly j, because we start with dp[0]=0 and others = 0, which is like we have only the empty set with weight 0 and value 0. But the value 0 for other weights is not achievable exactly (except by empty set? Actually empty set has weight 0, not weight 5. So we cannot achieve weight 5 with value 0 exactly; but we treat dp[5]=0 as a feasible state meaning we can achieve 0 value with weight at most 5? That's not exact weight. So it's a bit ambiguous.
Better to think: In this DP, the state dp[j] stores the maximum value for a subset of items with total weight at most j. Why? Because initially dp[j] = 0 for all j, which corresponds to the empty set, which has weight 0 ≤ j, so value 0 is feasible. Then when we consider an item with weight s, value p, for each j, we consider including the item: we need a previous state that had weight at most j-s, with value dp_old[j-s]. If we include the item, total weight becomes at most (j-s) + s = j, so it's feasible for capacity j. So we update dp[j] if it yields higher value. This maintains the invariant that dp[j] is the maximum value for weight ≤ j. Because at each step, we consider adding the item to a solution that fits within j-s, resulting in a solution that fits within j. The DP does not require the previous solution to have exact weight j-s; it can have any weight ≤ j-s, and dp_old[j-s] represents the best value for weight ≤ j-s. So indeed, after processing all items, dp[j] is the maximum value for total weight ≤ j.
So dp is non-decreasing: if j1 < j2, any solution with weight ≤ j1 is also ≤ j2, so dp[j2] ≥ dp[j1]. And our DP maintains this property? Let's check: Initially dp is non-decreasing (all zeros). When we update, we set dp[j] = max(dp[j], dp[j-s] + p). Since dp[j-s] is presumably the best for capacity j-s, and dp[j-s] ≤ dp[j]? Not necessarily before update, but after the update, we need to ensure that dp[j] does not become less than dp[j-1]. However, the recurrence might violate monotonicity if we are not careful? Let's examine. Suppose before processing an item, dp is non-decreasing. After processing, could dp[j] become less than dp[j-1]? Possibly if we only update dp[j] and not dp[j-1]? But dp[j-1] might also be updated, but we need to check. For monotonicity to hold, we need to ensure that after processing all items, for any j, dp[j] ≥ dp[j-1]. Let's see if the DP maintains that invariant. Suppose before processing an item, dp is monotonic. Consider the update for a fixed item. For each j from C down to s, we compute new = max(old_dp[j], old_dp[j-s] + p). Since we iterate descending, we use old_dp values (i.e., from previous item stage) for dp[j-s] because j-s < j and we haven't updated dp[j-s] yet in this iteration (since we go descending, we update larger j first). So we use old_dp[j-s] which is from previous stage. That's fine. Now after we update dp[j] to new, we need to check if monotonicity could break. Could it be that new < old_dp[j-1]? But old_dp[j-1] might be larger than new. However, after updating dp[j], dp[j-1] may later be updated (since we will process j-1 later in the loop) and could become even larger. So monotonicity might be temporarily violated during the loop, but after finishing the loop for this item, we need to see if the final array is monotonic. It should be, because for any j, dp[j] after processing all items is the maximum value for weight ≤ j. Since the set of feasible solutions for weight ≤ j includes those for weight ≤ j-1, dp[j] must be at least dp[j-1]. So yes, final dp will be monotonic. However, is it guaranteed that the DP algorithm (as implemented) yields that property? Since the DP correctly computes the maximum value for weight ≤ j (as argued), the resulting dp will be monotonic. So we don't need to do a prefix max.
Thus we can compute dp array of length C+1 (0..C) and then answer queries.
Now constraints: C up to 10^4, N up to 10^4. DP O(N*C) is about 100 million, which is okay in C++ with optimized loops. But memory: dp array of size 10001 (int) is fine.
But note: N=10000, C=10000, worst-case 100 million operations. Each operation is simple: compare and add. That should be fine within a second or two in C++. However, we need to be careful about using appropriate data types (int). Also note that values p_i up to 1e4, but total value could be up to N*max(p_i)=1e8, fits in int (2^31-1). But if we use 32-bit int, it's okay.
Now, there might be a nuance: M can also be up to 10000. The queries are trivial after dp. So overall solution straightforward.
But before finalizing, we should confirm if there's any other interpretation: The problem says "Santa must pack zero or k items for each kind of gifts." It might also be interpreted as for each kind, he can pack either 0 items total (i.e., none of that kind), or exactly k items total (i.e., one per child). That's what we used. There's no possibility of packing multiple copies of the same kind for a child, so indeed at most one per child per kind. So our reduction is correct.
Thus answer: ans[k] = k * dp[ floor(C/k) ].
Now we must implement DP.
Edge cases: floor(C/k) could be 0. Then dp[0] = 0. So ans[k] = 0.
Now check sample 3: C=1, N=1, M=2. dp[1]=1. k=2: floor(0)=0, ans=0.
All good.
Now we need to output M lines.
But wait: The problem says "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." So we output for each k.
Thus algorithm:
- Read C, N, M.
- Initialize dp array of size C+1 with 0.
- For each of N items:
read s, p.
for (int j = C; j >= s; j--)
if (dp[j] < dp[j-s] + p) dp[j] = dp[j-s] + p;
- After DP, optionally ensure dp is prefix max (though not needed, but we can for safety: for j=1 to C: dp[j] = max(dp[j], dp[j-1])).
- Then for k from 1 to M:
cap = C / k; // integer division (floor)
ans = k * dp[cap];
print ans.
But careful: cap = floor(C/k). Indeed C/k integer division in C++ truncates toward zero, which is floor for positive numbers.
Now we must consider performance: N up to 10^4, C up to 10^4, so 100M updates. This is borderline but typical for 1 second? In C++ with -O2, 100M simple operations might be around 0.2-0.3 seconds? Actually typical speed: ~200-300 million operations per second for simple loops? But it depends. 100M might be around 0.5-1 sec. Usually safe.
But we can also optimize: Since s_i and p_i up to 1e4, we might have many items. But DP is standard.
Alternatively, we could use the fact that C is only 1e4, and N is also 1e4, but we could consider using "divide and conquer DP" or "knapsack with bitset", but not needed.
But note: The DP we are doing is 0/1 knapsack. There is a possible optimization: Because we only need dp[cap] for caps that are floor(C/k) for k=1..M. That's at most M distinct capacities, but M can be up to 1e4, and C=1e4, so the distinct capacities are at most sqrt(C) distinct values? Actually floor(C/k) for k=1..M can have many values if M > C? But M ≤ 1e4, C ≤ 1e4, and M can be up to 1e4, so there could be up to C distinct values because floor(C/k) can take many values as k varies. In worst case, k from 1 to 1e4, floor(C/k) takes about 2sqrt(C) distinct values? Actually number of distinct values of floor(N/k) is O(sqrt(N)). For N=1e4, sqrt=100, distinct values about 200. That's not large. But we need dp for all capacities from 0 to C anyway to compute the DP? We could compute dp only up to C, then answer queries O(1) each. So DP O(NC) is fine.
But we need to be careful about memory and time. Also note that N and M can be 1e4, but C also 1e4, product 1e8. Acceptable.
One more nuance: The DP as described gives maximum total price sum p_i for a subset of types. However, is there any possibility that we might want to take multiple copies of the same type? But the problem says at most one per child per kind, and for fairness, same set per child. So each type either taken or not. So it's exactly 0/1 knapsack.
Thus solution straightforward.
But before finalizing, let's double-check sample 2: We computed and it matches.
One more thought: Could there be a scenario where for large k, capacity floor(C/k) is small, and we need to consider that we cannot take fractional items? Already accounted.
Thus solution: compute 0/1 knapsack dp.
Implementation details:
- Use vector dp(C+1, 0)
- For each item, for (int j = C; j >= s; --j) dp[j] = max(dp[j], dp[j-s] + p)
After processing all items, we can compute prefix maximum for safety: for (int i=1; i<=C; ++i) if (dp[i] < dp[i-1]) dp[i] = dp[i-1];
But as argued, it's not necessary. However, some DP implementations may not guarantee monotonicity if we do in-place updates? Actually typical 0/1 knapsack with zero initialization does yield dp[j] as max value with weight at most j, which is monotonic. Let's double-check with an edge case: Suppose we have two items: (5,100) and (6,1). Process first: after first, dp[5]=100, dp[6]=100? Let's compute: Process (5,100):
j=10: dp[10]=max(0,dp[5]+100) but dp[5] old =0 =>100? Wait careful: starting all zero. For j=10: dp[10]=max(0, dp[5]+100) = 100. But dp[5] is still 0, so 100. Actually j=10, j-s=5, dp[5] is old (0) -> 100. j=9: dp[9]=max(0,dp[4]+100)=100; ... j=5: dp[5]=max(0,dp[0]+100)=100. So after first, dp[5]=100, dp[6]=100? j=6: dp[6]=max(0,dp[1]+100)=100. So yes dp[6]=100. Then process second (6,1):
j=10: dp[10]=max(100, dp[4]+1) =>100; j=9: max(100, dp[3]+1)=>100; j=8: max(100, dp[2]+1)=>100; j=7: max(100, dp[1]+1)=>100; j=6: dp[6]=max(100, dp[0]+1)=100 (since 100>1). So final dp[6]=100. So monotonic.
What about scenario with item (C, high value) and smaller items? Already fine.
Thus we can skip prefix max.
Now complexity: O(N*C) = 100 million, which might be okay. But if both N and C are 10000, it's exactly 100 million loops. Each loop does a comparison, addition, assignment. Should be okay in C++.
But maybe we can optimize by using bitset to speed up? Not needed.
We need to be careful about reading input fast (use scanf). Use int.
Now potential pitfalls: The DP must use 0-1 knapsack, not unbounded. We are using descending loop for capacity, correct.
Now let's test with random small cases to ensure formula holds.
We can also reason about correctness: For a given k, we need to choose subset S to maximize k * sum_{i∈S} p_i subject to k * sum_{i∈S} s_i ≤ C. Since k>0, it's equivalent to maximize sum_{i∈S} p_i subject to sum_{i∈S} s_i ≤ floor(C/k). This is exactly the 0-1 knapsack problem with capacity floor(C/k). Since we are free to choose any subset, the maximum sum of p_i for capacity cap is dp[cap] as computed. Multiply by k yields total price.
Thus solution stands.
Now implement.
Pseudo-code:
int main() {
int C, N, M;
scanf("%d %d %d", &C, &N, &M);
vector dp(C+1, 0);
for (int i=0; i<N; i++) {
int s, p;
scanf("%d %d", &s, &p);
if (s > C) continue; // can't take anyway, skip to save time
for (int j = C; j >= s; j--) {
int val = dp[j-s] + p;
if (val > dp[j]) dp[j] = val;
}
}
// optional prefix max
for (int i=1; i<=C; i++) {
if (dp[i] < dp[i-1]) dp[i] = dp[i-1];
}
for (int k=1; k<=M; k++) {
int cap = C / k;
long long ans = (long long)k * dp[cap];
printf("%lld\n", ans);
}
return 0;
}
But careful: dp[cap] is int, but k*dp[cap] may be up to M * sum(p_i) ≤ 1e4 * (1e4 * 1e4?) Actually N up to 1e4, each p_i up to 1e4, so sum p_i ≤ 1e8. Multiply by k up to 1e4 gives 1e12, which exceeds 32-bit int. So we need 64-bit (long long). In C++, use long long. So ans = 1LL * k * dp[cap]. dp[cap] up to 1e8, k up to 1e4 => 1e12, fits in 64-bit (less than 2^63).