English · Original
Chinese · Translation
Formal · Original
_This problem has unusual memory constraint._
At evening, Igor and Zhenya the financiers became boring, so they decided to play a game. They prepared _n_ papers with the income of some company for some time periods. Note that the income can be positive, zero or negative.
Igor and Zhenya placed the papers in a row and decided to take turns making moves. Igor will take the papers from the left side, Zhenya will take the papers from the right side. Igor goes first and takes 1 or 2 (on his choice) papers from the left. Then, on each turn a player can take _k_ or _k_ + 1 papers from his side if the opponent took exactly _k_ papers in the previous turn. Players can't skip moves. The game ends when there are no papers left, or when some of the players can't make a move.
Your task is to determine the difference between the sum of incomes on the papers Igor took and the sum of incomes on the papers Zhenya took, assuming both players play optimally. Igor wants to maximize the difference, Zhenya wants to minimize it.
## Input
The first line contains single positive integer _n_ (1 ≤ _n_ ≤ 4000) — the number of papers.
The second line contains _n_ integers _a_1, _a_2, ..., _a__n_ ( - 105 ≤ _a__i_ ≤ 105), where _a__i_ is the income on the _i_\-th paper from the left.
## Output
Print the difference between the sum of incomes on the papers Igor took and the sum of incomes on the papers Zhenya took, assuming both players play optimally. Igor wants to maximize the difference, Zhenya wants to minimize it.
[samples]
## Note
In the first example it's profitable for Igor to take two papers from the left to have the sum of the incomes equal to 4. Then Zhenya wouldn't be able to make a move since there would be only one paper, and he would be able to take only 2 or 3..
_This problem has unusual memory constraint._
At evening, Igor and Zhenya the financiers became boring, so they decided to play a game. They prepared #cf_span[n] papers with the income of some company for some time periods. Note that the income can be positive, zero or negative.
Igor and Zhenya placed the papers in a row and decided to take turns making moves. Igor will take the papers from the left side, Zhenya will take the papers from the right side. Igor goes first and takes #cf_span[1] or #cf_span[2] (on his choice) papers from the left. Then, on each turn a player can take #cf_span[k] or #cf_span[k + 1] papers from his side if the opponent took exactly #cf_span[k] papers in the previous turn. Players can't skip moves. The game ends when there are no papers left, or when some of the players can't make a move.
Your task is to determine the difference between the sum of incomes on the papers Igor took and the sum of incomes on the papers Zhenya took, assuming both players play optimally. Igor wants to maximize the difference, Zhenya wants to minimize it.
The first line contains single positive integer #cf_span[n] (#cf_span[1 ≤ n ≤ 4000]) — the number of papers.
The second line contains #cf_span[n] integers #cf_span[a1, a2, ..., an] (#cf_span[ - 105 ≤ ai ≤ 105]), where #cf_span[ai] is the income on the #cf_span[i]-th paper from the left.
Print the difference between the sum of incomes on the papers Igor took and the sum of incomes on the papers Zhenya took, assuming both players play optimally. Igor wants to maximize the difference, Zhenya wants to minimize it.
In the first example it's profitable for Igor to take two papers from the left to have the sum of the incomes equal to #cf_span[4]. Then Zhenya wouldn't be able to make a move since there would be only one paper, and he would be able to take only #cf_span[2] or #cf_span[3]..
## Input
The first line contains single positive integer #cf_span[n] (#cf_span[1 ≤ n ≤ 4000]) — the number of papers.The second line contains #cf_span[n] integers #cf_span[a1, a2, ..., an] (#cf_span[ - 105 ≤ ai ≤ 105]), where #cf_span[ai] is the income on the #cf_span[i]-th paper from the left.
## Output
Print the difference between the sum of incomes on the papers Igor took and the sum of incomes on the papers Zhenya took, assuming both players play optimally. Igor wants to maximize the difference, Zhenya wants to minimize it.
[samples]
## Note
In the first example it's profitable for Igor to take two papers from the left to have the sum of the incomes equal to #cf_span[4]. Then Zhenya wouldn't be able to make a move since there would be only one paper, and he would be able to take only #cf_span[2] or #cf_span[3]..
**Definitions**
Let $ n \in \mathbb{Z}^+ $ be the number of papers.
Let $ A = (a_1, a_2, \dots, a_n) $ be a sequence of integers representing incomes.
Let $ S_I $ and $ S_Z $ be the total incomes collected by Igor and Zhenya, respectively.
**Game Rules**
- Igor starts and may take $ k = 1 $ or $ k = 2 $ papers from the **left**.
- Subsequently, if the opponent took $ k $ papers in the previous move, the current player must take either $ k $ or $ k+1 $ papers from their own side (Igor: left, Zhenya: right).
- Players alternate turns; Igor always takes from the left, Zhenya from the right.
- The game ends when no papers remain or when the current player cannot make a valid move (i.e., fewer than required papers remain on their side).
**Objective**
Compute $ S_I - S_Z $, assuming both players play optimally:
- Igor maximizes $ S_I - S_Z $,
- Zhenya minimizes $ S_I - S_Z $.
**Constraints**
$ 1 \leq n \leq 4000 $, $ -10^5 \leq a_i \leq 10^5 $
**State Representation**
Define $ \text{dp}[l][r][k][p] $ as the optimal value of $ S_I - S_Z $ achievable from the subarray $ A[l:r+1] $, where:
- $ l, r $: current left and right boundaries of remaining papers ($ 0 \leq l \leq r < n $),
- $ k $: number of papers taken in the previous move ($ k \in \{1, 2, \dots, n\} $),
- $ p \in \{0, 1\} $: current player ($ 0 $ for Igor, $ 1 $ for Zhenya).
**Recurrence**
If $ l > r $:
$$
\text{dp}[l][r][k][p] = 0
$$
If $ p = 0 $ (Igor's turn, takes from left):
$$
\text{dp}[l][r][k][0] = \max_{x \in \{k, k+1\} \text{ and } x \leq r-l+1} \left( \sum_{i=l}^{l+x-1} a_i + \text{dp}[l+x][r][x][1] \right)
$$
If $ p = 1 $ (Zhenya's turn, takes from right):
$$
\text{dp}[l][r][k][1] = \min_{x \in \{k, k+1\} \text{ and } x \leq r-l+1} \left( -\sum_{i=r-x+1}^{r} a_i + \text{dp}[l][r-x][x][0] \right)
$$
**Initial State**
$$
\text{Answer} = \max \left( \sum_{i=1}^{1} a_i + \text{dp}[1][n-1][1][1], \sum_{i=1}^{2} a_i + \text{dp}[2][n-1][2][1] \right)
$$
(using 0-based indexing: $ \sum_{i=0}^{0} a_i + \text{dp}[1][n-1][1][1] $, $ \sum_{i=0}^{1} a_i + \text{dp}[2][n-1][2][1] $)
**Output**
The value of $ S_I - S_Z $ under optimal play.
API Response (JSON)
{
"problem": {
"name": "F. Financiers Game",
"description": {
"content": "_This problem has unusual memory constraint._ At evening, Igor and Zhenya the financiers became boring, so they decided to play a game. They prepared _n_ papers with the income of some company for so",
"description_type": "Markdown"
},
"platform": "Codeforces",
"limit": {
"time_limit": 2000,
"memory_limit": 524288
},
"difficulty": "None",
"is_remote": true,
"is_sync": true,
"sync_url": null,
"sign": "CF729F"
},
"statements": [
{
"statement_type": "Markdown",
"content": "_This problem has unusual memory constraint._\n\nAt evening, Igor and Zhenya the financiers became boring, so they decided to play a game. They prepared _n_ papers with the income of some company for so...",
"is_translate": false,
"language": "English"
},
{
"statement_type": "Markdown",
"content": "_This problem has unusual memory constraint._\n\nAt evening, Igor and Zhenya the financiers became boring, so they decided to play a game. They prepared #cf_span[n] papers with the income of some compan...",
"is_translate": true,
"language": "Chinese"
},
{
"statement_type": "Markdown",
"content": "**Definitions** \nLet $ n \\in \\mathbb{Z}^+ $ be the number of papers. \nLet $ A = (a_1, a_2, \\dots, a_n) $ be a sequence of integers representing incomes. \nLet $ S_I $ and $ S_Z $ be the total income...",
"is_translate": false,
"language": "Formal"
}
]
}