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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <strings.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <tuple>
#include <cstdio>
#include <algorithm>    // std::sort

using namespace std;


//template<typename K, typename V>
//void print_vec(const vector< tuple<K, V> >& tupleVector) {
//    for (const auto& i : tupleVector) {
//      cout << get<0>(i) << ", " << get<1>(i) << endl;
//    }
//}


int main() {
    int n, k;
    scanf("%d %d\n", &n, &k);
    
    map<int, int> value2minpos;
    for (int i=0; i<n; ++i){
        int a;
        
        scanf("%d", &a);
        auto pos = value2minpos.find(a);
        if (pos==value2minpos.end()) 
            value2minpos[a] = i;        
    }
    
    vector< tuple<int, int> > minpos2value;
    for (auto it=value2minpos.begin(); it!=value2minpos.end(); it++) {
        minpos2value.push_back( make_tuple(it->second, it->first) );
    }
    sort(minpos2value.begin(), minpos2value.end());    
//    print_vec(minpos2value);

    if (minpos2value.size()<k) {
        printf("-1\n");
        return 0;
    }
    
    long cost = 0;
    for (int i=0; i<k; ++i) {
        cost += get<0>(minpos2value[i])-i;
    }
    
    printf("%ld\n", cost);
    return 0;
}