This is the hard version of the problem. The difference between the versions is that in the easy version all prices $a_i$ are different. You can make hacks if and only if you solved both versions of the problem.

Today is Sage's birthday, and she will go shopping to buy ice spheres. All $n$ ice spheres are placed in a row and they are numbered from $1$ to $n$ from left to right. Each ice sphere has a positive integer price. In this version, some prices can be equal.

An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them.

You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered.


-----Input-----

The first line contains a single integer $n$ $(1 \le n \le 10^5)$ — the number of ice spheres in the shop.

The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^9)$ — the prices of ice spheres.


-----Output-----

In the first line print the maximum number of ice spheres that Sage can buy.

In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them.


-----Example-----
Input
7
1 3 2 2 4 5 4

Output
3
3 1 4 2 4 2 5 


-----Note-----

In the sample it's not possible to place the ice spheres in any order so that Sage would buy $4$ of them. If the spheres are placed in the order $(3, 1, 4, 2, 4, 2, 5)$, then Sage will buy one sphere for $1$ and two spheres for $2$ each.

Difficulty: MEDIUM

Solutions:
n = int(input())
ar = list(map(int, input().split()))
ar = sorted(ar)
ans = [0] * n
j = 0
for i in range(1, n, 2):
	ans[i] = ar[j]
	j += 1
j = n // 2
for i in range(0, n, 2):
	ans[i] = ar[j]
	j += 1
cnt = 0
for i in range(1, n - 1):
	if ans[i] < ans[i - 1] and ans[i] < ans[i + 1]:
		cnt += 1
print(cnt)
print(*ans)


n = int(input())
F = [int(i) for i in input().split()]
F.sort()
if n % 2 != 0:
	A = F[:int((n - 1) / 2)]
	B = F[int((n - 1) / 2):]
	for i in range(int((n + 1) / 2)):
		F[2 * i] = B[i]
	for i in range(int((n - 1) / 2)):
		F[2 * i + 1] = A[i]
else:
	A1 = F[:int(n / 2)]
	B1 = F[int(n / 2):]
	for i in range(int(n / 2)):
		F[2 * i] = B1[i]
	for i in range(int(n / 2)):
		F[2 * i + 1] = A1[i]
count = 0
for i in range(1, n - 1):
	if F[i] < F[i - 1] and F[i] < F[i + 1]:
		count += 1
print(count)
print(*F)


