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<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
using ll=long long;
const int MAXN=(500005);
ll a[MAXN];
set<ll>s;
vector<ll>ans;
int n,k;
int main()
{ _
	cin>>n>>k;
	for(int i=1; i<=n; i++){
		cin>>a[i];
		if(s.find(a[i])==s.end()){
			ans.push_back(i);
			s.insert(a[i]);
		}
	}
	ll answer=0;
	if(ans.size()<k){
		cout<<"-1"; return 0;
	}
	for(int i=0; i<k; i++){
		answer+=(ans[i]);
		answer-=(i+1);
	}
	cout<<max((ll)-1, answer);
}