#include <bits/stdc++.h> using namespace std; typedef long long LL; const LL N = 5e5 + 10; bool met[N + 1]; int main() { LL n, k, at = 0, res = 0; cin >> n >> k; for (LL i = 0; i < n; ++i) { LL x; cin >> x; if (at < k && !met[x]) { res += i - at++; met[x] = true; } } cout << (at < k ? -1 : res) << '\n'; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <bits/stdc++.h> using namespace std; typedef long long LL; const LL N = 5e5 + 10; bool met[N + 1]; int main() { LL n, k, at = 0, res = 0; cin >> n >> k; for (LL i = 0; i < n; ++i) { LL x; cin >> x; if (at < k && !met[x]) { res += i - at++; met[x] = true; } } cout << (at < k ? -1 : res) << '\n'; return 0; } |