tiles. Alice and Bob take turns, Alice first. While more than one tile is left, the player to move:
- picks an integer x > 1 and removes the x leftmost tiles;
- adds the sum of their numbers to their score;
- puts a new tile carrying that sum at the front of the row.
The game ends when one tile is left. Alice wants the difference (Alice's score) − (Bob's score) as large as possible, Bob wants it as small as possible.
Return that difference when both play optimally.tiles=[3, -1, 4, -2] → 4
Alice merges every tile at once for 3 − 1 + 4 − 2 = 4, and Bob gets 0. Merging three tiles (+6) lets Bob merge the last two (+4) for a gap of 2; merging two (+2) lets Bob merge everything (+4) for a gap of −2.
tiles=[-5, -6] → -11
There is a single move: Alice must merge both tiles and gets −11.
tiles=[2, -3, 1] → 0
Merging everything gives a gap of 0. Merging the first two (−1) lets Bob merge the rest for 0, a gap of −1. Alice goes for 0.
tiles=[3, -1, 4, -2]
4
Run checks the first 3 tests in your browser. Submit checks all 78 tests on the server, hidden ones included.