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

#define LL long long
#define f first
#define s second

const int t = 5e5 + 9;
int tab[t], zl[t];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < n; i ++) cin >> tab[i];
    int ile = 0;
    LL wynik = 0;
    for(int i = 0; i < n; i ++) {
        if(ile >= m) break;
        if(zl[tab[i]] == 0) {
            zl[tab[i]] = 1;
            wynik += i - ile;
            ile ++;
        }
    }
    if(ile < m) cout << -1;
    else cout << wynik;
    return 0;
}