1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;
constexpr int MAX_N = 500007;
int marka[MAX_N];
bool wziete[MAX_N];
int n, k, rozne;
long long res = 0;
int main(){
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  cin >> n >> k;
  for(int i = 0, x;i < n;i++){
    cin >> x;
    if(k == 0) continue;
    if(wziete[x] == false){
      wziete[x] = true;
      k--;
      res += i - rozne++;
    }
  }
  if (k > 0) cout << "-1\n";
  else cout << res << '\n';
}