This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
using pii=pair<int,int>;
signed main(){
int n,q;
cin>>n>>q;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
map<int,int>mp;
vector<int>cnt;
for(int i=0;i<n;i++){
mp[a[i]]++;
}
int ans=0;
for(auto[f,s]:mp){
cnt.pb(s);
ans+=s*(s-1)/2+s;
}
sort(cnt.begin(),cnt.end(),greater<int>());
int inc1=cnt[0],inc2=cnt[0],sum=cnt[0];
for(int i=1;i<n;i++){
if(inc2<inc1)swap(inc1,inc2);
ans+=cnt[i]*(inc1+sum);
inc1+=sum+cnt[i];
inc2+=(i+1)*cnt[i];
sum+=cnt[i];
}
cout<<ans<<'\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |