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
37
38
39
40
41
42
43
44
45
46
47
48
#include <cstdio>
#include <cstring>

using namespace std;


int t[500001];
int d[500001];

int main()
{
  int n, k;
  scanf("%d %d", &n, &k);
  
  memset(d, 0, sizeof(d));
  
  for (int i = 1; i <= n; i++)
  {
    scanf("%d", t + i);
  }
  
  long long ret = 0;
  int c = 0;
  for (int i = 1; i <= n; i++)
  {
    if (d[t[i]] == 0)
    {
      c++; 
      ret += i - c;
      if (c == k)
      {
        break;
      }
      d[t[i]] = 1;
    }
  }
  
  if (c == k)
  {
    printf("%lld\n", ret);
  }
  else
  {
    printf("-1\n");
  }
  
  return 0;
}