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

constexpr int MAX_N = 5e5 + 7;
bool attendance[MAX_N];

int main() {
	ios_base::sync_with_stdio(false), cin.tie(NULL);
	
	int obecni = 0;
	int n, k;
	cin >> n >> k;
	int k_rep = k;
	int a;
	ll sum = 0ll;
	for (int i = 1; i <= n; ++i)
	{
		cin >> a;
		if (attendance[a] == false) ++obecni;
		if (k && attendance[a] == false)
		{
			sum += i;
			--k;
		}
		attendance[a] = true;
		if (i <= k_rep) sum -= i;
	}
	
	if (obecni >= k_rep && sum >= 0)
		cout << sum << '\n';
	else
		cout << -1 << '\n';
	
	return 0;
}