#include <bits/stdc++.h>
using namespace std;
int main() {
int n,k;
scanf("%d %d",&n,&k);
vector <int> t(n);
for (int i=0; i<n; i++) scanf("%d",&t[i]);
int c=0;
long long w=0;
vector <bool> v(n+1,0);
for (int i=0; c<k && i<n; i++) {
if (!v[t[i]]) {
v[t[i]]=1;
w+=(long long)(i-c);
c++;
}
}
if (c<k) puts("-1");
else printf("%lld\n",w);
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 | #include <bits/stdc++.h> using namespace std; int main() { int n,k; scanf("%d %d",&n,&k); vector <int> t(n); for (int i=0; i<n; i++) scanf("%d",&t[i]); int c=0; long long w=0; vector <bool> v(n+1,0); for (int i=0; c<k && i<n; i++) { if (!v[t[i]]) { v[t[i]]=1; w+=(long long)(i-c); c++; } } if (c<k) puts("-1"); else printf("%lld\n",w); return 0; } |
English