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
#include <iostream>
#include <unordered_set>

int main() {
    int n;
    int k;
    std::unordered_set<int> used;

    std::cin >> n >> k;

    int time = 0;
    // Go through the bottles until we have what we need
    for(int i = 0 ; i < n && used.size() < k; ++i) {
        int a = 0;
        std::cin >> a;
        // A brand not already used
        if(!used.count(a)) {
            // The time to move the bottle down towards the used set
            time += i - used.size();
            used.insert(a);
        }
    }

    // Didn't find the k needed brands
    if(used.size() < k) {
        std::cout << -1 << std::endl;
    } else {
        std::cout << time << std::endl;
    }

    return 0;
}