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
/*
 * =====================================================================================
 *
 *       Filename:  ora.cpp
 *
 *    Description:  
 *
 *        Version:  0.1.0
 *        Created:  07.12.2021
 *
 *         Author:  Michał Zagórski (zagura), <zagura6@gmail.com>
 *
 * =====================================================================================
 */
#include <cstdio>
#include <vector>
#include <cinttypes>

using namespace std;


int main() {
    size_t bottles, left_bottles;
    scanf("%zu %zu", &bottles, &left_bottles);
    vector<int> shelf (bottles);
    for (size_t i = 0; i < bottles; i++) {
        scanf("%d", &shelf[i]);
    }
    std::vector<bool> used(bottles + 1);
    used[shelf[0]] = true;
    int use_count = 1;
    uint64_t distance = 0;
    for (size_t i = 1; i < bottles; i++) {
        if (!used[shelf[i]]) {
            distance += i - use_count;
            use_count++;
            used[shelf[i]] = true;
        }
        if (use_count == left_bottles) {
            break;
        }

    }
    if (use_count == left_bottles) {
        printf("%" PRIu64 "", distance);
    } else {
        printf("-1");
    }
    return 0;
}