CF2104B.Move to the End

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

Move to the End

You are given an array aa consisting of nn integers.

For every integer kk from 11 to nn, you have to do the following:

  1. choose an arbitrary element of aa and move it to the right end of the array (you can choose the last element, then the array won't change);
  2. print the sum of kk last elements of aa;
  3. move the element you've chosen on the first step to its original position (restore the original array aa).

For every kk, you choose the element which you move so that the value you print is the maximum possible.

Calculate the value you print for every kk.

Input

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

Each test case consists of two lines:

  • the first line contains one integer nn (1n21051 \le n \le 2 \cdot 10^5);
  • the second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ai1091 \le a_i \le 10^9).

Additional constraint on the input: the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output

For each test case, print nn integers. The ii-th of these integers should be equal to the maximum value you can print if k=ik=i.

Note

Let's consider the first test case from the statement:

  • when k=1k = 1, you can move the 66-th element to the end, the array becomes [13,5,10,14,8,13,15][13, 5, 10, 14, 8, 13, 15], and the value you print is 1515;
  • when k=2k = 2, you can move the 66-th element to the end, the array becomes [13,5,10,14,8,13,15][13, 5, 10, 14, 8, 13, 15], and the value you print is 13+15=2813 + 15 = 28;
  • when k=3k = 3, you can move the 44-th element to the end, the array becomes [13,5,10,8,15,13,14][13, 5, 10, 8, 15, 13, 14], and the value you print is 15+13+14=4215 + 13 + 14 = 42;
  • when k=4k = 4, you can move the 55-th element to the end, the array becomes [13,5,10,14,15,13,8][13, 5, 10, 14, 15, 13, 8], and the value you print is 14+15+13+8=5014 + 15 + 13 + 8 = 50;
  • when k=5k = 5, you can move the 11-st element to the end, the array becomes [5,10,14,8,15,13,13][5, 10, 14, 8, 15, 13, 13], and the value you print is 14+8+15+13+13=6314 + 8 + 15 + 13 + 13 = 63;
  • when k=6k = 6, you can move the 11-st element to the end, the array becomes [5,10,14,8,15,13,13][5, 10, 14, 8, 15, 13, 13], and the value you print is 10+14+8+15+13+13=7310 + 14 + 8 + 15 + 13 + 13 = 73;
  • when k=7k = 7, you can move the 66-th element to the end, the array becomes [13,5,10,14,8,13,15][13, 5, 10, 14, 8, 13, 15], and the value you print is 13+5+10+14+8+13+15=7813 + 5 + 10 + 14 + 8 + 13 + 15 = 78.

Samples

4
7
13 5 10 14 8 15 13
6
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
1
42
2
7 5
15 28 42 50 63 73 78 
1000000000 2000000000 3000000000 4000000000 5000000000 6000000000 
42 
7 12 

在线编程 IDE

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