CF2171B.Yuu Koito and Minimum Absolute Sum

传统题 时间 2000 ms 内存 256 MiB 4 尝试 1 已通过 1 标签

Yuu Koito and Minimum Absolute Sum

The words in shoujo manga and love songs... they're always sparkling brightly. I don't need a dictionary to understand the meaning... but I've never felt them for myself.— Yuu Koito

Yuu is trying out the student council! Unfortunately, she is being forced to do clerical work... Touko wants her to fill out the blanks in various student council documents.

You are given a partially filled array of nonnegative integers a1,a2,,ana_1, a_2, \dots, a_n, where blank elements are denoted with 1-1. You would like to fill in the blank elements with nonnegative integers, such that the absolute value of the sum of the elements in its difference array is minimized.

More formally, let bb be the array of length n1n-1 such that bi=ai+1aib_i = a_{i+1} - a_i for all 1in11\leq i\leq n-1. Find the minimum possible value of b1+b2++bn1|b_1 + b_2 + \dots + b_{n-1}|, across all possible ways to fill in the blank elements of aa.

Additionally, output the array that achieves this minimum. If there are multiple such arrays, output the one that is lexicographically smallest^{\text{∗}}.

^{\text{∗}}For two arbitrary arrays cc and dd of length nn, we say that cc is lexicographically smaller than dd if there exists an index ii (1in1\leq i\leq n) such that cj=djc_j = d_j for all j<ij \lt i, and ci<dic_i \lt d_i. In other words, cc and dd differ in at least one index, and at the first index at which they differ, cic_i is smaller than did_i.

Input

The first line contains a single integer tt (1t1041 \leq t \leq 10^4)  — the number of test cases.

The first line of each test case contains a single integer nn (2n21052\leq n\leq 2\cdot 10^5).

The second line of each test case contains nn integers, a1,a2,,ana_1, a_2, \dots, a_n (1ai106-1\leq a_i \leq 10^6).

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

Output

For each test case, on the first line, output the minimum possible value of b1+b2++bn1|b_1 + b_2 + \dots + b_{n-1}|. Then, on the second line, output nn integers, the values of a1,a2,,ana_1, a_2, \dots, a_n in the lexicographically smallest array achieving this minimum.

Note

In the first example, we fill in the array a=[2,0,7,1]a = [2, 0, 7, 1], which yields the difference array b=[2,7,6]b = [-2, 7, -6].

The absolute value of the sum of the elements in bb is 11. It can be proven that this is the minimum possible. Furthermore, it can be proven that this is the lexicographically smallest array aa that achieves this minimum.

Samples

6
4
2 -1 7 1
4
-1 2 4 -1
8
2 -1 1 5 11 12 1 -1
3
-1 -1 -1
3
2 5 4
2
-1 5
1
2 0 7 1
0
0 2 4 0
0
2 0 1 5 11 12 1 2
0
0 0 0
2
2 5 4
0
5 5

在线编程 IDE

建议全屏模式获得最佳体验