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
// Szymon Rusiecki (06.12.2021)
#include <bits/stdc++.h>

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

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

	int *tab = new int [n];
	int *poz = new int [n + 1];

	for (int i = 0; i <= n; ++i)
		poz[i] = -1;

	std::set<int> S;
	for (int i = 0; i < n; ++i) {
		std::cin >> tab[i];
		S.insert(tab[i]);
		if (poz[tab[i]] == -1)
			poz[tab[i]] = i;
	}

	if (S.size() < k) {
		std::cout << -1;
		delete [] tab;
		return 0;
	}

	std::sort(poz, poz + n + 1);
	int it = 0;
	while (poz[it] == -1)
		++it;

	long long output = 0;
	for (int i = 0; i < k; ++i, ++it)
		output += poz[it] - i;

	std::cout << output;

	delete [] tab;
	return 0;
}