You are given a set Y of n distinct positive integers y_1, y_2, ..., y_{n}.

Set X of n distinct positive integers x_1, x_2, ..., x_{n} is said to generate set Y if one can transform X to Y by applying some number of the following two operation to integers in X:  Take any integer x_{i} and multiply it by two, i.e. replace x_{i} with 2·x_{i}.  Take any integer x_{i}, multiply it by two and add one, i.e. replace x_{i} with 2·x_{i} + 1. 

Note that integers in X are not required to be distinct after each operation.

Two sets of distinct integers X and Y are equal if they are equal as sets. In other words, if we write elements of the sets in the array in the increasing order, these arrays would be equal.

Note, that any set of integers (or its permutation) generates itself.

You are given a set Y and have to find a set X that generates Y and the maximum element of X is mininum possible.


-----Input-----

The first line of the input contains a single integer n (1 ≤ n ≤ 50 000) — the number of elements in Y.

The second line contains n integers y_1, ..., y_{n} (1 ≤ y_{i} ≤ 10^9), that are guaranteed to be distinct.


-----Output-----

Print n integers — set of distinct integers that generate Y and the maximum element of which is minimum possible. If there are several such sets, print any of them.


-----Examples-----
Input
5
1 2 3 4 5

Output
4 5 2 3 1 

Input
6
15 14 3 13 1 12

Output
12 13 14 7 3 1 

Input
6
9 7 13 17 5 11

Output
4 5 2 6 3 1

Difficulty: HARD
Tags: Graph traversal

String algorithms

Sorting

Greedy algorithms

Tree algorithms

Data structures
Skill types: Sorting

Data structures

Greedy algorithms

Starter code:

Solutions:
def solve():
	from heapq import merge
	input()
	a = set((int(x) for x in input().split(' ')))
	maxe = max(a)
	nodemap = {}

	class node:
		__slots__ = ('left', 'right')

		def __init__(self):
			self.right = self.left = None
	for x in a:
		while x > 0 and (not x in nodemap):
			nodemap[x] = node()
			x //= 2
	for x in nodemap.keys():
		y = x // 2
		if x > 1:
			if x % 2 == 1:
				nodemap[y].right = nodemap[x]
			else:
				nodemap[y].left = nodemap[x]
	root = nodemap[1]

	def f(x, root):
		if not root:
			return (-1, [])
		(lmax, larr) = f(2 * x, root.left)
		(rmax, rarr) = f(2 * x + 1, root.right)
		if x in a:
			return (max(lmax, x, rmax), list(merge(larr, [x], rarr)))
		if lmax > rmax:
			larr.pop()
			newmax = larr[-1] if len(larr) else -1
			return (max(newmax, x, rmax), list(merge(larr, [x], rarr)))
		elif lmax < rmax:
			rarr.pop()
			newmax = rarr[-1] if len(rarr) else -1
			return (max(lmax, x, newmax), list(merge(larr, [x], rarr)))
		else:
			return (-1, [])
	print(' '.join((str(x) for x in f(1, root)[1])))
solve()


from collections import defaultdict
import heapq
pow = [1]
for i in range(30):
	pow.append(pow[-1] * 2)
n = int(input())
b = list(map(int, input().split()))
d = defaultdict(lambda : 0)
for j in b:
	d[j] = 1
for j in range(n):
	b[j] = -b[j]
heapq.heapify(b)
ans = []
f = 1
while f:
	j = -heapq.heappop(b)
	can = 0
	for i in range(31):
		curr = (j - j % pow[i]) // pow[i]
		if curr < 1:
			break
		if d[curr] == 0:
			heapq.heappush(b, -curr)
			d[curr] = 1
			can = 1
			break
	if not can:
		heapq.heappush(b, -j)
		break
for j in range(n):
	b[j] = -b[j]
print(*b)


def main():
	from heapq import heapify, heapreplace
	input()
	s = set(map(int, input().split()))
	xx = [-x for x in s]
	heapify(xx)
	while True:
		x = -xx[0]
		while x != 1:
			x //= 2
			if x not in s:
				s.add(x)
				heapreplace(xx, -x)
				break
		else:
			break
	print(' '.join((str(-x) for x in xx)))

def __starting_point():
	main()
__starting_point()


import os
import sys
from io import BytesIO, IOBase
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')
from collections import Counter, defaultdict
import math

def matching(s):
	le = len(s)
	pi = [0] * le
	for i in range(1, le):
		j = pi[i - 1]
		while j > 0 and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	w = set()
	i = le - 1
	while pi[i] != 0:
		w.add(le - pi[i])
		i = pi[i] - 1
	return w
from heapq import heapify, heapreplace, heappop, heappush
input()
s = set(map(int, input().split()))
xx = [-x for x in s]
heapify(xx)
while True:
	x = -xx[0]
	while x != 1:
		x //= 2
		if x not in s:
			s.add(x)
			heappop(xx)
			heappush(xx, -x)
			break
	else:
		break
print(' '.join((str(-x) for x in xx)))