Skip to content
Decoder

Remove Stones to Minimize the Total

Medium

Problem

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.

Example 1

Input
piles = [5, 4, 9], k = 2
Output
12

Example 2

Input
piles = [4, 3, 6, 7], k = 3
Output
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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.