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
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
int arr[50012];
int ind[50012];
set<int>diff;
int n, k;

int how_long(){
    if(diff.size() < k) return (-1);
    int i=0, ans=0, si=0, j=0;
    set<int>was;
    while(si < k){
        was.insert(arr[i]);
        if(was.size() > si){
            ind[j]=i;
            ++j;
            ++si;
        }
        ++i;
    }
    for(i=0;i < k-1;++i){
        ans += (ind[i+1] - ind[0] - 1 - i);
    }
    return ans;
}

int main(){
    //bool are = true;
    scanf("%d%d", &n, &k);
    for(int i=0;i<n;++i){
        scanf("%d", &arr[i]);
        diff.insert(arr[i]);
    }
    printf("%d", how_long());
    return 0;
}