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
#include <bits/stdc++.h>
using namespace std;

int main() {
   //freopen("in/1.in", "r", stdin);
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n, k;
	cin >> n >> k;
	vector<int> nbrs(n);
	for (auto& nb : nbrs) cin >> nb;

	set<int> used;
	int64_t ans = 0;
	int rolling = 0;
	for (auto& nb : nbrs) {
		if (used.count(nb)) {
			rolling++;
		} else {
			used.insert(nb);
			ans += rolling;
			k--;
			if (k==0)
			break;
		}
	}
	if (k!=0)
		ans = -1;
	cout << ans;
}