Ctrl K

Cutting Down Sand Heaps

Medium
A building site has heaps of sand: heaps[i] is the weight of heap i in tons. A dump truck makes exactly moves trips. On each trip it goes to any heap weighing x and hauls away ⌊x / 2⌋ tons (half, rounded down). The truck may visit the same heap any number of times. Return the smallest possible total weight of sand left on the site after all the trips.
Example 1
heaps=[7, 12, 3], moves=2 → 13

First cut the heap of 12: 6 goes, 6 stays. Then the heap of 7: 3 goes, 4 stays. The heaps are [4, 6, 3], 13 in total.

Example 2
heaps=[20], moves=3 → 3

The same heap can be cut repeatedly: 20 → 10 → 5 → 3.

Example 3
heaps=[1, 1], moves=4 → 2

Nothing can be taken from a heap of 1 (half rounded down is 0), so the total stays 2.

Constraints
  • 1 ≤ len(heaps) ≤ 10⁵
  • 1 ≤ heaps[i] ≤ 10⁴
  • 1 ≤ moves ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsStudios for Recording Sessions MediumJobs With a Cooldown MediumSorted Into Runs of Three MediumSpread Out the Labels MediumSplicing Ropes Medium
loading Python…
Input
heaps=[7, 12, 3], moves=2
Expected
13

Run checks the first 3 tests in your browser. Submit checks all 83 tests on the server, hidden ones included.