Take Gifts From the Richest Pile

Easy

You are given an integer array gifts, where gifts[i] is the number of gifts in the i-th pile. Every second, you do the following:

  1. Choose the pile with the most gifts. If several piles are tied for the most, choose any of them.
  2. Shrink that pile to floor(sqrt(x)) gifts, where x is how many gifts it had.

Return the total number of gifts left in all the piles after k seconds.

Examples

Example 1

Input:  gifts = [25,64,9,4,100], k = 4
Output: 29
  • Second 1: the richest pile has 100, which becomes 10: [25,64,9,4,10]
  • Second 2: 64 becomes 8: [25,8,9,4,10]
  • Second 3: 25 becomes 5: [5,8,9,4,10]
  • Second 4: 10 becomes floor(3.16…) = 3: [5,8,9,4,3]

5 + 8 + 9 + 4 + 3 = 29. In second 4, the pile that shrank to 10 back in second 1 was the richest again.

Example 2

Input:  gifts = [1,1,1,1], k = 4
Output: 4

floor(sqrt(1)) = 1, so taking from a pile of 1 changes nothing. The total stays at 4.

Example 3

Input:  gifts = [10,10], k = 1
Output: 13

Both piles are tied for the most. Either one becomes floor(sqrt(10)) = 3, and the total is 3 + 10 = 13 either way.

Example 4

Input:  gifts = [1000000000], k = 3
Output: 13

The same pile is the richest every second: 1000000000 → 31622 → 177 → 13.

Example 5

Input:  gifts = [4,9,16], k = 6
Output: 4

16 → 4, then 9 → 3, then the two 4s become 2 and 2, then 3 → 1, then one 2 → 1. That leaves [1,1,2] in some order, which totals 4.

Constraints

  • 1 <= gifts.length <= 10^3
  • 1 <= gifts[i] <= 10^9
  • 1 <= k <= 10^3
  • The total can be as large as 1000 × 10^9, which does not fit in a 32-bit integer. Return a 64-bit value (long in Java, long long in C/C++).
