E.Thematic Contests
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 aiaiis 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, 88 problems 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.
一、原题地址
二、大致题意
现在有n个问题,每种问题有一种类型ai,现在要举办一系列的比赛,在每场比赛中只能选取类型相同的题目,并且现在有要求,这一系列的比赛中,后面的比赛题目数量要是前一次比赛的两倍。
询问最多能选取多少题目作为比赛用。
三、大致思路
题目的类型其实是没有用的,所以可以用map先处理出每种题目的数量。当晚想到这里然后开始写爆搜程序,TLE到结束。看了官方给出的题解,在这里要枚举每次能取的最大的那种题目数量,然后不断除二向前寻找可行的题目数,直到不满足或者遍历了全部的类型。
时间复杂度是O(nlogn)在这里的n并不是指所有题目的数量,而是指数量最大的类型的题目。
四、代码
#include <iostream>
#include<string.h>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
const LL inf=0x3f3f3f3f;
vector<int>q;
map<int,int>mmp;
int n,maxx;
int main()
{
scanf("%d", &n);
for (int i = 1;i <= n;i++)
{
int x;
scanf("%d", &x);
mmp[x]++;
}
map<int, int>::iterator it = mmp.begin(), en = mmp.end();
for (;it != en;it++)
{
maxx = max(maxx, it->second);
q.push_back(it->second);
}
sort(q.begin(),q.end());
int ans=0;
for(int i=1;i<=maxx;i++)
{
int sum=i;
int now=i;
int p=q.size()-1;
while(now%2==0&&p>0)
{
now/=2;
p--;
if(q[p]<now)break;
sum+=now;
}
ans=max(ans,sum);
}
printf("%d\n",ans);
}