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

using namespace std;

long long il, k, pom, wynik;
bool spis [1000000];
vector <long long> lista;


int main()
{
    cin >> il >> k;
    for (int q = 1; q <= il; q++){
        cin >> pom;
        if (!spis[pom]){
            spis[pom] = 1;
            lista.push_back(q-1);
        }
    }
    if (k > lista.size()){
        cout << -1;
        return 0;
    }
    for (int q = 0; q < k; q++){
        wynik += lista[q]-q;
    }
    cout << wynik;

}