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 <algorithm>
#include <cstdio>
#include <vector>
#include <set>

using namespace std;

bool a[5000001];

int main()
{
	long n, k, l;
	long long res = 0;
	scanf("%ld %ld", &n, &k);
	for (long i = 0; i < n && k > 0; ++i)
	{
		scanf("%ld", &l);
		if (a[l])
		  res += k;
		else
		{
		  --k;
			a[l] = true;
		}
	}

	printf("%lld", k > 0 ? -1 : res);
	return 0;
}