2025-08-20 15:49:12 +0000 UTC

Take Gifts From the Richest Pile

Code

class Solution:
    def pickGifts(self, gifts: List[int], k: int) -> int:
        for i in range(len(gifts)):
            gifts[i] = -gifts[i]
        heapq.heapify(gifts)
        for _ in range(k):
            val = int(math.sqrt(-heapq.heappop(gifts)))
            heapq.heappush(gifts, -val)
        return -sum(gifts)