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
#include <iostream>
#include <vector>
#include <set>

using namespace std;

int nbottles, required;
vector<int> bottles;
set<int> already_different;

int main() {
    scanf("%d%d", &nbottles, &required);
    for (int i = 0; i < nbottles; ++i) {
        int now;
        scanf("%d", &now);
        bottles.push_back(now);
    }

    already_different.insert(bottles[0]);
    int first_different = 1;
    int insert_position = 1;

    unsigned long long result = 0;

    while (already_different.size() < required) {
        while (already_different.count(bottles[first_different]) > 0) {
            first_different++;
            if (first_different >= nbottles) {
                break;
            }
        }
        if (first_different >= nbottles) {
            break;
        }

        result += first_different - insert_position;
//        printf("move %d[%d] -> %d\n", first_different, bottles[first_different], insert_position);
        insert_position++;
        already_different.insert(bottles[first_different]);
    }

    if (first_different >= nbottles) {
        printf("-1\n");
    } else {
        cout << result << endl;
    }

    return 0;
}