Polycarp has prepared $n$ competitive programming problems. The topic of the $i$-th problem is $a_i$, 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 $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of problems Polycarp has prepared.

The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) where $a_i$ is the topic of the $i$-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: $2$ problems of the topic $1$, $4$ problems of the topic $2$, $8$ problems of the topic $10$.

In the second example the optimal sequence of contests is: $3$ problems of the topic $3$, $6$ problems of the topic $6$.

In the third example you can take all the problems with the topic $1337$ (the number of such problems is $3$ so the answer is $3$) and host a single contest.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Sorting

Greedy algorithms


Solutions:
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = {}
for i in range(n):
	if a[i] not in b.keys():
		b[a[i]] = 0
	b[a[i]] += 1
c = []
for i in b:
	c.append(b[i])
e = sorted(c, reverse=True)
d = [e[0]]
a = e[0]
for i in range(len(c) - 1):
	a = min(e[i + 1], a // 2)
	d.append(a * (2 ** (i + 2) - 1))
	if a == 0:
		break
print(max(d))


import sys
import math
from collections import defaultdict, deque
n = int(sys.stdin.readline())
dic = defaultdict(int)
arr = list(map(int, sys.stdin.readline().split()))
for i in range(n):
	dic[arr[i]] += 1
l = []
for i in dic:
	l.append(dic[i])
l.sort()
two = []
num = 1
i = 0
m = len(l)
count = 0
ans = 0
for i in range(1, l[-1] + 1):
	pos = len(l) - 1
	cur = i
	res = cur
	while cur % 2 == 0 and pos > 0:
		cur /= 2
		pos -= 1
		if l[pos] < cur:
			break
		res += cur
	ans = max(ans, res)
print(int(ans))


def E():
	n = int(input())
	topics = [int(x) for x in input().split()]
	counter = {}
	for e in topics:
		if e in counter:
			counter[e] += 1
		else:
			counter[e] = 1
	s = list(counter.values())
	s.sort(reverse=True)
	choose = [s[0]]
	act = s[0]
	for i in range(1, len(s)):
		aux = min(act // 2, s[i])
		if aux == 0:
			break
		choose.append(aux * (2 ** (i + 1) - 1))
		act = aux
	print(max(choose))
E()


from collections import Counter
n = int(input())
a = sorted(Counter(map(int, input().split(' '))).values(), reverse=True)
(res, k) = (a[0], a[0])
for i in range(1, min(len(a), 30)):
	k = min(k // 2, a[i - 1] // 2, a[i])
	res = max(res, k * (pow(2, i + 1) - 1))
print(res)


from collections import defaultdict
n = int(input())
a = list(map(int, input().split()))
m = defaultdict(int)
for i in range(n):
	m[a[i]] += 1
count = []
for k in m:
	count.append(m[k])
count.sort()
k = len(count) - 1
ans = 0
if k == 0:
	print(count[k])
	return
for i in range(count[-1], 0, -1):
	tmp = i
	res = i
	pos = k
	while tmp % 2 == 0 and pos > 0:
		tmp = tmp // 2
		pos -= 1
		if count[pos] < tmp:
			break
		res += tmp
	ans = max(res, ans)
print(ans)


n = int(input())
a = [int(x) for x in input().split()]
dtt = dict()
for t in a:
	if t in dtt:
		dtt[t] += 1
	else:
		dtt[t] = 1
lts = [dtt[x] for x in dtt]
lts.sort(reverse=True)
mt = [lts[0]]
pl = lts[0]
for d in range(1, len(lts)):
	cl = min(pl // 2, lts[d])
	if cl == 0:
		break
	cs = cl * (2 ** (d + 1) - 1)
	mt.append(cs)
	pl = cl
print(max(mt))


from collections import Counter

def main():
	from sys import stdin, stdout
	it = iter(map(int, stdin.read().split()))
	n = next(it)
	topic_list = [next(it) for _ in range(n)]
	cnt_list = sorted(Counter(topic_list).values())
	mx_val = cnt_list[-1]
	res = 0
	for curr_val in range(1, mx_val + 1):
		curr_res = curr_val
		idx = len(cnt_list) - 1
		while curr_val % 2 == 0 and idx > 0:
			curr_val //= 2
			idx -= 1
			if cnt_list[idx] < curr_val:
				break
			curr_res += curr_val
		res = max(res, curr_res)
	stdout.write(str(res))
main()


from bisect import bisect_left, bisect_right
from collections import Counter as C
n_ = int(input())
l = sorted(C(map(int, input().split())).values())
n = len(l)
i = res = 0

def f(x):
	i = 0
	for m in range(33):
		t = x << m
		i = bisect_left(l, t, i)
		if i == n:
			return t - x
		i += 1
res = 0
for x in range(1, n_ + 1):
	res = max(res, f(x))
print(res)


t = int(input())
arr = list(map(int, input().split()))
mp = {}
dis = 0
for i in range(t):
	if arr[i] in mp:
		mp[arr[i]] += 1
	else:
		mp[arr[i]] = 1
		dis += 1
a = [0] * dis
j = 0
for i in mp:
	a[j] = mp[i]
	j += 1
a.sort()
k = a[-1]
ans = k
if k & 1:
	k -= 1
while k > 0:
	j = k
	dm = k
	j //= 2
	for i in range(dis - 2, -1, -1):
		if a[i] >= j:
			dm += j
		else:
			break
		if j & 1:
			break
		j //= 2
	if dm > ans:
		ans = dm
	k -= 2
print(ans)


p_line = [int(x) for x in input().strip().split(' ')]
size = p_line[0]
a = [int(x) for x in input().strip().split(' ')]
num_to_occ = {}
for x in a:
	if x not in num_to_occ:
		num_to_occ[x] = 0
	num_to_occ[x] += 1
topics = []
for (_, occ) in num_to_occ.items():
	topics.append(occ)
topics.sort(reverse=True)
ba = 0
for start in range(1, topics[0] + 1):
	my_a = start
	act = start
	for i in range(1, len(topics)):
		if act % 2 == 1:
			break
		act = act / 2
		if act > 0 and topics[i] >= act:
			my_a += act
		else:
			break
	ba = max(ba, my_a)
print(int(ba))


from collections import Counter
n = int(input())
a = list(map(int, input().split()))
N = 2 * 10 ** 5
store = [0] * (N + 2)
b = Counter(a)
for i in b:
	store[b[i]] += 1
for i in range(N, 0, -1):
	store[i] += store[i + 1]
i = N
ans = 0
maxi = 0
while i >= 1:
	j = i
	count = 1
	add = 0
	while True:
		if store[j] >= count:
			add += j
			maxi = max(maxi, add)
		elif store[j] < count and add > 0:
			break
		if j % 2 == 1:
			break
		j //= 2
		count += 1
	i -= 1
print(maxi)


def main():
	import sys
	input = sys.stdin.readline
	n = int(input())
	a = list(map(int, input().split()))
	dic = dict()
	for i in a:
		if dic.get(i) is None:
			dic[i] = 1
		else:
			dic[i] += 1
	b = list(dic.values())
	b.sort()

	def solve(first):
		x = first
		i = -1
		m = len(b)
		ret = 0
		while i + 1 < m:
			(l, r) = (i + 1, m - 1)
			while l <= r:
				mid = (l + r) // 2
				if b[mid] >= x:
					i = mid
					r = mid - 1
				else:
					l = mid + 1
			if i < l:
				break
			ret += x
			x *= 2
		return ret
	ans = 0
	for i in range(1, n + 1):
		ans = max(ans, solve(i))
	print(ans)
main()


from collections import Counter
n = int(input())
a = list(map(int, input().split()))
cnt = Counter(a)
arr = [b for (_, b) in cnt.items()]
arr.sort()
if len(arr) == 1:
	print(arr[0])
	exit(0)
mx = 0
for i in range(1, arr[-1] + 1):
	j = i
	mx_tmp = j
	idx = len(arr) - 2
	while j & 1 == 0 and idx > -1:
		j >>= 1
		if j <= arr[idx]:
			mx_tmp += j
			idx -= 1
		else:
			break
	mx = max(mx, mx_tmp)
print(mx)


from collections import *
from math import *

def ch(mx):
	if mx > n:
		return 0
	m = l[0] // (1 << mx - 1)
	for i in range(mx):
		m = min(m, l[i] // (1 << mx - i - 1))
	return m * ((1 << mx) - 1)
n = int(input())
mx = ceil(log(n, 2))
a = list(map(int, input().split()))
c = Counter(a)
l = list(c.values())
l.sort(reverse=True)
n = len(l)
ans = 1
for i in range(1, mx + 1):
	ans = max(ans, ch(i))
print(ans)


n = int(input())
nums = list(map(int, input().split()))
mapa = {}
for nu in nums:
	mapa[nu] = mapa.get(nu, 0) + 1
li = [mapa[x] for x in mapa]
li.sort(reverse=True)
a = li[0]
b = li[0] // 2
c = 1
while len(li) > c:
	if b == 0:
		break
	if li[c] >= b:
		silo = b * (2 ** (c + 1) - 1)
		a = max(a, silo)
		b = b // 2
		c += 1
	else:
		silo = li[c] * (2 ** (c + 1) - 1)
		a = max(a, silo)
		b = li[c] // 2
		c += 1
print(a)


n = int(input())
t = [int(l) for l in input().split(' ')]
topics = {}
for i in t:
	if not i in topics:
		topics[i] = 0
	topics[i] += 1
arr = []
for i in topics:
	arr.append(topics[i])
arr = sorted(arr)

def helper(k):
	if k > len(arr):
		return 0
	return min((arr[-1 - j] // 2 ** (k - j - 1) for j in range(k)))
print(max((helper(i) * (2 ** i - 1) for i in range(1, 18))))


n = int(input())
d = dict()
l = list(map(int, input().split()))
for i in range(n):
	if l[i] not in d:
		d.update({l[i]: 1})
	else:
		d[l[i]] += 1
if len(d) == 1:
	for j in d:
		print(d[j])
else:
	ans1 = 0
	if len(d) <= 44:
		a = []
		for j in sorted(d.values()):
			a.append(j)
			ans1 = 0
		for i in range(len(a)):
			e = a[i]
			ans = 0
			for j in range(i + 1, len(a)):
				if e * 2 <= a[j]:
					ans += 2 * e
					e *= 2
				else:
					break
			e = a[i]
			for j in range(i, -1, -1):
				if e % 2 != 0:
					break
				elif e <= a[j]:
					ans += e
					e = e // 2
				else:
					break
			ans1 = max(ans1, ans)
			e = a[i] - 1
			ans = 0
			for j in range(i + 1, len(a)):
				if e * 2 <= a[j]:
					ans += 2 * e
					e *= 2
				else:
					break
			e = a[i] - 1
			for j in range(i, -1, -1):
				if e % 2 != 0:
					break
				elif e <= a[j]:
					ans += e
					e = e // 2
				else:
					break
			ans1 = max(ans1, ans)
	t = 0
	ans = 0
	rt = 1
	for j in sorted(d.values(), reverse=True):
		if t == 0:
			ans += j
			prev = j
			t += 1
			rt = 1
		else:
			if t <= 44:
				rt = rt * 2
			e = prev // 2
			if j >= e:
				if e * 2 == prev:
					ans += e
					prev = e
				elif t <= 28 and e >= rt - 1:
					ans += e - rt + 1
					prev = e
				else:
					break
			elif t > 44 or (rt * 2 - 1) * j >= ans:
				ans = (rt * 2 - 1) * j
				prev = j
			else:
				break
			t += 1
	print(max(ans1, ans))


from sys import stdin, stdout
import collections
N = int(input())
arr = [int(x) for x in stdin.readline().split()]
seq = collections.Counter(arr)
seq = list(seq.values())
seq.sort(reverse=True)
L = 0
first = 0
res = 0
for i in range(len(seq)):
	if i == 0:
		L = 1
		first = seq[i]
	else:
		if first == 1:
			break
		if seq[i] >= first // 2:
			L += 1
			first = first // 2
		else:
			L += 1
			first = seq[i]
	s = first * (2 ** L - 1)
	res = max(res, s)
print(res)


from collections import defaultdict
from sys import stdin

class Solver:

	@staticmethod
	def build_dict(l):
		mp = defaultdict(int)
		for val in l:
			mp[val] = mp[val] + 1
		return mp

	@staticmethod
	def build_cnt_list(mp):
		lst = [v for (k, v) in list(mp.items())]
		return lst

	def __init__(self, l):
		self.__L = l

	@staticmethod
	def get_largest_power_of_2(n):
		for p in range(30, -1, -1):
			if 1 << p <= n:
				return 1 << p
		return 1

	def solve(self):
		mp = Solver.build_dict(self.__L)
		cnt_lst = Solver.build_cnt_list(mp)
		cnt_lst.sort()
		cnt_lst.reverse()
		ans = cnt_lst[0]
		for start_cand in range(cnt_lst[0], 0, -1):
			if start_cand % 2 == 0:
				ans2 = 0
				nxt = start_cand
				for cnt in cnt_lst:
					if cnt >= nxt:
						ans2 += nxt
						if nxt % 2 != 0:
							break
						nxt //= 2
					else:
						break
				ans = max(ans, ans2)
		return ans

def main():
	n = int(stdin.readline())
	inp = list(map(int, stdin.readline().split()))
	solver = Solver(inp)
	print(solver.solve())
main()


def fun(l, r, bound):
	global val
	ind = -1
	while l <= r:
		mid = (l + r) // 2
		if val[mid] >= bound:
			r = mid - 1
			ind = mid
		else:
			l = mid + 1
	return ind
n = int(input())
a = list(map(int, input().split()))
d = {}
for i in range(n):
	try:
		d[a[i]] += 1
	except:
		d[a[i]] = 1
val = list(d.values())
val.sort()
ans = 0
for i in range(1, 200100):
	temp = 0
	add = i
	ind = fun(0, len(val) - 1, add)
	while ind != -1:
		temp += add
		add = add * 2
		ind = fun(ind + 1, len(val) - 1, add)
	ans = max(ans, temp)
print(ans)


def main():
	buf = input()
	n = int(buf)
	buf = input()
	buflist = buf.split()
	a = list(map(int, buflist))
	appearance = {}
	for i in a:
		if not i in appearance:
			appearance.update({i: 1})
		else:
			appearance[i] += 1
	appearance = [[k, v] for (k, v) in dict(sorted(appearance.items(), key=lambda x: x[1])).items()]
	max_num = appearance[-1][1]
	pos = 0
	for i in range(1, max_num + 1):
		num = 0
		prob = i
		for j in range(pos, len(appearance)):
			if appearance[j][1] >= prob:
				num += prob
				prob *= 2
		if num > max_num:
			max_num = num
		while appearance[pos][1] == i and pos < len(appearance) - 1:
			pos += 1
	print(max_num)
main()


import sys
from math import *

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())
n = mints()
a = list(mints())
a.sort()
p = -1
c = -1
b = []
for i in range(len(a)):
	if a[i] != p:
		if c != -1:
			b.append((c, p))
		c = 1
		p = a[i]
	else:
		c += 1
if c != -1:
	b.append((c, p))
b.sort(reverse=True)
p = -1
r = 0
for i in range(len(b)):
	if p == -1:
		p = b[i][0]
	else:
		p = min(p // 2, b[i][0])
	if p != 0:
		r = max(r, p * (2 ** (i + 1) - 1))
print(r)


import bisect
import sys
input = sys.stdin.readline
n = int(input())
ar = list(map(int, input().split()))
dic = {}
for i in ar:
	if i in dic:
		dic[i] += 1
	else:
		dic[i] = 1
li = list(dic.values())
li.sort()
ans = max(li)
le = len(li)
for i in range(1, n + 1):
	l = 0
	r = le
	po = 0
	st = i
	while l != r:
		xx = bisect.bisect(li, st, l, r)
		if xx == l:
			l += 1
			po += 1
			st *= 2
		elif xx == le:
			if st > li[-1]:
				break
			elif st == li[-1]:
				po += 1
				break
		else:
			if st == li[xx - 1]:
				l = xx
			else:
				l = xx + 1
			po += 1
			st *= 2
	ans = max(ans, i * (2 ** po - 1))
print(ans)


n = int(input())
p = list(map(int, input().split()))
p.sort()
a = []
for i in range(n):
	if len(a) == 0 or p[i] != p[i - 1]:
		a.append(1)
	else:
		a[len(a) - 1] += 1
a.sort(reverse=True)
ans = a[0]
k = a[0]
for i in range(1, min(len(a), 17)):
	k = min(k // 2, a[i])
	ans = max(ans, k * (2 ** (i + 1) - 1))
print(ans)


n = int(input())
l = list(map(int, input().split()))
from collections import Counter
c = Counter(l)
L = [c[i] for i in list(c)]
L.sort(reverse=True)
(res, k) = (L[0], L[0])
for i in range(1, min(len(L), 30)):
	k = min(k // 2, L[i - 1] // 2, L[i])
	res = max(res, k * (pow(2, i + 1) - 1))
print(res)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
from collections import defaultdict
from itertools import permutations
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
a = [int(x) for x in input().split()]
dtt = dict()
for t in a:
	if t in dtt:
		dtt[t] += 1
	else:
		dtt[t] = 1
lts = [dtt[x] for x in dtt]
lts.sort(reverse=True)
mt = [lts[0]]
pl = lts[0]
for d in range(1, len(lts)):
	cl = min(pl // 2, lts[d])
	if cl == 0:
		break
	cs = cl * (2 ** (d + 1) - 1)
	mt.append(cs)
	pl = cl
print(max(mt))


from collections import Counter
n = int(input())
a = list(map(int, input().split()))
c = Counter(a)
l = sorted(c.values())
size = len(l)
ans = 0
for i in range(1, l[-1] + 1):
	temp_ans = 0
	cur = i
	pos = size - 1
	while cur <= l[pos] and pos >= 0:
		temp_ans += cur
		pos -= 1
		if cur & 1:
			break
		cur >>= 1
	ans = max(ans, temp_ans)
print(ans)


import sys
n = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
from collections import Counter
c = Counter(A)
LIST = list(c.values())
LIST.sort(reverse=True)
if len(LIST) == 1:
	print(LIST[0])
	sys.exit()
LL = len(LIST)
y = LIST[0]
ANS = y
if y % 2 == 1:
	y = y - 1
for x in range(y, 0, -2):
	an = 0
	for j in range(LL):
		if x % 2 ** j == 0 and LIST[j] >= x // 2 ** j:
			an += x // 2 ** j
		else:
			break
	if an > ANS:
		ANS = an
print(ANS)


n = int(input())
arr = list(map(int, input().split()))
d = {}
powers = []
for i in range(32):
	powers.append(2 ** i)
for i in arr:
	if i in d.keys():
		d[i] += 1
	else:
		d[i] = 1
arr1 = []
for i in d.keys():
	arr1.append(d[i])
arr1.sort(reverse=True)
maxx = arr1[0]
curr = arr1[0]
for i in range(1, len(arr1)):
	if curr > 2 * arr1[i]:
		maxx = max(maxx, arr1[i] * (powers[i + 1] - 1))
		curr = arr1[i]
	else:
		maxx = max(maxx, curr // 2 * (powers[i + 1] - 1))
		curr = curr // 2
	if curr == 0:
		break
print(maxx)


from collections import Counter
from bisect import bisect_left, bisect_right
import sys

def input():
	return sys.stdin.readline().rstrip()

def slv():
	n = int(input())
	a = list(map(int, input().split()))
	b = list(dict(Counter(a)).values())
	b.sort()
	ans = 0
	for k in range(1, n + 1):
		(tmp, idx, v) = (0, bisect_left(b, k), k)
		while idx < len(b):
			if b[idx] >= v:
				tmp += v
				v *= 2
			idx += 1
		ans = max(ans, tmp)
	print(ans)
	return

def main():
	t = 1
	for i in range(t):
		slv()
	return
main()


import sys
input = sys.stdin.readline

def main():
	n = int(input())
	a = list(map(int, input().split()))
	data_stat = {}
	for i in range(n):
		t = data_stat.get(a[i], 0) + 1
		data_stat[a[i]] = t
	current = sorted(data_stat.items(), key=lambda x: x[1])
	answer = 0
	for i in range(1, current[-1][1] + 1):
		pos = len(current) - 1
		cur = i
		res = cur
		while cur % 2 == 0 and pos > 0:
			cur //= 2
			pos -= 1
			if current[pos][1] < cur:
				break
			res += cur
		answer = max(answer, res)
	print(answer)
	return
main()


n = int(input())
a = [int(x) for x in input().split()]
dic = {}
for item in a:
	if item not in dic:
		dic[item] = 1
	else:
		dic[item] += 1
contests = []
answer = 0
for item in dic:
	contests.append(dic[item])
contests.sort(reverse=True)
for item in range(1, contests[0] + 1):
	i = 1
	counter = problem = item
	while i < len(contests):
		if problem % 2 == 0 and problem // 2 <= contests[i]:
			problem //= 2
			counter += problem
		else:
			break
		i += 1
	answer = max(answer, counter)
print(answer)


import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
DEBUG = False
if DEBUG:
	n = 2 * 10 ** 5
	a = [i for i in range(1, 100 + 1)] * 2000
c = {}
for aa in a:
	c[aa] = c.get(aa, 0) + 1
l = list(c.values())
l.sort()
ans = 0
for k in range(1, l[-1] + 1):
	score = k
	v = k
	pos = len(l) - 1
	while v % 2 == 0 and pos > 0:
		v //= 2
		pos -= 1
		if l[pos] < v:
			break
		score += v
	ans = max(ans, score)
print(ans)


from collections import Counter
n = int(input())
a = list(map(int, input().split()))
c = list(Counter(a).values())
c.sort(reverse=True)
best = 0
current = c[0]
for (i, val) in enumerate(c):
	pos = int(min(current / 2 ** i, val))
	if pos == 0:
		break
	current = pos * 2 ** i
	total = sum([current // 2 ** j for j in range(i + 1)])
	best = max(best, total)
print(best)


import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
from functools import lru_cache
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
if os.path.exists('out.txt'):
	sys.stdout = open('out.txt', 'w')

def input():
	return sys.stdin.readline()

def mapi(arg=0):
	return map(int if arg == 0 else str, input().split())
n = mapi()
a = list(mapi())
res = 0
mp = defaultdict(int)
for i in a:
	mp[i] += 1
b = sorted(mp.values())
maxn = max(b)
for i in range(maxn, 0, -1):
	tmp = i
	curmax = i
	j = len(b) - 1
	while j > 0 and tmp % 2 == 0:
		tmp //= 2
		j -= 1
		if b[j] >= tmp:
			curmax += tmp
		else:
			break
	res = max(res, curmax)
print(res)


from collections import Counter as C
i = input
i()
a = sorted(C(map(int, i().split())).values())[::-1]
(r, k) = (a[0], a[0])
for i in range(1, min(len(a), 30)):
	k = min(k // 2, a[i - 1] // 2, a[i])
	r = max(r, k * (pow(2, i + 1) - 1))
print(r)


n = map(int, input().split())
a = list(map(int, input().strip().split()))
alla = {}
for x in a:
	if alla.get(x) == None:
		alla.update({x: 0})
	alla.update({x: alla[x] + 1})
als = set(a)
res = []
for x in als:
	res.append(alla[x])
res.sort()
n = len(res)
ans = 0
for i in range(1, res[n - 1] + 1):
	curs = i
	cur = i
	ans = max(ans, curs)
	j = n - 2
	if cur % 2 == 1:
		continue
	cur = cur // 2
	while j >= 0 and res[j] >= cur:
		curs += cur
		ans = max(ans, curs)
		j -= 1
		if cur % 2 != 0:
			break
		cur = cur // 2
print(ans)


def calc(am, con):
	ans = am
	for i in range(len(con)):
		if am % 2 != 0:
			if con[i] >= am and i != 0:
				ans += am
			return ans
		if i == 0:
			am //= 2
			continue
		if con[i] >= am:
			ans += am
			am //= 2
		else:
			return ans
	return ans

def main():
	n = int(input())
	l = list(map(int, input().split()))
	dic = dict()
	for i in range(n):
		dic[l[i]] = 0
	for i in range(n):
		dic[l[i]] += 1
	l = list(dic.values())
	l.sort(reverse=True)
	ans = [0] * (l[0] + 1)
	for i in range(1, l[0] + 1):
		ans[i] = calc(i, l)
	print(max(ans))
main()


def nono(nu, a, b, c):
	if c == len(nu):
		return a
	if nu[c] >= b:
		silo = b * (2 ** (c + 1) - 1)
		return nono(nu, max(a, silo), b // 2, c + 1)
	else:
		silo = nu[c] * (2 ** (c + 1) - 1)
		return nono(nu, max(a, silo), nu[c] // 2, c + 1)
n = int(input())
nums = list(map(int, input().split()))
mapa = {}
li = []
for nu in nums:
	mapa[nu] = mapa.get(nu, 0) + 1
for (ma, c) in mapa.items():
	li.append(c)
li.sort(reverse=True)
res = li[0]
nu = li
a = li[0]
b = li[0] // 2
c = 1
while len(nu) > c:
	if nu[c] >= b:
		silo = b * (2 ** (c + 1) - 1)
		a = max(a, silo)
		b = b // 2
		c += 1
	else:
		silo = nu[c] * (2 ** (c + 1) - 1)
		a = max(a, silo)
		b = nu[c] // 2
		c += 1
print(a)


import sys
from collections import Counter
from bisect import bisect_left

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
N = INT()
A = LIST()
C = sorted(Counter(A).values())
M = len(C)
ans = 0
for x in range(1, N + 1):
	idx = 0
	cnt = 0
	while 1:
		idx = bisect_left(C, x, lo=idx)
		if idx < M:
			cnt += x
			x *= 2
			idx += 1
		else:
			break
	ans = max(ans, cnt)
print(ans)


n = int(input())
a = [int(i) for i in input().split()]
dtt = dict()
for v in a:
	if v not in dtt:
		dtt[v] = 0
	dtt[v] += 1
lts = [dtt[i] for i in dtt]
lts.sort(reverse=True)
mt = [lts[0]]
pl = lts[0]
for i in range(1, len(lts)):
	cl = min(pl // 2, lts[i])
	if cl == 0:
		break
	cs = cl * (2 ** (i + 1) - 1)
	mt.append(cs)
	pl = cl
print(max(mt))


from collections import Counter

def GPSum(a, r, n):
	return a * (pow(r, n) - 1) // (r - 1)
n = int(input())
aa = [int(i) for i in input().split()]
c = Counter(aa)
ss = sorted([int(i) for i in c.values()])[::-1]
minn = [ss[0]]
cur = ss[0]
ans = ss[0]
for i in range(0, len(ss)):
	if i == 0:
		ans = ss[0]
	else:
		cur = min(cur // 2, ss[i])
		if cur:
			ans = max(ans, GPSum(cur, 2, i + 1))
print(ans)


n = int(input())
a = list(map(int, input().split()))
counts = {}
for guy in a:
	if guy in counts:
		counts[guy] += 1
	else:
		counts[guy] = 1
clist = [counts[guy] for guy in counts]
clist.sort(reverse=True)
bylen = [clist[0]]
curr = clist[0]
for i in range(len(clist) - 1):
	curr = min(clist[i + 1], curr // 2)
	bylen.append(curr * (2 ** (i + 2) - 1))
	if curr == 0:
		break
print(max(bylen))


from collections import Counter
n = int(input())
aa = [int(i) for i in input().split()]
c = Counter(aa)
cc = sorted([int(i) for i in c.values()])[::-1]
ans = cc[0]
ori = cc[0]
for i in range(1, len(cc)):
	ori = min(ori // 2, cc[i])
	if ori:
		ans = max(ans, ori * (pow(2, i + 1) - 1))
print(ans)


from collections import Counter
n = int(input())
a = Counter([int(x) for x in input().split()])
x = sorted(a.values())
j = 0
an = -1
for i in range(1, max(x) + 1):
	while x[j] < i:
		j += 1
	p = i
	c = 0
	for q in range(j, len(x)):
		if x[q] >= p:
			p *= 2
			c += 1
	an = max(an, (pow(2, c) - 1) * i)
print(an)


n = int(input())
a = list(map(int, input().split()))
d = {}
for i in a:
	d[i] = d.get(i, 0) + 1
b = sorted(list(d.values()))
b = b[::-1]
l = 1
ba = b[0]
for i in b[1:]:
	lo = (2 ** l - 1) * (ba % 2)
	if i >= ba / 2:
		if lo > ba // 2:
			break
		else:
			ba //= 2
			l += 1
	else:
		lo = lo + (2 ** l - 1) * (ba // 2 - i)
		if i >= lo:
			ba = i
			l += 1
		else:
			break
ans = ba
for _ in range(l - 1):
	ans += ba * 2
	ba *= 2
print(ans)


n = int(input())
l = [int(i) for i in input().split()]
from collections import Counter
c = Counter(l)
l = [c[i] for i in c]
maxi = 0
l.sort()
m = l[-1]
n = len(l)
for i in range(1, m + 1):
	now = i
	pos = n - 1
	res = now
	while now % 2 == 0 and pos > 0:
		now //= 2
		pos -= 1
		if l[pos] >= now:
			res += now
		else:
			break
	maxi = max(maxi, res)
print(maxi)


import math
import collections
import bisect
import heapq
import time
import itertools
import sys
N = int(input())
A = [int(x) for x in input().split()]
wc = collections.Counter(A)
counts = [c for (w, c) in wc.items()]
counts.sort()
lencounts = len(counts)
vals = {v: v for v in range(1, counts[0] + 1)}
for i in range(1, lencounts):
	new_vals = {}
	for v in range(1, counts[i] + 1):
		if v % 2 == 0:
			new_vals[v] = vals[v // 2] + v if v // 2 in vals else v
		else:
			new_vals[v] = v
	for (k, v) in new_vals.items():
		if k in vals:
			vals[k] = max(vals[k], v)
		else:
			vals[k] = v
print(max(vals.values()))


from sys import stdin, stdout
from math import *
from heapq import *

def main():
	n = int(stdin.readline())
	a = [int(x) for x in stdin.readline().split()]
	count = dict()
	for x in a:
		count[x] = count.get(x, 0) + 1
	f = []
	for (key, val) in count.items():
		f.append(val)
	f.sort(reverse=True)
	res = f[0]
	sm = f[0]
	mn = f[0]
	l = 1
	for x in f[1:]:
		l = l + 1
		mn = min(floor(mn / 2), x)
		sm = mn * ((1 << l) - 1)
		res = max(res, sm)
		if mn <= 1:
			break
	stdout.write(str(res))
	return 0
main()


n = int(input())
pro = [int(i) for i in input().split()]
book = {}
for i in pro:
	if i in book:
		book[i] += 1
	else:
		book[i] = 1
book = [book[i] for i in book]
book = sorted(book, reverse=1)
book.insert(0, 0)
ans = 0
for i in range(1, book[1] + 1):
	(pos, cur, tem) = (2, i, i)
	while cur & 1 == 0 and pos < len(book):
		cur >>= 1
		if book[pos] < cur:
			break
		tem += cur
		pos += 1
	ans = max(ans, tem)
print(ans)


from collections import Counter, defaultdict
n = int(input())
l = list(map(int, input().split()))
c = Counter(l)
l1 = []
for i in c:
	l1.append(c[i])
l1.sort()
ans = l1[-1]
l2 = [defaultdict(int) for i in range(len(l1))]
for i in range(len(l1)):
	a = 1
	while a <= l1[i]:
		if l2[i][a] == 1:
			a += 1
			continue
		c = a
		j = i + 1
		x = a
		while j < len(l1) and l1[j] >= 2 * x:
			l2[j][2 * x] = 1
			c += 2 * x
			x *= 2
			j += 1
		ans = max(ans, c)
		a += 1
print(ans)


def check(x):
	ans = 0
	for i in range(len(fre)):
		if fre[i] >= x:
			ans += x
		else:
			break
		if x % 2:
			break
		x = x // 2
		if x == 0:
			break
	return ans
n = int(input())
lis = list(map(int, input().split()))
d = {}
fre = []
for i in lis:
	if i in d:
		d[i] += 1
	else:
		d[i] = 1
for i in d:
	fre.append(d[i])
fre.sort(reverse=True)
ans = 0
for i in range(fre[0] + 1):
	ans = max(ans, check(i))
print(ans)


from collections import Counter
n = int(input())
a = sorted(Counter(map(int, input().split())).values())
n = len(a)
ans = 0
for j in range(1, a[-1] + 1):
	tt = j
	i = n - 1
	while not j & 1 and i > 0:
		j //= 2
		i -= 1
		if a[i] < j:
			break
		tt += j
	ans = max(ans, tt)
print(ans)


from collections import defaultdict
from bisect import *
al = defaultdict(int)
a = int(input())
z = list(map(int, input().split()))
z.sort()
count = 1
ans = []
for i in range(1, len(z)):
	if z[i] == z[i - 1]:
		count += 1
	else:
		ans.append(count)
		count = 1
ans.append(count)
ans.sort()
from collections import defaultdict
mini = 1
maa = max(ans)
fin = 0
for i in range(mini, maa + 1):
	maxa = i
	total = i
	r = bisect_left(ans, i)
	while 1:
		m = bisect_left(ans, 2 * maxa)
		if m == len(ans):
			break
		if m > r:
			maxa = 2 * maxa
			total += maxa
			r = m
		else:
			r += 1
			if r < len(ans):
				maxa = 2 * maxa
				total += maxa
			else:
				break
	fin = max(fin, total)
print(fin)


from collections import defaultdict
import bisect
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
cnt = defaultdict(lambda : 0)
for i in a:
	cnt[i] += 1
x = [cnt[i] for i in cnt]
x.sort()
ans = 0
l = x[-1]
for i in range(1, l + 1):
	y = bisect.bisect_left(x, i - 0.5)
	j = 2 * i
	cnt = i
	while True:
		z = bisect.bisect_left(x, j - 0.5)
		if y >= z:
			z = y + 1
		if z == len(x):
			ans = max(ans, cnt)
			break
		y = z
		cnt += j
		j *= 2
print(ans)


from collections import Counter
n = int(input())
a = sorted(list(Counter([int(i) for i in input().split()]).values()), reverse=True)
mx = 0
c = a[0]
for (i, j) in enumerate(a):
	t = min(c // 2 ** i, j)
	if t == 0:
		break
	c = t * 2 ** i
	arr = [c // 2 ** j for j in range(i + 1)]
	mx = max(sum(arr), mx)
print(mx)


n = int(input())
q = list(map(int, input().split()))
c = {}
for i in q:
	if i not in c:
		c[i] = 0
	c[i] += 1
w = []
for i in c:
	w.append(c[i])
w.sort()
m = 0
for i in range(max(w)):
	j = len(w) - 1
	c = i + 1
	r = c
	while j > 0 and c % 2 == 0:
		c //= 2
		j -= 1
		if w[j] < c:
			break
		r += c
	m = max(m, r)
print(m)


import collections, atexit, math, sys, bisect
sys.setrecursionlimit(1000000)

def getIntList():
	return list(map(int, input().split()))
try:
	import numpy

	def dprint(*args, **kwargs):
		print(*args, file=sys.stderr)
	dprint('debug mode')
except Exception:

	def dprint(*args, **kwargs):
		pass
inId = 0
outId = 0
if inId > 0:
	dprint('use input', inId)
	sys.stdin = open('input' + str(inId) + '.txt', 'r')
if outId > 0:
	dprint('use output', outId)
	sys.stdout = open('stdout' + str(outId) + '.txt', 'w')
	atexit.register(lambda : sys.stdout.close())
N = getIntList()
za = getIntList()
cc = collections.Counter(za)
zt = []
for x in cc:
	zt.append(cc[x])
zt.sort()
re = zt[-1]

def findmid(l, r, e):
	if l >= len(zt):
		return -1
	if e <= zt[l]:
		return l
	if e > zt[r]:
		return -1
	while l + 1 < r:
		mid = (l + r) // 2
		if zt[mid] < e:
			l = mid
		else:
			r = mid
	return r
for first in range(1, re // 2 + 1):
	nowr = 0
	t = first
	ind = -1
	while 1:
		ind = findmid(ind + 1, len(zt) - 1, t)
		if ind < 0:
			break
		nowr += t
		t *= 2
	re = max(re, nowr)
print(re)