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
#include <iostream>

using namespace std;

const int MAX_N = 5 * 100 * 1000 + 5;

bool bottle_used[MAX_N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, k;
  cin >> n >> k;

  for (int i = 0; i <= n; ++i) {
    bottle_used[i] = false;
  }

  int length_set = 0;

  long long result = 0;

  for (int i = 0; i < n; ++i) {
    int bottle;
    cin >> bottle;

    if (!bottle_used[bottle]) {
      bottle_used[bottle] = true;
      result += i - length_set;
      length_set++;

      if (length_set == k)
        break;
    }
  }

  if (length_set < k)
    cout << -1;
  else
    cout << result;
  
  return 0;
}