1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
using namespace std;
 
int main(int argc, char* argv[]) {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    long long i, n, k, t, u = 0, p = 0;
    cin >> n >> k;
    vector<bool> w(n + 1);
    for (i = 0; i < n && u != k; i++) {
		cin >> t;
		if (w[t])
			p += i;
		else
			u++;
		w[t] = 1;
	}
	cout << (u == k ? (((i) * (i - 1)) - (u * (u - 1))) / 2 - p : -1) << "\n";
    return 0;
}