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

using namespace std;

/*
 5 3
 3 3 3 1 2

 3 2
 1 1 1
 */

int main() {
    int n, k;
    cin >> n >> k;
    int i;
    int arr[n];
    for (i = 0; i < n; i++) {
        cin >> arr[i];
    }

    int outside = k;
    int result = 0;


    std::unordered_set<int> uniqueBottles;
    for (i = 0; i < k; i++) {
        if (uniqueBottles.count(arr[i])) {
            while (outside < n && uniqueBottles.count(arr[outside])) {
                outside++;
            }
            if (outside >= n) {
                std::cout << -1 << std::endl;
                return 0;
            }
            result += (outside - i);
            uniqueBottles.insert(arr[outside]);
            outside++;
        } else {
            uniqueBottles.insert(arr[i]);
        }
    }
    std::cout << result << std::endl;
    return 0;
}