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

using namespace std;

vector<int> tab;
bool set[500000];

int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; ++i) {
    int x;
    scanf("%d", &x);
    tab.push_back(x);
  }

  int unique = 0;
  int nonUnique = 0;
  int counter = 0;

  for (int i = 0; i < n; ++i) {
    if (!set[tab[i]]) {
      set[tab[i]] = true;
      unique++;
      counter += nonUnique;
      if (unique == k) {
        printf("%d\n", counter);
        return 0;
      }
    } else {
      nonUnique++;
    }
  }
  printf("-1\n");
}