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
#include <bits/stdc++.h>
using namespace std;
int tab[500005];
bool tab2[500005];
int main ()
{
	int n,k,l=0,lb=0;
	cin>>n>>k;
	for (int i=0;i<n;i++)
	{
		cin>>tab[i];
	}
	for (int i=0;i<n;i++)
	{
		if (tab2[tab[i]]==0) //we found the right item!!!!!!!!!!!!!!!!!!!!!!
		{
			tab2[tab[i]]=1;   //lest we find the same twice
			l=l+i-lb;        //we count
			lb++;			//we increase the quantity
		}
		if (lb==k) break;
	}
	if (lb<k) cout<<-1;
	else cout<<l;
	return 0;
}