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
/* 2021
 * Maciej Szeptuch
 */
#include <cstdio>

bool seen[524288];
int bottles;
int brand;
int found;
int pushing;
int wanted;
long long int result;

int main(void)
{
    scanf("%d %d", &bottles, &wanted);
    for(int b = 0; b < bottles && found < wanted; ++b)
    {
        scanf("%d", &brand);
        if(seen[brand])
            pushing += b < wanted;

        else
        {
            seen[brand] = true;
            ++found;
            if(b >= wanted)
                --pushing;
        }

        result += pushing;
    }

    printf("%lld\n", found == wanted ? result : -1);
    return 0;
}