#include <bits/stdc++.h>
using namespace std;
int main()
{
cin.tie(nullptr)->sync_with_stdio(0);
int n, k;
cin >> n >> k;
vector<bool> used(n + 1);
int ptr = 0;
long long r = 0;
for (int i = 0; i < n && ptr < k; i++) {
int a;
cin >> a;
if (!used[a]) r += i - ptr++;
used[a] = true;
}
cout << (ptr < k ? -1 : r) << '\n';
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <bits/stdc++.h> using namespace std; int main() { cin.tie(nullptr)->sync_with_stdio(0); int n, k; cin >> n >> k; vector<bool> used(n + 1); int ptr = 0; long long r = 0; for (int i = 0; i < n && ptr < k; i++) { int a; cin >> a; if (!used[a]) r += i - ptr++; used[a] = true; } cout << (ptr < k ? -1 : r) << '\n'; } |
English