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

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n, k; cin >> n >> k;
    int a[n+5], wynik = 0, tab[k+5], tmp = 1;
    for (int i = 0; i < n; i++) cin >> a[i];
    tab[0] = 0;
    for (int i = 1; i < n; i++) {
        //cout << i << " " << a[i] << '\n';
        if (tmp == k) break;
        if (a[i-1] != a[i]) {
            bool x = false;
            for (int j = 0; j < tmp; j++) {
                //cout << a[tab[j]] << '\n';
                if (a[i] == a[tab[j]]) x = true;
            }
            if (!x) {
                //cout << "weszlo " << i << " " << a[i] << " " << wynik << '\n';
                wynik += i - tmp;
                tmp++;
                tab[tmp - 1] = i;
            }
            //cout << wynik << " " << tmp << " " << tab[tmp - 1] << '\n';
        }
    }
    if (tmp == k) cout << wynik << '\n';
    else cout << "-1" << '\n';
    return 0;
}