1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <bits/stdc++.h>
using namespace std;

int n, k, zlicz[600000], a, poz, used, wyn;

signed main()
{
    ios_base::sync_with_stdio(false);
    cin >> n >> k;
    for (int i = 0; i < n; ++i) {
        cin >> a;
        if (zlicz[a] == 0) {
            wyn = wyn + i - poz;
            poz++;
            used++;
        }
        if (used >= k) break;
        zlicz[a]++;
    }
    if (used < k) cout << -1 << endl;
    else cout << wyn << endl;
}