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

using namespace std;

int main() {
  int n, k;
  scanf("%d%d", &n, &k);

  int p = 0;
  long long int out = 0;
  set<int> s;

  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    if (p < k && s.find(x) == s.end()) {
      s.insert(x);
      out += i - p;
      p++;
    }
  }

  printf("%lld\n", p == k ? out : -1);
}