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
#include <iostream>
#include <unordered_set>
#include <vector>

int main() {
    std::ios_base::sync_with_stdio(0);
    size_t n, k;
    std::cin >> n >> k;

    size_t last_idx_ok = 0;
    size_t result = 0;

    std::unordered_set<int> seen_brands;
    int x;
    std::cin >> x;
    seen_brands.insert(x);

    for (size_t idx = 1; idx < n && last_idx_ok + 1 < k; ++idx) {
        std::cin >> x;
        if (seen_brands.insert(x).second) {
            ++last_idx_ok;
            // std::cerr << "Moving idx=" << idx << " v=" << x << " to " << last_idx_ok << "; cost=" << (idx - last_idx_ok) << std::endl;
            result += idx - last_idx_ok;
        }
    }

    if (last_idx_ok + 1 < k) {
        std::cout << -1 << std::endl;
    } else {
        std::cout << result << std::endl;
    }
}