#include <bits/stdc++.h>
using namespace std;
int n,m,i,cnt,tot,a[500500];
bool u[500500];
long long r;
int main() {
scanf("%d%d",&n,&m);
for (i=0; i<n; i++) scanf("%d",&a[i]);
for (i=0; i<n; i++) if (!u[a[i]]) {
u[a[i]]=true;
r+=cnt;
if (++tot==m) break;
} else ++cnt;
if (i>=n) puts("-1"); else printf("%lld\n",r);
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <bits/stdc++.h> using namespace std; int n,m,i,cnt,tot,a[500500]; bool u[500500]; long long r; int main() { scanf("%d%d",&n,&m); for (i=0; i<n; i++) scanf("%d",&a[i]); for (i=0; i<n; i++) if (!u[a[i]]) { u[a[i]]=true; r+=cnt; if (++tot==m) break; } else ++cnt; if (i>=n) puts("-1"); else printf("%lld\n",r); return 0; } |
English