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

int main()
{
    int n, k;
    cin >> n >> k;
    vector<bool> first_occurrence(1 + n, true);
    vector<int> positions;
    for (int i = 0; i < n; ++i) {
        int a;
        cin >> a;
        if (first_occurrence[a]) {
            positions.push_back(i);
            first_occurrence[a] = false;
        }
    }
    if ((int) positions.size() < k) {
        cout << -1 << endl;
        return 0;
    }
    long long answer = 0;
    for (int i = 1; i < k; ++i) {
        answer += positions[i] - i;
    }
    cout << answer << endl;
    return 0;
}