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
51
52
53
54
55
#include <iostream>
#include <unordered_set>
#include <vector>

using namespace std;

bool got[500007];

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

    int n, k, x;
    long long res = 0;

    cin >> n >> k;

    int bottles_needed = k;

    for (auto i = 0; i < k; i++) {
        cin >> x;
        if (!got[x]) {
            got[x] = true;
            bottles_needed--;
        } else {
            res += (long long)bottles_needed;
        }
    }

    if (bottles_needed > 0) {
        int extra_bottle_num = 1;

        for (auto i = 0; i < n - k; i++) {
            cin >> x;
            if (!got[x]) {
                got[x] = true;
                bottles_needed--;

                res += (long long)(i + 1 - extra_bottle_num);
                extra_bottle_num++;

                if (bottles_needed == 0) {
                    break;
                }
            }
        }
    }

    if (bottles_needed == 0) {
        cout << res;
    } else {
        cout << -1;
    }

    return 0;
}