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 "stdio.h"
#include <array>
#include <cstdint>

int main() {
	int n, k;
	scanf("%d %d", &n, &k);
	std::array<bool, 500001> seen = {};
	int64_t res = 0;
	int good_count = 0;
	for (int i = 0; i < n; ++i) {
		int x;
		scanf("%d", &x);
		if (!seen[x]) {
			res += i - good_count;
			++good_count;
			if (good_count == k) {
				printf("%lld\n", res);	
				return 0;
			}
		}

		seen[x] = true;
	}

	printf("-1\n");	

	return 0;
}