#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 5e5;
int n, k;
bool uzyta[MAX_N + 10];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
vector<int> pos;
pos.reserve(MAX_N + 10);
cin >> n >> k;
int a;
for (int i = 1; i <= n; ++i) {
cin >> a;
if (!uzyta[a]) {
uzyta[a] = true;
pos.push_back(i);
}
}
if (pos.size() < k) {
cout << -1 << '\n';
return 0;
}
long long int koszt = 0;
for (int i = 1; i <= k; ++i) koszt += pos[i - 1] - i;
cout << koszt << '\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 28 29 30 31 32 33 34 35 36 37 | #include <bits/stdc++.h> using namespace std; const int MAX_N = 5e5; int n, k; bool uzyta[MAX_N + 10]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); vector<int> pos; pos.reserve(MAX_N + 10); cin >> n >> k; int a; for (int i = 1; i <= n; ++i) { cin >> a; if (!uzyta[a]) { uzyta[a] = true; pos.push_back(i); } } if (pos.size() < k) { cout << -1 << '\n'; return 0; } long long int koszt = 0; for (int i = 1; i <= k; ++i) koszt += pos[i - 1] - i; cout << koszt << '\n'; } |
English