#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
int numberOfBottles, kLeft;
cin >> numberOfBottles >> kLeft;
vector<bool> visited(numberOfBottles + 1);
long long answer = 0;
int lastPlace = 0;
for (int i = 0; i < numberOfBottles; i++) {
int value;
cin >> value;
if (visited[value]) {
continue;
}
visited[value] = true;
answer += (i - lastPlace++);
if (lastPlace == kLeft) {
break;
}
}
cout << (lastPlace == kLeft ? answer : -1);// << endl;
}
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 | #include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(0); int numberOfBottles, kLeft; cin >> numberOfBottles >> kLeft; vector<bool> visited(numberOfBottles + 1); long long answer = 0; int lastPlace = 0; for (int i = 0; i < numberOfBottles; i++) { int value; cin >> value; if (visited[value]) { continue; } visited[value] = true; answer += (i - lastPlace++); if (lastPlace == kLeft) { break; } } cout << (lastPlace == kLeft ? answer : -1);// << endl; } |
English