#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
set<int> left;
long long result = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (left.count(x) == 0 && left.size() < k) {
left.insert(x);
result = result + i - left.size() + 1;
}
}
if (left.size() == k) {
cout << result;
} else {
cout << -1;
}
}
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 main() { int n, k; cin >> n >> k; set<int> left; long long result = 0; for (int i = 0; i < n; i++) { int x; cin >> x; if (left.count(x) == 0 && left.size() < k) { left.insert(x); result = result + i - left.size() + 1; } } if (left.size() == k) { cout << result; } else { cout << -1; } } |
English