#include <bits/stdc++.h>
using namespace std;
int n, k, a, nextpos, doned;
long long int res;
bool done[500010];
int main() {
cin >> n >> k;
nextpos = 0;
for (int i=0; i<n; i++) {
cin >> a;
if (!done[a] && doned < k) {
res += i - nextpos;
done[a] = true;
nextpos++;
doned++;
}
}
if (doned < k) {
cout << -1 << endl;
} else{
cout << res << endl;
}
}
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; int n, k, a, nextpos, doned; long long int res; bool done[500010]; int main() { cin >> n >> k; nextpos = 0; for (int i=0; i<n; i++) { cin >> a; if (!done[a] && doned < k) { res += i - nextpos; done[a] = true; nextpos++; doned++; } } if (doned < k) { cout << -1 << endl; } else{ cout << res << endl; } } |
English