| Codeforces Round 1138 (Div. 3) |
|---|
| Finished |
Returning home and wrapping themselves in a warm blanket, Sunny and Aubrey pulled out a box of old photos. In total, they have $$$m$$$ photos. The $$$i$$$-th photo has an emotional tone $$$a_i$$$: some pictures are very quiet and peaceful, while others are bright, noisy, and joyful.
The friends want to arrange an evening of memories and view the photos according to a special system. To do this, they choose a number of photos $$$n$$$ ($$$1 \le n \le m$$$) that is a power of two, select any $$$n$$$ photos from the box, and arrange them in a row: $$$p_1, p_2, \dots, p_n$$$.
Then, a tournament of memories is held, consisting of $$$\log_2 n$$$ rounds:
The process ends when a single photo remains. Help Sunny and Aubrey choose the number $$$n$$$, the photos themselves, and their initial order $$$p_1, p_2, \dots, p_n$$$ to maximize the total resonance over all rounds.
Each test consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $$$m$$$ and $$$k$$$ ($$$1 \le m \le 2 \cdot 10^5$$$, $$$0 \le k \le 10^9$$$) — the total number of photos and the cost per comparison.
The second line contains $$$m$$$ integers $$$a_1, a_2, \dots, a_m$$$ ($$$-10^9 \le a_i \le 10^9$$$) — the emotional tones of the photos.
The sum of $$$m$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print a single integer — the maximum possible total resonance over the entire comparison process.
The answer may not fit into a 32-bit signed integer.
54 310 20 5 153 0-5 0 51 4-28 00 0 0 0 1 1 1 12 0-1 -1
1610040
In the first test case use the order $$$20,5,15,10$$$. The first two pairs score $$$15-3=12$$$ and $$$5-3=2$$$. The final compares $$$20$$$ and $$$15$$$ for another $$$2$$$ points, giving $$$16$$$ in total.
In the second test case choose the photos with tones $$$-5$$$ and $$$5$$$. Their one comparison scores $$$10$$$.
In the third test case there is just one photo and no comparison, so the score is $$$0$$$.
In the fourth test case, pair each zero with a one in the first round. These four pairs score one point each. Only ones remain, so all later comparisons score zero. Each point requires a zero to lose, and there are only four zeros.
| Name |
|---|


