#P1844F2. Min Cost Permutation (Hard Version)
Min Cost Permutation (Hard Version)
No submission language available for this problem.
Description
The only difference between this problem and the easy version is the constraints on and .
You are given an array of positive integers , and a (possibly negative) integer .
Across all permutations of the array , consider the minimum possible value of Find the lexicographically smallest permutation of the array that achieves this minimum.
A sequence is lexicographically smaller than a sequence if and only if one of the following holds:
- is a prefix of , but ;
- in the first position where and differ, the sequence has a smaller element than the corresponding element in .
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains two integers and (, ).
The second line of each test case contains integers ().
It is guaranteed that the sum of over all test cases does not exceed .
For each test case, output integers , the lexicographically smallest permutation of that achieves the minimum .
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains two integers and (, ).
The second line of each test case contains integers ().
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output integers , the lexicographically smallest permutation of that achieves the minimum .
Note
In the first test case, it can be proven that the minimum possible value of is , and the permutation is the lexicographically smallest permutation of that achieves this minimum: .
In the second test case, the minimum possible value of is , and is the lexicographically smallest permutation of that achieves this.
In the third test case, there is only one permutation .