Ctrl K

Harvest From Shrinking Piles

Medium
You are given coin piles piles and a number moves. Your score starts at 0. In one move you choose any pile, add its size v to the score, and the pile is then left with ⌈v / 3⌉ coins (division by 3 rounded up). Make exactly moves moves (the same pile may be chosen any number of times) and return the largest possible score.
Example 1
piles=[2, 12, 5], moves=3 → 21

Take 12 (the pile becomes 4), then 5 (becomes 2), then 4. Total 12 + 5 + 4 = 21.

Example 2
piles=[7], moves=4 → 12

A single pile: 7, then ⌈7/3⌉ = 3, then 1, then 1 again. Total 12.

Example 3
piles=[6, 6, 6], moves=3 → 18
Constraints
  • 1 ≤ len(piles) ≤ 10⁵
  • 1 ≤ moves ≤ 10⁵
  • 1 ≤ piles[i] ≤ 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
piles=[2, 12, 5], moves=3
Expected
21

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