#include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(0); int n, k; cin >> n >> k; vector<int> a(n), pier(n, -1); int powt = -1, rozne = 0; for (int i = 0; i < n; ++i) { cin >> a[i]; --a[i]; if (pier[a[i]] == -1) { pier[a[i]] = i; ++rozne; } else { if (powt == -1) powt = i; } } if (rozne < k) { cout << "-1"; return 0; } if (powt >= k || powt == -1) { cout << "0"; return 0; } for (int i = 0; i < powt; ++i) pier[a[i]] = -1; vector<int> posy; for (int i = 0; i < n; ++i) if (pier[i] != -1) posy.push_back(pier[i]); sort(posy.begin(), posy.end()); ll odp = 0; for (int i = powt, j = 0; i < k; ++i, ++j) odp += posy[j] - i; cout << odp; return 0; }
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(0); int n, k; cin >> n >> k; vector<int> a(n), pier(n, -1); int powt = -1, rozne = 0; for (int i = 0; i < n; ++i) { cin >> a[i]; --a[i]; if (pier[a[i]] == -1) { pier[a[i]] = i; ++rozne; } else { if (powt == -1) powt = i; } } if (rozne < k) { cout << "-1"; return 0; } if (powt >= k || powt == -1) { cout << "0"; return 0; } for (int i = 0; i < powt; ++i) pier[a[i]] = -1; vector<int> posy; for (int i = 0; i < n; ++i) if (pier[i] != -1) posy.push_back(pier[i]); sort(posy.begin(), posy.end()); ll odp = 0; for (int i = powt, j = 0; i < k; ++i, ++j) odp += posy[j] - i; cout << odp; return 0; } |