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
34
35
36
37
38
39
40
41
#include <iostream>
#include <vector>

long long solution() {
    int n;
    int k;
    std::cin >> n >> k;

    std::vector<int> tab(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> tab[i];
    }

    std::vector<bool> got(n, false);
    got[tab[0] - 1] = true;
    int how_many = 1;

    long long result = 0;
    int i = 1;

    while (how_many < k && i < n) {
        if (!got[tab[i] - 1]) {
            got[tab[i] - 1] = true;
            result += static_cast<long long>(i - how_many);
            ++how_many;
        }
        ++i;
    }

    if (how_many < k) {
        return -1;
    }
    else {
        return result;
    }
}

int main() {
    std::cout << solution() << "\n";
    return 0;
}