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
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;

#define INF 500005

int n,k,a;
vector<int> shelf;
bool seen[INF];

#define MIN(_a,_b) (_a)<(_b)?(_a):(_b)

int main()
{
  scanf("%d %d", &n, &k);
  for(int i=0;i<n;i++) {
    scanf("%d", &a);
    shelf.push_back(a);
    seen[n]=0;
  }
  long long res = 0;
  long long steps = 0;
  for(int i=0;i<n;i++) {
    if(seen[shelf[i]]==0){
      steps+=i-res;
      res++;
      seen[shelf[i]]=1;
    }
    if(res==k) break;
  }
  if(res==k) printf("%lld\n", steps);
  else printf("-1\n");
  return 0;
}