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
#include<bits/stdc++.h>
using namespace std;

void solve() {
    int k, n;
    cin >> n >> k;
    vector<int> marki(n, 0);
    for (int i = 0; i < n; i++){
        cin >> marki[i];
    }
    int pos = -1;
    unordered_set<int> unik;
    vector<int> reps;
    while (unik.size() < k) {
        pos++;
        if (pos == n) {
            cout << -1 << "\n";
            return;
        } else if (unik.find(marki[pos]) != unik.end()) {
            reps.push_back(pos);
        } else {
            unik.insert(marki[pos]);
        }  
    }
    long long wyn = 0;
    for (int i = 0; i < pos - k + 1; i++) {
        wyn += (pos - i) - reps[reps.size()-1-i];
    }

    cout << wyn << "\n";
    
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}