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

using namespace std;

const int NMAX = 5 * 100 * 1000 + 7;
int n, k;

int a[NMAX];

bool used[NMAX];

void prep() {
	for(int i = 0; i < NMAX; i++)
		used[i] = false;
}

int main() {
	scanf("%d %d", &n, &k);
	for(int i = 0; i < n; i++)
		scanf("%d", &a[i]);

	long long int result = 0, last_searched = 0, unique_bottles = 0;
	for(long long int i = 0; i < k; i++) {
		for(last_searched = min(i, last_searched);last_searched < n; last_searched++) {
	//		printf("i = %d, last_searched = %d\n", i, last_searched);
			if(!used[a[last_searched]]){
				result += last_searched - i;
				unique_bottles++;
				used[a[last_searched]] = true;
	//			printf("BREAK");
				break;
			}
		}

	}
	//for (int i = 0; i <= n ; i++)
	//	printf("%d ", used[i]);
	//printf("unique = %d, last_searched = %d, result = %d", unique_bottles, last_searched, result);
	if(unique_bottles == k)
		printf("%lld", result);
	else
		printf("-1");
	return 0;
}