CSES - List game

The list game has two players that alternate in removing numbers from a list. In each turn, a player removes either the first or the last number. The game ends when all numbers have been removed.

The score of a player is the sum of the numbers removed by that player. The winner of the game is the player with the higher score. If both players end up with the same sum, the result is a draw. Your task is to find if the player with the first turn always wins if they play optimally.

For example, if the list is [2,1,3][2,1,3], the first player always wins. The first player can first remove 33 and then 11. At the end of the game, the first player's sum is 44 and the other player's sum is 22.

On the other hand, if the list is [1,3,1][1,3,1], the first player can lose. The first player must remove either of the 11's, and then the other player can remove 33. The first player's final sum is 22 while the other player's sum is 33 giving the win to the other player.

In a file listgame.py, implement the function first_wins that takes a list of integers as a parameter. The function should return True if the first player always wins, and False otherwise.

The function should be efficient when the length of the list is in the range 1501 \dots 50.

def first_wins(numbers):
    # TODO

if __name__ == "__main__":
    print(first_wins([2, 1, 3])) # True
    print(first_wins([1, 3, 1])) # False

    print(first_wins([1])) # True
    print(first_wins([1, 1])) # False
    print(first_wins([1, 5])) # True
    print(first_wins([1, 1, 1])) # True
    print(first_wins([1, 2, 3, 4])) # True
    print(first_wins([1, 3, 3, 7, 4, 2, 1])) # False

    print(first_wins([1] * 50)) # False
    print(first_wins([1, 2] * 25)) # True