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>

int32_t main()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(NULL);
	std::cout.tie(NULL);

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

	std::set <int64_t> unique;
	std::vector <int64_t> a(n);
	std::vector <int64_t> unique_prefix(n);
	for (int64_t i = 0; i < n; ++i)
	{
		std::cin >> a[i];
		unique.insert(a[i]);
		unique_prefix[i] = unique.size();
	}

	// for (auto i : unique_prefix) std::cout << i << " ";
	// std::cout << '\n';

	if (k > (int64_t)unique.size())
	{
		std::cout << "-1\n";
	}
	else
	{
		int64_t last = std::find(unique_prefix.begin(), unique_prefix.end(), k) - unique_prefix.begin();
		std::vector <bool> remove(last + 1, 0);
		for (int64_t i = 1; i <= last; ++i)
		{
			if (unique_prefix[i] == unique_prefix[i - 1]) remove[i] = 1;
		}
		// for (auto i : remove) std::cout << i << " ";
		// std::cout << '\n';
		int64_t already_removed = 0;
		int64_t result = 0;
		for (int64_t i = last; i >= 0; --i)
		{
			if (remove[i])
			{
				result += (last - i - already_removed);
				already_removed += 1;
			}
		}
		std::cout << result << '\n';
	}

	return 0;
}