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
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
using namespace std;



int main()
{
    std::ios_base::sync_with_stdio(false);
    int i, n, k, num;
    bool spr[500100] = { false };
    cin >> n >> k;
    int time = 0, set = 0;
    for (i = 0; i < n; i++) {
        cin >> num;
        if (spr[num] == false) {
            time += (i - set);
            set++;
            //cout << "i=" << i << " set=" << set << " time=" << time << '\n';
            spr[num] = true;
            if (k == set) {
                break;
            }
        }
    }
    if (k != set) {
        cout << "-1" << '\n';
    }
    else {
        cout << time << '\n';
    }
    return 0;
}