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
#include<bits/stdc++.h>
using namespace std;

int32_t main(){
    ios::sync_with_stdio(false);
    int n,k;
    cin >> n >> k;
    vector<int> tab(n);
    for(int i=0;i<n;i++) 
        cin >> tab[i];
    vector<int> seen(n+1);
    long long diff = 0, d=0;
    long long res = 0;
    for(int i=0;i<n;i++) {
        if(!seen[tab[i]]) {
            seen[tab[i]] = 1;
            diff++;
            res += d;
        }
        else
            d++;
        if(diff == k) {
            cout<<res;
            return 0;
        }
    }
    cout<<-1;
}