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
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n, k;
    cin >> n >> k;
    
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    
    unordered_set<int> uniques;
    ll res = 0LL;
    for (int i = 0; i < n && (int)uniques.size() < k; ++i)
    {
        if (uniques.count(a[i]))
            continue;
        
        res += (ll)(i - uniques.size());
        uniques.insert(a[i]);
    }
    
    cout << ((int)uniques.size() == k ? res : -1) << "\n";

    return 0;
}