codeforces1077E.Thematic Contests(思维+STL)

本文探讨了一个比赛问题的优化算法,旨在最大化使用具有相同主题的竞赛问题的数量,遵循特定的规则:后续比赛的问题数量必须是前一比赛的两倍。文章详细介绍了如何通过枚举最大类型题目的数量并向前寻找可行的题目数来解决这个问题,最终实现问题数量的最大化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值