#P9045. Distinct Orange Juice Brands

    ID: 22205 Type: Default 1000ms 256MiB

Distinct Orange Juice Brands

Distinct Orange Juice Brands

You are given a row of n bottles of orange juice. The brand of the i-th bottle is given by \(a_i\). You are allowed to swap two adjacent bottles at a cost of 1 unit per swap.

Your task is to compute the minimum cost required to rearrange the bottles so that the leftmost k bottles have pairwise distinct brands.

If it is impossible to achieve this condition, output -1.

Note: It can be shown that if you choose k bottles from positions \(i_1, i_2, \ldots, i_k\) (with \(i_1 < i_2 < \cdots < i_k\)) to be the ones in the first k positions, then the cost required is \(\sum_{j=1}^k (i_j - j)\). Thus, the problem reduces to selecting k bottles (each of a different brand) with the minimum possible sum of their original positions.

inputFormat

The first line contains two integers \(n\) and \(k\) (the number of bottles and the number of distinct bottles required in the leftmost segment, respectively).

The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) representing the brands of the bottles.

You may assume that \(1 \leq k \leq n \leq 10^5\) and \(1 \leq a_i \leq 10^9\).

outputFormat

Output a single integer representing the minimum cost to achieve the required arrangement. If it is impossible, output -1.

sample

4 3
1 2 1 3
1