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
#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector
#include <set>
#define MAXN 500005

using namespace std;

int n,k;
bool brand_exists[MAXN];
vector<int> a;
long long res;

int main() {
    scanf("%d%d", &n, &k);
    for (int i=0; i<n; ++i){
        int brand;
        scanf("%d", &brand);
        if (!brand_exists[brand]){
            brand_exists[brand] = true;
            a.push_back(i);
            if (a.size() == k){
                break;
            }
        }
    }

    if (a.size() < k){
        printf("-1");
        return 0;
    }

    int i=0;
    for (auto x: a) {
        res += x - i;
        i++;
    }
    
    printf("%lld", res);
    return 0;
}