Optimal Partition
Contest: Codeforces Round 783 (Div. 1)
Difficulty rating: 2100
Tags: data structures, dp

You are given an array $$$a$$$ consisting of $$$n$$$ integers. You should divide $$$a$$$ into continuous non-empty subarrays (there are $$$2^{n-1}$$$ ways to do that).

Let $$$s=a_l+a_{l+1}+\ldots+a_r$$$. The value of a subarray $$$a_l, a_{l+1}, \ldots, a_r$$$ is:

- $$$(r-l+1)$$$ if $$$s>0$$$,
- $$$0$$$ if $$$s=0$$$,
- $$$-(r-l+1)$$$ if $$$s<0$$$.

Input format:
The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^5$$$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$).

The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$).

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

Output format:
For each test case print a single integer — the maximum sum of values you can get with an optimal parition.

Constraints:
Time limit: 4.0 s
Memory limit: 256.0 MB

Note:
Test case $$$1$$$: one optimal partition is $$$[1, 2]$$$, $$$[-3]$$$. $$$1+2>0$$$ so the value of $$$[1, 2]$$$ is $$$2$$$. $$$-3<0$$$, so the value of $$$[-3]$$$ is $$$-1$$$. $$$2+(-1)=1$$$.

Test case $$$2$$$: the optimal partition is $$$[0, -2, 3]$$$, $$$[-4]$$$, and the sum of values is $$$3+(-1)=2$$$.

Example input:
5
3
1 2 -3
4
0 -2 3 -4
5
-1 -2 3 -1 -1
6
-1 2 -3 4 -5 6
7
1 -1 -1 1 -1 -1 1

Example output:
1
2
1
6
-1

Editorial:
Let $$$dp_i$$$ be the answer for the first $$$i$$$ elements, and $$$v_{(i, j)}$$$ the value of the subarray $$$[i, j]$$$. With prefix sums it is easy to calculate $$$v_{(i, j)}$$$ quickly. With this we can get a $$$n^2$$$ solution: $$$dp_i=max(dp_j+v_{(j+1, i)})$$$ for $$$j<i$$$.
Lets call a segment winning, drawing, or losing, if the value of it is positive, $$$0$$$, or negative respectively. There is an optimal solution if the length of the drawing and losing segments are $$$1$$$. (The task is solvable without this observation, but it is harder to implement.) Proof: For a losing segment in the worst case we can get two losing segments with the same total length (the same value).
For a drawing segment with length $$$k$$$ if $$$k$$$ is even than the answer is the same if we split it into two segments with length $$$k/2$$$. For odd $$$k$$$ if the sum in the first $$$(k-1)/2$$$ or last $$$(k-1)/2$$$ elements is negative, than it is possible to increase the answer, otherwise one can split the segment into $$$(k-1)/2$$$, $$$1$$$, and $$$(k-1)/2$$$ long segments, and the answer for the new partition can't lessen.
So there is an optimal solution when only winning segments might be longer than $$$1$$$. It is easy to handle the $$$1$$$ long segments. For each $$$i$$$ ($$$1≤i≤n$$$) we have to find $$$j$$$, $$$0<=j<i$$$, where $$$v_{(j+1, i)}>0$$$, and $$$dp_j+v_{(j+1, i)}$$$ is maximal ($$$dp_0=0$$$).
If we store the prefix sums, and assign a permutation according to the prefix sums, than we can get all the positions, where $$$v_{(j+1, i)}>0$$$.
Than $$$v_{(j+1, i)}=i-j$$$.
So when we calculate $$$dp_i$$$, we should update with $$$dp_i-i$$$. This way, finding the optimal $$$j$$$ for each $$$i$$$ is just a prefix maximum. One can solve the problem with Fenwick tree or segment tree.
Final complexity is $$$O(n \cdot log(n))$$$.