Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/dp-chetlardan-olish

Taking from the ends

Medium 1000 ms 128 MB 66%
Share

$n$ cards with numbers lie in a row on a table. Two players move in turn, the first player starts: on each move a player takes the card at the left or right end of the row, and its number is added to their score. The game ends when the cards run out. The first player tries to make the difference between their score and the second player's score as large as possible, and the second player tries to make it as small as possible. What is this difference if both play optimally?

A row of cards: the players take from the ends in turn, the first player's cards are highlighted

Input

The first line contains a positive integer $n$. The second line contains $n$ positive integers — the numbers on the cards.

Output

One integer — the difference between the scores of the first and second players.

Note

$1 \le n \le 3000$
$1 \le a_{i} \le 10^{9}$

Examples

Input #1
4
10 80 90 30
Output #1
10
Input #2
1
7
Output #2
7

Sign in to submit a solution

Signing up is free and takes a minute.