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

const int maxN = 5e5 + 10;
int N, K;
bool used[maxN];

int main() {
    int to_skip = 0;
    int taken = 0;
    long long result = 0;
    scanf("%d%d", &N, &K);

    for (int x, i = 0; i < N && taken < K; ++i) {
        scanf("%d", &x);
        if (used[x])
            to_skip++;
        else {
            taken++;
            result += to_skip;
            used[x] = 1;
        }
    }

    printf("%lld\n", taken == K ? result : -1LL);
    return 0;
}