Array
Time limit5sMemory limit256 MB
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 . Chiaki can replace an element with another integer . Let the resulting array be . Chiaki would like to know the minimum value of , where is the number of distinct integers in .
Input
There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:
The first line contains an integer (), the length of the array.
The second line contains integers ().
It is guaranteed that the sum of over all test cases will not exceed .
Output
For each test case, output an integer denoting the answer.