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
45
46
47
48
49
50
51
52
53
#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define vc vector
#define mt make_tuple
int main()
{
  int n,k;
  cin>>n>>k;
  int tab[n];
  set<int> S;
  vector<int> W;
  int licznik = 0;
  for(int i=0;i<n;i++)
  {
    cin>>tab[i];
    if(!S.count(tab[i]))
    {
      S.insert(tab[i]);
    }
    else
    {
      W.push_back(i);
    }
    if(S.size()==k)
    {
      licznik = i;
      break;
    }

  }
  if(S.size()<k)
  {
    cout<<-1;
    return 0;
  }
  int g = W.size();
  int suma = 0;
  for(int i = g-1;i>=0;i--)
  {
    suma+= (licznik-W[i]);
    licznik--;
  }
  cout<<suma;

  
  
}