n = int(input())
array = list(map(int, input().split()))
array.sort()
output = []
array1 = array[0:n // 2]
array2 = array[n // 2:]
i = 0
j = 0
count = 0
while i < n // 2 and j < (n - 1) // 2:
	b = array2[j]
	a = array1[i]
	output.append(b)
	output.append(a)
	if b > a:
		count += 1
	i += 1
	j += 1
if n % 2 != 0:
	output.append(array2[-1])
else:
	output.append(array2[-1])
	output.append(array1[-1])
print(count)
print(' '.join(map(str, output)))


def f():
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	newarr = []
	for i in range(n // 2):
		newarr.append(arr[i + n // 2])
		newarr.append(arr[i])
	if n % 2:
		newarr.append(arr[n - 1])
	total = 0
	i = 1
	while i + 1 < n:
		if newarr[i - 1] > newarr[i] and newarr[i] < newarr[i + 1]:
			total += 1
		i += 2
	newarr = list(map(str, newarr))
	newarr = ' '.join(newarr)
	print(total)
	print(newarr)
f()


n = int(input())
a = list(map(int, input().split()))
a.sort()
b = [0] * n
k = 0
for j in range(1, n, 2):
	b[j] = a[k]
	k = k + 1
for j in range(0, n, 2):
	b[j] = a[k]
	k = k + 1
m = 0
for j in range(1, n - 1, 2):
	if b[j] != b[j - 1] and b[j] != b[j + 1]:
		m = m + 1
print(m)
print(' '.join(map(str, b)))


n = int(input())
l = list(map(int, input().split()))
l = sorted(l)
ans = [0 for _ in range(n)]
t = n - 1
x = 0
if n % 2 == 0:
	x = 1
for i in range(n - 1 - x, -1, -2):
	ans[i] = l[t]
	t -= 1
t = 0
for i in range(1, n, 2):
	ans[i] = l[t]
	t += 1
t = 0
for i in range(1, n - 1):
	if ans[i] < ans[i - 1] and ans[i] < ans[i + 1]:
		t += 1
print(t)
print(*ans)


import itertools
import math
import sys
from collections import defaultdict

def stdinWrapper():
	data = '7\n1 1 1 1 2 2 2 2 1 \n\n\n'
	for line in data.split('\n'):
		yield line
if '--debug' not in sys.argv:

	def stdinWrapper():
		while True:
			yield input()
inputs = stdinWrapper()

def inputWrapper():
	return next(inputs)

def getType(_type):
	return _type(inputWrapper())

def getArray(_type):
	return [_type(x) for x in inputWrapper().split()]

def solve(a):
	a = sorted(a)
	start = len(a) // 2
	total = 0
	result = [-1 for _ in range(len(a))]
	pos = 0
	while pos < len(a):
		result[pos] = a[start + pos // 2]
		pos += 1
		if pos >= len(a):
			break
		result[pos] = a[pos // 2]
		if pos != len(a) - 1:
			total += 1 if result[pos] < result[pos - 1] else 0
		pos += 1
	return (total, result)
n = getType(int)
a = getArray(int)
(total, result) = solve(a)
print(total)
print(' '.join([str(x) for x in result]))


n = int(input())
a = list(map(int, input().split()))
a.sort()
mi = a[:n // 2]
ma = a[n // 2:]
l = []
for i in range(n):
	if i % 2 == 0:
		l.append(ma[i // 2])
	else:
		l.append(mi[i // 2])
ans = 0
for i in range(1, n - 1):
	if l[i - 1] > l[i] < l[i + 1]:
		ans += 1
print(ans)
print(*l)


def reOrder(arr):
	resArray = []
	arr = sorted(arr)
	mid = len(arr) // 2
	i = 0
	j = mid
	while i < mid and j < len(arr):
		resArray.append(arr[j])
		resArray.append(arr[i])
		i += 1
		j += 1
	while j < len(arr):
		resArray.append(arr[j])
		j += 1
	return resArray

def countPeaks(arr):
	if len(arr) <= 2:
		return 0
	res = 0
	i = 0
	j = i + 2
	while j < len(arr):
		left = arr[i]
		right = arr[j]
		middle = arr[i + 1]
		if middle < left and middle < right:
			res += 1
		i += 1
		j += 1
	return res
n = int(input())
arr = list(map(int, input().split()))
resArray = reOrder(arr)
res = countPeaks(resArray)
print(res)
print(*resArray)


n = int(input())
mas = list(map(int, input().split()))
mas = sorted(mas)
middle = n // 2
masans = []
ans = 0
if n > 1:
	for i in range(n // 2):
		if mas[i] < mas[i + middle]:
			ans += 1
		masans.append(mas[i + middle])
		masans.append(mas[i])
	if n % 2 != 0:
		masans.append(mas[n - 1])
	if n % 2 == 0:
		if masans[-1] < masans[-2]:
			ans -= 1
else:
	ans = 0
	masans = [mas[0]]
print(ans)
for x in masans:
	print(x, end=' ')


n = int(input())
l = list(map(int, input().split()))
l.sort()
j = 0
mid = len(l) // 2
a = [0] * (n + 2)
for i in range(mid):
	(a[i + j], a[i + 1 + j]) = (l[mid + i], l[i])
	j += 1
if n % 2 != 0:
	a[n - 1] = l[n - 1]
p = 0
for i in range(1, n):
	if a[i - 1] > a[i] and a[i] < a[i + 1]:
		p += 1
print(p)
a = a[:n]
s = ' '.join(map(str, a))
print(s)


import os, sys
from io import BytesIO, IOBase

def main():
	n = int(input())
	a = [int(X) for X in input().split()]
	a.sort()
	x = a[:n // 2]
	y = a[n // 2:]
	ma = []
	for i in range(len(x)):
		ma.append(y[i])
		ma.append(x[i])
	if n % 2 == 1:
		ma.append(y[-1])
	c = 0
	for i in range(1, n - 1):
		if ma[i] < ma[i - 1] and ma[i] < ma[i + 1]:
			c += 1
	print(c)
	print(*ma)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


n = int(input(''))
arr = list(map(int, input().split()))
arr.sort()
i = 0
sol = [0] * n
for j in range(1, n, 2):
	sol[j] = arr[i]
	i += 1
for j in range(0, n, 2):
	sol[j] = arr[i]
	i += 1
cnt = 0
for j in range(1, n - 1):
	if sol[j] < sol[j - 1] and sol[j] < sol[j + 1]:
		cnt += 1
print(cnt)
print(*sol)


import io
import os
from collections import Counter, defaultdict, deque

def solve(N, nums):
	nums.sort()
	mid = len(nums) // 2
	ret = []
	if len(nums) % 2 == 1:
		for (x, y) in zip(nums[:mid], nums[mid:-1]):
			ret.append(y)
			ret.append(x)
		ret.append(nums[-1])
	else:
		for (x, y) in zip(nums[:mid], nums[mid:]):
			ret.append(y)
			ret.append(x)
	count = 0
	for i in range(1, N - 1):
		if ret[i - 1] > ret[i] and ret[i] < ret[i + 1]:
			count += 1
	if DEBUG:
		assert set(nums) == set(ret)
	return str(count) + '\n' + ' '.join(map(str, ret))
DEBUG = False
if DEBUG:
	import random
	from itertools import permutations
	random.seed(0)
	for _ in range(100000):
		N = random.randint(1, 7)
		A = [random.randint(1, 10) for i in range(N)]
		print('tc', _, N, A)
		ans1 = solve(N, A)
		brute = 0
		for ret in permutations(A):
			count = 0
			for i in range(1, N - 1):
				if ret[i - 1] > ret[i] and ret[i] < ret[i + 1]:
					count += 1
			brute = max(brute, count)
		ansInt = int(ans1.split('\n')[0])
		print('brute', brute, 'mine', ans1)
		assert brute == ansInt
		print(ans1)
	exit()
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
(N,) = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
ans = solve(N, A)
print(ans)


n = int(input())
arr = [int(x) for x in input().split()]
arr.sort()
(arr[0::2], arr[1::2]) = (arr[n // 2:], arr[:n // 2])
ans = 0
for i in range(1, n - 1):
	if arr[i] < arr[i - 1] and arr[i] < arr[i + 1]:
		ans += 1
print(ans)
print(*arr)


n = int(input())
a = [int(x) for x in input().split()]
asort = sorted(a)
j = 0
ans = [0] * len(a)
for i in range(1, len(a), 2):
	ans[i] = asort[j]
	j += 1
for k in range(0, len(a), 2):
	ans[k] = asort[j]
	j += 1
total = 0
for num in range(1, len(ans) - 1, 2):
	if ans[num] < ans[num - 1] and ans[num] < ans[num + 1]:
		total += 1
print(total)
print(*ans)


t = int(input())
ice = list(map(int, input().split()))
new_ice = []
count = 0
ice.sort()
if len(ice) % 2 != 0:
	for i in range(0, len(ice) // 2 + 1):
		new_ice.append(ice[len(ice) // 2 + i])
		new_ice.append(ice[i])
	del new_ice[-1]
elif len(ice) <= 2:
	for i in range(len(ice)):
		new_ice.append(ice[i])
else:
	for i in range(0, len(ice) // 2):
		new_ice.append(ice[len(ice) // 2 + i])
		new_ice.append(ice[i])
for i in range(1, len(new_ice) - 1, 1):
	if new_ice[i] < new_ice[i - 1] and new_ice[i] < new_ice[i + 1]:
		count += 1
print(count)
print(' '.join(map(str, new_ice)))


n = int(input())
A = list(map(int, input().split()))
A = sorted(A)
B = A[0:n // 2]
C = A[n // 2:]
a = []
for i in range(len(B)):
	a.append(C[i])
	a.append(B[i])
if n % 2 != 0:
	a.append(C[-1])
ans = 0
for i in range(1, n - 1):
	if a[i] < a[i - 1] and a[i] < a[i + 1]:
		ans += 1
print(ans)
for x in a:
	print(x, end=' ')
print()


n = int(input())
l = list(map(int, input().split()))
l.sort(reverse=True)
ans = [0] * n
for i in range(1, n, 2):
	ans[i] = l.pop()
for i in range(0, n, 2):
	ans[i] = l.pop()
count = 0
for i in range(1, n - 1):
	if ans[i - 1] > ans[i] and ans[i] < ans[i + 1]:
		count += 1
print(count)
print(*ans)


def solve():
	n = int(input())
	p = list(map(int, input().split()))
	if n <= 2:
		print(0)
		print(*p)
		return
	p.sort()
	a = [0] * n
	l = 0
	k = n
	if n % 2 == 0:
		k -= 1
	for i in range(1, k, 2):
		a[i] = p[l]
		l += 1
	l = n - 1
	for i in range(k - 1, -1, -2):
		a[i] = p[l]
		l -= 1
	if n % 2 == 0:
		a[-1] = p[l]
	cnt = 0
	for i in range(1, n - 1):
		if a[i] < a[i - 1] and a[i] < a[i + 1]:
			cnt += 1
	print(cnt)
	print(*a)
	return

def main():
	t = 1
	for _ in range(t):
		solve()
main()


import sys, os.path
from collections import *
from copy import *
import math
mod = 10 ** 9 + 7
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
n = int(input())
arr = list(map(int, input().split()))
res = [0] * n
arr.sort()
j = 1
for i in range(n // 2):
	res[j] = arr[i]
	j += 2
j = 0
for i in range(n // 2, n):
	res[j] = arr[i]
	j += 2
x = 0
for i in range(1, n - 1):
	x += res[i - 1] > res[i] < res[i + 1]
print(x)
print(*res)


n = int(input())
arr = [int(num) for num in input().split()]
arr.sort()
arr0 = arr[:n // 2]
arr1 = arr[n // 2:]
p0 = 0
p1 = 0
ans = []
while p0 < len(arr0) and p1 < len(arr1):
	ans.append(arr1[p1])
	ans.append(arr0[p0])
	p0 += 1
	p1 += 1
if n % 2:
	ans.append(arr1[-1])
ans_num = 0
for ind in range(1, n - 1):
	if ans[ind - 1] > ans[ind] < ans[ind + 1]:
		ans_num += 1
print(ans_num)
print(*ans)


n = int(input())
lst = list(map(int, input().split()))
lst.sort()
result = lst[n // 2:]
for i in range(n // 2):
	result.insert(2 * i + 1, lst[i])
cnt = 0
for i in range(1, n - 1):
	if result[i - 1] > result[i] and result[i] < result[i + 1]:
		cnt += 1
print(cnt)
print(' '.join(map(str, result)))


import sys
try:
	sys.stdin = open('input.txt', 'r')
except FileNotFoundError:
	pass

def next_array():
	return [int(i) for i in input().split()]

def prefix(lst):
	assert len(lst)
	pre = [lst[0]]
	for i in range(1, len(lst)):
		pre.append(lst[i] + pre[i - 1])
	return pre

def prefix_max(lst):
	assert len(lst)
	pre = [lst[0]]
	for i in range(1, len(lst)):
		pre.append(max(lst[i], pre[i - 1]))
	return pre

def solve():
	n = int(input())
	lst = next_array()
	lst.sort()
	out = [None] * len(lst)
	for i in range(len(lst) // 2, len(lst)):
		out[(i - len(lst) // 2) * 2] = lst[i]
	for i in range(0, len(lst) // 2):
		out[i * 2 + 1] = lst[i]
	c = 0
	for i in range(1, len(lst) - 1, 2):
		if out[i] < out[i - 1] and out[i] < out[i + 1]:
			c += 1
	print(c)
	print(' '.join((str(i) for i in out)))
solve()


n = int(input())
a = list(map(int, input().split()))
a.sort()
ind = n // 2
res = []
i = 0
f = True
for j in range(n):
	if f == True:
		res.append(a[ind])
		ind += 1
		f = False
	else:
		res.append(a[i])
		i += 1
		f = True
ans = 0
for i in range(1, n - 1):
	if res[i - 1] > res[i] and res[i] < res[i + 1]:
		ans += 1
print(ans)
print(*res)


n = int(input())
a = list(map(int, input().split()))
a.sort()
arr = [0 for i in range(n)]
ind = 0
for i in range(1, n, 2):
	arr[i] = a[ind]
	ind += 1
for i in range(0, n, 2):
	arr[i] = a[ind]
	ind += 1
count = 0
for i in range(1, n - 1):
	if arr[i] < arr[i - 1] and arr[i] < arr[i + 1]:
		count += 1
print(count)
print(*arr)


n = int(input())
ls = list(map(int, input().split()))
ls.sort()
ReturnLs = []
i = 0
for j in range(n // 2, n):
	ReturnLs.append(ls[j])
	if i != n // 2:
		ReturnLs.append(ls[i])
	i += 1
count = 0
for i in range(1, n - 1, 2):
	if ReturnLs[i] < ReturnLs[i + 1] and ReturnLs[i - 1] > ReturnLs[i]:
		count += 1
print(count)
print(*ReturnLs)


import math
count = math.ceil(int(input()) / 2) - 1
a = sorted(list(map(int, input().split())))
a1 = a[:count]
a2 = a[-count - 1:]
b = count
c = ''
for (x, y) in zip(a1, a2):
	if x == y:
		count -= 1
	c += str(y) + ' ' + str(x) + ' '
c += str(a[-1]) + ' '
for i in a[b:-b - 1]:
	c += str(i)
print(count)
print(c)


n = int(input())
a = list(map(int, input().split()))
if n <= 2:
	print(0)
	print(' '.join(map(str, a)))
else:
	a.sort()
	b = [0] * n
	j = 0
	for i in range(1, n, 2):
		b[i] = a[j]
		j += 1
	for i in range(0, n, 2):
		b[i] = a[j]
		j += 1
	c = 0
	for i in range(1, n - 1, 2):
		if b[i - 1] > b[i] < b[i + 1]:
			c += 1
	print(c)
	print(' '.join(map(str, b)))


n = int(input())
a = list(map(int, input().split()))
a.sort()
i = 0
j = 0
a1 = a[:n // 2]
a2 = a[n // 2:]
ans = []
while i < len(a1) and j < len(a2):
	ans.append(a2[j])
	j += 1
	ans.append(a1[i])
	i += 1
while i < len(a1):
	ans.append(a1[i])
	i += 1
while j < len(a2):
	ans.append(a2[j])
	j += 1
c = 0
for i in range(1, len(ans) - 1):
	if ans[i] < ans[i - 1] and ans[i] < ans[i + 1]:
		c += 1
print(c)
print(*ans)


n = int(input())
a = list(map(int, input().split()))
a.sort()
ans = [None for _ in range(n)]
i = 0
j = 1
while j < n:
	ans[j] = a[i]
	i += 1
	j += 2
k = i
j = 0
while k < n and j < n:
	ans[j] = a[k]
	k += 1
	j += 2
count = 0
for i in range(1, n - 1):
	if ans[i] < ans[i + 1] and ans[i] < ans[i - 1]:
		count += 1
print(count)
print(' '.join(map(str, ans)))


n = int(input())
a = list(sorted(list(map(int, input().split()))))
k = n // 2
b = []
j = k
l = 0
for i in range(n):
	if i % 2 == 0:
		b.append(a[j])
		j += 1
	else:
		b.append(a[l])
		l += 1
c = 0
for i in range(1, n - 1, 2):
	if b[i] < b[i + 1] and b[i] < b[i - 1]:
		c += 1
print(c)
print(*b, sep=' ')


n = int(input())
l = list(map(int, input().split()))
l.sort()
l1 = [0] * n
(j, ans) = (0, 0)
for i in range(1, n, 2):
	l1[i] = l[j]
	j += 1
for i in range(0, n, 2):
	l1[i] = l[j]
	j += 1
for i in range(1, n - 1):
	if l1[i] < l1[i + 1] and l1[i] < l1[i - 1]:
		ans += 1
print(ans)
print(*l1)


from collections import defaultdict as dd
from sys import stdin, stdout
from bisect import bisect_left
from math import log, sqrt
n = int(input())
a = list(map(int, input().split()))
ans = [0] * n
a.sort()
k = 0
c = 0
for i in range(1, n, 2):
	ans[i] = a[k]
	k += 1
for i in range(0, n, 2):
	ans[i] = a[k]
	k += 1
for i in range(1, n - 1):
	if ans[i] < ans[i + 1] and ans[i] < ans[i - 1]:
		c += 1
print(c)
print(*ans)


n = int(input())
arr = list(map(int, input().split()))
arr.sort()
resArr = [0] * n
count = 0
j = 0
for i in range(1, n, 2):
	resArr[i] = arr[j]
	j += 1
for i in range(0, n, 2):
	resArr[i] = arr[j]
	j += 1
for i in range(1, n - 1):
	if resArr[i] < resArr[i - 1] and resArr[i] < resArr[i + 1]:
		count += 1
print(count)
print(' '.join(map(str, resArr)))


n = int(input())
s = input()
L = s.split()
for i in range(len(L)):
	L[i] = int(L[i])
myLen = len(L)
if myLen <= 2:
	print(0)
	print(s)
	quit()
L.sort()
gr = myLen // 2
cnt = gr if myLen % 2 else gr - 1
v = L[0]
for i in range(gr, myLen - 1):
	if L[i] > v < L[i + 1]:
		k = i
		break
else:
	print(0)
	print(s)
	quit()
shift = k - gr
L1 = []
for i in range(gr, gr + shift):
	L1.append(L[i])
nn = cnt - shift
for i in range(nn):
	L1.append(L[k + i])
	L1.append(L[i])
L1.append(L[-1])
for i in range(shift):
	L1.append(L[i + nn])
if not myLen % 2:
	L1.append(L[cnt])
nSales = 0
for i in range(1, len(L1), 2):
	if i + 1 < len(L1) and L1[i - 1] > L1[i] < L1[i + 1]:
		nSales += 1
print(nSales)
L1 = [str(e) for e in L1]
print(' '.join(L1))


import sys
input = sys.stdin.readline
n = int(input())
x = sorted(list(map(int, input().split())))
(a, b) = (x[:n // 2], x[n // 2:])
tmp = []
for i in range(n // 2):
	tmp.append(b[i])
	tmp.append(a[i])
if n % 2:
	tmp.append(b[-1])
cnt = 0
for i in range(1, n - 1, 2):
	if tmp[i + 1] > tmp[i] < tmp[i - 1]:
		cnt += 1
print(cnt)
print(*tmp)


n = int(input())
a = [int(i) for i in input().split()]
b = [0] * n
a.sort()
j = 0
for i in range(1, n, 2):
	b[i] = a[j]
	j += 1
for i in range(0, n, 2):
	b[i] = a[j]
	j += 1
count = 0
for i in range(1, n - 1):
	if b[i] < b[i - 1] and b[i + 1] > b[i]:
		count += 1
print(count)
print(*b)


from math import ceil
n = int(input())
a = list(map(int, input().split()))
a.sort()
i = 1
cur = 0
res = [0 for i in range(n)]
while i < len(a):
	res[i] = a[cur]
	i += 2
	cur += 1
i = 0
while i < len(a):
	res[i] = a[cur]
	cur += 1
	i += 2
w = 0
for i in range(1, n - 1):
	if res[i] < res[i - 1] and res[i] < res[i + 1]:
		w += 1
print(w)
print(' '.join([str(x) for x in res]))


n = int(input())
(*arr,) = map(int, input().split())
arr.sort()
half = n // 2
res = [-1] * n
if n % 2 == 0:
	res[::2] = arr[-half:]
	res[1::2] = arr[:-half]
else:
	res[::2] = arr[-half - 1:]
	res[1::2] = arr[:-half - 1]
ans = 0
for i in range(1, n - 1):
	if res[i - 1] > res[i] < res[i + 1]:
		ans += 1
print(ans)
print(*res)


n = int(input())
arr = list(map(int, input().split()))
res = [0] * n
arr.sort()
for i in range(n):
	if i < n // 2:
		res[i * 2 + 1] = arr[i]
	else:
		res[(i - n // 2) * 2] = arr[i]
x = 0
for i in range(1, n - 1):
	if res[i - 1] > res[i] < res[i + 1]:
		x += 1
print(x)
print(*res)


n = int(input())
l = list(map(int, input().split()))
lena = len(l)
l.sort()
left = l[:lena // 2]
right = l[lena // 2:]
left.sort()
right.sort()
answer = []
count = 0
for i in range(len(right)):
	answer.append(right[i])
	if i < len(left):
		answer.append(left[i])
		if answer[-1] < answer[-2] and i < len(right) - 1:
			count += 1
print(count)
print(*answer)


n = int(input())
a = [int(i) for i in input().split()]
a.sort()
ans = []
p = 0
if n % 2:
	for i in range(n // 2, n):
		ans.append(a[i])
		if i < n - 1:
			ans.append(a[p])
			p += 1
else:
	for i in range(n // 2, n):
		ans.append(a[i])
		ans.append(a[p])
		p += 1
cnt = 0
for i in range(1, n - 1):
	if ans[i] < ans[i - 1] and ans[i] < ans[i + 1]:
		cnt += 1
print(cnt)
print(*ans)


n = int(input())
a = list(map(int, input().split()))
a.sort()
ans = []
count = 0
c = 0
for i in range(n // 2, n):
	ans.append(a[i])
	if c < n // 2:
		ans.append(a[c])
		c = c + 1
for i in range(1, n, 2):
	if i + 1 < n:
		if ans[i] < ans[i - 1] and ans[i] < ans[i + 1]:
			count = count + 1
print(count)
print(*ans)


import sys, os.path, math
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
sys.setrecursionlimit(10 ** 5)

def mod():
	return 10 ** 9 + 7

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

def ii():
	return int(sys.stdin.readline())

def li():
	return list(sys.stdin.readline().strip())

def mii():
	return map(int, sys.stdin.readline().split())

def lii():
	return list(map(int, sys.stdin.readline().strip().split()))

def solve():
	a = ii()
	b = sorted(lii())
	c = 0
	d = []
	e = len(b)
	if a % 2 == 0:
		for i in range(int(a / 2)):
			d.append(b[i + int(a / 2)])
			d.append(b[i])
	else:
		b.insert(math.floor(a / 2), ' ')
		for i in range(math.ceil(a / 2)):
			d.append(b[i + math.ceil(a / 2)])
			d.append(b[i])
	if e % 2 == 0:
		for i in range(1, a - 1, 2):
			if d[i - 1] > d[i] < d[i + 1]:
				c += 1
			else:
				pass
		print(c)
		for x in d:
			print(x, end=' ')
	else:
		d.remove(' ')
		for i in range(1, a - 1, 2):
			if d[i - 1] > d[i] < d[i + 1]:
				c += 1
			else:
				pass
		print(c)
		for x in d:
			print(x, end=' ')
for _ in range(1):
	solve()


n = int(input())
arr = list(map(int, input().strip().split()))
if n % 2 == 0:
	ans = n // 2 - 1
else:
	ans = n // 2
arr.sort()
low = arr[:n // 2]
hi = arr[n // 2:]
arr[::2] = hi
arr[1::2] = low
for i in range(1, n - 1, 2):
	if arr[i] == arr[i - 1] or arr[i] == arr[i + 1]:
		ans -= 1
print(ans)
print(*arr, sep=' ')


n = int(input())
a = list(map(int, input().split()))
a.sort()
d = [0 for i in range(n)]
i = 0
j = n // 2
for z in range(0, n // 2):
	d[2 * i] = a[j]
	d[2 * i + 1] = a[i]
	i += 1
	j += 1
if j == n - 1:
	d[n - 1] = a[n - 1]
k = 0
for i in range(1, n - 1):
	if d[i] < d[i - 1] and d[i] < d[i + 1]:
		k += 1
print(k)
print(' '.join(list(map(str, d))))


n = int(input())
l = list(sorted(map(int, input().split())))
left = l[0:n // 2]
right = l[n // 2:n]
arr = [-1 for x in range(n)]
count = 0
flag = True
j = 0
k = 0
for i in range(n):
	if flag == True:
		arr[i] = right[j]
		j += 1
		flag = False
	elif flag == False:
		arr[i] = left[k]
		k += 1
		flag = True
for a in range(1, n - 1):
	if arr[a] < arr[a - 1] and arr[a] < arr[a + 1]:
		count += 1
print(count)
print(*arr)


from math import *
from bisect import *
from collections import *
from random import *
from decimal import *
import sys
input = sys.stdin.readline

def inp():
	return int(input())

def st():
	return input().rstrip('\n')

def lis():
	return list(map(int, input().split()))

def ma():
	return map(int, input().split())
t = 1
while t:
	t -= 1
	n = inp()
	a = lis()
	r = [0 for i in range(n)]
	a.sort()
	(i, j) = (0, n // 2)
	for k in range(n):
		if k % 2 ^ 1:
			r[k] = a[j]
			j += 1
		else:
			r[k] = a[i]
			i += 1
	s = 0
	for i in range(1, n - 1):
		if r[i] < r[i - 1] and r[i] < r[i + 1]:
			s += 1
	print(s)
	print(*r)


from sys import stdin
n = int(stdin.readline())
a = sorted(map(int, input().split()))
m = (n - 1) // 2
c = 0
b = []
for i in range(n):
	b.append(a[(1 - i % 2) * (n // 2) + i // 2])
for i in range(1, m + 1):
	if b[i * 2 - 1] < min(b[i * 2 - 2], b[i * 2]):
		c += 1
print(c)
for i in range(n):
	print(b[i], end=' ')


n = int(input())
a = list(map(int, input().split()))
a = sorted(a)
x = a[:n // 2]
a = a[n // 2:]
(l, k, m) = ([], 0, 0)
for i in range(n):
	if i % 2 == 0:
		l.append(a[k])
		k += 1
	else:
		l.append(x[m])
		m += 1
m = 0
for i in range(1, n - 1):
	if l[i] < l[i - 1] and l[i] < l[i + 1]:
		m += 1
print(m)
for i in l:
	print(i, end=' ')


def main():
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	l = []
	if n < 3:
		print(0)
		print(*a)
		return
	half = n // 2
	for i in range(half):
		l.append(a[half + i])
		l.append(a[i])
	if n % 2 != 0:
		l.append(a[-1])
	total = 0
	for j in range(1, n - 1):
		if l[j] < l[j - 1] and l[j] < l[j + 1]:
			total += 1
	print(total)
	print(*l)
main()


n = int(input())
list1 = list(map(int, input().split()))
list1.sort()
if n <= 2:
	print(0)
	print(*list1)
else:
	temp = [-1] * len(list1)
	count = 0
	for i in range(1, len(list1), 2):
		temp[i] = list1[count]
		count += 1
	for i in range(0, len(list1), 2):
		temp[i] = list1[count]
		count += 1
	s = 0
	for i in range(1, len(list1) - 1):
		if temp[i] < temp[i - 1] and temp[i] < temp[i + 1]:
			s += 1
	print(s)
	print(*temp)


n = int(input())
a = [int(i) for i in input().split()]
a.sort()
res = [0] * n
res[::2] = a[n // 2:]
res[1::2] = a[:n // 2]
ans = 0
for i in range(1, n - 1):
	ans += res[i - 1] > res[i] < res[i + 1]
print(ans)
print(*res)


n = int(input())
l = list(map(int, input().split()))
ans = 0
l.sort()
la = l[-1]
k = []
if n < 3:
	print(0)
	print(*l)
elif n % 2 != 0:
	ans = 0
	l1 = l[0:n // 2]
	l2 = l[n // 2:]
	k.append(l[n // 2])
	for i in range(n // 2, n - 1):
		ma1 = l[i]
		ma2 = l[i + 1]
		if l[i - n // 2] < ma1 and l[i - n // 2] < ma2:
			k.append(l[i - n // 2])
			k.append(ma2)
			ans += 1
		else:
			k.append(l[i - n // 2])
			k.append(ma2)
	print(ans)
	print(*k)
else:
	ans = 0
	l1 = l[0:n // 2 - 1]
	l2 = l[n // 2 - 1:]
	k.append(l[n // 2])
	for i in range(n // 2, n - 1):
		ma1 = l[i]
		ma2 = l[i + 1]
		if l[i - n // 2] < ma1 and l[i - n // 2] < ma2:
			k.append(l[i - n // 2])
			k.append(ma2)
			ans += 1
		else:
			k.append(l[i - n // 2])
			k.append(ma2)
	k.append(l[n // 2 - 1])
	print(ans)
	print(*k)


import sys
n = int(input())
lst1 = list(map(int, input().split()))
a = [None] * n
lst1.sort()
index = ans = 0
for i in range(1, n, 2):
	a[i] = lst1[index]
	index += 1
for i in range(0, n, 2):
	a[i] = lst1[index]
	index += 1
for i in range(1, n - 1, 2):
	if a[i] < a[i - 1] and a[i] < a[i + 1]:
		ans += 1
print(ans)
print(*a)


import traceback
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
sys.stdin = IOWrapper(sys.stdin)
input = lambda : sys.stdin.readline().rstrip('\r\n')
import math
from collections import defaultdict as dfd, Counter, deque
from functools import lru_cache
from itertools import accumulate

def geti():
	return int(input())

def gets():
	return input()

def getil():
	return list(map(int, input().split()))

def getsl():
	return input().split()

def getinps(s):
	inps = s.split()
	m = {'i': geti, 's': gets, 'il': getil, 'sl': getsl}
	if len(inps) == 1:
		return m[s]()
	return [m[k]() for k in inps]

def get2d(nrows, ncols, n=0):
	return [[n] * ncols for r in range(nrows)]

def get_acc(a):
	return list(accumulate(a))

def get_ncr(n, r, mod=10 ** 9 + 7):
	num = den = 1
	r = min(r, n - r)
	for i in range(r):
		num = num * (n - i) % mod
		den = den * (i + 1) % mod
	return num * pow(den, mod - 2, mod) % mod

def get_nc2(n):
	return n * (n - 1) // 2

def get_npr(n, r):
	if n < r:
		return 0
	return math.factorial(n) // math.factorial(r)

def put(a):
	return ' '.join(map(str, a))
inf = float('inf')
mod = 10 ** 9 + 7

def main():
	((N,), a) = getinps('il il')
	a.sort()
	ans = [0] * N
	p = 0
	for i in range(1, N, 2):
		ans[i] = a[p]
		p += 1
	for i in range(0, N, 2):
		ans[i] = a[p]
		p += 1
	k = 0
	for i in range(1, N - 1):
		k += ans[i - 1] > ans[i] < ans[i + 1]
	print(k)
	print(*ans)
try:
	ans = main()
	ans = str(ans)
except Exception as e:
	print(e)
	traceback.print_exc()


from collections import defaultdict as dd
import math
import sys
import heapq
import copy
input = sys.stdin.readline

def nn():
	return int(input())

def li():
	return list(input())

def mi():
	return list(map(int, input().split()))

def lm():
	return list(map(int, input().split()))

def solve():
	n = nn()
	ice = lm()
	ice.sort()
	out = []
	for i in range(n):
		if i % 2 == 1:
			out.append(ice[i // 2])
		else:
			out.append(ice[i // 2 + n // 2])
	good = 0
	for i in range(n - 1):
		if i % 2 == 1:
			if out[i] < out[i + 1] and out[i] < out[i - 1]:
				good += 1
	print(good)
	print(*out)
solve()


n = int(input())
a = [int(x) for x in input().split()]
b = [0] * n
a.sort()
k = 0
c = 0
for i in range(1, n, 2):
	b[i] = a[k]
	k += 1
for i in range(0, n, 2):
	b[i] = a[k]
	k += 1
for i in range(1, n - 1):
	if b[i] < b[i - 1] and b[i] < b[i + 1]:
		c += 1
print(c)
print(*b)


n = int(input())
a = list(map(int, input().split()))
a.sort()
cur = 0
i = 1
res = [0] * n
while i < n:
	res[i] = a[cur]
	i += 2
	cur += 1
i = 0
while i < n:
	res[i] = a[cur]
	i += 2
	cur += 1
ans = 0
for i in range(1, n - 1):
	if res[i - 1] > res[i] and res[i] < res[i + 1]:
		ans += 1
print(ans)
print(*res)


n = int(input())
a = sorted(list(map(int, input().split())))
arr = [0] * n
i = 0
for j in range(n // 2, n):
	arr[i] = a[j]
	i += 2
i = 1
for j in range(n // 2):
	arr[i] = a[j]
	i += 2
cnt = 0
for i in range(1, n - 1):
	if arr[i - 1] > arr[i] < arr[i + 1]:
		cnt += 1
print(cnt)
print(*arr)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
arr = sorted(list(map(int, input().split())))
if n == 0:
	print(0)
	print(*arr)
elif n == 2:
	print(0)
	arr.sort()
	print(*arr)
else:
	ls = [0] * n
	var = 0
	var2 = n // 2
	for i in range(n):
		if (i + 1) % 2 == 0:
			ls[i] = arr[var]
			var += 1
		else:
			ls[i] = arr[var2]
			var2 += 1
	cntt = 0
	for i in range(1, n - 1):
		if ls[i] < ls[i + 1] and ls[i] < ls[i - 1]:
			cntt += 1
	print(cntt)
	print(*ls)


def solve():
	n = int(input().strip())
	a = list(map(int, input().strip().split()))
	a.sort()
	a1 = a[:n // 2]
	a2 = a[n // 2:]
	flag = True
	cursor1 = 0
	cursor2 = 0
	prevPrice = -100
	ans = 0
	for i in range(len(a2)):
		if i + 1 < len(a2):
			ans += 1 if a2[i] > a1[i] and a1[i] < a2[i + 1] else 0
	print(ans)
	for i in range(n):
		if not flag:
			print(a1[cursor1], end=' ')
			cursor1 += 1
		else:
			print(a2[cursor2], end=' ')
			cursor2 += 1
		flag = not flag
t = 1
for _ in range(t):
	solve()


n = int(input())
A = [int(x) for x in input().split()]
A.sort()
R = [None] * n
R[1::2] = A[:n // 2]
R[0::2] = A[n // 2:]
r = 0
for i in range(1, n - 1):
	r += R[i - 1] > R[i] < R[i + 1]
print(r)
print(*R)


n = int(input())
list1 = list(map(int, input().split()))
list1 = sorted(list1)
res = []
first = list1[:n // 2]
second = list1[n // 2:]
c = 0
if n % 2 == 0:
	for i in range(len(second)):
		res.append(second[i])
		res.append(first[i])
else:
	for i in range(len(second) - 1):
		res.append(second[i])
		res.append(first[i])
	res.append(second[-1])
for i in range(n - 1):
	if res[i] < res[i - 1] and res[i] < res[i + 1]:
		c += 1
print(c)
for num in res:
	print(num, end=' ')


def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
(n, l) = (inp(), inlt())
res = []
l.sort()
for i in range(n // 2):
	if i + n // 2 < n:
		res.append(l[i + n // 2])
		res.append(l[i])
if n % 2:
	res.append(l[-1])
cnt = 0
for i in range(1, len(res) - 1):
	if res[i - 1] > res[i] < res[i + 1]:
		cnt += 1
print(cnt)
print(' '.join([str(x) for x in res]))


import math
from heapq import heappush, heappop, heapify
import random
import string
from collections import deque
from bisect import bisect, bisect_left, bisect_right, insort
import sys
input = sys.stdin.readline
S = lambda : input().rstrip()
I = lambda : int(S())
M = lambda : map(int, S().split())
L = lambda : list(M())
H = 1000000000 + 7
n = I()
l = L()
l.sort()
b = [0] * n
(b[::2], b[1::2]) = (l[n // 2:], l[:n // 2])
print(sum((b[i - 1] > b[i] < b[i + 1] for i in range(1, n - 1))))
print(*b)


n = int(input())
p = [int(x) for x in input().split()]
p.sort()
a = [None] * n
k = 0
if n % 2 == 1:
	for i in range(1, n, 2):
		a[i] = p[k]
		k += 1
	for i in range(0, n, 2):
		a[i] = p[k]
		k += 1
else:
	for i in range(1, n, 2):
		a[i] = p[k]
		k += 1
	for i in range(0, n, 2):
		a[i] = p[k]
		k += 1
ct = 0
for i in range(1, n - 1):
	if a[i] < a[i - 1] and a[i] < a[i + 1]:
		ct += 1
print(ct)
for e in a:
	print(e, end=' ')
print()


n = int(input())
a = sorted(map(int, input().split()))
l = [a[(1 - i % 2) * (n // 2) + i // 2] for i in range(n)]
c = 0
for i in range(1, n - 1):
	if l[i - 1] > l[i] < l[i + 1]:
		c += 1
print(c)
print(' '.join(map(str, l)))


n = int(input())
l = list(map(int, input().split()))
l.sort()
ans = []
if n % 2 == 1:
	ans.append(l[n // 2])
	for i in range(n // 2):
		ans.append(l[i])
		ans.append(l[(n + 1) // 2 + i])
else:
	for i in range(n // 2):
		ans.append(l[(n + 1) // 2 + i])
		ans.append(l[i])
num = 0
for i in range(1, n - 1):
	if ans[i] < min(ans[i - 1], ans[i + 1]):
		num += 1
print(num)
print(' '.join(map(str, ans)))


n = int(input())
a = [int(i) for i in input().split()]
a.sort()
a1 = [0] * n
cnt = 0
a1[0] = a[n // 2]
if n % 2 != 0:
	for i in range(0, n // 2):
		a1[i * 2 + 1] = a[i]
		a1[i * 2 + 2] = a[n // 2 + i + 1]
else:
	for i in range(0, n // 2 - 1):
		a1[1 + i * 2] = a[i]
		a1[i * 2 + 2] = a[n // 2 + i + 1]
	a1[n - 1] = a[n // 2 - 1]
if n == 1:
	print(0)
	print(a1[0])
else:
	s = str(a1[0]) + ' '
	for i in range(1, n - 1):
		s += str(a1[i]) + ' '
		if a1[i] < a1[i + 1] and a1[i] < a1[i - 1]:
			cnt += 1
	s += str(a1[n - 1])
	print(cnt)
	print(s)


n = int(input())
a = list(map(int, input().split()))
a.sort()
ind = 0
ans = [0] * n
cnt = 0
for i in range(1, n, 2):
	ans[i] = a[ind]
	ind += 1
for i in range(0, n, 2):
	ans[i] = a[ind]
	ind += 1
for i in range(1, n - 1):
	if ans[i] < ans[i - 1] and ans[i] < ans[i + 1]:
		cnt += 1
print(cnt)
print(*ans)


def do(l, n):
	o = l[0:n // 2]
	k = l[n // 2:n]
	nlist = []
	for i in range(n // 2):
		nlist.append(str(k[i]))
		nlist.append(str(o[i]))
	if n % 2 == 1:
		nlist.append(str(k[n // 2]))
	return nlist
num = int(input())
l = list(map(int, input().split()))
l.sort()
a = do(l, num)
cnt = 0
for i in range(1, num - 1):
	if int(a[i - 1]) > int(a[i]) and int(a[i + 1]) > int(a[i]):
		cnt += 1
print(cnt)
print(' '.join(a))


from sys import *
input = stdin.readline
listInput = lambda : list(map(int, input().strip().split()))
lineInput = lambda : map(int, input().strip().split())
sJoin = lambda a, sep: f'{sep}'.join(a)
arrJoin = lambda a, sep: f'{sep}'.join(map(str, a))

def main():
	n = int(input())
	arr = sorted(listInput())
	k = set(arr)
	a = arr[:n // 2]
	b = arr[n // 2:]
	l = list(zip(b, a))
	ans = []
	j = 1
	c = 0
	for i in l:
		ans.append(i[0])
		ans.append(i[1])
	if n % 2 == 1:
		ans.append(arr[-1])
	while j < n - 1:
		if ans[j - 1] > ans[j] < ans[j + 1]:
			c += 1
		j += 2
	print(c)
	print(*ans)
main()


n = int(input())
a = sorted(map(int, input().split()))
b = [0] * n
(b[1::2], b[::2]) = (a[:n // 2], a[n // 2:])
print(sum((b[i - 1] > b[i] < b[i + 1] for i in range(1, n - 1))))
print(*b)


n = int(input())
array = list(map(int, input().split(' ')))
array.sort()
menores = array[:int(n / 2)]
maiores = array[int(n / 2):]
index = 0
solucao = []
qtd = 0
flag_left = False
for i in range(n):
	if i % 2 != 0:
		solucao.append(menores[index])
		if menores[index] < solucao[i - 1]:
			flag_left = True
		else:
			flag_left = False
		index += 1
	else:
		solucao.append(maiores[int(i / 2)])
		if flag_left and maiores[int(i / 2)] > solucao[i - 1]:
			qtd += 1
			flag_left = False
print(qtd)
solucao = [str(int) for int in solucao]
print(' '.join(solucao))


import sys
n = int(sys.stdin.readline())
price = [int(v) for v in sys.stdin.readline().strip().split(' ')]
price.sort()
result = []
start = int(n / 2)
for i in range(0, start):
	result.append(price[i + start])
	result.append(price[i])
if n % 2 == 1:
	result.append(price[n - 1])
num = 0
for i in range(1, n - 1, 2):
	if result[i] < result[i - 1] and result[i] < result[i + 1]:
		num += 1
print(num)
print(' '.join((str(v) for v in result)))


n = int(input())
arr = [int(x) for x in input().split()]
arr.sort()
arr1 = arr[0:n // 2]
arr2 = arr[n // 2:]
arr3 = []
for i in range(n // 2):
	arr3.append(arr2[i])
	arr3.append(arr1[i])
if n % 2 != 0:
	arr3.append(arr2[-1])
ans = 0
for i in range(1, n - 1):
	if arr3[i - 1] > arr3[i] and arr3[i + 1] > arr3[i]:
		ans += 1
print(ans)
print(*arr3)


n = int(input())
a = [int(x) for x in input().split()]
a.sort()
ans = 0
ansarr = []
if n < 3:
	print(0)
	print(' '.join([str(x) for x in a]))
elif n % 2 == 0:
	for i in range(n // 2):
		ansarr.append(a[n // 2 + i])
		ansarr.append(a[i])
	for i in range(1, len(a) - 1, 2):
		if ansarr[i] < ansarr[i - 1] and ansarr[i] < ansarr[i + 1]:
			ans += 1
	print(ans)
	print(' '.join([str(x) for x in ansarr]))
else:
	for i in range(n // 2):
		ansarr.append(a[n // 2 + i])
		ansarr.append(a[i])
	ansarr.append(a[len(a) - 1])
	for i in range(1, len(a), 2):
		if ansarr[i] < ansarr[i - 1] and ansarr[i] < ansarr[i + 1]:
			ans += 1
	print(ans)
	print(' '.join([str(x) for x in ansarr]))


n = int(input())
arr = list(map(int, input().split(' ')))
arr = list(sorted(arr))
p = n // 2
resultarr = []
i = 0
while p < n:
	resultarr.append(arr[p])
	if i < n // 2:
		resultarr.append(arr[i])
	i += 1
	p += 1
result = 0
for i in range(1, len(arr) - 1):
	if resultarr[i - 1] > resultarr[i] and resultarr[i + 1] > resultarr[i]:
		result += 1
print(result)
print(' '.join(list(map(str, resultarr))))


def solve(n, ices):
	ices = sorted(ices)
	(former, latter) = (ices[:n // 2], ices[n // 2:])
	res = 0
	res_ice = []
	for i in range((n - 1) // 2):
		res_ice.append(latter[i])
		res_ice.append(former[i])
		if former[i] < latter[i]:
			res += 1
	res_ice.append(latter[-1])
	if n % 2 == 0:
		res_ice.append(former[-1])
	return (res, ' '.join(list(map(str, res_ice))))

def main():
	n = int(input())
	ices = list(map(int, input().split()))
	(res1, res2) = solve(n, ices)
	print(res1)
	print(res2)
main()


from sys import stdin, stdout
from math import *
testing = True

def tp(s='\n'):
	if testing:
		stdout.write(s)

def main():
	n = int(stdin.readline())
	arr = [int(i) for i in stdin.readline().split()]
	arr.sort()
	out = [0] * n
	for i in range(floor(n / 2)):
		out[2 * i + 1] = arr[i]
	for i in range(ceil(n / 2)):
		out[2 * i] = arr[i + floor(n / 2)]
	count = 0
	for i in range(1, n - 1):
		if out[i] < out[i + 1] and out[i] < out[i - 1]:
			count += 1
	print(count)
	print(' '.join([str(i) for i in out]))
main()


n = int(input())
a = [int(inp) for inp in input().split()]
a.sort()
i = 0
j = len(a) // 2
b = []
sw = 1
while j < len(a) or i < len(a) // 2:
	if sw:
		b.append(a[j])
		j += 1
	else:
		b.append(a[i])
		i += 1
	sw = 1 - sw
res = 0
i = 1
while i < len(b) - 1:
	if b[i] < b[i - 1] and b[i] < b[i + 1]:
		res += 1
		i += 1
	i += 1
print(res)
print(*b, sep=' ')


from collections import Counter
n = int(input())
a = list(map(int, input().split()))
a = sorted(a)
b = [0] * n
c = 0
for i in range(1, n, 2):
	b[i] = a[c]
	c += 1
for i in range(0, n, 2):
	b[i] = a[c]
	c += 1
s = 0
for i in range(1, n - 1):
	if b[i] < b[i - 1] and b[i] < b[i + 1]:
		s += 1
print(s)
print(*b)


n = int(input())
arr = list(map(int, input().split()))
arr.sort()
ans = [-1] * n
k = 0
c = 0
for i in range(1, n, 2):
	ans[i] = arr[k]
	k += 1
for i in range(n):
	if ans[i] == -1:
		ans[i] = arr[k]
		k += 1
for i in range(1, n - 1, 2):
	if ans[i] < ans[i - 1] and ans[i] < ans[i + 1]:
		c += 1
print(c)
for i in range(n):
	print(ans[i], end=' ')


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ALPHA = 'abcdefghijklmnopqrstuvwxyz'
M = 10 ** 9 + 7
EPS = 1e-06

def Ceil(a, b):
	return a // b + int(a % b > 0)

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
n = Int()
_a = sorted(array())
a = sorted(list(_a))
small = a[:n // 2]
large = a[n // 2:]
ans = [0] * n
si = 0
li = 0
for i in range(n):
	if i % 2:
		ans[i] = small[si]
		si += 1
	else:
		ans[i] = large[li]
		li += 1
c = 0
for i in range(1, n - 1):
	if ans[i - 1] > ans[i] and ans[i] < ans[i + 1]:
		c += 1
print(c)
print(*ans)


from math import *
from random import *
from bisect import *
from math import *
n = int(input())
mas = sorted(list(map(int, input().split())))
otv = [0 for i in range(n)]
nxt = 0
for i in range(1, n, 2):
	otv[i] = mas[nxt]
	nxt += 1
for i in range(0, n, 2):
	otv[i] = mas[nxt]
	nxt += 1
res = 0
for i in range(1, n - 1):
	if otv[i] < otv[i + 1] and otv[i] < otv[i - 1]:
		res += 1
print(res)
print(' '.join(map(str, otv)))


from math import ceil
n = int(input())
l = list(map(int, input().split()))
l.sort()
if len(l) == 1:
	print(0)
	print(*l)
	exit()
ans = [0] * n
c = 0
for i in range(1, n, 2):
	ans[i] = l[c]
	c += 1
for i in range(0, n, 2):
	ans[i] = l[c]
	c += 1
final = 0
for i in range(1, n - 1):
	if ans[i] < ans[i - 1] and ans[i] < ans[i + 1]:
		final += 1
print(final)
print(*ans)


n = int(input())
nums = list(map(int, input().split()))
nums.sort(reverse=True)
res = [None] * n
b = (n - 1) // 2
for i in range(1, n, 2):
	res[i] = nums.pop()
for i in range(0, n, 2):
	res[i] = nums.pop()
b = 0
for i in range(1, n - 1):
	if res[i - 1] > res[i] and res[i] < res[i + 1]:
		b += 1
print(b)
for x in res:
	print(x, end=' ')
print()


n = int(input())
arr = list(map(int, input().split()))
arr.sort()
res = []
for i in range(n // 2):
	res.append(arr[n // 2 + i])
	res.append(arr[i])
if n % 2:
	res.append(arr[-1])
res2 = 0
for i in range(1, n - 1):
	if res[i] < res[i - 1] and res[i] < res[i + 1]:
		res2 += 1
print(res2)
print(' '.join([str(i) for i in res]))


from bisect import bisect_left
import os
import sys
from io import BytesIO, IOBase
from collections import Counter, defaultdict
from collections import deque
from functools import cmp_to_key
import math
import heapq
import re

def sin():
	return input()

def ain():
	return list(map(int, sin().split()))

def sain():
	return input().split()

def iin():
	return int(sin())
MAX = float('inf')
MIN = float('-inf')
MOD = 1000000007

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	s = set()
	for p in range(2, n + 1):
		if prime[p]:
			s.add(p)
	return s

def readTree(n, m):
	adj = [deque([]) for _ in range(n + 1)]
	for _ in range(m):
		(u, v) = ain()
		adj[u].append(v)
		adj[v].append(u)
	return adj

def main():
	n = iin()
	l = ain()
	l.sort()
	d = [0] * n
	c = 0
	for i in range(1, n, 2):
		d[i] = l[c]
		c += 1
	for i in range(0, n, 2):
		d[i] = l[c]
		c += 1
	ans = 0
	for i in range(1, n - 1, 2):
		if d[i - 1] > d[i] < d[i + 1]:
			ans += 1
	print(ans)
	print(*d)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
if os.getcwd() == 'D:\\code':
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
	input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
import math
import random
input = sys.stdin.readline

def inp():
	return int(input())

def inara():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
n = inp()
ara = inara()
ara.sort()
ans = [0] * n
i = 1
j = 0
while i < n:
	ans[i] = ara[j]
	j += 1
	i += 2
i = 0
while j < n:
	ans[i] = ara[j]
	i += 2
	j += 1
tot = 0
for i in range(1, n - 1):
	if ans[i - 1] > ans[i]:
		if ans[i + 1] > ans[i]:
			tot += 1
print(tot)
print(*ans)


from sys import *
input = stdin.readline
for _ in range(1):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	b = [0] * n
	k = n // 2
	j = k
	for i in range(0, n, 2):
		b[i] = a[j]
		j += 1
	j = 0
	for i in range(1, n, 2):
		b[i] = a[j]
		j += 1
	ans = 0
	for i in range(1, n - 1):
		if b[i] < b[i - 1] and b[i] < b[i + 1]:
			ans += 1
	stdout.write(str(ans) + '\n')
	print(*b)


def solve(n, A):
	A.sort()
	v = [0] * n
	j = 0
	for i in range(1, n, 2):
		v[i] = A[j]
		j += 1
	for i in range(0, n, 2):
		v[i] = A[j]
		j += 1
	count = 0
	for i in range(1, n - 1):
		pre = i - 1
		cur = i
		next = i + 1
		if v[pre] > v[cur] and v[next] > v[cur]:
			count += 1
	print(count)
	return v
res = []
n = int(input())
in_ = map(int, input().split(' '))
for c in in_:
	res.append(int(c))
print(*solve(n, res))


n = int(input())
aaa = list(map(int, input().split()))
aaa.sort()
m0 = (n - 1) // 2
m1 = n // 2
ans = 0
order = [0] * n
for i in range(m1, n):
	order[2 * (i - m1)] = aaa[i]
for i in range(m1):
	order[2 * i + 1] = aaa[i]
	if order[2 * i] > order[2 * i + 1]:
		ans += 1
if n % 2 == 0 and order[-2] > order[-1]:
	ans -= 1
print(ans)
print(*order)


n = int(input())
a = sorted(map(int, input().split()))
mas = [0] * n
j = 1
for i in range(n):
	if j >= n:
		j = 0
	mas[j] = a[i]
	j += 2
ans = 0
for i in range(1, n - 1):
	ans += mas[i - 1] > mas[i] < mas[i + 1]
print(ans)
print(*mas)


t = 1
for _ in range(t):
	n = int(input())
	l = [int(j) for j in input().split()]
	l.sort()
	(c, cnt) = (0, 0)
	k = [0] * n
	for i in range(n // 2):
		k[c] = l[n // 2 + i]
		c += 1
		k[c] = l[i]
		c += 1
	if n % 2 != 0:
		k[n - 1] = l[n - 1]
	for i in range(1, n - 1):
		if k[i - 1] > k[i] and k[i + 1] > k[i]:
			cnt += 1
	print(cnt)
	print(*k)


a = int(input())
b = [int(x) for x in input().split()]
b.sort()
c = []
d = []
result = [0] * a
ans = 0
k = -1
l = 0
m = 0
if a == 1:
	print(0)
	print(*b)
else:
	for i in range(a // 2):
		c.append(b[i])
	for j in range(i + 1, a):
		d.append(b[j])
	while k + 1 != a:
		k += 1
		if (k + 1) % 2 != 0:
			result[k] = d[l]
			l += 1
		else:
			result[k] = c[m]
			m += 1
	for n in range(1, a - 1):
		if result[n] < result[n - 1] and result[n] < result[n + 1]:
			ans += 1
	print(ans)
	print(*result)


n = int(input())
l = [int(i) for i in input().split()]
l.sort()
p = 0
new = []
for i in range(n // 2, n):
	new.append(l[i])
	if p < n // 2:
		new.append(l[p])
		p += 1
x = 0
for i in range(1, n - 1):
	if new[i] < new[i - 1] and new[i] < new[i + 1]:
		x += 1
print(x)
print(*new)


import sys
input = sys.stdin.readline
ins = lambda : input().rstrip()
ini = lambda : int(input().rstrip())
inm = lambda : map(int, input().rstrip().split())
inl = lambda : list(map(int, input().split()))
out = lambda x, s='\n': print(s.join(map(str, x)))
n = ini()
a = sorted(inl())
ans = []
count = 0
for i in range(n // 2):
	ans.append(a[i + n // 2])
	ans.append(a[i])
if n % 2:
	ans.append(a[-1])
for i in range(1, n - 1):
	count += 1 if ans[i] < ans[i - 1] and ans[i] < ans[i + 1] else 0
print(count)
print(*ans)


n = int(input())
arr = list(map(int, input().split()))
arr.sort()
ans = []
f = 0
less = arr[:n // 2]
gre = arr[n // 2:]
l = 0
while l != n // 2:
	ans.append(gre[l])
	ans.append(less[l])
	l += 1
if n % 2 != 0:
	ans.append(gre[-1])
for i in range(1, n - 1):
	if ans[i - 1] > ans[i] and ans[i] < ans[i + 1]:
		f += 1
print(f)
print(*ans)


from sys import *
from collections import deque
n = int(stdin.readline().strip())
things = sorted([int(x) for x in stdin.readline().strip().split(' ')])
count = 0
final = []
big = deque(things[n // 2:])
small = deque(things[:n // 2])
for i in range(len(small)):
	left = big.popleft()
	if big and left > small[0] and (big[0] > small[0]):
		count += 1
		final.append(left)
		final.append(small.popleft())
	else:
		final.append(left)
		final.append(small.pop())
if big:
	final.append(big.pop())
print(count)
print(' '.join(map(str, final)))


from sys import *
input = stdin.readline
listInput = lambda : list(map(int, input().strip().split()))
lineInput = lambda : map(int, input().strip().split())
sJoin = lambda a, sep: f'{sep}'.join(a)
arrJoin = lambda a, sep: f'{sep}'.join(map(str, a))

def main():
	n = int(input())
	arr = sorted(listInput())
	(i, j) = (0, n // 2)
	ans = []
	while i < n // 2:
		ans.append(arr[j])
		ans.append(arr[i])
		i += 1
		j += 1
	if n % 2 == 1:
		ans.append(arr[j])
	c = 0
	j = 1
	while j < n - 1:
		if ans[j - 1] > ans[j] < ans[j + 1]:
			c += 1
		j += 2
	print(c)
	print(*ans)
main()


import sys
input = sys.stdin.readline
N = int(input())
A = [int(_) for _ in input().split()]
sA = sorted(A)
B = ['?'] * N
for i in range(N // 2):
	B[2 * i + 1] = sA[i]
for i in range(N // 2, N):
	B[2 * (i - N // 2)] = sA[i]
answer = 0
for i in range(N // 2):
	if 2 * i + 2 < N:
		if B[2 * i + 1] < B[2 * i] and B[2 * i + 1] < B[2 * i + 2]:
			answer += 1
print(answer)
print(' '.join(map(str, B)))


N = int(input())
List = [int(x) for x in input().split()]
List.sort()
count = 0
arr = [0] * N
index = 0
for i in range(1, N, 2):
	arr[i] = List[index]
	index += 1
for i in range(0, N, 2):
	arr[i] = List[index]
	index += 1
count = 0
for i in range(1, N - 1):
	if arr[i] < arr[i - 1] and arr[i] < arr[i + 1]:
		count += 1
print(count)
print(*arr)


(z, zz) = (input, lambda : list(map(int, z().split())))
zzz = lambda : [int(i) for i in stdin.readline().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac
from itertools import permutations as permu

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
num = 1
for _ in range(num):
	n = int(z())
	arr = szzz()
	lst = [-1] * n
	k = n // 2
	for i in range(k):
		lst[i * 2 + 1] = arr[i]
	for i in range(k, n):
		lst[(i - k) * 2] = arr[i]
	ans = 0
	for i in range(1, n - 1):
		if lst[i - 1] > lst[i] and lst[i] < lst[i + 1]:
			ans += 1
	print(ans)
	print(*lst)


n = int(input())
l = list(map(int, input().split()))
l.sort()
if n < 3:
	print(0)
	print(*l)
else:
	arr = [0] * n
	j = 0
	for i in range(1, n, 2):
		arr[i] = l[j]
		j += 1
	for i in range(0, n, 2):
		arr[i] = l[j]
		j += 1
	ans = 0
	for i in range(1, n - 1):
		if arr[i - 1] > arr[i] and arr[i + 1]:
			ans += 1
	print(ans)
	print(*arr)


n = int(input())
a = list(map(int, input().split()))
a.sort()
l = len(a) // 2
f = a[:l]
s = a[l:]
t = [0] * len(a)
ind = 0
for i in range(len(s)):
	t[ind] = s[i]
	ind += 2
ind = 1
for i in range(len(f)):
	t[ind] = f[i]
	ind += 2
x = 0
for i in range(1, len(t) - 1):
	if t[i] < t[i + 1] and t[i] < t[i - 1]:
		x += 1
print(x)
print(' '.join([str(i) for i in t]))


def main():
	n = int(input())
	lst = list(map(int, input().split()))
	lst = sorted(lst)
	new_lst = []
	for i in range(n // 2):
		new_lst.append(lst[n // 2 + i])
		new_lst.append(lst[i])
	if n % 2 == 1:
		new_lst.append(lst[n - 1])
	cnt = 0
	for i in range(1, n - 1):
		if new_lst[i] < new_lst[i - 1] and new_lst[i] < new_lst[i + 1]:
			cnt += 1
	print(cnt)
	line = ''
	for i in new_lst:
		line += str(i) + ' '
	print(line)
t = 1
for i in range(t):
	main()


n = int(input())
arr = [int(el) for el in input().split()]
ans = [0] * n
arr.sort()
take = 0
count = 0
for i in range(1, n, 2):
	ans[i] = arr[count]
	count += 1
for i in range(0, n, 2):
	ans[i] = arr[count]
	count += 1
for i in range(1, n - 1):
	if ans[i - 1] > ans[i] and ans[i + 1] > ans[i]:
		take += 1
print(take)
print(*ans)


n = int(input())
l = sorted(list(map(int, input().split())))
x = []
if n <= 2:
	print(0)
	print(*l)
elif n % 2 == 0:
	a = l[:n // 2]
	b = l[n // 2:]
	ans = 0
	for i in range(n // 2):
		x.append(b[i])
		x.append(a[i])
	for j in range(1, n - 1, 2):
		if x[j] < x[j + 1] and x[j] < x[j - 1]:
			ans += 1
	print(ans)
	print(*x)
else:
	a = l[:n // 2]
	b = l[n // 2:]
	ans = 0
	for i in range(n // 2):
		x.append(b[i])
		x.append(a[i])
	x.append(b[-1])
	for j in range(1, n, 2):
		if x[j] < x[j + 1] and x[j] < x[j - 1]:
			ans += 1
	print(ans)
	print(*x)


n = int(input())
arr = list(map(int, input().split()))
arr.sort()
small = arr[:len(arr) // 2]
large = arr[len(arr) // 2:]
i = 0
while i < len(arr):
	if i % 2 == 0:
		arr[i] = large[i // 2]
	else:
		arr[i] = small[i // 2]
	i += 1
ans = 0
for i in range(1, len(arr) - 1):
	if arr[i] < arr[i + 1] and arr[i] < arr[i - 1]:
		ans += 1
print(ans)
for i in arr:
	print(i, end=' ')


n = int(input())
l = list(map(int, input().split()))
l.sort()
p = l[0:n // 2]
q = l[n // 2:]
w = []
c = 0
for i in range(n // 2):
	w.append(q[i])
	w.append(p[i])
if n % 2 == 1:
	w.append(q[-1])
for i in range(1, len(w) - 1):
	if w[i] < w[i - 1] and w[i] < w[i + 1]:
		c += 1
print(c)
for i in w:
	print(i, end=' ')
print()


def winner(nums, n):
	nums = sorted(nums)
	ans = []
	i = 0
	j = n // 2
	count = 0
	while j < n:
		ans.append(nums[j])
		if i < n // 2:
			ans.append(nums[i])
			if j != n - 1 and nums[i] != nums[j]:
				count += 1
		j += 1
		i += 1
	return (ans, count)
n = int(input())
(nums, count) = winner([int(i) for i in input().split()], n)
print(count)
for num in nums:
	print(num, end=' ')


n = int(input())
data = list(map(int, input().split()))

def cal(data, n):
	data.sort()
	res = [0] * n
	m = n // 2
	res[0::2] = data[m:]
	res[1::2] = data[:m]
	ans = 0
	for i in range(1, n, 2):
		if i == n - 1:
			break
		if res[i - 1] > res[i] and res[i] < res[i + 1]:
			ans += 1
	print(ans)
	s = ' '.join(list(map(str, res)))
	print(s)
cal(data, n)


import sys
n = int(input())
spheres = list(map(int, input().split()))
spheres.sort()
small = spheres[:n // 2]
big = spheres[n // 2:]
res = []
for i in range(n):
	if i % 2 == 0:
		res.append(big[i // 2])
	else:
		res.append(small[i // 2])
c = 0
for i in range(n):
	if i == 0 or i == n - 1:
		continue
	if res[i - 1] > res[i] < res[i + 1]:
		c += 1
print(c)
print(*res)


n = int(input())
a = list(map(int, input().split()))
a.sort()
b = [0] * n
for i in range((n + 1) // 2 - 1):
	b[2 * i + 1] = a[i]
if n % 2 == 0:
	b[n - 1] = a[n // 2 - 1]
for i in range((n + 1) // 2 - n % 2, n):
	b[2 * i - 2 * ((n + 1) // 2 - n % 2)] = a[i]
cnt = 0
for i in range(n):
	if i > 0 and i < n - 1 and (b[i] < b[i - 1]) and (b[i] < b[i + 1]):
		cnt += 1
print(cnt)
print(' '.join(map(str, b)))


import sys
import math
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split(' ')))
A.sort()
X = N // 2
it1 = iter(A[:X])
it2 = iter(A[X:])
x = True
y = True
ans = []
while x or y:
	try:
		ans.append(next(it2))
	except StopIteration:
		x = False
	try:
		ans.append(next(it1))
	except StopIteration:
		y = False
count = 0
for i in range(1, N - 1):
	if ans[i] < ans[i - 1] and ans[i] < ans[i + 1]:
		count += 1
print(count)
print(' '.join(map(str, ans)))


import os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
a = list(map(int, input().split()))
a = sorted(a)
b = [0] * n
c = 0
for i in range(1, n, 2):
	b[i] = a[c]
	c += 1
for i in range(0, n, 2):
	b[i] = a[c]
	c += 1
s = 0
for i in range(1, n - 1):
	if b[i] < b[i - 1] and b[i] < b[i + 1]:
		s += 1
print(s)
print(*b)


n = int(input())
a = input().split(' ')
if n <= 2:
	print(0)
	print(' '.join(a))
	exit(0)
a = [int(s) for s in a]
a = sorted(a)
b = [0] * n
stepb = 1
for i in range(0, n):
	b[stepb] = a[i]
	stepb += 2
	if stepb >= n:
		stepb = 0
cnt = 0
for i in range(1, n - 1):
	if b[i] < b[i - 1] and b[i] < b[i + 1]:
		cnt += 1
print(cnt)
print(' '.join([str(s) for s in b]))


def check(arr):
	n = len(arr)
	ans = 0
	for i in range(1, n - 1):
		if arr[i] < arr[i - 1] and arr[i] < arr[i + 1]:
			ans += 1
	return ans
n = int(input())
a = list(map(int, input().split()))
arr = [None] * n
a.sort()
start = 0
for i in range(1, n, 2):
	arr[i] = a[start]
	start += 1
for i in range(n):
	if arr[i] == None:
		arr[i] = a[start]
		start += 1
print(check(arr))
print(*arr)


n = int(input())
a = list(map(int, input().split()))
a.sort()
x = [x for x in a]
start = 0
for i in range(1, n, 2):
	x[i] = a[start]
	start += 1
for i in range(0, n, 2):
	x[i] = a[start]
	start += 1
ans = 0
for i in range(1, n, 2):
	if i == n - 1:
		continue
	if x[i] < x[i - 1] and x[i] < x[i + 1]:
		ans += 1
print(ans)
print(*x)


n = int(input())
a = list(map(int, input().split()))
a.sort()
b = a[:n // 2]
c = a[n // 2:]
A = []
for i in c:
	A.append(i)
index = 1
indB = 0
while index < n:
	A.insert(index, b[indB])
	indB += 1
	index += 2
index = 1
ans = 0
while index < len(A) - 1:
	if A[index - 1] > A[index] < A[index + 1]:
		ans += 1
	index += 2
print(ans)
answer = ''
for i in A:
	answer += str(i) + ' '
print(answer)


n = int(input())
a = list(map(int, input().split()))
a.sort()
b = n // 2
c = []
i = 0
while len(c) < n:
	c.append(a[b])
	if len(c) == n:
		break
	c.append(a[i])
	i += 1
	b += 1
ans = 0
i = 0
while i < n - 1:
	if c[i] > c[i + 1]:
		ans += 1
	i += 2
if n % 2 == 0 and c[-1] != c[-2]:
	ans -= 1
print(ans)
print(' '.join((str(x) for x in c)))


from __future__ import division, print_function
import os
import sys
from difflib import SequenceMatcher
from io import BytesIO, IOBase
from collections import Counter, deque
from heapq import heapify, heappush, heappop
from math import gcd, ceil, sqrt, log2, log
from itertools import accumulate, repeat, permutations, compress
from operator import itemgetter
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def rl():
	return list(map(int, input().split()))

def arp(arr):
	return ' '.join([str(i) for i in arr])
n = int(input())
arr = rl()
arr.sort()
if n <= 2:
	print(0)
	print(arp(arr))
else:
	ans = deque([])
	ans1 = deque([])
	for i in range(n):
		if i <= n // 2 - 1:
			ans.append(arr[i])
		else:
			ans1.append(arr[i])
	array = []
	for i in range(n):
		if i & 1:
			array.append(ans.popleft())
		else:
			array.append(ans1.popleft())
	cnt = 0
	if n & 1:
		for i in range(1, n, 2):
			if array[i - 1] > array[i] < array[i + 1]:
				cnt += 1
		print(cnt)
		print(arp(array))
	else:
		for i in range(1, n - 1, 2):
			if array[i - 1] > array[i] < array[i + 1]:
				cnt += 1
		print(cnt)
		print(arp(array))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')


n = int(input())
a = list(map(int, input().split()))
a.sort()
x = []
j = 0
for i in range(int(n // 2), n):
	x.append(a[i])
	if j < int(n // 2):
		x.append(a[j])
		j += 1
ans = 0
for i in range(1, n - 1):
	if x[i] < x[i + 1] and x[i] < x[i - 1]:
		ans += 1
print(ans)
for i in x:
	print(i, end=' ')
print()


n = int(input())
bolas = []
valores = input().split()
cont = 0
for e in range(n):
	bolas.append(int(valores[e]))
bolas.sort()
v1 = bolas[0:n // 2]
v2 = bolas[n // 2:]
v3 = []
for e in range(n // 2):
	v3.append(v2[e])
	v3.append(v1[e])
if n % 2 != 0:
	v3.append(v2[-1])
for e in range(1, n - 1):
	if v3[e - 1] > v3[e] and v3[e] < v3[e + 1]:
		cont += 1
print(cont)
for e in v3:
	print(e, end=' ')


import sys

def answer(n, a):
	a.sort()
	ah = a[n // 2:]
	al = a[:n // 2]
	ans = []
	for i in range(len(ah)):
		if i < len(ah):
			ans.append(ah[i])
		if i < len(al):
			ans.append(al[i])
	num_spheres = 0
	for i in range(1, n - 1):
		if ans[i] < ans[i - 1] and ans[i] < ans[i + 1]:
			num_spheres += 1
	print(num_spheres)
	print(*ans)
	return

def main():
	n = int(sys.stdin.readline())
	a = list(map(int, sys.stdin.readline().split()))
	answer(n, a)
	return
main()


t = 1
for _ in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	arr = sorted(arr)
	temp = [0 for x in range(n)]
	c = 0
	for i in range(1, n, 2):
		temp[i] = arr[c]
		c += 1
	for i in range(0, n, 2):
		temp[i] = arr[c]
		c += 1
	ans = 0
	val = n
	if n % 2 == 0:
		val -= 1
	for i in range(1, val, 2):
		if temp[i] < temp[i - 1] and temp[i] < temp[i + 1]:
			ans += 1
	print(ans)
	print(*temp)


x = int(input())
arr = list(map(int, input().split()))
arr.sort()
temp = []
n = 0
if x % 2 != 0:
	mid = x // 2
	arr1 = arr[:mid]
	arr2 = arr[mid:]
	for i in range(mid):
		temp.append(arr2[i])
		temp.append(arr1[i])
	temp.append(arr[-1])
	for i in range(1, x, 2):
		if temp[i] < temp[i - 1] and temp[i] < temp[i + 1]:
			n += 1
	print(n)
	print(*temp)
else:
	mid = x // 2
	arr1 = arr[:mid]
	arr2 = arr[mid:]
	for i in range(mid):
		temp.append(arr2[i])
		temp.append(arr1[i])
	for i in range(1, x - 1, 2):
		if temp[i] < temp[i - 1] and temp[i] < temp[i + 1]:
			n += 1
	print(n)
	print(*temp)


n = int(input())
a = list(map(int, input().split()))
a.sort()
ans = [0] * n
(ans[1::2], ans[::2]) = (a[:n // 2], a[n // 2:])
print(sum((ans[i - 1] > ans[i] < ans[i + 1] for i in range(1, n - 1))))
print(*ans)


import sys
n = int(sys.stdin.readline())
pricelist = [int(v) for v in sys.stdin.readline().strip().split(' ')]
pricelist.sort()
newlist = [0 for i in range(0, n)]
start = int(n / 2)
for i in range(0, start):
	newlist[i * 2] = pricelist[i + start]
	newlist[i * 2 + 1] = pricelist[i]
if n % 2 == 1:
	newlist[n - 1] = pricelist[n - 1]
total = 0
for i in range(1, n - 1, 2):
	if newlist[i] < newlist[i - 1] and newlist[i] < newlist[i + 1]:
		total += 1
print(total)
print(' '.join((str(v) for v in newlist)))


n = int(input())
esferas = list(map(int, input().split(' ')))
esferas.sort()
limite = int(n / 2)
menores = esferas[:limite]
maiores = esferas[limite:]
indice = 0
reordenadas = []
compras = 0
esquerda = False
for i in range(n):
	if i % 2 == 0:
		reordenadas.append(maiores[int(i / 2)])
		if esquerda and maiores[int(i / 2)] > reordenadas[i - 1]:
			compras += 1
			esquerda = False
	else:
		reordenadas.append(menores[indice])
		if menores[indice] < reordenadas[i - 1]:
			esquerda = True
		else:
			esquerda = False
		indice += 1
print(compras)
reordenadas = [str(int) for int in reordenadas]
print(' '.join(reordenadas))


n = int(input())
a = sorted(map(int, input().split()))
m = (n - 1) // 2
(i, j) = (0, m + 1)
ans = 0
temp = a[m]
if n < 3:
	print(ans)
	for i in a:
		print(i, end=' ')
else:
	z = n * [0]
	z[::2] = a[n // 2:]
	z[1::2] = a[:n // 2]
	for i in range(m):
		if z[2 * i + 1] < z[2 * i] and z[2 * i + 1] < z[2 * i + 2]:
			ans += 1
	print(ans)
	for i in z:
		print(i, end=' ')