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
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;

int main() {
    std::ios::sync_with_stdio(false);

    int numberOfBottles, numberOfBottleTypesNeeded;
    cin >> numberOfBottles >> numberOfBottleTypesNeeded;

    unordered_set<int> usedBottleTypes;
    vector<int> bottles(numberOfBottles);

    for (int i = 0; i < numberOfBottles; i++) {
        cin >> bottles[i];
    }

    long long int minTime = 0;

    int i = 1, j = 1;
    usedBottleTypes.insert(bottles[0]);

    while (i < numberOfBottles && usedBottleTypes.size() < numberOfBottleTypesNeeded) {
        while (j < numberOfBottles && usedBottleTypes.find(bottles[j]) != usedBottleTypes.end()) {
            j++;
        }
        
        if (j == numberOfBottles) {
            break;
        }

        usedBottleTypes.insert(bottles[j]);
        minTime += j - i;
        i++;
        j++;
    }
    
    if (usedBottleTypes.size() < numberOfBottleTypesNeeded) {
        minTime = -1;
    }

    cout << minTime;
}