#include<bits/stdc++.h> #define fi first #define se second using namespace std; const int N=5e5; int tab[N+10]; bool vis[N+10]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n,k; cin>>n>>k; long long ans=0,cnt=0; for(int i=1;i<=n;i++) { cin>>tab[i]; if(k==0) continue; if(vis[tab[i]]) cnt++; else { vis[tab[i]]=true; k--; ans+=cnt; } } cout<<(k==0 ? ans:-1)<<"\n"; 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 28 29 30 31 32 | #include<bits/stdc++.h> #define fi first #define se second using namespace std; const int N=5e5; int tab[N+10]; bool vis[N+10]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n,k; cin>>n>>k; long long ans=0,cnt=0; for(int i=1;i<=n;i++) { cin>>tab[i]; if(k==0) continue; if(vis[tab[i]]) cnt++; else { vis[tab[i]]=true; k--; ans+=cnt; } } cout<<(k==0 ? ans:-1)<<"\n"; return 0; } |