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
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int N = 5e5 + 7;
bool was[N];

int main() {
    int n, k;
    scanf("%d%d", &n, &k);

    int pos = 0, diff = 0;
    ll res = 0;
    for (int i = 0; i < n && diff < k; ++i) {
        int a;
        scanf("%d", &a);
        if (!was[a]) {
            res += i - pos++;
            ++diff;
            was[a] = true;
        }
    }

    if (diff < k)
        res = -1;
    printf("%lld", res);

    return 0;
}