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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

int main(int argc, char** argv) {
	
	long n, k;
	long b[500001];
	set<long> a;
	long ile=0, powtarza=0, pop_size=0;
	long i, j;
	cin >> n >> k;
	for (long i=0; i<n; ++i)
	{
		cin >> b[i];
	}
	b[n]=-1;
	
	for (i=0; i<n; ++i)
	{
		if(a.size()==k) break; // znalazlo k butelek
		
		pop_size = a.size();
		a.insert(b[i]);
		if(pop_size==a.size()) //powtarza sie element
		{
			powtarza++;			
		}
		else {  // nie powtarza sie
			ile+= powtarza;			
		}
		
	}
	if(a.size()==k)
		cout << ile << endl;
	else cout << -1 << endl;
	
	return 0;
}