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;

const int M_SIZE = 500001;

int n,k;

int a[M_SIZE];

set<int> values;

vector<long long> dist;

int main() {

    cin>>n>>k;

    for(size_t i=0; i<n; i++){
        cin>>a[i];
        if(values.count(a[i]) == 0){
            dist.push_back(i - dist.size());
        }
        values.insert(a[i]);
    }
    
    if(values.size() < k){
        cout<<-1;
        return 0;
    }

    long long result = 0;

    for(size_t i=0; i<k; i++)
        result += dist[i];

    cout<<result;

    return 0;
}