#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int n, k; scanf("%d %d", &n, &k); vector<int> a(n); vector<char> vis(n+1); int diff = 0; for (auto &i : a) { scanf("%d", &i); } ll res = 0; for (int i = 0; i < n && diff < k; i++) { if (vis[a[i]]) { continue; } res += i - diff; diff++; vis[a[i]] = 1; } printf("%lld", diff == k ? res : -1); }
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 | #include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int n, k; scanf("%d %d", &n, &k); vector<int> a(n); vector<char> vis(n+1); int diff = 0; for (auto &i : a) { scanf("%d", &i); } ll res = 0; for (int i = 0; i < n && diff < k; i++) { if (vis[a[i]]) { continue; } res += i - diff; diff++; vis[a[i]] = 1; } printf("%lld", diff == k ? res : -1); } |