1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <bits/stdc++.h>
using namespace std;

bool K [500001];

int main()
{
    int n, k, t = 0, sum = 0;
    scanf("%d%d", &n, &k);

    int T[n];

    for (int i = 0; i < n; i++)
        scanf("%d", &T[i]);

    for (int i = 0; i < n && t < k; i++) {

        if (!K[T[i]]) {
            K[T[i]] = 1;

            sum += i - t;
            t++;
        }
    }
    if (t != k)
        sum = -1;

    printf("%d", sum);

    return 0;
}