#P17331. 「TPOI-2A」Min Mex
「TPOI-2A」Min Mex
Problem Description
For an array , define as the smallest positive integer that does not appear in .
Esc gives you an array of length . You may perform the following operation any number of times:
- Choose an integer with and an integer with . Pay a cost of to change into .
Find the minimum total cost to make . If it is impossible to make no matter how many operations you perform, output .
Input Format
This problem contains multiple test cases.
The first line contains a positive integer , meaning the number of test cases.
For each test case:
The first line contains two integers .
The second line contains integers .
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 .
For the second test case, it is clear that there is no valid solution.
[Constraints]
This problem uses bundled testdata and enables subtask dependencies.
| Score | Special property | Dependencies | |
|---|---|---|---|
| All are pairwise distinct | None | ||
| ^ | |||
| None |
For of the testdata, it is guaranteed that , , , and .
Translated by ChatGPT 5