#include <iostream> #include <vector> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> but(n); for (int i = 0; i < n; i++) cin >> but[i]; vector<int> if_but(n, 0); int cnt = 0; long long ans = 0; for (int i = 0; i < n; i++) { if (if_but[but[i]]) continue; if_but[but[i]] = 1; ans += i - cnt; cnt++; } if (cnt < k) cout << -1; else cout << ans; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <iostream> #include <vector> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> but(n); for (int i = 0; i < n; i++) cin >> but[i]; vector<int> if_but(n, 0); int cnt = 0; long long ans = 0; for (int i = 0; i < n; i++) { if (if_but[but[i]]) continue; if_but[but[i]] = 1; ans += i - cnt; cnt++; } if (cnt < k) cout << -1; else cout << ans; } |