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
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <iterator>

int main()
{
    std::ios_base::sync_with_stdio(false);
    long long int k,n;
    std::cin>>n>>k;
    std::vector<bool> bottles(n, false);
    long long int l, i = 0;
    long long int sum = 0;
    while(l<k and i<n)
    {
        int bottle;
        std::cin>>bottle;
        if( bottles[bottle-1]) sum+=(i+1);
        else {l+=1; bottles[bottle-1] = true;}
        ++i;
    }
    if(l<k)
    {
        std::cout<<-1<<std::endl;
    }
    else
    {
        long long int x = i-k;
        long long int res = i*x - sum - 0.5*x*(x-1);
        std::cout<<res<<std::endl;
    }
    return 0;
}