Good Bye 2025
C
First or Second
Codeforces
1200
原题
← BD →
翻译中…

There are $n$ children standing in a line, where the niceness of the $i$-th child is $a_i$. Santa is deciding which children belong on the nice list and which belong on the naughty list.

翻译中…

An integer $X$ is initially set to $0$. Santa will perform the following operation exactly $n-1$ times:

翻译中…
  • Choose the first or second child in line and remove them from the line.
  • Let $w$ be the niceness of the chosen child.
  • If the first child was chosen, add them to the nice list and add $w$ to $X$.
    • If the second child was chosen, add them to the naughty list and subtract $w$ from $X$.
翻译中…

Note that after all operations, exactly one child remains unassigned to a list.

翻译中…

Determine the maximum possible value of $X$ that Santa can obtain after all $n-1$ operations.

翻译中…

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$ ($2\le n\le 2\cdot 10^5$) — the number of children.

翻译中…

The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($-10^9\le a_i\le 10^9$) — the niceness of each child.

翻译中…

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

翻译中…

Output

翻译中…

For each test case, print a single integer — the maximum possible value of $X$ that Santa can obtain after all $n-1$ operations.

翻译中…

Note

翻译中…

In the first test case, Santa will perform exactly one operation. If he chooses the first child, he will add $a_1=2$ to $X$ to get $X=2$; if he chooses the second child, he will subtract $a_2=-3$ from $X$ to get $X=3$. Thus, the answer is $3$.

翻译中…

In the second test case, it is optimal to select the first child in all three operations. The value of $X$ will be $1+4+3=8$.

翻译中…

In the third test case, below is an optimal sequence of operations:

翻译中…

#TypeNiceness of remaining children$X$ after operation0—$[-4, 2, 3, -6]$$0$1First$[2, 3, -6]$$-4$2First$[3, -6]$$-2$3Second$[3]$$4$

翻译中…

In the fourth test case, below is an optimal sequence of operations:

翻译中…

#TypeNiceness of remaining children$X$ after operation0—$[-2, -3, 4, 10, -9]$$0$1Second$[-2, 4, 10, -9]$$3$2First$[4, 10, -9]$$1$3First$[10, -9]$$5$4First$[-9]$$15$

翻译中…

样例

Input #1
7
2
2 -3
4
1 4 3 4
4
-4 2 3 -6
5
-2 -3 4 10 -9
5
-12345678 -1000000000 -999999999 1000000000 -999999999
2
-7 1
5
7 -6 -1 -8 -8
Output #1
3
8
4
15
2987654321
-1
29
InputCopy OutputCopy
题目信息
题号
2178C
所属比赛
时间限制
2 s
内存限制
256 MB
标签
动态规划
贪心
模拟

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