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

using namespace std;

int main(){
    vector<int> tab;
    int n,k,a,c=0,wyn = 0;
    cin >> n >> k;
    vector<bool>in(n,false);
    for(int i = 0; i < n; i ++){
        cin >> a;
        tab.push_back(a);
    }
    for(int i=0;i<min(n,k);i++){
        if(in[tab[i]]){
            c = min(k - i,c + 1);
            k++;
        }
        else
            c = max(0,c - 1);
        in[tab[i]]= true;
        wyn += c;
    }
    if(k == n + 1)
        cout << "-1" << endl;
    else
        cout << wyn << endl;
}