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
#include <cstdio>
#include <algorithm>
#define lld long long int

using namespace std;

int k, n;
int a[500002];

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    int b;
    scanf("%d", &b);
    if (!a[b]) a[b] = i;
  }
  sort(a + 1, a + 1 + n);

  lld res = 0;
  int bt = 0;
  while (!a[bt]) bt++;
  if (n - bt + 1 < k) {
    printf("-1\n");
    return 0;
  }
  for (int i = 1; i <= k; i++) {
    res += a[bt] - i;
    bt++;
  }
  printf("%lld\n", res);
  return 0;
}