Codeforces Round 1049 (Div. 2)
C
Ultimate Value
Codeforces
1500
原题
← BD →
翻译中…

Let's define a function $f(a)$ for an array $a$ of length $n$ as $$f(a) = \textrm{cost} + (a_1 - a_2 + a_3 - a_4 \cdots a_n)$$ where $\textrm{cost}$ is zero initially.

翻译中…

Now consider the scenario where Alice and Bob are given an array $a$ of length $n$. They play a game taking at most $10^{100}$ turns alternately with Alice going first.

翻译中…

In each turn, they must perform any one (only one) of the following operations:

翻译中…
  • End the game for both Alice and Bob.
  • Choose two indices $l,r$ with $1 \le l \le r \le n$ and swap $a_l$ and $a_r$; this adds $(r - l)$ to the $\textrm{cost}$.
翻译中…

Assume that Alice tries to maximize $f(a)$ and Bob tries to minimize it.

翻译中…

Your task is to determine the final value of $f(a)$ assuming both players play optimally.

翻译中…

Input

翻译中…

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

翻译中…

The first line of each test case contains a single integer $n$ ($1 \le n \le 2\cdot10^5$) — the length of the array $a$.

翻译中…

The second line contains $n$ integers $a_1,a_2,a_3,\ldots,a_n$ ($1 \le a_i \le 10^9$) — the elements of the array $a$.

翻译中…

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

翻译中…

Output

翻译中…

For each testcase, output a single integer — the final value of $f(a)$ assuming both players play optimally.

翻译中…

Note

翻译中…

For the first testcase, it is optimal for Alice to end the game on her first turn.

翻译中…

So the final value of $\textrm{cost} = 0$ and $f(a) = 0 + 1000 - 1 = 999$.

翻译中…

For the fourth testcase, it is optimal for Alice to swap $a_1$ and $a_6$, and then it is optimal for Bob to end the game on his first turn.

翻译中…

So the final value of $\textrm{cost} = 5$ and $f(a) = 5 + 15 - 14 + 1 - 14 + 1 - 1=-7$.

翻译中…

样例

Input #1
5
2
1000 1
5
9 9 9 9 9
4
7 1 8 4
6
1 14 1 14 1 15
9
31 12 14 22 89 6 78 25 91
Output #1
999
13
12
-7
265
InputCopy OutputCopy
题目信息
题号
2140C
时间限制
2 s
内存限制
256 MB
标签
数据结构
博弈论
贪心

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