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

const int MAX_N = 500000;
int t1[MAX_N];
bool t2[MAX_N+1];

int main() {
	int n, k;
	cin >> n >> k;
	int ctrl = 0;
	for (int i = 0; i < n && ctrl < k; ++i) {
		int a;
		cin >> a;
		if (t2[a] == 0) {
			t2[a] = 1;
			t1[ctrl] = i;
			++ctrl;
		}
	}
	
	if (ctrl < k) {
		cout << "-1";
	} else {
		int z = 0;
		for (int i = 1; i < k; ++i) {
			z += t1[i] - i;
		}
		cout << z;
	}
		
	return 0;
}