Skip to content

Remove Stones to Minimize the Total

MediumHeap / Priority Queue
Decode it first. Clues, constraints, pattern and plan before any code.

You have piles of stones, where piles[i] is the size of pile i. You must do exactly k operations. In one operation, pick any pile and remove floor(pile / 2) stones from it. You may pick the same pile more than once.

Return the minimum possible total number of stones left after k operations.

piles = [5, 4, 9],    k = 2  →  12
piles = [4, 3, 6, 7], k = 3  →  12

In the first example, halve the 9 (remove 4, leaving 5), then halve one of the 5s (remove 2, leaving 3): 3 + 4 + 5 = 12.

Constraints

  • 1 <= piles.length <= 10^5
  • 1 <= piles[i] <= 10^4
  • 1 <= k <= 10^5