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
#include <cstdio>
#include <vector>

int main()
{
    int n, k;
    scanf("%d%d", &n, &k);
    std::vector<int> a(n);
    for (int i=0; i<n; ++i) {
        scanf("%d", &a[i]);
    }

    long result = 0;
    int target = 0;
    std::vector<bool> t(n+1, false);
    for (int i=0; i<n; ++i) {
        if (!t[a[i]]) {
            t[a[i]] = true;
            result += i - target;
            if (++target == k) {
                printf("%ld\n", result);
                return 0;
            }
        }
    }
    printf("-1\n");
}