Range Minimum Query is a well-known problem: given an array of distinct integers with size $n=2^k$ and $m$ queries, find the minimum element on subsegment $[L_i,R_i]$.

One of the most efficient and famous solutions to this problem is a segment tree. A segment tree is a full binary tree with $2\cdot n-1$ nodes where the leaves contain the values of the original array and each non-leaf node contains the minimum value of its entire subtree.

Usually, a segment tree is represented as an array of integers with $2\cdot n-1$ elements. The left child of the $i^{\mbox{th}}$ node is in the $(2\cdot i+1)^{th}$ cell, and the right child is in the $(2\cdot i+2)^{th}$ cell. For example, $A=[1,1,3,1,2,3,4]$ represents the following segment tree where the first number in a node describes the array index, $\boldsymbol{i}$, in $\mbox{A}$ and the second number denotes the value stored at index $\boldsymbol{i}$ (which corresponds to the minimum value in that node's subtree):

You've just used $n$ distinct integers to construct your first segment tree and saved it as an array, $\mbox{A}$, of $2\cdot n-1$ values. Unfortunately, some evil guy came and either shuffled or altered the elements in your array. Can you use the altered data to restore the original array? If no, print NO on a new line; otherwise, print two lines where the first line contains the word YES and the second line contains $2\cdot n-1$ space-separated integers denoting the array's original values. If there are several possible original arrays, print the lexicographically smallest one.

Input Format

The first line contains a single integer, $n$, denoting the size of the array. 

The second line contains $2\cdot n-1$ space-separated integers denoting the shuffled values of the segment tree.

Constraints

$1\leq n\leq2^{18}$
$n$ is a power of two.
Each value in the segment tree is between $-10^9$ and $\mathbf{10^{9}}$.

Output Format

Print NO if this array could not be constructed by shuffling some segment tree. Otherwise, print YES on the first line, and $2\cdot n-1$ space-separated integers describing the respective values of the original array on the second line. If there are several possible answers, print the lexicographically smallest one.

Sample Input 0

4
3 1 3 1 2 4 1

Sample Output 0

YES
1 1 3 1 2 3 4 

Explanation 0 

This is the same segment tree shown in the Problem Statement above.

Sample Input 1

2
1 1 1

Sample Output 1

NO 

Explanation 1 

A segment tree with three nodes would consist of a root, a left child, and a right child. Because all three numbers in this array are the same and the leaves of the segment tree must be $n$ distinct integers, it's not possible to reconstruct the original array.

Difficulty: MEDIUM_HARD
Tags: Constructive algorithms

Starter code:

Solutions:
import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


from collections import defaultdict
from sys import stderr
import bisect as bi

def errprt(*arg):
	return
	print(*arg, file=stderr)

def findtree(arr):
	census = defaultdict(int)
	for elt in arr:
		census[elt] += 1
	if 2 * len(census) - 1 != len(arr):
		errprt('census', len(census), len(arr))
		return None
	countedarr = sorted(census.items())
	(root, k) = countedarr[0]
	if len(arr) != (1 << k) - 1:
		errprt('k', k, len(arr))
		return None
	ans = [root]
	row = [root]
	for step in range(k - 1, 0, -1):
		childlist = [k for (k, v) in countedarr if v == step]
		matchup = getmatchup(row, childlist)
		if matchup is None:
			return None
		row = []
		for pair in matchup:
			row += pair
		ans += row
	return ans

def getmatchup(plist, clist):
	npairs = len(plist)
	if len(clist) != npairs:
		return None
	nextavail = [0] * npairs
	ans = []
	for p in plist:
		orig = ichild = bi.bisect(clist, p)
		while ichild < npairs and nextavail[ichild] != 0:
			ichild = nextavail[ichild]
		if ichild == npairs:
			return None
		ans.append((p, clist[ichild]))
		nextavail[ichild] = na = ichild + 1
		while orig != na:
			t = nextavail[orig]
			nextavail[orig] = na
			orig = t
	return ans
n = int(input())
arr = [int(fld) for fld in input().strip().split()]
result = findtree(arr)
if result is None:
	print('NO')
else:
	print('YES')
	print(' '.join((str(i) for i in result)))


from collections import Counter
from math import log
n = eval(input())
a = [int(x) for x in input().split()]
s = set(a)
if len(s) == n:
	c = Counter(a)
	s = sorted(c.items(), key=lambda x: (x[1], -x[0]), reverse=True)
	depth = int(log(n, 2)) + 1
	i = 0
	a = []
	correct = True
	for d in range(depth):
		p = 2 ** d
		offset = p // 2 - 1
		j = 0
		nextlvl = s[i:p]
		while i < p:
			if i != 0:
				prev = a[j + offset]
				a.append(prev)
				found = False
				for (idx, x) in enumerate(nextlvl):
					if x[0] > prev:
						a.append(x[0])
						del nextlvl[idx]
						found = True
						break
				if not found:
					correct = False
					break
			else:
				a.append(s[i][0])
			if s[i][1] != depth - d:
				correct = False
				break
			j += 1
			i += 1
		if not correct:
			break
	if correct:
		print('YES')
		print(' '.join(map(str, a)))
	else:
		print('NO')
else:
	print('NO')


from collections import Counter
from math import log
n = eval(input())
c = Counter([int(x) for x in input().split()])
s = sorted(c.items(), key=lambda x: (x[1], -x[0]), reverse=True)
if len(s) != n:
	print('NO')
	exit()
depth = int(log(n, 2)) + 1
i = 0
a = []
correct = True
for d in range(depth):
	p = 2 ** d
	offset = p // 2 - 1
	j = 0
	nextlvl = s[i:p]
	while i < p:
		if i != 0:
			prev = a[j + offset]
			a.append(prev)
			found = False
			k = 0
			for x in nextlvl:
				if x[0] > prev:
					a.append(x[0])
					del nextlvl[k]
					found = True
					break
				k += 1
			if not found:
				correct = False
				break
		else:
			a.append(s[i][0])
		if s[i][1] != depth - d:
			correct = False
			break
		j += 1
		i += 1
	if not correct:
		break
if correct:
	print('YES')
	print(' '.join(map(str, a)))
else:
	print('NO')


import collections
N = 4
A = [3, 1, 3, 1, 2, 4, 1]
N = 16
A = '853983969 -978042718 -978042718 450604419 -165623018 -978042718 547711710 -73146423 -978042718 385413807 70205259 547711710 -298759545 70205259 -165623018 -165623018 754254573 836644654 263361326 253722771 298624814 263361326 -978042718 -165623018 -335447872 -31265955 -335447872 -31265955 70205259 298624814 -335447872'
A = list(map(int, A.split()))
A_ = sorted(list(set(A)))
Ah = {}
for i in range(0, len(A_)):
	Ah[A_[i]] = i
B = []
for a in A:
	B.append(Ah[a] + 1)
A = B
N = int(input())
A = list(map(int, input().split()))
K = 0
n_ = N
while not n_ & 1:
	n_ >>= 1
	K += 1
elem_count = {}
for a in A:
	if a not in elem_count:
		elem_count[a] = 0
	elem_count[a] += 1
count_to_elem = []
for k in range(0, K + 2):
	count_to_elem.append([])
for (elem, count) in elem_count.items():
	if count > K + 1:
		print('NO')
		exit(0)
	count_to_elem[count].append(elem)
is_consistent = True
for k in range(1, K + 1):
	is_consistent = is_consistent and len(count_to_elem[k]) == 1 << K - k
	count_to_elem[k].sort()
is_consistent = is_consistent and len(count_to_elem[K + 1]) == 1
if not is_consistent:
	print('NO')
	exit(0)
print('YES')
count_to_elem_idx = [0] * (K + 2)
bfs_queue = collections.deque()
bfs_queue.append([count_to_elem[K + 1][0], K])
actual_rmq = []
while len(bfs_queue) > 0:
	curr_node = bfs_queue.popleft()
	curr_value = curr_node[0]
	actual_rmq.append(curr_value)
	next_level = curr_node[1]
	if next_level > 0:
		bfs_queue.append([curr_value, next_level - 1])
		idx = count_to_elem_idx[next_level]
		while True:
			pick_value = count_to_elem[next_level][idx]
			if pick_value > curr_value:
				break
			idx += 1
		if idx == count_to_elem_idx[next_level]:
			count_to_elem_idx[next_level] += 1
		else:
			del count_to_elem[next_level][idx]
		bfs_queue.append([pick_value, next_level - 1])
print(' '.join(map(str, actual_rmq)))


def merge(l1, l2):
	l = []
	for l1_el in l1:
		l.append(l1_el)
		for l2_el in l2:
			if l2_el > l1_el:
				l.append(l2_el)
				l2.remove(l2_el)
				break
	return l

def verify_segment_tree(tree, i):
	if i >= len(tree):
		return True
	cond1 = 2 * i + 1 >= len(tree) or tree[i] == tree[2 * i + 1]
	cond2 = 2 * i + 2 >= len(tree) or tree[i] < tree[2 * i + 2]
	if not (cond1 and cond2):
		return False
	return verify_segment_tree(tree, 2 * i + 1) and verify_segment_tree(tree, 2 * i + 2)

def build_segment_tree(n, v):
	d = {}
	for i in v:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
	d_reversed = {}
	for (i, n_i) in d.items():
		if n_i not in d_reversed:
			d_reversed[n_i] = []
		d_reversed[n_i].append(i)
	segment_tuples = [(n_i, i_s) for (n_i, i_s) in d_reversed.items()]
	segment_tuples = sorted(segment_tuples, reverse=True)
	height = 1
	while 2 ** height - 1 < 2 * n - 1:
		height += 1
	if segment_tuples[0][0] != height or len(segment_tuples[0][1]) != 1:
		return (False, None)
	for i in range(1, len(segment_tuples)):
		tup = segment_tuples[i]
		if tup[0] != height - i or len(tup[1]) != 2 ** (i - 1):
			return (False, None)
	for tup in segment_tuples:
		tup[1].sort()
	next_level = list(segment_tuples[0][1])
	segment_tree = list(next_level)
	for tup in segment_tuples[1:]:
		next_level = merge(next_level, tup[1])
		segment_tree += next_level
	return (verify_segment_tree(segment_tree, 0), segment_tree)
n = int(input())
v = [int(number) for number in input().split(' ')]
[is_rmq, v] = build_segment_tree(n, v)
if is_rmq == False:
	print('NO')
else:
	print('YES')
	v = [str(i) for i in v]
	print(' '.join(v))


import math

def reconstruct_segment_tree(arr):
	height = math.floor(math.log(len(arr) + 1, 2))
	if 2 ** height - 1 != len(arr):
		return None
	counts = {}
	for a in arr:
		if a not in counts:
			counts[a] = 0
		counts[a] += 1
	nums_by_count = [[] for i in range(height)]
	for (num, count) in counts.items():
		if count <= 0 or height < count:
			return None
		nums_by_count[count - 1].append(num)
	for nums in nums_by_count:
		nums.sort()
	count = 0
	for i in range(height):
		count += len(nums_by_count[height - i - 1])
		if count != 2 ** i:
			return None
	arr = []
	for l in range(height):
		level = nums_by_count[height - 1]
		for l2 in range(l):
			prev_level = level
			level = 2 * len(prev_level) * [0]
			for (i, x) in enumerate(prev_level):
				level[2 * i] = x
			to_add = nums_by_count[height - l2 - 2].copy()
			for i in range(len(prev_level)):
				for j in range(len(to_add)):
					if level[2 * i] < to_add[j]:
						level[2 * i + 1] = to_add[j]
						to_add.pop(j)
						break
				else:
					return None
		arr += list(level)
	return arr
n = int(input().strip())
arr = [int(x) for x in input().strip().split()]
segment_tree = reconstruct_segment_tree(arr)
if segment_tree is None:
	print('NO')
else:
	print('YES')
	print(' '.join((str(x) for x in segment_tree)))


from collections import Counter
import math
n = int(input())
if n == 0:
	assert False
shuffled = [int(x) for x in input().split()]
frequencies = Counter(shuffled)
s = sorted(shuffled)
result = [0] * len(shuffled)
rows = int(math.ceil(math.log(n, 2))) + 1
availableNodes = [[0]]
for i in range(1, rows):
	availableNodes.append([x for x in range(pow(2, i + 1) - 2, pow(2, i) - 2, -2)])
if len(frequencies) != n:
	print('NO')
	import sys
	sys.exit()
success = True
x = s[0]
index = 0
f = frequencies[x]
if f != rows:
	print('NO')
	import sys
	sys.exit()
node = 0
for j in range(f):
	result[node] = x
	node += node + 1
for i in range(1, n):
	while s[index] == x:
		index += 1
	x = s[index]
	f = frequencies[x]
	startrow = rows - f
	placed = False
	for l in reversed(range(len(availableNodes[startrow]))):
		node = availableNodes[startrow][l]
		if result[node - 1] < x:
			del availableNodes[startrow][l]
			placed = True
			for j in range(f):
				result[node] = x
				node += node + 1
			break
	if not placed:
		success = False
		break
if success:
	print('YES')
	print(*result)
else:
	print('NO')


n = int(input())
m = 0
tmp = 1
while tmp != n:
	tmp *= 2
	m += 1
lst = list(map(int, input().split()))
lst = sorted(lst)
count = 1
tmp = lst[0]
clst = []
l = 1
for i in range(1, 2 * n - 1):
	if lst[i] == tmp:
		count += 1
	else:
		clst.append([tmp, count])
		l += 1
		tmp = lst[i]
		count = 1
clst.append([tmp, count])
slst = [[] for _ in range(m + 1)]
arr = ['n' for _ in range(2 * n - 1)]
tmp = 0
try:
	for i in range(l):
		tmp = clst[i]
		slst[m + 1 - tmp[1]].append(tmp[0])
	tmp = slst[0][0]
	shift = 1
	for k in range(m + 1):
		arr[shift - 1] = tmp
		shift += shift
	for i in range(1, m + 1):
		count = (1 << i) - 1
		while slst[i]:
			j = 0
			tmp = slst[i]
			while tmp[j] < arr[count]:
				j += 1
			count += 2
			shift = count
			for k in range(m - i + 1):
				arr[shift - 1] = tmp[j]
				shift += shift
			slst[i].pop(j)
	print('YES')
	print(*arr)
except:
	print('NO')


import sys
import math
n = int(input().strip())
ar = [int(x) for x in input().strip().split(' ')]
d = {}
for x in ar:
	d[x] = d.get(x, 0) + 1
depth = math.log(n, 2) + 1
row = [x for x in d if d[x] == depth]
fail = False
if len(row) != 1:
	fail = True
res = [0] * (2 * n - 1)
if not fail:
	res[0] = row[0]
	for i in range(1, 2 * n - 1):
		tmp = math.log(i + 1, 2)
		if tmp % 1 == 0:
			depth = depth - 1
			row = sorted([x for x in d if d[x] == depth])
			if len(row) != (i + 1) / 2:
				fail = True
				break
		if (i - 1) % 2 == 0:
			res[i] = res[(i - 1) // 2]
		else:
			res[i] = next((x for x in row if x > res[i - 1]), -1)
			if res[i] == -1:
				fail = True
				break
			row.remove(res[i])
if fail:
	print('NO')
else:
	print('YES')
	print(*res)


import sys

def build():
	arr[0] = hsort[0]
	preoff = 0
	ncur = 2
	while ncur <= n:
		npre = ncur // 2
		for i in range(preoff, preoff + npre):
			arr[2 * i + 1] = arr[i]
		ladd = sorted(hsort[npre:ncur])
		curoff = preoff + npre
		for i in range(curoff, curoff + ncur, 2):
			for j in range(0, len(ladd)):
				if ladd[j] > arr[i]:
					arr[i + 1] = ladd[j]
					del ladd[j]
					break
		preoff = preoff + npre
		ncur = ncur + ncur
n = int(input())
arr = list(map(int, input().split()))
org = sorted(arr[:])
hist = dict()
for a in arr:
	hist[a] = hist.get(a, 0) + 1
hsort = sorted(hist, key=hist.get, reverse=True)
if len(hsort) != n:
	print('NO')
	sys.exit()
build()
tmp = sorted(arr[:])
if tmp != org:
	print('NO')
	sys.exit()
print('YES')
for a in arr:
	print(a, end=' ')


from math import *
from collections import *

def reconstruct(res, index, level, prev, xfreq, freqs):
	if not level:
		xf = xfreq[index]
		for ip in range(len(freqs[xf])):
			if prev != None and freqs[xf][ip] < res[prev]:
				continue
			res[index] = freqs[xf][ip]
			del freqs[xf][ip]
			return True
		return False
	if not reconstruct(res, index * 2 + 1, level - 1, prev, xfreq, freqs):
		return False
	res[index] = res[index * 2 + 1]
	if not reconstruct(res, index * 2 + 2, level - 1, index, xfreq, freqs):
		return False
	return True

def main():
	N = int(input())
	X = map(int, input().strip().split())
	L = int(log2(N))
	POS1 = [[] for n in range(N)]
	dd = N
	for l in range(L + 1):
		s = 0
		p = 2 ** l - 1
		while s < N:
			POS1[s].append(p)
			s += dd
			p += 1
		dd //= 2
	FREQ1 = [[] for l in range(L + 2)]
	XFREQ = [0] * (2 * N - 1)
	for pi in POS1:
		lenp = len(pi)
		FREQ1[lenp].append(pi)
		for xp in pi:
			XFREQ[xp] = lenp
	FREQ2 = [[] for n in range(L + 2)]
	for x in Counter(X).items():
		if x[1] > L + 1:
			print('NO')
			return
		FREQ2[x[1]].append(x[0])
	for i in range(L + 2):
		FREQ2[i].sort()
	res = [0] * (2 * N - 1)
	if reconstruct(res, 0, L, None, XFREQ, FREQ2):
		print('YES')
		print(' '.join(map(str, res)))
	else:
		print('NO')
main()


import math
from collections import Counter

def f(n, L):
	k = int(math.log2(n))
	dfreq = Counter(L)
	levels = []
	first_lev = sorted([ky for (ky, v) in dfreq.items() if v == k + 1])
	if len(first_lev) != 1:
		return (False, [])
	root = first_lev[0]
	levels.append([root])
	j = k
	while j > 0:
		level = sorted([ky for (ky, v) in dfreq.items() if v == j])
		prev_level = levels[-1]
		if len(level) != 2 ** (k - j):
			return (False, [])
		next_level = []
		for i in range(2 ** (k - j)):
			t = 0
			while prev_level[i] > level[t]:
				t += 1
			next_level.extend([prev_level[i], level[t]])
			del level[t]
		levels.append(next_level)
		j -= 1
	return (True, levels)

def main():
	N = int(input().strip())
	L = list(map(int, input().strip().split(' ')))
	(Bool, res) = f(N, L)
	if Bool == 0:
		print('NO')
	else:
		print('YES')
		print(' '.join((' '.join((str(q) for q in p)) for p in res)))
main()


import sys
import math

def is_valid_segment_tree(t):
	length = len(t)
	for i in range(length - n):
		if t[i] != min(t[2 * i + 1], t[2 * i + 2]):
			return False
	return True

def restore_segment_tree(candidates, n, i, c_i, depth, lower, output):
	candidate = None
	for num in candidates[c_i]:
		if num > lower:
			candidate = num
			break
	if candidate is None:
		return None
	if i >= n - 1:
		output[i] = candidate
		candidates[c_i].remove(candidate)
		return candidate
	if restore_segment_tree(candidates, n, 2 * i + 1, c_i, depth - 1, lower, output) is None:
		return None
	if restore_segment_tree(candidates, n, 2 * i + 2, depth - 1, depth - 1, candidate, output) is None:
		return None
	output[i] = min(output[2 * i + 1], output[2 * i + 2])
	return output[i]

def solve(n, a):
	k = int(math.log(n, 2))
	length = len(a)
	values = [None] * n
	value_to_index = {}
	v_i = 0
	for num in sorted(a):
		if num not in value_to_index:
			values[v_i] = num
			value_to_index[num] = v_i
			v_i += 1
	for i in range(length):
		a[i] = value_to_index[a[i]]
	freq = {}
	for num in a:
		freq[num] = freq.get(num, 0) + 1
	candidates = []
	for (number, frequency) in sorted(freq.items(), key=lambda x: x[1]):
		key = frequency - 1
		if len(candidates) > key:
			candidates[key].append(number)
		else:
			candidates.append([number])
	if len(candidates) != k + 1:
		print('NO')
		return
	for i in range(len(candidates)):
		candidates[i] = sorted(candidates[i])
	output = [None] * length
	restore_segment_tree(candidates, n, 0, k, k, -10 ** 9 - 1, output)
	impossible_to_restore = any([num is None for num in output])
	if not impossible_to_restore:
		for i in range(length):
			output[i] = values[output[i]]
		if is_valid_segment_tree(output):
			print('YES')
			print(' '.join([str(i) for i in output]))
			return
	print('NO')
n = int(input().strip())
a = list(map(int, input().strip().split(' ')))
solve(n, a)


from collections import Counter, defaultdict
from math import log
from bisect import bisect_right
ans = 'YES'
str_output = ''
n = int(input())
output = []
layers = int(log(n, 2) + 0.5) + 1
arr_count = Counter(map(int, input().strip().split(' ')))
arr_count_rev = defaultdict(list)
for (num, num_count) in arr_count.items():
	arr_count_rev[num_count].append(num)
for l in range(1, layers + 1):
	arr_count_rev[l].sort()
	if len(arr_count_rev[l]) != 2 ** max(0, layers - l - 1):
		ans = 'NO'
		break
output.append(arr_count_rev[layers])
try:
	for layer in reversed(range(1, layers)):
		next_layer = []
		for num in output[-1]:
			idx = bisect_right(arr_count_rev[layer], num)
			next_layer.append(num)
			next_layer.append(arr_count_rev[layer].pop(idx))
		output.append(next_layer)
	str_output = ' '.join([' '.join(map(str, arr)) for arr in output])
except:
	ans = 'NO'
print(ans)
if ans == 'YES':
	print(str_output)


from collections import Counter
import math
import operator
import heapq

def immediate_parent(i):
	assert i > 0
	return (i + 1) // 2 - 1

def fail():
	print('NO')
	exit()

def binarysearch(list1, i, j, target):
	assert j >= i
	if j - i <= 4:
		for ix in range(i, j + 1):
			if list1[ix] > target:
				return ix
		return None
	mid = (i + j) // 2
	if list1[mid] < target:
		return binarysearch(list1, mid + 1, j, target)
	else:
		return binarysearch(list1, i, mid, target)
n = int(input())
orig_list = list(map(int, input().split()))
if n == 0 or n & n - 1 != 0 or len(orig_list) != 2 * n - 1:
	fail()
ordered_freqs = sorted(Counter(orig_list).items(), key=operator.itemgetter(1), reverse=True)
num_levels = int(math.log2(n) + 1)
expected_counts = [1] * num_levels
for i in range(2, num_levels):
	expected_counts[i] = expected_counts[i - 1] * 2
huge_lists = [None for i in range(num_levels)]
prev_vals = None
for unit_freq in reversed(range(1, num_levels + 1)):
	branch_count = 0
	ex_count = expected_counts[num_levels - unit_freq]
	curr_vals = [0 for i in range(ex_count)]
	for (ix, (val, freq)) in enumerate(ordered_freqs):
		if freq >= unit_freq:
			if freq != unit_freq:
				fail()
			branch_count += 1
			if branch_count > ex_count:
				fail()
			curr_vals[ix] = val
		else:
			break
	if branch_count != ex_count:
		fail()
	curr_vals.sort()
	assert prev_vals == None or len(prev_vals) == len(curr_vals)
	if prev_vals != None:
		for i in range(ex_count):
			curr_ix = binarysearch(curr_vals, 0, len(curr_vals) - 1 - i, prev_vals[i])
			if curr_ix == None:
				fail()
			removed = curr_vals.pop(curr_ix)
			curr_vals.append(removed)
	ordered_freqs = ordered_freqs[ix:]
	if prev_vals != None:
		curr_vals = [val for pair in zip(prev_vals, curr_vals) for val in pair]
	prev_vals = curr_vals
	huge_lists[num_levels - unit_freq] = curr_vals
print('YES')
for i in range(num_levels):
	print(' '.join((str(e) for e in huge_lists[i])), end=' ')


from collections import Counter
from collections import deque

def main():
	n = int(input())
	inn = sorted(Counter(map(int, input().split())).items(), key=lambda x: (-x[1], x[0]))
	if len(inn) != n:
		print('NO')
		return
	result = [None] * (2 * n - 1)
	skipped = deque()
	last_position = 0
	for element in inn:
		error = False
		if skipped:
			last_position = skipped.popleft()
			while skipped and (not result[last_position]):
				last_position = skipped.popleft()
		new_position = result.index(None, last_position)
		skipping = False
		while result[0] and result[int((new_position - 2) / 2)] > element[0]:
			if not skipped:
				skipping = True
				skipped.append(new_position)
			if not skipping and skipped:
				new_position = skipped.popleft()
				while skipped and (not result[new_position]):
					new_position = skipped.popleft()
			new_position = result.index(None, new_position + 1)
		last_position = new_position + 1
		result[new_position] = element[0]
		for i in range(element[1] - 1):
			new_position = new_position * 2 + 1
			try:
				result[new_position] = element[0]
			except IndexError:
				print('NO')
				error = True
				break
		if error:
			break
	else:
		print('YES')
		print(*result)
main()


debug = False

def initial_check(x):
	if len(x[-1]) != 1:
		return False
	c = 0
	for i in range(len(x) - 2, 0, -1):
		if debug:
			print(i, c, len(x[i]), 2 ** c)
		if len(x[i]) != 2 ** c:
			return False
		c += 1
	return True

def insert(tree, i, x):
	while i < len(tree):
		tree[i] = x
		i = 2 * i + 1

def construct_segment_tree(x, n):
	tree = [None for i in range(2 * n - 1)]
	insert(tree, 0, x[-1][0])
	for level in range(2, len(x)):
		data = x[-level]
		if debug:
			slots = list(range(2 ** (level - 1), 2 ** level, 2))
			print(level, data, slots)
		start_slot = 2 ** (level - 1)
		for i in data:
			moving_start = True
			for slot in range(start_slot, 2 ** level, 2):
				if tree[slot] is not None:
					if moving_start:
						start_slot += 2
					continue
				moving_start = False
				if i > tree[(slot - 2) // 2]:
					insert(tree, slot, i)
					break
			else:
				if debug:
					print('Could not accommodate', i)
				return False
		if debug:
			print(tree)
	return tree

def inverse_rmq(a, n):
	x = [[] for i in range(len(bin(n)[2:]) + 1)]
	d = dict()
	for i in a:
		d[i] = d.get(i, 0) + 1
		if d[i] >= len(x):
			return False
	if debug:
		print(d)
	for (i, v) in d.items():
		x[v].append(i)
	for s in x:
		s.sort()
	if debug:
		print(x)
	if not initial_check(x):
		return False
	return construct_segment_tree(x, n)

def relabel(a):
	d = sorted(list(set(a)))
	for i in range(len(a)):
		a[i] = d.index(a[i]) + 1
n = int(input())
a = [int(x) for x in input().split()]
if debug:
	print(a)
	relabel(a)
	print(a)
result = inverse_rmq(a, n)
if result:
	print('YES')
	print(' '.join(map(str, result)))
else:
	print('NO')


from typing import Optional, List
from collections import Counter
from math import log2

def inverse_rmq(n: int, A: List[int]) -> Optional[List[int]]:
	assert len(A) == 2 * n - 1
	count_lookup = Counter(A)
	tree_level = int(log2(2 * n))
	A_p = list()
	for i in range(tree_level):
		cur_level = tree_level - i
		if i == 0:
			min_v = min(count_lookup.keys())
			if count_lookup[min_v] == cur_level:
				A_p.append(min_v)
				count_lookup[min_v] -= 1
			else:
				return None
		else:
			parent_vs = list()
			for v in A_p[-2 ** (i - 1):]:
				if count_lookup[v] == cur_level:
					parent_vs.append(v)
					count_lookup[v] -= 1
				else:
					return None
			remain_vs = sorted((v for (v, cnt) in count_lookup.items() if cnt == cur_level), reverse=True)
			for pv in parent_vs:
				buf = list()
				while True:
					try:
						rv = remain_vs.pop()
					except IndexError:
						return None
					if pv < rv:
						A_p += [pv, rv]
						remain_vs += reversed(buf)
						buf.clear()
						count_lookup[rv] -= 1
						break
					else:
						buf.append(rv)
	return A_p
input_n = int(input())
input_vs = list(map(int, input().rstrip().split()))
output = inverse_rmq(input_n, input_vs)
if output is not None:
	print(f"YES\n{' '.join(map(str, output))}")
else:
	print('NO')


def findorigarr():
	size = int(input())
	origsize = size
	array = map(int, input().split())
	height = 0
	while size > 1:
		height += 1
		size = size // 2
	levelarr = []
	for _ in range(height + 1):
		levelarr.append([])
	freq = {}
	for a in array:
		if a in freq:
			freq[a] += 1
		else:
			freq[a] = 1
	for f in freq:
		if height < freq[f] - 1:
			print('NO')
			return
		levelarr[height - freq[f] + 1].append(f)
	for l in levelarr:
		l.sort()
	segsize = 2 * origsize - 1
	origarr = [None] * segsize
	if len(levelarr[0]) != 1:
		print('NO')
		return
	origarr[0] = levelarr[0][0]
	added = set()
	added.add(levelarr[0][0])
	i = 1
	while i <= height:
		expectedlen = 2 ** (i - 1)
		if len(levelarr[i]) != expectedlen:
			print('NO')
			return
		for l in levelarr[i]:
			k = findindextoupdate(origarr, l)
			if k == -1 or l in added:
				print('NO')
				return
			added.add(l)
			origarr[2 * k + 1] = origarr[k]
			origarr[2 * k + 2] = l
			origarr[k] = 1000000001
			refreshtree(origarr, (k - 1) // 2)
		reloadtree(origarr, i - 1)
		i += 1
	print('YES')
	print(' '.join((str(item) for item in origarr)))

def findindextoupdate(origarr, l):
	i = 0
	while True:
		if origarr[i] > l:
			return -1
		c1 = 2 * i + 1
		if origarr[c1] is None:
			return i
		i = c1 if origarr[c1] <= l else c1 + 1

def refreshtree(origarr, k):
	while k > 0:
		c1 = 2 * k + 1
		c2 = c1 + 1
		oldval = origarr[k]
		origarr[k] = origarr[c1] if origarr[c1] < origarr[c2] else origarr[c2]
		if oldval == origarr[k]:
			return
		k = (k - 1) // 2
	origarr[0] = origarr[1] if origarr[1] < origarr[2] else origarr[2]

def reloadtree(origarr, i):
	ii = 2 ** i - 1
	while ii < 2 ** (i + 1) - 1:
		refreshtree(origarr, ii)
		ii += 1
findorigarr()


import math
from collections import Counter
from heapq import heappush as push
from heapq import heappop as pop

def depth(id_):
	return k - int(math.log2(id_ + 1))

def is_unique(arr):
	for i in range(1, len(arr)):
		if arr[i] == arr[i - 1]:
			return False
	return True
n = int(input())
a = list(map(int, input().split()))
k = int(math.log2(n)) + 1
d = [[] for _ in range(k + 1)]
a = sorted(Counter(a).items(), key=lambda x: x[0])
ans = [0] * (2 * n - 1)
d[k] = [0]

def find(num):
	for i in range(k, 0, -1):
		if len(d[i]) > 0 and num >= i:
			return (True, i)
	return (False, None)
flg = True
for (val, num) in a:
	while num > 0:
		(flg, i) = find(num)
		if flg == False:
			break
		cur = pop(d[i])
		for j in range(i):
			ans[cur] = val
			if 2 * cur + 2 < 2 * n - 1:
				push(d[i - j - 1], 2 * cur + 2)
			cur = 2 * cur + 1
		num -= i
	if flg == False:
		break
if flg == False:
	print('NO')
elif is_unique(sorted(ans[-n:])) == False:
	print('NO')
else:
	print('YES')
	print(' '.join([str(x) for x in ans]))


import collections
N = 4
A = [3, 1, 3, 1, 2, 4, 1]
N = 16
A = '853983969 -978042718 -978042718 450604419 -165623018 -978042718 547711710 -73146423 -978042718 385413807 70205259 547711710 -298759545 70205259 -165623018 -165623018 754254573 836644654 263361326 253722771 298624814 263361326 -978042718 -165623018 -335447872 -31265955 -335447872 -31265955 70205259 298624814 -335447872'
A = list(map(int, A.split()))
A_ = sorted(list(set(A)))
Ah = {}
for i in range(0, len(A_)):
	Ah[A_[i]] = i
B = []
for a in A:
	B.append(Ah[a] + 1)
A = B
N = int(input())
A = list(map(int, input().split()))
K = 0
n_ = N
while not n_ & 1:
	n_ >>= 1
	K += 1
elem_count = {}
for a in A:
	if a not in elem_count:
		elem_count[a] = 0
	elem_count[a] += 1
count_to_elem = []
for k in range(0, K + 2):
	count_to_elem.append([])
for (elem, count) in elem_count.items():
	if count > K + 1:
		print('NO')
		exit(0)
	count_to_elem[count].append(elem)
is_consistent = True
for k in range(1, K + 1):
	is_consistent = is_consistent and len(count_to_elem[k]) == 1 << K - k
	count_to_elem[k].sort()
is_consistent = is_consistent and len(count_to_elem[K + 1]) == 1
if not is_consistent:
	print('NO')
	exit(0)
print('YES')
count_to_elem_idx = [0] * (K + 2)
bfs_queue = collections.deque()
bfs_queue.append([count_to_elem[K + 1][0], K])
actual_rmq = []
while len(bfs_queue) > 0:
	curr_node = bfs_queue.popleft()
	curr_value = curr_node[0]
	actual_rmq.append(curr_value)
	next_level = curr_node[1]
	if next_level > 0:
		bfs_queue.append([curr_value, next_level - 1])
		idx = count_to_elem_idx[next_level]
		while True:
			pick_value = count_to_elem[next_level][idx]
			if pick_value > curr_value:
				break
			idx += 1
		if idx == count_to_elem_idx[next_level]:
			count_to_elem_idx[next_level] += 1
		else:
			del count_to_elem[next_level][idx]
		bfs_queue.append([pick_value, next_level - 1])
print(' '.join(map(str, actual_rmq)))


from heapq import heappush, heappop
from collections import Counter
n = int(input())
N = n + n
a = Counter(map(int, input().split()))
o = [0] * N
k = {n.bit_length(): [1]}
for (i, c) in sorted(a.items()):
	x = k.get(c, [])
	if not x:
		print('NO')
		break
	x = heappop(x)
	o[x] = i
	x += x
	while x < N:
		o[x] = i
		c -= 1
		heappush(k.setdefault(c, []), x + 1)
		x += x
else:
	print('YES')
	print(*o[1:])


import collections
N = 4
A = [3, 1, 3, 1, 2, 4, 1]
N = 16
A = '853983969 -978042718 -978042718 450604419 -165623018 -978042718 547711710 -73146423 -978042718 385413807 70205259 547711710 -298759545 70205259 -165623018 -165623018 754254573 836644654 263361326 253722771 298624814 263361326 -978042718 -165623018 -335447872 -31265955 -335447872 -31265955 70205259 298624814 -335447872'
A = list(map(int, A.split()))
A_ = sorted(list(set(A)))
Ah = {}
for i in range(0, len(A_)):
	Ah[A_[i]] = i
B = []
for a in A:
	B.append(Ah[a] + 1)
A = B
N = int(input())
A = list(map(int, input().split()))
K = 0
n_ = N
while not n_ & 1:
	n_ >>= 1
	K += 1
elem_count = {}
for a in A:
	if a not in elem_count:
		elem_count[a] = 0
	elem_count[a] += 1
count_to_elem = []
for k in range(0, K + 2):
	count_to_elem.append([])
for (elem, count) in elem_count.items():
	if count > K + 1:
		print('NO')
		exit(0)
	count_to_elem[count].append(elem)
is_consistent = True
for k in range(1, K + 1):
	is_consistent = is_consistent and len(count_to_elem[k]) == 1 << K - k
	count_to_elem[k].sort()
is_consistent = is_consistent and len(count_to_elem[K + 1]) == 1
if not is_consistent:
	print('NO')
	exit(0)
print('YES')
count_to_elem_idx = [0] * (K + 2)
bfs_queue = collections.deque()
bfs_queue.append([count_to_elem[K + 1][0], K])
actual_rmq = []
while len(bfs_queue) > 0:
	curr_node = bfs_queue.popleft()
	curr_value = curr_node[0]
	actual_rmq.append(curr_value)
	next_level = curr_node[1]
	if next_level > 0:
		bfs_queue.append([curr_value, next_level - 1])
		idx = count_to_elem_idx[next_level]
		while True:
			pick_value = count_to_elem[next_level][idx]
			if pick_value > curr_value:
				break
			idx += 1
		if idx == count_to_elem_idx[next_level]:
			count_to_elem_idx[next_level] += 1
		else:
			del count_to_elem[next_level][idx]
		bfs_queue.append([pick_value, next_level - 1])
print(' '.join(map(str, actual_rmq)))


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import heapq
import sys
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
from bisect import bisect_right
from collections import defaultdict
INF = 10 ** 10
n = int(input())
a = list(map(int, input().strip().split()))
cnt = defaultdict(int)
inv = defaultdict(list)
bad = False
ans = [INF] * (2 * n - 1)
for x in a:
	cnt[x] += 1
if len(cnt) != n:
	bad = True
for x in cnt:
	inv[cnt[x]].append(x)
for c in inv:
	inv[c].sort()

def longest_path(i):
	path = []
	path.append(i)
	i = 2 * i + 1
	while i < 2 * n - 1:
		path.append(i)
		i = 2 * i + 1
	return path
for i in range(2 * n - 1):
	if ans[i] == INF:
		path = longest_path(i)
		depth = len(path)
		idx = bisect_right(inv[depth], ans[(i - 1) // 2] if i > 0 else -INF)
		if idx == len(inv[depth]):
			bad = True
			continue
		for j in path:
			ans[j] = inv[depth][idx]
		inv[depth].pop(idx)
for i in range(n - 1):
	if ans[i] != min(ans[2 * i + 1], ans[2 * i + 2]):
		bad = True
if bad:
	print('NO')
else:
	print('YES')
	print(' '.join(map(str, ans)))


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
banuu = int(input())
sonuu = list(map(int, input().split()))
nonuu = dict()
for rrr in sonuu:
	if rrr not in nonuu:
		nonuu[rrr] = 0
	nonuu[rrr] += 1
if len(nonuu) < banuu:
	print('NO')
	sys.exit()
nonuu_valla = 1
lotu = 1
while 2 ** (lotu - 1) < banuu:
	lotu += 1
prev = dict()
kalas = [0] * (banuu + banuu - 1)
while nonuu_valla <= banuu:
	kgf = list()
	q1 = list()
	for key in prev:
		q1.append((key, prev[key]))
	for key in nonuu:
		if nonuu[key] == lotu:
			kgf.append(key)
	if len(prev) == 0:
		kalas[0] = kgf[0]
		prev[kgf[0]] = 0
		nonuu[kgf[0]] -= 1
		nonuu_valla *= 2
		lotu -= 1
		continue
	kgf.sort()
	q1.sort()
	q2 = nonuu_valla // 2 - 1
	batu = list()
	nn = 0
	for rrr in kgf:
		if rrr in prev:
			kalas[prev[rrr] * 2 + 1] = rrr
			prev[rrr] = prev[rrr] * 2 + 1
			nonuu[rrr] -= 1
		else:
			while nn < len(q1):
				if q1[nn][0] < rrr:
					heapq.heappush(batu, q1[nn][1])
					nn += 1
				else:
					break
			if len(batu) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(batu)
			kalas[temp * 2 + 2] = rrr
			prev[rrr] = temp * 2 + 2
			nonuu[rrr] -= 1
	nonuu_valla *= 2
	lotu -= 1
print('YES')
for rrr in kalas:
	print(rrr, end=' ')


import sys
from bisect import bisect_right

def z(hsgdsgadgd, b):
	B = sorted(b)
	r = []
	for i in range(len(hsgdsgadgd)):
		ind = bisect_right(B, hsgdsgadgd[i])
		r += [hsgdsgadgd[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
hsgdsgadgd = list(map(int, input().split()))
asdnjksahdsahd = {}
for e in hsgdsgadgd:
	if not e in asdnjksahdsahd:
		asdnjksahdsahd[e] = 0
	asdnjksahdsahd[e] += 1
assdjashdahhaf = {}
for (i, v) in asdnjksahdsahd.items():
	if not v in assdjashdahhaf:
		assdjashdahhaf[v] = 0
	assdjashdahhaf[v] += 1
for i in range(1, nl):
	if not i in assdjashdahhaf or assdjashdahhaf[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
adbsadsagdsad = [[] for _ in range(nl + 2)]
for (i, v) in asdnjksahdsahd.items():
	adbsadsagdsad[v].append(i)
assdnsadsadaf = adbsadsagdsad[nl]
sdadaaf = list(assdnsadsadaf)
for i in range(1, nl):
	assdnsadsadaf = z(assdnsadsadaf, adbsadsagdsad[nl - i])
	sdadaaf += assdnsadsadaf
print('YES')
print(' '.join(map(str, sdadaaf)))


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
count = {}
for (i, v) in occ.items():
	if not v in count:
		count[v] = 0
	count[v] += 1
for i in range(1, nl):
	if not i in count or count[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
from bisect import bisect_right

def z(a, b):
	B = sorted(b)
	r = []
	for i in range(len(a)):
		ind = bisect_right(B, a[i])
		r += [a[i], B[ind]]
		del B[ind]
	return r
n = int(input())
nl = 0
x = n
while x:
	nl += 1
	x >>= 1
a = list(map(int, input().split()))
occ = {}
for e in a:
	if not e in occ:
		occ[e] = 0
	occ[e] += 1
cnt_occ = {}
for (i, v) in occ.items():
	if not v in cnt_occ:
		cnt_occ[v] = 0
	cnt_occ[v] += 1
for i in range(1, nl):
	if not i in cnt_occ or cnt_occ[i] != 1 << nl - 1 - i:
		print('NO')
		sys.exit(0)
ah = [[] for _ in range(nl + 2)]
for (i, v) in occ.items():
	ah[v].append(i)
sofar = ah[nl]
res = list(sofar)
for i in range(1, nl):
	sofar = z(sofar, ah[nl - i])
	res += sofar
print('YES')
print(' '.join(map(str, res)))


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')


import sys
import heapq
n = int(input())
a = list(map(int, input().split()))
freq = dict()
for i in a:
	if i not in freq:
		freq[i] = 0
	freq[i] += 1
if len(freq) < n:
	print('NO')
	sys.exit()
exp_freq = 1
depth = 1
while 2 ** (depth - 1) < n:
	depth += 1
prev = dict()
ans = [0] * (n + n - 1)
while exp_freq <= n:
	v = list()
	v1 = list()
	for key in prev:
		v1.append((key, prev[key]))
	for key in freq:
		if freq[key] == depth:
			v.append(key)
	if len(prev) == 0:
		ans[0] = v[0]
		prev[v[0]] = 0
		freq[v[0]] -= 1
		exp_freq *= 2
		depth -= 1
		continue
	v.sort()
	v1.sort()
	cur = exp_freq // 2 - 1
	pq = list()
	j = 0
	for i in v:
		if i in prev:
			ans[prev[i] * 2 + 1] = i
			prev[i] = prev[i] * 2 + 1
			freq[i] -= 1
		else:
			while j < len(v1):
				if v1[j][0] < i:
					heapq.heappush(pq, v1[j][1])
					j += 1
				else:
					break
			if len(pq) == 0:
				print('NO')
				sys.exit()
			temp = heapq.heappop(pq)
			ans[temp * 2 + 2] = i
			prev[i] = temp * 2 + 2
			freq[i] -= 1
	exp_freq *= 2
	depth -= 1
print('YES')
for i in ans:
	print(i, end=' ')