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

using namespace std;

int main()
{
int N,K,k,a,i,v;
int *otype;
long long int T;

	scanf("%d %d",&N,&K);
	
	otype = (int*) malloc(N*sizeof(int));
	for (i = 0; i < N; i++) otype[i] = 0;
	a = i = k = 0;
	T = 0;
	while (k < K && i < N)
	{
		scanf("%d",&v);
		if (otype[v-1] == 0)
		{
			k++;
			otype[v-1] = 1;
			if (a > 0)
			{
				T += i - a;
				a++;
			}
		}
		else if (a == 0)
		{
			a = i;
		}
		i++;
	}
	free(otype);

	if (k < K) printf("-1\n");
	else printf("%lld\n",T);
	
	return 0;
}