【Codeforces1077E】Thematic Contests(二分+枚举)

题目链接

E. Thematic Contests

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Copy

18
2 1 2 10 2 10 10 2 2 1 10 10 10 10 1 1 10 10

output

Copy

14

input

Copy

10
6 6 6 3 6 1000000000 3 3 6 6

output

Copy

9

input

Copy

3
1337 1337 1337

output

Copy

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个问题,每个问题都属于某个话题,现在要举行一系列比赛,每个比赛只能用同种类的问题而且问题数是前一场比赛的2倍(第一场比赛问题数可以任意),并且每两场比赛的问题不能有重复,要求使比赛总问题数最大。

【解题思路】

先记录每个问题的数量排个序,然后通过枚举第一场比赛的问题数来计算比赛总问题数。因为已知第一场比赛问题数,可以通过二分查找下一场比赛应有的问题数是否在数组中,当查找到一个位置之后将这个值删除就不影响下一次查找了,但这样交了会超时。所以查找的时候需要记录一下上一次查找到的位置,然后这次查找要在上一次的基础上找。

【代码】

#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
int main()
{
    int n,x;
    scanf("%d",&n);
    map<int,int>mp;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        mp[x]++;
    }
    map<int,int>::iterator it;
    vector<int>v;
    for(it=mp.begin();it!=mp.end();it++)
        v.push_back(it->second);
    sort(v.begin(),v.end());
    int ans=0;
    for(int i=1;i<=v[v.size()-1];i++)
    {
        int sum=0,q=0;
        for(int j=i;j<=v[v.size()-1];j*=2)
        {
            int p=lower_bound(v.begin()+q,v.end(),j)-v.begin();
            if(p!=v.size())
            {
                sum+=j;
                q=p+1;
            }
            else break;
        }
        ans=max(ans,sum);
    }
    printf("%d\n",ans);
}

猜你喜欢

转载自blog.csdn.net/qq_39826163/article/details/84337783
今日推荐