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

using namespace std;

#define MAXN 500000
int a[MAXN+10];

int main(){
	int n, k, inspos=0;
	long long time = 0;
	set<int> lset;
	scanf("%d%d", &n, &k);
	for (int i = 0; i < n; ++i) {
		scanf("%d", a+i);
	}
	for (int i = 0; i < n && inspos < k; ++i) {
		if (lset.count(a[i]) == 0) {
			lset.insert(a[i]);
			time += (i-inspos);
			++inspos;
		}
	}
	if (inspos == k)
		printf("%lld\n", time);
	else
		printf("-1\n");
	return 0;
}