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
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, k; cin >> n >> k;
    vector<int>r(n);
    for (int i = 0; i < n; i++) cin >> r[i];
    vector<bool>t(n+1);
    int pos = 0;
    long long ans = 0;
    for (int i = 0; i < n; i++)
    {
        if (!k) break;
        if (!t[r[i]])
        {
            t[r[i]] = true;
            ans += (long long)(i-pos);
            pos++;
            k--;
        }
    }
    if (k) cout << -1;
    else cout << ans;
}