#P9045. [PA 2021] Oranżada

[PA 2021] Oranżada

Problem Description

There is a row of nn bottles of orange juice, where the brand of the ii-th bottle is aia_i.

You may spend a cost of 11 unit to swap two adjacent bottles of orange juice.

Find the minimum cost to make the leftmost kk bottles have pairwise distinct brands.

Input Format

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

The second line contains nn integers a1,a2,,ana_1, a_2, \cdots, a_n.

Output Format

Output one integer in one line: if a solution exists, output the minimum cost; otherwise, output 1-1.

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 33 and 44, then swap the bottles at positions 44 and 55, then swap the bottles at positions 22 and 33, and finally swap the bottles at positions 33 and 44, for a total of 44 operations.

Sample #2 Explanation

Clearly, there is no solution.

Constraints

For 100%100\% of the testdata, 1k,ain5×1051 \leq k, a_i \leq n \leq 5 \times 10^5.

Translated by ChatGPT 5