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;
typedef long long ll;
#ifdef LOCAL
#define debug(...) __VA_ARGS__
#else
#define debug(...) {}
#endif
bool used[500001];
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie();
    cout.tie();
    ll i;
    ll n,m;
    cin>>n>>m;
    int tab[n+1];
    ll aktpos = 0;
    ll wy = 0;
    for (i = 1; i < n+1; i++){
        cin>>tab[i];
        if (!used[tab[i]] && aktpos < m){
            aktpos++;
            wy += i-aktpos;
        }
        used[tab[i]] = 1;
    }
    if (aktpos < m) cout<<-1<<"\n";
    else cout<<wy<<"\n";
    return 0;
}