1. 程式人生 > >【Codeforces1077E】Thematic Contests(二分+列舉)

【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);
}