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 <cstdio>
#include <vector>
using namespace std;

#define FOR(i,a,b) for(int (i)=(int)(a); (i)!=(int)(b); ++(i))

int n, k;
vector<bool> exists;
vector<int> P;
int main() {
    scanf("%d %d", &n, &k);
    exists.resize(n, false);
    FOR(i,0,n) {
        int a;
        scanf("%d", &a);
        --a;
        if (!exists[a]) {
            exists[a] = true;
            P.push_back(i);
        }
    }

    if (P.size() < k) {
        printf("-1\n");
        return 0;
    }

    int result = 0;
    FOR(i,0,k) result += (P[i] - i);
    printf("%d\n", result);
}