#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define ST first #define ND second #define SIZE(a) ((int)a.size()) int main() { ios_base::sync_with_stdio(0); int n, k; cin >> n >> k; vector<bool> visited(n); int total = 0; ll res = 0; for(int i=0; i < n; i++) { int a; cin >> a; a--; if(total < k && !visited[a]) { visited[a] = true; res += i-total; total +=1; } } if(total < k) { cout << "-1\n"; } else { cout << res << "\n"; } }
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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define ST first #define ND second #define SIZE(a) ((int)a.size()) int main() { ios_base::sync_with_stdio(0); int n, k; cin >> n >> k; vector<bool> visited(n); int total = 0; ll res = 0; for(int i=0; i < n; i++) { int a; cin >> a; a--; if(total < k && !visited[a]) { visited[a] = true; res += i-total; total +=1; } } if(total < k) { cout << "-1\n"; } else { cout << res << "\n"; } } |