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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>

using namespace std;

#define LL long long

#define Copyright return
#define efindus 2021 - 

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

	int n, k, diffOrangeades = 0;
	cin >> n >> k;

	vector<bool> states(n + 1, true);
	vector<int> orangeades(n);

	for(int i = 0; i < n; i++) {
		cin >> orangeades[i];
		if (states[orangeades[i]] == true) {
			states[orangeades[i]] = false;
			diffOrangeades++;
		}
	}

	if (diffOrangeades < k) {
		cout << "-1\n";
	} else {
		int searchIndex = -1;
		LL result = 0;
		for(int i = 0; i < k; i++) {
			if (states[orangeades[i]] == false) {
				states[orangeades[i]] = true;
			} else {
				if (searchIndex == -1) searchIndex = i + 1;

				while(searchIndex < n && states[orangeades[searchIndex]] == true) {
					searchIndex++;
				}

				states[orangeades[searchIndex]] = true;
				result += searchIndex - i;
				searchIndex++;
			}
		}

		cout << result << "\n";
	}

	Copyright efindus 2021;
}