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

using namespace std;

#define MAX_NUMBER_OF_ELEMENTS 500000

int main() {
    int n, k;
    int arr[MAX_NUMBER_OF_ELEMENTS];
    bool was[MAX_NUMBER_OF_ELEMENTS];
    int w = 0;
    cin >> n >> k;
    int out = -((k-1)*k)/2;
    for (int i = 0; i < n; i++) cin >> arr[i];
    for (int i = 0; i < n && w < k ; i++){
        if (!was[arr[i]]) {
            out += i;
            was[arr[i]] = true;
            w++;
        }
    }
    if (w != k) cout << -1;
    else cout << out;
    return 0;
}