#1962

Remove Stones to Minimize the Total

specialist · 635 · lc medium +30 · premium · verified · 65.5% accepted · 1,960 likes · top 70%

Description

You have a 0-indexed integer array piles of stone piles and an integer k. Exactly k times, perform this operation: pick any pile piles[i] and discard floor(piles[i] / 2) of its stones. A pile may be chosen more than once.

Return the smallest possible total stone count remaining after completing all k operations, where floor(x) denotes rounding x down to the nearest integer.

Example 1:

Input: piles = [5,4,9], k = 2
Output: 12
Explanation: Steps of a possible scenario are:
- Apply the operation on pile 2. The resulting piles are [5,4,5].
- Apply the operation on pile 0. The resulting piles are [3,4,5].
The total number of stones in [3,4,5] is 12.

Example 2:

Input: piles = [4,3,6,7], k = 3
Output: 12
Explanation: Steps of a possible scenario are:
- Apply the operation on pile 2. The resulting piles are [4,3,3,7].
- Apply the operation on pile 3. The resulting piles are [4,3,3,4].
- Apply the operation on pile 0. The resulting piles are [2,3,3,4].
The total number of stones in [2,3,3,4] is 12.

Code

1
2
3