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 <cstdlib>
#include <cassert>

static const size_t MAX_N = 5 * 100 * 1000;

bool seen[MAX_N + 1] = {0};

int main() {
    int n, k;
    assert(2 == scanf("%d %d\n", &n, &k));

    int unique_seen = 0;
    long long int seconds_needed = 0;
    for (int i = 0; i < n; i++) {
        int x;
        assert(1 == scanf("%d", &x));
        if (!seen[x]) {
            seconds_needed += i - unique_seen;

            seen[x] = true;
            unique_seen++;

            if (unique_seen == k) {
                printf("%lld\n", seconds_needed);
                return 0;
            }
        }
    }

    printf("-1\n");
    return 0;
}