#P9045. [PA 2021] Oranżada
[PA 2021] Oranżada
Problem Description
There is a row of bottles of orange juice, where the brand of the -th bottle is .
You may spend a cost of unit to swap two adjacent bottles of orange juice.
Find the minimum cost to make the leftmost bottles have pairwise distinct brands.
Input Format
The first line contains two integers .
The second line contains integers .
Output Format
Output one integer in one line: if a solution exists, output the minimum cost; otherwise, output .
5 3
3 3 3 1 2
4
3 2
1 1 1
-1
Hint
Sample #1 Explanation
The optimal plan is to first swap the bottles at positions and , then swap the bottles at positions and , then swap the bottles at positions and , and finally swap the bottles at positions and , for a total of operations.
Sample #2 Explanation
Clearly, there is no solution.
Constraints
For of the testdata, .
Translated by ChatGPT 5