Under construction

Still being built. Things may change

Array

Time limit5sMemory limit256 MB

Summary
Pick one index to change to any integer y, paying |a_x - y|, so that the weighted sum of prefix distinct counts plus that cost is minimized.
Level

Hard8 of 10

Topics
Array, Hash map, Prefix sum, Greedy
Solved
No attempts yet

Problem

Chiaki has an array of integers a1,a2,…,ana_1,a_2,\dots,a_n. Chiaki can replace an element axa_x with another integer yy. Let the resulting array be b1,b2,…,bnb_1,b_2,\dots,b_n. Chiaki would like to know the minimum value of ∣ax−y∣+∑k=1nk⋅ck|a_x-y| + \sum_{k=1}^{n} k \cdot c_k, where ckc_k is the number of distinct integers in b1,b2,…,bkb_1,b_2,\dots,b_k.

Input

There are multiple test cases. The first line of the input contains an integer TT, indicating the number of test cases. For each test case:

The first line contains an integer nn (1≤n≤1061 \le n \le 10^6), the length of the array.

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤1091 \le a_i \le 10^9).

It is guaranteed that the sum of nn over all test cases will not exceed 10610^6.

Output

For each test case, output an integer denoting the answer.

Examples1

  1. Example 1

    Input
    1
    4
    1 2 3 4
    
    Expected output
    22