#include<bits/stdc++.h> using namespace std; constexpr int MAXN = 5e5 + 7; int n, k; int first_of_type[MAXN]; vector<int> v; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) { int a; cin >> a; if (first_of_type[a] == 0){ first_of_type[a] = i; v.push_back(i); } } sort(v.begin(), v.end()); if (v.size() < k){ cout << "-1\n"; return 0; } long long res = 0; for (int i = 0; i < k; i++) res += (long long) v[i]; res -= k * (k + 1) / 2; cout << res << "\n"; 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 | #include<bits/stdc++.h> using namespace std; constexpr int MAXN = 5e5 + 7; int n, k; int first_of_type[MAXN]; vector<int> v; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) { int a; cin >> a; if (first_of_type[a] == 0){ first_of_type[a] = i; v.push_back(i); } } sort(v.begin(), v.end()); if (v.size() < k){ cout << "-1\n"; return 0; } long long res = 0; for (int i = 0; i < k; i++) res += (long long) v[i]; res -= k * (k + 1) / 2; cout << res << "\n"; return 0; } |