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
#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
	int n, k;
	scanf("%d %d", &n, &k);
	std::vector<bool> is_brand_in_place;
	is_brand_in_place.assign(n, false);
	int number_of_brands_in_place = 0;
	long long result = 0;
	for (int i = 0; i < n; i++)
	{
		int brand;
		scanf("%d", &brand);
		brand--;
		if (!is_brand_in_place[brand])
		{
			result += i - number_of_brands_in_place;
			number_of_brands_in_place++;
			is_brand_in_place[brand] = true;
			if (number_of_brands_in_place == k)
				break;
		}
	}
	if (k > number_of_brands_in_place)
		printf("-1\n");
	else
		printf("%lld\n", result);
	return 0;
}