> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Take Gifts From the Richest Pile

> Tested Python solution for LeetCode 2558 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 2558, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Heap (Priority Queue)](/catalog/topics/heap-priority-queue), [Simulation](/catalog/topics/simulation). [View on LeetCode](https://leetcode.com/problems/take-gifts-from-the-richest-pile/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2558   # by problem number
lcpy gen -s take_gifts_from_the_richest_pile   # by problem name
```

## Problem

You are given an integer array `gifts` denoting the number of gifts in various piles. Every second, you do the following:

* Choose the pile with the maximum number of gifts.
* If there is more than one pile with the maximum number of gifts, choose any.
* Reduce the number of gifts in the pile to the floor of the square root of the original number of gifts in the pile.

Return *the number of gifts remaining after* `k` *seconds.*

### Examples

```
Input: gifts = [25,64,9,4,100], k = 4
Output: 29
Explanation:
The gifts are taken in the following way:
- In the first second, the last pile is chosen and 10 gifts are left behind.
- Then the second pile is chosen and 8 gifts are left behind.
- After that the first pile is chosen and 5 gifts are left behind.
- Finally, the last pile is chosen again and 3 gifts are left behind.
The final remaining gifts are [5,8,9,4,3], so the total number of gifts remaining is 29.
```

```
Input: gifts = [1,1,1,1], k = 4
Output: 4
Explanation:
In this case, regardless which pile you choose, you have to leave behind 1 gift in each pile.
That is, you can't take any pile with you.
So, the total gifts remaining are 4.
```

### Constraints

* 1 \<= gifts.length \<= 10\<sup>3\</sup>
* 1 \<= gifts\[i] \<= 10\<sup>9\</sup>
* 1 \<= k \<= 10\<sup>3\</sup>

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/take_gifts_from_the_richest_pile/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/take_gifts_from_the_richest_pile/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import heapq
from math import isqrt


class Solution:
    # Time: O(k log n + n)
    # Space: O(n)
    def pick_gifts(self, gifts: list[int], k: int) -> int:
        heap = [-gift for gift in gifts]
        heapq.heapify(heap)
        for _ in range(k):
            top = -heapq.heappop(heap)
            if top <= 1:
                heapq.heappush(heap, -top)
                break
            heapq.heappush(heap, -isqrt(top))
        return -sum(heap)
```

## Complexity

| Time | Space |
| - | - |
| O(k log n + n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.