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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <tuple>
#include <random>
#include <cstdlib>
#include <time.h>
#include <limits>
#include <set>
#include <map>

#define INF 1000000000000000000

int main() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(0);
    int n, k, counter = 0, score = 0;
    std::cin >> n >> k;
    std::vector<int> vec(n);
    std::vector<int> taken(n, false);
    for (int i = 0; i < n; i++)
    {
        std::cin >> vec[i];
        if (counter != k && !taken[vec[i] - 1]) {
            score += i - counter;
            counter++;
            taken[vec[i] - 1] = true;
        }
    }
    if (counter == k) {
        std::cout << score;
    }
    else
        std::cout << -1;
}