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
#include <iostream>
#include <algorithm>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main() {
	int n,k,x;
	bool t[500001];
	for(int i=0;i<500001;i++){
		t[i]=false;
	}
	int ile=0;
	int res =0;
	scanf("%d%d", &n, &k);
	for(int i=0;i<n;i++){//3 3 3 1 2
		scanf("%d", &x);
		if(t[x]==false){
			t[x]=true;
			res+=(i-ile);
			ile++;
		}
		if(ile==k){
			printf("%d\n", res);
			return 0;
		}
		
	}



		printf("%d\n", -1);
	
  
	return 0;
}