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
#include <bits/stdc++.h>
#define fi first
#define sc second
#define forn(i,p,k) for(int i=(p);i<=(k);++i)
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
bool us[500013];

int main()
{
    ios_base::sync_with_stdio(0);
    int n,k,cnt=0,a;
    long long res=0;
    cin>>n>>k;
    forn(i,1,n) {
        cin>>a;
        if(us[a] == 0) {
            cnt++;
            if (cnt <= k)   res += (i - cnt);
            us[a] = true;
        }
    }
    cout<<(cnt < k ? -1 : res);
}