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
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define nd second
#define st first
#define mp make_pair
int tab[500008];
bool ist[500008];
bool obec[500008];
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, k, suma=0, last=1;
    long long wyn=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        cin>>tab[i];
        if(obec[tab[i]]==0){
            obec[tab[i]]=1;
            suma+=1;
        }
    }
    if(suma<k)
    {
        cout<<-1;
        return 0;
    }
    for(int i=1;i<=n&&k!=0;i++)
        {
        if(ist[tab[i]]==0)
        {
            k--;
            ist[tab[i]]=1;
            wyn+=i-last;
            last=last+1;
        }
    }
    cout<<wyn;
}