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
#include <iostream>
#include "bits/stdc++.h"

using namespace std;
using vi = vector<int>;
using usi = unordered_set<int>;

int main() {
    int n, k;

    cin >> n >> k;
    vi v;
    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        v.push_back(x);
    }


    usi brands;
    int last_important =0;
    for(auto bottle: v) {
        brands.insert(bottle);
        last_important++;
        if(brands.size() == k) {
            break;
        }
    }
    if(brands.size() != k) {
        cout << -1;
        exit(0);
    }

    int result = 0;
    int begin = 0;
    for (int i = 0; i < v.size(); ++i) {
        if(brands.count(v[i]) > 0) {
            brands.erase(v[i]);
            result += (i - begin);
            begin += 1;
        }
    }
    cout <<  result;

    return 0;
}