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

int n, k;
long long result = 0;
std::set<int> seen;

int main() {
  scanf("%d %d\n", &n, &k);
  for (int i=0; i<n; i++) {
    int c;
    scanf("%d", &c);
    if (seen.find(c) == seen.end()) { // new one
      result += (i - seen.size());
      seen.insert(c);
    }
    if (seen.size() == k) {
      printf("%ld\n", result);
      return 0;
    }
  }
  printf("-1\n");
  return 0;
}