#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); int n, k; cin >> n >> k; long long res = -1LL*k*(k-1)/2; vector<int> V(n+1); for (int i = 0; i<n && k; i++) { int x; cin >> x; if (!V[x]++) { k--; res+=i; } } if (k) res =-1; cout << res << endl; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); int n, k; cin >> n >> k; long long res = -1LL*k*(k-1)/2; vector<int> V(n+1); for (int i = 0; i<n && k; i++) { int x; cin >> x; if (!V[x]++) { k--; res+=i; } } if (k) res =-1; cout << res << endl; } |