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 <bits/stdc++.h>
using namespace std;
int n, k, t1[500005], t2[500005];
long long out1, a;
bool b[500005];
int main()
{
    scanf("%d%d", &n, &k);
    for (int i = 0; i < n; i++)
    {
        scanf("%lld", &a);
        if(b[a])
        {
            ++t2[i - 1];
            --i;
            --n;
            continue;
        }
        b[a] = 1;
        t1[i] = a;
    }
    a = t2[0];
    if(n < k)
    {
        printf("-1");
        return 0;
    }
    for (int i = 1; i < k; i++)
    {
        out1 += a;
        a += t2[i];
    }
    printf("%lld", out1);
}