Ctrl K

Keep the Lighter Half

Hard
Stones lie in a row, with weights given by the array weights. The game goes in rounds, and your score starts at 0. Each round you cut the current row into two non-empty parts, a left one and a right one. The heavier part (the one with the larger total weight) is thrown away, and the total weight of the lighter part is added to your score; the lighter part becomes the row for the next round. If the totals are equal, you choose which part to keep. The game ends when a single stone is left. Return the highest score you can get.
Example 1
weights=[3, 1, 2, 4] → 5

Cut into [3, 1] and [2, 4]: the lighter part [3, 1] stays, +4. Then cut it into [3] and [1]: [1] stays, +1. That makes 5, and no plan does better.

Example 2
weights=[5, 5] → 5

The parts are equal, so either may stay: +5, and a single stone is left.

Example 3
weights=[8] → 0

There is only one stone and nothing to cut, so the score is 0.

Constraints
  • 1 ≤ len(weights) ≤ 500
  • 1 ≤ weights[i] ≤ 10⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsGrab Up to Three Cards HardMouse, Cat and Cheese HardMerge the Front Tiles HardDuel for the End Cards MediumDuel for the End Heaps Medium
loading Python…
Input
weights=[3, 1, 2, 4]
Expected
5

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