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
32
33
#include <cstdio>
#include <set>
#define MAX_N 500010
using namespace std;

set<int> all, pos;

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

    long long res = 0;
    int mov = 0;

    for (int i = 0; i < n; i++) {
        scanf("%d", &x);

        if (all.find(x) != all.end()) {
            // repeated
            mov++;

        } else {
            // not repeated
            all.insert(x);
            res += mov;
            if ((int) all.size() == k) {
                printf("%lld", res);
                return 0;
            }
        }
    }
    printf("-1");
}