TypeScript
Loading editor…
Test cases34
Case 1
gifts = [25,64,9,4,100]
k = 4
Case 2
gifts = [1,1,1,1]
k = 4
Case 3
gifts = [10,10]
k = 1
Case 4
gifts = [1000000000]
k = 3
Case 5
gifts = [4,9,16]
k = 6
Case 6
gifts = [25,64,9,4,100]
k = 1
Case 7
gifts = [25,64,9,4,100]
k = 1000
Case 8
gifts = [1]
k = 1
Case 9
gifts = [2]
k = 1
Case 10
gifts = [3]
k = 1
Case 11
gifts = [4]
k = 1
Case 12
gifts = [8]
k = 1
Case 13
gifts = [99,100]
k = 1
Case 14
gifts = [100,99]
k = 1
Case 15
gifts = [100,99]
k = 2
Case 16
gifts = [15,16,17]
k = 3
Case 17
gifts = [1000000000,1000000000]
k = 1
Case 18
gifts = [999999999,1000000000]
k = 2
Case 19
gifts = [1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000]
k = 1
Case 20
gifts = [1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000]
k = 1000
Case 21
gifts = [1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000]
k = 450
Case 22
gifts = [999950884]
k = 1
Case 23
gifts = [999950883]
k = 1
Case 24
gifts = [5,19,11,2,7]
k = 5
Case 25
gifts = [1,2,3,4,5,6,7,8,9,10]
k = 10
Case 26
gifts = [10,9,8,7,6,5,4,3,2,1]
k = 3
Case 27
gifts = [703066963,582997883,707780908,403922477,385579374,807848636,768692701]
k = 3
Case 28
gifts = [205196152,628981078,37199995,823689712,965625943,546835832,979599778,269967134,139103467,707166625,356669148,565709225,722118619,450893013,173591938,244089603,550737370,924503398,10861812,222629024]
k = 15
Case 29
gifts = [885682148,351930231,775608397,397339535,91065227,188857285,443286187,872057111,509701632,557938411,983752538,630079182,419336,162723511,686324264,118457559,668638192,694530800,721870559,894658568,382341810,426836713,111007982,11740721,348501730,148840355,884044913,558119418,288636004,179149896,265404533,201213935,704442228,494002983,181726619,390124614,859867602,66842697,278069103,304514205,600854377,725478316,952472862,189297717,520602445,189721140,166554894,406485513,665108128,134631904]
k = 200
Case 30
gifts = [170968456,152461468,421361203,177445837,201322407,850219689,100525431,960205761,821108688,413794637,776639045,486410697,795753481,330742650,108259411,17114181,127923641,745390316,340364829,191102499,898272056,111163479,687835118,293541037,94963588,108016038,349214358,452622484,857898219,772993999,217029638,553115857,171527167,847115907,210922617,344629245,315657375,944035046,829565347,108173197,483395363,504941630,682076702,486426737,169186344,491441146,961984307,465507019,904527098,96779896,546463709,792932143,121959265,135103822,84810381,609351095,680401106,94868927,531183251,858131102,235295868,509950837,35603138,186462950,185558779,463908939,192573338,438298750,374215185,865127970,590283723,923721583,349262385,799349626,991408768,968661236,244808462,49785007,668312354,224355270,562342411,374137406,702528471,195339774,987646031,617993630,21369995,299270258,45656996,425452669,137388117,506364847,550733244,839927327,319471955,370678158,555722158,335505894,467205409,944051756]
k = 37
Case 31
gifts = [798919240,985657598,480951485,721604816,440545002,914701636,846452313,635702605,670659973,195498381,792068285,603713204,529698491,497704701,787339106,640833043,962612221,770946473,132708954,212349523,931798596,20355554,489895712,176089064,35457654,178288947,19495474,235793354,481254030,616974618,436108557,165983052,216504552,19477203,159048615,371232004,383106728,119165130,720925334,373905726,491369079,808039057,24657085,852633409,479004381,26423409,87271622,33844273,323111898,22890633,743135716,84378407,977068821,416851780,826236574,340158971,54208065,271380241,26682813,762712094,314827787,590223362,418819798,198567254,388717071,486100865,755727940,405771128,579984761,790372241,204853397,935917276,977321288,685377246,435264873,580219451,755348479,771124058,418817344,307678774,526903022,262718472,619738706,758686990,912996871,568918669,709777373,992341508,933653934,801258980,44253857,623985499,891875355,834310998,499996125,537052352,281061757,854137388,306658582,845344383,252148411,140848222,547891131,959343385,374845186,281835265,397413274,870388575,433799985,906554991,608703921,257643081,229301135,865524670,209142469,767151134,977371102,952628202,105672777,934216857,557754116,421378571,362831275,249446769,125091741,170638961,501985322,848656361,54401236,126372991,769777579,1996507,439432097,754644481,725240836,794059824,644081550,430473565,738733359,202963087,660782658,36653435,802382577,140323790,383741437,95971461,564971060,234265695,846469538,650316025,54968699,731554295,931652273,240409645,344471080,975060672,754062770,537101879,869833569,826444814,448619693,612729123,49840698,462008036,98797205,129963327,111476725,481404041,824101733,478788888,555145344,280526567,608091160,597373106,420003150,487378101,652800457,202770462,830445479,935598917,783377819,176109588,977327309,114679096,317928360,169642502,365942761,587811700,214094009,627136269,437775232,495182918,460123543,815687988,66033285,640545581,502163737,292226898,510813498,507603249,260164516,591729828,447193071,216540061,144748419,996149484,638743476,77836365,289225332,570585259,808479236,957370895,66992913,339061239,429906090,463176099,833894636,246527570,958313576,570386028,729586007,746886195,751268286,822707541,576326104,3078939,240756214,111748126,283764392,273179133,765561705,21564154,846710194,113889698,937870319,777521961,824508728,390620980,880963371,861984223,563407220,218418268,709326655,547630791,748733674,942939926,597445219,303982880,701277727,407732145,987428534,122126602,652702079,465600915,739771215,919403665,583839998,479426306,682833727,767896557,453826690,379330782,760948471,434057454,524088418,261765214,178292518,830220937,773209456,121777038,355965741,99890819,185197336,16272234,129431350,485077639,737717120,879922842,50908526,360724287,3072062,907952968,542314594,607720619,296470742,991922256,999123266,6752366,544030543,330393814,667542555,212807972,399552335,210153184,81055147,897033171,478381097,853928371,718755839,273698961]
k = 1000
Case 32
gifts = [238516920,488586845,709805209,175167541,465738488,182256017,793889100,753128765,934678627,158651577,407803872,929651244,175647916,989023514,390229583,844974980,787461144,235645031,95720720,875130070,149677367,118506142,451648135,41264341,508554332,159500906,756485161,435483564,818252873,83900096,178509072,436110735,882187791,242971708,851910902,924375230,678208804,471036932,984634665,237785374,394232144,378081522,136362758,444630147,529107343,93612040,343871603,113160608,413018116,774857889,613634864,142167136,391059416,586795406,317944511,975095149,527359446,15858391,173059924,36595903,648721091,324086155,15969138,642735377,443982651,511381053,725078164,485471543,976588169,147567800,216306948,199672435,875947503,851210664,29264931,208211146,230069603,304757097,407678545,260889958,557036457,701250826,382567049,199456991,977864881,9212826,626411917,345061855,265115875,692482664,809879798,820620814,105116185,597955520,637070187,875256976,197533927,617411688,87746669,668084849,328367804,130172098,420024087,544231709,40994041,686432924,202263167,762070312,989666372,457218471,526370208,811309710,880535027,582531654,892597431,987573450,903080179,364551113,9232844,196563846,308044441,638763129,875839587,540083949,961592921,932561746,320335304,12464171,86173574,579644348,547405719,863254137,595846014,240666304,884289765,760978931,712438079,997435752,831482165,818013843,460994014,15093813,367845674,581173048,137125219,253377349,151525107,25420290,588394494,4104640,828470977,27327387,745418357,454808158,326910929,508143276,409300632,682842615,944827275,772361137,780097617,260149049,163386826,869561382,818860091,128544539,323045935,81309445,757862916,664149409,263448872,839981835,967946524,547250363,665226583,418017432,730913029,196926136,959996012,730273120,676162003,548076312,812940266,710865704,245788062,217002214,408259292,780281779,334682388,990421864,737042988,529026149,411019423,169452556,23168632,685775967,596354834,451331406,370488515,459520114,638268392,377265302,394775440,539387889,239482137,853180567,915859185,724782793,972508153,59929235,450239525,787693433,437323656,478031343,317431444,435179378,983824745,530943512,185533825,75241828,23074288,395586729,739342085,727606349,574869115,690039181,269477571,223909680,998462294,98106672,654051850,575183953,88398899,148100760,406032898,318889772,648850674,552666720,890596259,508341316,707126270,58117709,423077333,448586905,85220451,323069181,563277825,928476836,567686859,210333906,154977974,941185647,184012200,353468306,556133368,842775383,316204280,61756592,670661624,863185094,505718591,525131605,729836514,690580566,938378746,65256423,333741499,189046484,116675288,71171275,454634865,256758620,216688957,637717526,224338456,4070525,993811107,554335551,207203556,185899768,136969582,595657806,571096742,908893651,365412665,259677841,203499929,945345869,385117184,375604925,350353623,428213832,796260980,956861578,900626847,570209258,332052581,439266347,342676541,232074983,9958451,915092975,710633960,322736811,110133688,591978015,241586683,914864491,86065245,181598973,758478197,746967634,94823075,154231214,673540157,528075102,585257992,337636394,969210935,542691837,227388875,903564720,687594968,928894146,814412587,939223448,490818883,41780924,837065528,333927172,205636216,329635344,931714318,251135104,89846010,571466295,943927682,955734296,434092930,487113903,762726689,41405017,771345760,339144482,437863655,313293652,458486016,697053969,687670657,949832216,872072816,104444552,292404299,58179884,108223962,917621710,521809096,664228129,516381558,549318165,17356907,858136726,176354058,894463921,389595315,871664720,237720330,507783702,669766789,475198373,492703911,409193898,85385223,638466159,328809782,594669611,563813522,637977069,140670239,946037754,235837374,873357752,845501897,200778462,544096208,414020853,410946078,218186917,745979122,213664955,61636582,427871103,905258535,215048772,262912908,614930347,940276105,771390565,572723077,310533470,478338841,685403669,594168422,411267521,471211629,527979376,995575827,797497154,135893381,476807096,105900426,242053349,60833110,581445558,716487702,286363659,379492167,219917224,884995390,554376277,358506306,577117225,654042035,589112765,838759364,617698166,56492907,69281500,302590837,997007445,642860328,255040459,365777739,992663554,674284747,319092987,129631540,162501910,957120497,49868178,749601735,340713516,313296222,168396793,304318270,346597567,670867315,416070579,155988971,876477855,419889990,520771940,839070917,720439343,386676045,923055796,487121790,751555869,784056415,560468174,981507822,732895092,640297528,114164853,998395705,808266742,139109477,614391087,896416385,624118398,786566484,301743714,86259862,726230638,751967544,445206249,363390272,207342818,140158706,476224326,33984429,748970106,739647889,103636898,589838142,48605926,868050983,349500731,449291174,274450808,481145568,285384565,968195641,164403559,682522454,307232185,185331841,451461985,679898364,937799270,961199941,930337208,525331504,314140649,505820432,310211200,35950493,434287334,37243027,318118554,143061518,341672815,446878959,102958803,987546783,751824859,264505253,707633581,553174841,680407898,993456328,863039247,351584328,807984274,740301393,701051453,869447688,847483633,260540862,44959264,301275697,338958302,41480122,712167641,537828983,512639549,157996113,164165398,140061523,86406466,313419851,182819466,988193079,234756995,123417028,693483369,590415630,165673388,498342920,641891424,1107655,474693415,882867395,473742176,353172967,935166383,951679856,114480178,25430768,100185019,669301619,623535805,427020508,944517073,689207323,542217442,696433581,810535442,981728404,210183669,190801306,908552173,780494998,45075814,20143600,925225184,822294971,124657157,27872418,313228636,90941656,297487462,757583785,657416804,732541918,849735566,22603926,656366138,966520183,716792683,209551931,487637067,682246597,561841970,496250439,984804832,634654706,518240951,750824326,604824964,411960923,56201649,105639828,177090912,687958030,39789389,60912040,702782640,466278285,51554893,463521165,79278617,510286438,918711741,557127496,204582115,575036370,213921028,418367487,460123782,139151373,262732512,964411230,582268299,912322253,819672060,359079446,92149396,640437271,16401162,167181989,880844371,259626848,340865096,930714446,117184228,536640320,432620240,742857624,727379805,890026136,117431449,901706553,266621794,324352269,413508826,256450839,619239736,252913412,571341339,81257242,464379943,35444790,82973108,584183594,783668939,657304089,170208227,427143549,994098707,397227422,294767841,723334560,499125987,418571596,323372139,407434304,235797118,22891863,557798848,564499051,606120474,134343671,329153929,29709430,267709736,430520749,260719381,304794565,224219436,233769160,465071261,997434448,381493886,269677340,81571364,993140451,365002452,611079805,518159464,957239359,996314800,461683949,14801718,925373813,913153841,200261598,933355685,830002439,399872372,890114619,411882865,944889067,87248886,777284435,460604842,176752146,540919750,388880916,403568965,291289684,132179896,331631984,366602071,35734476,499744404,10328445,362890883,263340661,777300309,759781478,144608261,603378254,70600194,409059491,669787714,694687918,657709389,584683823,335010154,553034035,286782691,182055979,926284367,50121614,475922141,349412577,786992150,192118134,350342820,260949263,540152638,690062058,454927217,852762910,564637651,531745011,695988571,139066775,683344659,71607328,530098058,690648781,636227363,581080750,300326498,200017374,26528777,220038022,957222951,81230255,102097894,2574882,888371194,162971363,931488183,178963832,829893269,703353413,571521041,586498533,91975299,764447738,801906600,330391858,490519539,886116364,979034005,992275709,368697544,11993824,657915025,69574040,496736425,4066323,563330482,562195527,107909015,744723891,600658151,827499666,507043892,739218137,347380805,912510211,758424747,584628956,591713401,349373003,966431591,202661715,167324071,909282270,157177838,481055551,173251222,766271445,677357264,285297939,456330739,502748902,146905030,629211596,743656961,830212929,421733245,762754063,46986334,108418214,223554921,899226344,818667196,205876129,144900034,946450270,663838850,459098040,932361639,839390956,916633867,876486637,992313064,369137272,785566809,950796073,75355558,854012066,908484330,929070783,60516822,630128947,454660292,169293853,938665419,531608181,228729649,25775761,590638930,951389843,654227173,576027805,955975818,404316325,232479412,599345310,805690301,327083903,4913183,557493650,216500422,35292010,776324148,899868944,587108373,161782521,355782228,192666888,506661049,490489779,394617261,440039803,746864057,610363070,337711811,738717525,812183119,304997664,953493722,134913830,777792163,853862290,91176186,903693302,122058842,356689600,655687565,818659485,901153976,226250430,875745071,248683276,368773433,419419585,120595445,626960544,424069350,638883988,591309104,609369981,703165297,938872466,184879362,365256491,89662536,349669224,893535715,187431569,272566541,919788305,788412724,448258052,333080556,352992137,250567804,527002189,767900758,621674164,415953111,450703910,140898214,551828114,597197476,371203455,834241506,512207577,205823706,417942759,684973390,792856814,889481012,605999054,566061065,494171126,959113295,91234657,361189362,603777012,533405697,3910359,605289491,911423688,804380884,859556101,106062927,151914925,127660113,831891599,794473213,524669533,476801173,928540094,774584735,136663039,509754980,28221811,182153907,402103986,784503281,599271084,545463618,460339197,954286864,74126277,261035681,846886990,609201367,741715868,899772586,565047410,716037763,248562435,181012026,61971399,391635332,140671476,637861989,666854292,170315557,848701412,483789920,196568268,435705560,47106986,322558682,266447704,873045699,119891291,276741086,614180067,605606788,991468697,526448892,776904808,893167343,338231931,139655286,751750516,813284584,805018519,488588144]
k = 1000
Case 33
gifts = [9,28,25,9,30,38,1,41,32,41]
k = 4
Case 34
gifts = [10,21,5,42,4,46,23,50,11,2,8,37,17,26,3,7,27,42,13,8,50,12,17,40,4,17,10,48,38,8,44,5,36,19,13,11,41,21,31,27]
k = 40