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
#include <cstdio>

int n, k, d;
long long ans;
int bottles[500005];
int counts[500005];
int in_pos[500005];

long long solve()
{
	long long result;
	int placed;
	int cur_pos;

	result = 0LL;

	in_pos[bottles[0]] = 1;
	placed = 1;
	cur_pos = 1;
	while (placed < k)
	{
		while (in_pos[bottles[cur_pos]]) cur_pos++;
		in_pos[bottles[cur_pos]] = 1;
		result += (long long)(cur_pos - placed);
		placed++;
	}

	return result;
}

int main()
{
	scanf("%d %d", &n, &k);
	for (int i = 0; i < n; ++i)
	{
		scanf("%d", &bottles[i]);
		counts[bottles[i]]++;
		if (counts[bottles[i]] == 1) d++;
	}
	if (d < k) ans = -1LL;
	else ans = solve();
	printf("%lld\n", ans);
	return 0;
}