#P17331. 「TPOI-2A」Min Mex

    ID: 19609 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 显示难度普及 上传者: 标签>数学贪心洛谷原创O2优化构造洛谷月赛

「TPOI-2A」Min Mex

Problem Description

For an array aa, define mex⁡{a}\operatorname{mex}\{a\} as the smallest positive integer that does not appear in aa.

Esc gives you an array aa of length nn. You may perform the following operation any number of times:

  • Choose an integer ii with 1≤i≤n1 \le i \le n and an integer xx with 0≤x<ai0 \le x < a_i. Pay a cost of xx to change aia_i into ai−xa_i - x.

Find the minimum total cost to make mex⁡{a}=k\operatorname{mex}\{a\}=k. If it is impossible to make mex⁡{a}=k\operatorname{mex}\{a\}=k no matter how many operations you perform, output −1-1.

Input Format

This problem contains multiple test cases.

The first line contains a positive integer TT, meaning the number of test cases.

For each test case:

The first line contains two integers n,kn,k.

The second line contains nn integers aia_i.

Output Format

For each test case, output one integer per line representing the answer.

5
4 3
2 3 4 5
3 1
3 2 1
3 2
3 2 1
4 3
1 1 2 4
5 3
1 1 1 1 1
2
-1
1
0
-1

Hint

[Sample #1 Explanation]

For the first test case, you can change the original array to 2 1 4 5, with a cost of 22.

For the second test case, it is clear that there is no valid solution.

[Constraints]

This problem uses bundled testdata and enables subtask dependencies.

Subtask\text{Subtask} Score Special property Dependencies
11 3030 All aia_i are pairwise distinct None
22 n≤8n \le 8 ^
33 4040 None 1,21,2

For 100%100\% of the testdata, it is guaranteed that 1≤T≤1031\le T\le 10^3, 1≤k,n≤2×1051\le k,n\le2\times10^5, ∑n≤106\sum n \le 10^6, and 1≤ai≤1091\le a_i\le10^9.

Translated by ChatGPT 5