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
#include <bits/stdc++.h>
#include <iostream>
#include <unordered_set>

using namespace std;
long long n, k;
unordered_set<long long> brands;
long long b, counter, res;
int main() {
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        cin >> b;
        if (brands.find(b) == brands.end()) {
            res += (i - counter);
            counter++;
            brands.insert(b);
            if (counter == k) {
                cout << res << endl;
                return 0;
            }
        }
    }
    cout << "-1" << endl;
    return 0;
}