Ctrl K

Melting Ice Cream

Medium
There are n cups of ice cream on the counter; cup i holds a portion of size scoops[i]. You take exactly k cups, one per turn. After every turn, each cup still on the counter melts a little: its portion shrinks by 1, but never below zero. Return the largest possible total size of the portions you take.
Example 1
scoops=[2, 6, 4], k=2 → 9

Take the 6 first. The rest melt to [1, 3], so the second pick gets 3. That is 6 + 3 = 9.

Example 2
scoops=[1, 1, 2], k=3 → 2

Take the 2; the two 1s melt down to nothing, and the remaining picks add 0: 2 + 0 + 0 = 2.

Example 3
scoops=[5, 5, 5, 5], k=4 → 14
Constraints
  • 1 ≤ n = len(scoops) ≤ 2·10⁵
  • 1 ≤ scoops[i] ≤ 10⁸
  • 1 ≤ k ≤ n
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBiggest Number From Pieces MediumStudios for Recording Sessions MediumLow, High, Low MediumStrictly Low, High, Low MediumFewest Meetings to Cancel Medium
loading Python…
Input
scoops=[2, 6, 4], k=2
Expected
9

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