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 <cstdio>
#include <cstring>

#define MAX_N 500000

int main ()
{
  long long unsigned S [MAX_N/64+1];
  memset (S, 0, sizeof (S));
  int n, k, a;
  long long r = 0;
  scanf ("%i%i", &n, &k);
  for (int i=0, j=0; i<n; i++)
  {
    scanf ("%i", &a);
    if (!(S [a/64] & 1llu<<a%64))
    {
      r += i-j;
      if (++j==k)
      {
        printf ("%lli\n", r);
        return 0;
      }
      S [a/64] |= 1llu<<a%64;
    }
  }
  printf ("-1\n");
  return 0;
}