Codeforces Round 1092 (Unrated, Div. 1, Based on THUPC 2026 — Finals)
D
EXPloration, EXPloitation, and Gain Some EXPerience!
Codeforces
2800
原题
← CE →
翻译中…

Years later, Shiro and White both enrolled in THU. After learning about the EXP3 algorithm for the "Multi-armed Bandit" problem in class, they suddenly had a whim and devised a game unrelated to this.

翻译中…

There are $n$ positions arranged in a row, numbered $1,2,\ldots,n$ from front to back. Each position can be in one of two states: blackened or unblackened. Shiro's piece starts at position $1$, and White's piece starts at position $2$. At the beginning of the game, only positions $1$ and $2$ are blackened.

翻译中…

The players take turns making moves, with Shiro going first. In each turn, the current player must choose their own piece and jump forward $1$ to $4$ steps to an unblackened position, and blacken the position they land on. If a player cannot make a legal move (i.e., all positions within $1$ to $4$ steps forward from their current position are either beyond $n$ or already blackened), the player will skip this turn (i.e., make no move).

翻译中…

The game ends when both Shiro and White cannot make any valid move. It is easy to prove that the game is guaranteed to terminate within a finite number of steps.

翻译中…

For each $3\le k\le n$, position $k$ has a positive integer weight $a_k$. Whenever a player blackens such a position $k$, their score increases by $a_k$. Both Shiro and White start with a score of $0$, and both of them aim to maximize the difference between their own score and their opponent's score by the end of the game.

翻译中…

Let $d=s_1 - s_2$, where $s_1$ is Shiro's score and $s_2$ is White's score. Shiro and White want to know: if both players act according to optimal strategies, what will be the value of $d$ after the game ends? Since this problem is too complex, they have turned to you from THUSAA. You have to help them solve the problem.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows.

翻译中…

The first line of each test case contains a single integer $n$ ($6\le n\le 10^5$) — the number of positions.

翻译中…

The second line contains $n-2$ integers $a_3, a_4, \ldots, a_n$ ($1\le a_i\le 10^9$) — the weights of positions $3$ to $n$.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

翻译中…

Output

翻译中…

For each test case, output a single integer — the final value of $d$.

翻译中…

Note

翻译中…

The following is the explanation for the first example test:

翻译中…

We use the character O to denote a position that has not been visited by anyone, S to denote a position visited by Shiro, and W to denote a position visited by White. Then, the final configuration can be represented by a string of length $n-2$, and this string uniquely corresponds to the sequence of moves made by both players.

翻译中…

For the first test case, consider Shiro's first move as the starting player:

翻译中…
  • She cannot move to position $2$, because it is already occupied by White;
  • If her first move is to position $3$, then the final state is SWWS (score difference is $-6$);
  • If her first move is to position $4$, then the final state is OSSW (score difference is $5$);
  • If her first move is to position $5$, then the final state is OOSW (score difference is $-1$);
翻译中…

Therefore, the optimal final state is OSSW, with a score difference of $5$.

翻译中…

For the second test case, one possible final state is SWSWSWSW, with a score difference of $0$.

翻译中…

For the third test case, White can secure the farthest position $10$ in two moves, which Shiro cannot reach. Therefore, one possible strategy for Shiro is to make a large forward move to prevent White from delaying further. The corresponding final state is OOSWSSSW, with a score difference of $-7$.

翻译中…

For the fourth test case, one possible final state is OOSWWWS, with a score difference of $8$.

翻译中…

For the fifth test case, one possible final state is WWSWSS, with a score difference of $90$.

翻译中…

For the sixth test case, one possible final state is OOSWSOWS, with a score difference of $1\,000\,000\,000$.

翻译中…

样例

Input #1
6
6
1 6 3 4
10
1 1 1 1 1 1 1 1
10
1 1 1 1 1 1 1 10
9
1 1 1 1 1 1 10
8
10 1 1 1 1 100
10
1000000000 1 1000000000 1 1000000000 1 1000000000 1
Output #1
5
0
-7
8
90
1000000000
InputCopy OutputCopy
Input #2
6
9
7 6 2 2 5 8 7
10
8 26 18 1 11 9 15 9
11
8 3 9 2 3 4 8 8 7
12
5 6 5 3 1 2 1 1 5 4
15
6 6 7 2 2 2 5 2 2 4 7 7 7
18
7 4 5 1 2 6 7 5 7 3 7 3 6 5 6 6
Output #2
5
13
8
3
9
9
InputCopy OutputCopy
题目信息
题号
2215D
时间限制
5 s
内存限制
1024 MB
标签
位运算
暴力
动态规划
贪心
模拟

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。