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
56
57
58
59
60
#include <cstdio>
#include <cassert>
#include <algorithm>
#include <vector>
#include <unordered_set>


using namespace std;

int n, k;
vector<int> t;
unordered_set<int> s;

long long find_result()
{
        int i = 0;
        int j;
        long long result = 0;

        while (i < n && s.count(t[i]) == 0) {
                s.insert(t[i]);
                i++;
        }

        j = i+1;

        while (s.size() < k) {
                while (j < n && s.count(t[j]) > 0) {
                        j++;
                }

                if (j >= n) {
                        return -1;
                }

                s.insert(t[j]);
                result += j-i;
                i++;
                j++;
        }

        return result;
}

int main()
{
        int i;

        scanf("%d %d\n", &n, &k);
        
        for(i = 0; i < n; ++i) {
                int a;
                scanf("%d", &a);
                t.push_back(a);
        }
 
        printf("%lld\n", find_result());

        return 0;
}