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
35
36
37
38
39
40
41
42
43
44
45
#include <bits/stdc++.h>
#define ll long long

using namespace std;
const int mxn = 5e5 + 5;
int tab[mxn];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int n, k; cin>>n>>k;
    int poz = 2;
    vector<int> v(n+1);
    for(int i = 1; i <= n; i++) cin>>v[i];
    v[0] = v[1];
    ll ans = 0;
    int i = 2;
    tab[v[1]] = 1; 
    k--;
    while(k && i <= n)
    {
        if(v[i] == v[i-1])
        {
            i++;
        }
        else
        {
            if(i > n)
            break;
            if(!tab[v[i]])
            {
                ans+=(i-poz);
                poz++;
                k--;
                tab[v[i]] = 1;
            } 
            i++;
        }
    }
    if(k == 0) cout<<ans<<"\n";
    else cout<<"-1\n";
    return 0;
}