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;
using ll = long long;

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int n, k; cin>>n>>k;
	vector<int> a(n); for(auto& i: a) cin>>i;
	vector<bool> taken(n+1);
	int cnt = 0;
	ll ans = 0;
	for(int i=0; i<n && cnt < k; ++i) {
		if(!taken[a[i]]) {
			taken[a[i]] = 1;
			ans += i - cnt;
			cnt++;
		}
	}
	if(cnt < k) {
		cout<<-1<<'\n';
	}
	else {
		cout<<ans<<'\n';
	}
}