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
//============================================================================
// Name        : 1b-ora.cpp
//============================================================================

#include <iostream>
#include <set>
using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	set<int> different;
	unsigned int n, k, a, moves = 0, result = 0;
	cin >> n;
	cin >> k;

	cin >> a;
	different.insert(a);

	for (unsigned int i = 1; i < n; ++i) {
		cin >> a;
		if (different.count(a) > 0) {
			moves++;
		} else {
			different.insert(a);
			result += moves;
			if (different.size() == k) {
				break;
			}
		}
	}
	if (different.size() == k) {
		cout << result << endl;
	} else {
		cout << "-1" << endl;
	}

	return 0;
}