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
/*
 * ora.cpp
 *
 *  Created on: Dec 7, 2021
 *      Author: A.Mulawa
 */

#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <bits/stdc++.h>

int main(int argc, char **argv)
{
    const int MAX_NO = 5*100000 + 1;

    int n, k;
    scanf("%d %d", &n, &k);
    int t[MAX_NO];
    int r = 0;

    memset(t, 0, MAX_NO * sizeof(int));

    int res = 0;
    unsigned long sres = 0;
    for (int i = 0; i < n; i++)
    {
        int w;
        scanf("%d", &w);
        if (t[w] == 0)
        {
            t[w] = 1;
            ++r;
            if (r <= k)
            {
                sres += res;
            }
        }
        else
        {
            res++;
        }
    }

    if (r >= k)
    {
        printf("%lu", sres);
    }
    else
    {
        printf("%d", -1);
    }

};