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
#include<bits/stdc++.h>
using namespace std;
void fastio()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
}
constexpr int X = 5e5 + 10;
bool A[X];
int main()
{
    fastio();
    int n, k;
    cin >> n >> k;
    int x;
    long long int sum = 0;
    int current = 0;
    for (int i = 0; i < n; i++)
    {
        cin >> x;
        if (!A[x] && (k > current))
        {
            A[x] = true;
            sum += i - current;
            current++;
        }
    }
    if (k != current)cout << "-1\n";
    else
        cout << sum << "\n";
}