Codeforces Round 1005 (Div. 2)
C
Remove the Ends
Codeforces
1300
原题
← BD →
翻译中…

You have an array $a$ of length $n$ consisting of non-zero integers. Initially, you have $0$ coins, and you will do the following until $a$ is empty:

翻译中…
  • Let $m$ be the current size of $a$. Select an integer $i$ where $1 \le i \le m$, gain $|a_i|$$^{\text{∗}}$ coins, and then:
  • if $a_i \lt 0$, then replace $a$ with $[a_1,a_2,\ldots,a_{i - 1}]$ (that is, delete the suffix beginning with $a_i$);
    • otherwise, replace $a$ with $[a_{i + 1},a_{i + 2},\ldots,a_m]$ (that is, delete the prefix ending with $a_i$).
翻译中…

Find the maximum number of coins you can have at the end of the process.

翻译中…

$^{\text{∗}}$Here $|a_i|$ represents the absolute value of $a_i$: it equals $a_i$ when $a_i \gt 0$ and $-a_i$ when $a_i \lt 0$.

翻译中…

Input

翻译中…

The first line contains an integer $t$ ($1 \le t \le 10^4$) — the number of testcases.

翻译中…

The first line of each testcase contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $a$.

翻译中…

The second line of each testcase contains $n$ integers $a_1,a_2,\ldots,a_n$ ($-10^9 \le a_i \le 10^9$, $a_i \ne 0$).

翻译中…

The sum of $n$ across all testcases does not exceed $2 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, output the maximum number of coins you can have at the end of the process.

翻译中…

Note

翻译中…

An example of how to get $23$ coins in the first testcase is as follows:

翻译中…
  • $a = [3, 1, 4, -1, -5, \color{red}{-9}] \xrightarrow{i = 6} a = [3, 1, 4, -1, -5] $, and get $9$ coins.
  • $a = [\color{red}{3}, 1, 4, -1, -5] \xrightarrow{i = 1} a = [1, 4, -1, -5] $, and get $3$ coins.
  • $a = [\color{red}{1}, 4, -1, -5] \xrightarrow{i = 1} a = [4, -1, -5] $, and get $1$ coin.
  • $a = [4, -1, \color{red}{-5}] \xrightarrow{i = 3} a = [4, -1] $, and get $5$ coins.
  • $a = [4, \color{red}{-1}] \xrightarrow{i = 2} a = [4] $, and get $1$ coin.
  • $a = [\color{red}{4}] \xrightarrow{i = 1} a = [] $, and get $4$ coins.
翻译中…

After all the operations, you have $23$ coins.

翻译中…

An example of how to get $40$ coins in the second testcase is as follows:

翻译中…
  • $a = [-10, -3, -17, \color{red}{1}, 19, 20] \xrightarrow{i = 4} a = [19, 20] $, and get $1$ coin.
  • $a = [\color{red}{19}, 20] \xrightarrow{i = 1} a = [20] $, and get $19$ coins.
  • $a = [\color{red}{20}] \xrightarrow{i = 1} a = [] $, and get $20$ coins.
翻译中…

After all the operations, you have $40$ coins.

翻译中…

样例

Input #1
3
6
3 1 4 -1 -5 -9
6
-10 -3 -17 1 19 20
1
1
Output #1
23
40
1
InputCopy OutputCopy
题目信息
题号
2064C
时间限制
3 s
内存限制
256 MB
标签
暴力
构造
动态规划
贪心

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