CSES - Removal Game | Trò chơi loại bỏ

View as PDF



Authors:
Problem types
Points: 1700 (p) Time limit: 1.0s Memory limit: 512M Input: stdin Output: stdout

There is a list of \(n\) numbers and two players who move alternately. On each move, a player removes either the first or last number from the list, and their score increases by that number. Both players try to maximize their scores.

What is the maximum possible score for the first player when both players play optimally?

Input

  • The first input line contains an integer \(n\): the size of the list.
  • The next line has \(n\) integers \(x_1,x_2, \ldots, x_n\): the contents of the list.

Output

  • Print the maximum possible score for the first player.

Constraints

  • \(1 \leq n \leq 5000\)
  • \(−10^9 \leq x_i \leq 10^9\)

Example

Sample input

4  
4 5 1 3  

Sample output

8


Comments (6)

Most recent
Loading comments...