i carries the number tiles[i]. Two players take turns, the first player moving first. On a turn, a player removes an end tile, either the leftmost or the rightmost, and scores the sum of the numbers on the tiles **still in the row** after the move. The game ends when no tiles are left.
The second player knows they cannot win, so they try to keep the score gap as small as possible, while the first player tries to make it as large as possible.
Both play optimally. Return the final gap: the first player's score minus the second player's.tiles=[2, 7, 3] → 3
The first player removes 2 and scores 7 + 3 = 10. The second does best by removing 3 for 7 points, then the first removes 7 for 0. The gap is 10 − 7 = 3; any other opening gives the first player less.
tiles=[4, 4] → 4
The first player removes either tile for 4 points, and the second removes the last one for 0.
tiles=[6, 1, 5, 2] → 11
tiles=[2, 7, 3]
3
Run checks the first 3 tests in your browser. Submit checks all 99 tests on the server, hidden ones included.