E. Thematic Contests
time limit per test:1 second memory limit per test:256 megabytes
Polycarp has prepared nn competitive programming problems. The topic of the ii-th problem is aiai, and some problems' topics may coincide.
Polycarp has to host several thematic contests. All problems in each contest should have the same topic, and all contests should have pairwise distinct topics. He may not use all the problems. It is possible that there are no contests for some topics.
Polycarp wants to host competitions on consecutive days, one contest per day. Polycarp wants to host a set of contests in such a way that:
- number of problems in each contest is exactly twice as much as in the previous contest (one day ago), the first contest can contain arbitrary number of problems;
- the total number of problems in all the contests should be maximized.
Your task is to calculate the maximum number of problems in the set of thematic contests. Note, that you should not maximize the number of contests.
Input
The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of problems Polycarp has prepared.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) where aiai is the topic of the ii-th problem.
Output
Print one integer — the maximum number of problems in the set of thematic contests.
Examples
input
18
2 1 2 10 2 10 10 2 2 1 10 10 10 10 1 1 10 10
output
14
input
10
6 6 6 3 6 1000000000 3 3 6 6
output
9
input
3
1337 1337 1337
output
3
Note
In the first example the optimal sequence of contests is: 22 problems of the topic 11, 44 problems of the topic 22, 88problems of the topic 1010.
In the second example the optimal sequence of contests is: 33 problems of the topic 33, 66 problems of the topic 66.
In the third example you can take all the problems with the topic 13371337 (the number of such problems is 33 so the answer is 33) and host a single contest.
算是一道比较有意思的题目了。tag是贪心和排序,但关键好像并不是他俩。
关键是遍历可能起始值时,使用二分完成二倍值的查找。复杂度n(logn)。
#include <bits/stdc++.h>
using namespace std;
int number[200010];
int save[200010];
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>number[i];
sort(number,number+n);
int m=0;
for(int i=0;i<n;i++){
save[m]++;
if(number[i+1]!=number[i])
m++;
}
sort(save,save+m);
//for(int i=0;i<m;i++)
//cout<<save[i]<<" ";
int mm=save[m-1],xx=0;
//cout<<mm;
for(int i=1;i<=mm;i++){
int temp=i,sum=0,j=0;
int *work=lower_bound(save,save+m,temp);
do{
sum+=temp;
temp*=2;
work=lower_bound(work+1,save+m,temp); //二分
}while(work<save+m && ++j<m);
xx=max(sum,xx);
}
cout<<xx;
return 0;
}