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

using namespace std;

int main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr);

    size_t n, k;
    cin >> n >> k;

    size_t have = 0;
    vector<bool> used(n);

    int64_t result = 0;

    for(size_t i = 0; i < n; i++) {
        size_t a;
        cin >> a; a--;

        if(have < k and not used[a]) {
            result += i - have;
            used[a] = true;
            have++;
        }
    }

    cout << (have < k ? -1 : result) << endl;
}