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
44
45
46
47
48
#include <iostream>
#include <vector>
#include <set>

using namespace std;

int main(){
	int n,k;
	cin>>n>>k;
	vector<int> bootles;
	for(int i=0;i<n;i++){
		int ai;
		cin>>ai;
		bootles.push_back(ai);
	}
	set<int> drinksBrands;
	//////////////////////////
	int bootlesCounter=0;
	long long timeCounter=0;
	int lastIndex=0;
	for(int i=0;i<k;i++){
		if(drinksBrands.find(bootles[i])==drinksBrands.end()){
			drinksBrands.insert(bootles[i]);
			bootlesCounter++;
		}
		else{
			//moving bootles
			lastIndex = max(lastIndex,i);
			while(lastIndex < n && drinksBrands.find(bootles[lastIndex])!=drinksBrands.end())
				lastIndex++;
			if(lastIndex < n && drinksBrands.find(bootles[lastIndex])==drinksBrands.end()){
				drinksBrands.insert(bootles[lastIndex]);
				bootlesCounter++;
				timeCounter+=lastIndex-i;
			}
			else break;
		}
		
	}
	
	if(bootlesCounter==k)
		cout<<timeCounter<<endl;
	else
		cout<<-1<<endl;
	
	
	return 0;
}