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
#include<cstdio>
#include<algorithm>
#define S 500007

using namespace std;
typedef long long ll;

bool T[S];

int main(void){
    int n,k;
    scanf("%d %d",&n,&k);
    int tmp = 0;
    ll ans = 0;
    int x;
    for(int i = 1; i <= n;i++){
        scanf("%d",&x);
        if(tmp < k && !T[x]){
            T[x] = 1;
            ans += (ll)(i - tmp - 1);
            tmp++;
        }
    }
    if(tmp < k){
        printf("-1");
    }else{
        printf("%lld",ans);
    }



    return 0;
}