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
54
55
#include <iostream>
#include <vector>
#include <set>
#include <deque>

int main() {
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(0);

	int n, k;
	std::cin >> n >> k;

	std::vector<int> data(n);
	for (auto& i : data) std::cin >> i;

	std::set<int> allBottles;
	for (auto& i : data) allBottles.insert(i);
	if (allBottles.size() < k) {
		std::cout << -1;
		return 0;
	}

	int seconds = 0;
	std::set<int> beginningBottles;
	std::deque<int> duplicateBottles;

	for (int i = 0; i < k; i++) {
		if (beginningBottles.count(data[i])) 
			duplicateBottles.push_back(i);
		else
			beginningBottles.insert(data[i]);
	}

	for (int i = k; duplicateBottles.size() > 0; i++) {
		if (beginningBottles.count(data[i])) continue;
		else {
			seconds += (i - duplicateBottles.front());
			duplicateBottles.pop_front();
		}
	}

	std::cout << seconds;

	return 0;
}


/*
___
ABACAC


____
ABAAAAACDEF
*/