- heapify(q) 初始化一个列表q成为小根堆
- 这道题取反使之成为大根堆
- heappop(q) 弹出堆顶
- heappush(q, e) 将e插入堆中
python
class Solution:
def maxKelements(self, nums: List[int], k: int) -> int:
q = [-x for x in nums]
heapify(q)
ans = 0
for _ in range(k):
x = heappop(q)
ans += -x
heappush(q, -((-x + 2) // 3))
return ans