values. Cut it into one or more contiguous pieces so that every element belongs to exactly one piece.
The cost of a piece uses alternating signs: its first element counts with a plus, the second with a minus, the third with a plus again, and so on. For example, the piece [a, b, c, d] costs a - b + c - d.
Return the largest possible total cost of all pieces.values=[2, -3, -1, 5] → 9
Split into [2, -3, -1] and [5]: 2 - (-3) + (-1) = 4, plus 5, total 9.
values=[-4, -2, -6] → 0
Pieces [-4] and [-2, -6]: -4 + (-2 - (-6)) = -4 + 4 = 0.
values=[7] → 7
values=[2, -3, -1, 5]
9
Run checks the first 3 tests in your browser. Submit checks all 106 tests on the server, hidden ones included.