#include <bits/stdc++.h>
int main() {
std::ios_base::sync_with_stdio(0); std::cin.tie(NULL);
int n, k;
std::cin >> n >> k;
std::vector<int> v(n, 0);
for (int i = 0; i < n; i++) std::cin >> v[i];
std::vector<int> found(n+1, 0);
int res = 0;
int found_cnt = 0;
for (int i = 0; i < n && found_cnt < k; i++) {
if (!found[v[i]]) {
found[v[i]] = 1;
res += i - found_cnt;
found_cnt++;
}
}
if (found_cnt < k) {
std::cout << "-1\n";
} else
std::cout << res << "\n";
}
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 | #include <bits/stdc++.h> int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); int n, k; std::cin >> n >> k; std::vector<int> v(n, 0); for (int i = 0; i < n; i++) std::cin >> v[i]; std::vector<int> found(n+1, 0); int res = 0; int found_cnt = 0; for (int i = 0; i < n && found_cnt < k; i++) { if (!found[v[i]]) { found[v[i]] = 1; res += i - found_cnt; found_cnt++; } } if (found_cnt < k) { std::cout << "-1\n"; } else std::cout << res << "\n"; } |
English