Statement : C2. We Be Flipping (Hard Version)
This is the hard version of the problem. The difference between the versions is that in this version, you must maximise the sum. You can hack only if you solved all versions of this problem.
You have an array $a$ of length $n$ which consists of non-zero (but possibly negative) integers. You will perform the following operation at most $n$ times (possibly none):
- select an index $i$ ($1 \le i \le n$) such that $a_i \gt 0$
- then for each $j$ where $1 \le j \le i$ do $a_j := -a_j$.
Output a valid sequence of operations of length at most $n$ which $\color{red}{\text{maximises}}$ the sum of $a$ at the end.
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 testcase contains an integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the array $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$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
For each testcase, output a single integer $k$ ($0 \le k \le n$) — the number of operations you will perform.
Now output $k$ integers $b_1,\ldots,b_k$ where $b_i$ is the index you perform the $i$th operation on.
After performing the operations the sum of $a$ should be maximal.
Input
5
5
-1 -2 -3 -5 -4
4
5 7 10 19
5
1 -3 2 -1 10
4
16 -13 -18 -16
11
2 -10 -11 3 -10 15 7 18 16 17 -9
Output
0
0
2
1 3
0
6
6 3 1 5 4 7
Note
In the first testcase, no operations are possible.
In the second testcase, the sum is already maximal.
In the third testcase, operations are made as follows:
- $[1, -3, 2, -1, 10] \xrightarrow{i = 1} [\color{red}{-1}, -3, 2, -1, 10]$
- $[-1, -3, 2, -1, 10] \xrightarrow{i = 3} [\color{red}{1, 3, -2}, -1, 10]$
This has sum $11$, which can be proven to be maximal.