Sereja loves integer sequences very much. He especially likes stairs.

Sequence a_1, a_2, ..., a_{|}a| (|a| is the length of the sequence) is stairs if there is such index i (1 ≤ i ≤ |a|), that the following condition is met: a_1 < a_2 < ... < a_{i} - 1 < a_{i} > a_{i} + 1 > ... > a_{|}a| - 1 > a_{|}a|.

For example, sequences [1, 2, 3, 2] and [4, 2] are stairs and sequence [3, 1, 2] isn't.

Sereja has m cards with numbers. He wants to put some cards on the table in a row to get a stair sequence. What maximum number of cards can he put on the table?


-----Input-----

The first line contains integer m (1 ≤ m ≤ 10^5) — the number of Sereja's cards. The second line contains m integers b_{i} (1 ≤ b_{i} ≤ 5000) — the numbers on the Sereja's cards.


-----Output-----

In the first line print the number of cards you can put on the table. In the second line print the resulting stairs.


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

Output
5
5 4 3 2 1

Input
6
1 1 2 2 3 3

Output
5
1 2 3 2 1

Difficulty: EASY
Tags: Sorting

Implementation

Greedy algorithms
Skill types: Sorting

Greedy algorithms

Starter code:

Solutions:
from itertools import groupby
n = int(input())
tab = sorted(list(map(int, input().split())))
while len(tab) > 1 and tab[-1] == tab[-2]:
	tab.pop()
S = []
result = []
for (num, iter_elems) in groupby(tab):
	amount = len(list(iter_elems))
	if amount > 1:
		result += [num]
		S += [num]
	else:
		result += [num]
for x in S[-1::-1]:
	result += [x]
print(len(result))
print(' '.join(map(str, result)))


from collections import Counter
m = int(input())
a = list(map(int, input().split()))
a.sort()
mx = a[-1]
d = Counter(a)
l1 = []
l2 = []
for i in d:
	if i != mx:
		if d[i] >= 2:
			l1.append(i)
			l2.append(i)
		else:
			l1.append(i)
	else:
		l1.append(i)
stair = l1 + l2[::-1]
print(len(stair))
print(*stair)


from collections import Counter
n = int(input())
a = list(map(int, input().strip().split()))[:n]
ans = []
c = Counter(a)
a = sorted(a)
if len(a) == 1:
	ans.append(a[0])
else:
	ans.append(a[0])
	c[a[0]] -= 1
	index = 0
	for i in range(1, n):
		if a[i] != ans[index]:
			ans.append(a[i])
			c[a[i]] -= 1
			index = index + 1
max_value = ans[len(ans) - 1]
i = n - 1
while i >= 0:
	if a[i] != max_value and c[a[i]] >= 1:
		ans.append(a[i])
		c[a[i]] = 0
	i = i - 1
print(len(ans))
for i in ans:
	print(i, end=' ')


from collections import Counter
from itertools import groupby
input()
B = sorted(map(int, input().split()))
C = Counter(B)
B = [k for (k, _) in groupby(B)]
B += [b for b in B[-2::-1] if C[b] > 1]
print(len(B))
print(*B)


from collections import Counter
m = int(input())
b = list(map(int, input().split()))
s = sorted(set(b))
c = Counter(b)
a = []
for x in s[:-1]:
	if c[x] > 1:
		a.append(x)
a = a[::-1]
print(len(s) + len(a))
print(' '.join(map(str, s)), ' '.join(map(str, a)))


import math
n = int(input())
arr = [0] * int(5000.0 + 5)
b = map(int, input().split())
for x in b:
	arr[x] += 1
flag = 0
res = []
for i in range(int(5000.0 + 5) - 1, 0, -1):
	if arr[i] == 0:
		continue
	if flag == 0:
		flag = 1
		res = [i]
	elif arr[i] == 1:
		res += [i]
	else:
		res = [i] + res + [i]
print(len(res))
print(' '.join(map(str, res)))


from collections import deque

class CodeforcesTask381BSolution:

	def __init__(self):
		self.result = ''
		self.n = 0
		self.sequence = []

	def read_input(self):
		self.n = int(input())
		self.sequence = [int(x) for x in input().split(' ')]

	def process_task(self):
		self.sequence.sort()
		mx = self.sequence.pop(-1)
		result = deque()
		result.append(mx)
		while self.sequence:
			adding = self.sequence.pop(-1)
			if adding < result[0]:
				result.appendleft(adding)
			elif adding < result[-1]:
				result.append(adding)
		print(len(result))
		for c in result:
			print(c, end=' ')

	def get_result(self):
		return self.result
Solution = CodeforcesTask381BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


import sys
f = sys.stdin
f.readline()
a = sorted([int(x) for x in f.readline().split()])
b = [a[0]]
c = [True]
for i in range(1, len(a)):
	if a[i] != a[i - 1]:
		b.append(a[i])
	c.append(a[i] != a[i - 1])
for i in range(len(a) - 1, -1, -1):
	if a[i] < b[-1] and (not c[i]):
		b.append(a[i])
print(len(b))
for i in b:
	print(i, end=' ')


from collections import *
n = int(input())
l = list(map(int, input().split()))
m = max(l)
s = sorted(list(set(l)))
d = defaultdict(int)
for i in l:
	d[i] += 1
r = []
for i in d:
	if d[i] >= 2 and i != m:
		r.append(i)
r.sort(reverse=True)
t = s + r
print(len(t))
print(*t)


a = []
b = []
for x in sorted(map(int, [*open(0)][1].split())):
	if len(a) < 1 or a[-1] != x:
		a += [x]
	elif len(b) < 1 or b[0] != x:
		b = [x] + b
if len(b) > 0 and a[-1] == b[0]:
	b = b[1:]
print(len(a) + len(b))
print(*a, *b)


n = int(input())
a = list(map(int, input().split()))
b = sorted(a, reverse=True)
t2 = []
t1 = [b[0]]
count = 1
for i in range(n):
	if b[i] != b[0]:
		if b[i] == b[i - 1]:
			count += 1
		else:
			count = 1
		if count == 1:
			t1.append(b[i])
		elif count == 2:
			t2.append(b[i])
t3 = sorted(t2) + t1
print(len(t3))
print(' '.join((str(i) for i in t3)))


import math
T = 5005
n = int(input())
a = [0] * T
b = map(int, input().split())
for x in b:
	a[x] += 1
flag = 0
answer = 0
ans = []
for i in range(T - 1, 0, -1):
	if a[i] > 0:
		if flag == 0:
			answer += 1
			flag = 1
			ans = [i]
		elif a[i] == 1:
			ans = ans + [i]
		else:
			ans = [i] + ans + [i]
print(len(ans))
print(' '.join(map(str, ans)))


from collections import deque
n = int(input())
b = [int(s) for s in input().split()]
q = deque(sorted(b))
s = []
front = []
while q:
	x = q.popleft()
	if not front or front[-1] != x:
		front.append(x)
	elif s and s[-1] == x:
		continue
	else:
		s.append(x)
ans = front
if front and s and (front[-1] == s[-1]):
	s.pop()
ans += list(reversed(s))
print(len(ans))
print(' '.join((str(x) for x in ans)))


from collections import defaultdict, deque
N = int(input())
MyDict = defaultdict(int)
for i in list(map(int, input().split())):
	MyDict[i] += 1
(First, Last) = ([], deque())
Check = True
Keys = sorted(list(MyDict.keys()))
for key in Keys[:-1]:
	if MyDict[key] > 1:
		First.append(key)
		Last.appendleft(key)
	else:
		if Check:
			First.append(key)
		else:
			Last.appendleft(key)
		Check = not Check
Answer = First + [max(MyDict.keys())] + list(Last)
print(len(Answer))
print(*Answer)


n = int(input())
a = list(map(int, input().split()))
a.sort()
last = 0
v = [0] * n
b = []
for i in range(n):
	if a[i] > last:
		last = a[i]
		b.append(a[i])
		v[i] = 1
for i in range(n - 1, -1, -1):
	if not v[i] and a[i] < last:
		last = a[i]
		b.append(a[i])
		v[i] = 1
print(len(b))
for i in range(len(b)):
	print(b[i], end=' ')


__author__ = 'Adela'

def main():
	m = int(input())
	b = [0 for i in range(0, 5001)]
	res = []
	maxi = 0
	for k in input().split():
		nr = int(k)
		b[nr] += 1
		if nr > maxi:
			maxi = nr
	for i in range(0, maxi + 1):
		if b[i] != 0:
			res.append(i)
			b[i] -= 1
	for j in range(maxi - 1, 0, -1):
		if b[j] != 0:
			res.append(j)
	print(len(res))
	for k in res:
		print(k, end=' ')

def __starting_point():
	main()
__starting_point()


n = int(input())
li = list(map(int, input().split(' ', n)[:n]))
li.sort()
ans = []
hs = [0] * 5001
for i in range(n):
	hs[li[i]] += 1
for i in range(5001):
	if hs[i] != 0:
		ans.append(i)
		hs[i] -= 1
a2 = []
for i in range(5001):
	if hs[i] != 0:
		a2.append(i)
		hs[i] -= 1
a2.reverse()
if len(a2) > 0:
	if a2[0] == ans[len(ans) - 1]:
		ans += a2[1:]
	else:
		ans += a2
print(len(ans))
ans = [str(i) for i in ans]
print(' '.join(ans))


from collections import Counter
__author__ = 'asmn'
n = int(input())
a = Counter(list(map(int, input().split())))
keys = sorted(a.keys())
for i in range(len(keys) - 2, -1, -1):
	if a[keys[i]] > 1:
		keys.append(keys[i])
print(len(keys))
print(' '.join(map(str, keys)))


import sys

def main():
	l = sys.stdin.readline()
	l = sys.stdin.readline()
	numbers = {}
	for el in l.split():
		el = int(el)
		try:
			numbers[el] = numbers[el] + 1
		except KeyError:
			numbers[el] = 1
	nr = numbers.keys()
	nr = sorted(list(set(nr)))
	count = 0
	final = []
	for n in nr:
		if numbers[n] > 0:
			final.append(n)
			numbers[n] -= 1
			count += 1
	nr = sorted(nr, reverse=True)
	for n in nr:
		if n != final[-1]:
			if numbers[n] > 0:
				final.append(n)
				numbers[n] -= 1
				count += 1
	print(count)
	for el in final[:-1]:
		print(el, end=' ')
	print(final[-1])
main()


from collections import deque
m = int(input())
b = [int(i) for i in input().split()]
b.sort(reverse=True)
ans = deque()
ans.append(b[0])
for i in b[1:]:
	if i < ans[-1]:
		ans.append(i)
	elif i < ans[0]:
		ans.appendleft(i)
print(len(ans))
print(' '.join([str(i) for i in ans]))


n = int(input())
l = list(map(int, input().split()))
dic = {}
for i in range(len(l)):
	dic[l[i]] = 0
for i in range(len(l)):
	dic[l[i]] = dic[l[i]] + 1
s = sorted(list(set(l)))
f = []
r = []
for i in range(len(s) - 1):
	if dic[s[i]] > 1:
		f = f + [s[i]]
		r = [s[i]] + r
	else:
		f = f + [s[i]]
ans = f + [s[-1]] + r
print(len(ans))
print(' '.join([str(i) for i in ans]))


n = input()
cards = input().split(' ')
cards = [int(cards[i]) for i in range(len(cards))]
if len(cards) == len(list(set(cards))):
	print(n)
	cards.sort()
	for i in range(len(cards) - 1, 0, -1):
		print(cards[i], end=' ')
	print(cards[0], end='\n')
else:
	cards.sort()
	mycards = [cards[0]]
	cards[0] = None
	for i in range(1, len(cards)):
		if cards[i] > mycards[-1]:
			mycards.append(cards[i])
			cards[i] = None
	for i in range(len(cards) - 1, -1, -1):
		if cards[i] == None:
			continue
		if cards[i] < mycards[-1]:
			mycards.append(cards[i])
			cards[i] = None
	print(len(mycards))
	for i in range(len(mycards) - 1):
		print(mycards[i], end=' ')
	print(mycards[-1], end='\n')


n = int(input())
l = [int(i) for i in input().split()]
if n == 1:
	print(1)
	print(l[0])
else:
	ans = sorted(list(set(l)))
	d = dict()
	for i in l:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	i = 50001
	while i >= 0:
		try:
			z = d[i]
			if z > 1:
				ans.append(i)
			i -= 1
		except:
			i -= 1
	i = 0
	new = []
	while i + 1 < len(ans):
		if ans[i] != ans[i + 1]:
			new.append(ans[i])
			i += 1
		else:
			while i + 1 < len(ans) and ans[i] == ans[i + 1]:
				i += 1
			new.append(ans[i])
			i += 1
	if ans[-1] != new[-1]:
		new.append(ans[-1])
	print(len(new))
	print(*new)


import sys
import collections
case = int(sys.stdin.readline())
test = sys.stdin.readline().split(' ')
num = [int(x) for x in test]
stats = collections.defaultdict(int)
for i in num:
	if stats[i] < 2:
		stats[i] += 1
stats[max(stats)] = 1
rev = []
print(sum(stats.values()))
for i in sorted(stats):
	if stats[i] == 2:
		rev.append(i)
	print(i, end=' ')
for i in rev[::-1]:
	print(i, end=' ')


def sereja_and_stairs():
	m = input()
	b = input()
	nr_allcards = int(m)
	cards = list(map(int, b.split(' ')))
	nr_cards = [0] * 5001
	max_card = 0
	for card in cards:
		nr_cards[card] += 1
		max_card = max(max_card, card)
	up_stairs = []
	down_stairs = []
	for card in range(1, max_card + 1):
		if nr_cards[card] > 0:
			nr_cards[card] -= 1
			down_stairs.append(card)
		if nr_cards[card - 1] > 0:
			up_stairs.append(card - 1)
	down_stairs.reverse()
	stairs = up_stairs + down_stairs
	nr_stairs = len(stairs)
	stairs = ' '.join(map(str, stairs))
	print('{nr:d}\n{stairs:s}'.format(nr=nr_stairs, stairs=stairs))
	return

def __starting_point():
	sereja_and_stairs()
__starting_point()


n = int(input())
arr = list(map(int, input().split()))
arr.sort(reverse=True)
arr2 = [arr[0]]
minL = minR = arr[0]
for i in arr:
	if i < minR:
		arr2.append(i)
		minR = i
	elif i < minL:
		arr2.insert(0, i)
		minL = i
print(len(arr2))
print(*arr2)


m = int(input())
ml = list(map(int, input().split()))
ml.sort()
sl = [ml[0]]
if len(ml) > 1:
	fl = [ml[1]]
	for i in range(2, m):
		if sl[len(sl) - 1] < ml[i]:
			sl.append(ml[i])
		elif fl[len(fl) - 1] < ml[i]:
			fl.append(ml[i])
	if sl[len(sl) - 1] == fl[len(fl) - 1]:
		fl.pop()
	fl.reverse()
	sl += fl
print(len(sl))
for i in sl:
	print(i, end=' ')


n = int(input())
test = list(map(int, input().split()))
test1 = []
result = []
test.sort(reverse=True)
num = test.count(test[0])
test1.append(test[0])
mark = 0
for i in range(num, len(test)):
	if test[i] != test1[len(test1) - 1]:
		test1.append(test[i])
		mark = 0
	elif mark > 0:
		continue
	else:
		test1.append(test[i])
		mark += 1
result.append(test1[0])
for j in range(1, len(test1)):
	if test1[j] != result[len(result) - 1]:
		result.append(test1[j])
	else:
		result.insert(0, test1[j])
print(len(result))
print(' '.join(map(str, result)))


m = int(input())
x = 0
b = []
a = list(map(int, input().split()))
a.sort()
for i in range(m):
	if a[i] > x:
		b.append(a[i])
		x = a[i]
		a[i] = 0
a.reverse()
for i in range(m):
	if a[i] < x and a[i] != 0:
		b.append(a[i])
		x = a[i]
print(len(b))
print(*b)


(n, p) = (input(), [0] * 5001)
for i in map(int, input().split()):
	p[i] += 1
a = [i for i in range(1, 5001) if p[i]]
b = [i for i in a[:-1] if p[i] > 1]
print(len(a) + len(b))
print(' '.join(map(str, a)) + ' ' + ' '.join(map(str, reversed(b))))


n = int(input())
c = list(map(int, input().split()))
c2 = sorted(c)
c3 = [c2[0]]
c4 = []
for i in range(1, len(c2)):
	if c2[i] != c2[i - 1]:
		c3.append(c2[i])
	elif c2[i] != c2[len(c2) - 1]:
		if c4 == []:
			c4 = [c2[i]]
		elif c4[-1] != c2[i]:
			c4.append(c2[i])
n = c4 + c3[::-1]
print(len(n))
print(*n)


i = int(input())
l = sorted(list(map(int, input().split())))
la = ''
n = []
t = []
c = 0
for x in l:
	if la == x:
		if c != 1:
			c = 1
			n.append(x)
	else:
		t.append(x)
		c = 0
	la = x
if len(n) > 0:
	if l[-1] == n[-1]:
		n.pop(-1)
print(len(t) + len(n))
print(' '.join([str(elem) for elem in t + list(reversed(n))]))


import sys
import math
n = int(sys.stdin.readline())
an = [int(x) for x in sys.stdin.readline().split()]
d = dict()
for i in an:
	if i in d:
		d[i] += 1
	else:
		d[i] = 1
t = list(d.keys())
t.sort(reverse=True)
resr = []
resl = []
c = 1
resr.append(str(t[0]))
for i in t[1:]:
	if d[i] > 1:
		c += 2
		resr.append(str(i))
		resl.append(str(i))
	else:
		c += 1
		resr.append(str(i))
k = list(resl[::-1])
k.extend(resr)
print(c)
print(' '.join(k))


n = int(input())
a = list(map(int, input().split()))
stairs = []
sriats = []
sriats1 = []
a.sort()
t = None
for element in a:
	if t != element:
		t = element
		stairs.append(t)
	else:
		sriats.append(t)
t = None
for element in sriats:
	if t != element:
		t = element
		sriats1.append(t)
if sriats1 and sriats1[-1] == stairs[-1]:
	sriats1.pop()
sriats1.reverse()
print(len(stairs) + len(sriats1))
print(*stairs, *sriats1)
b = 0


(n, t) = (int(input()), sorted(map(int, input().split())))
(a, b, k) = ([t[0]], [], False)
for i in range(1, n):
	if t[i] == t[i - 1]:
		k = True
	else:
		if k:
			k = False
			b.append(t[i - 1])
		a.append(t[i])
print(len(a) + len(b))
print(' '.join(map(str, a)) + ' ' + ' '.join(map(str, reversed(b))))


import sys
n = int(input())
A = list(map(int, input().split()))
(f, ans) = ([], [])
i = 0
x = max(A)
for i in range(x + 1):
	f.append(0)
i = 0
for i in range(n):
	f[A[i]] = f[A[i]] + 1
i = 0
while i <= x:
	if f[i] > 0:
		ans.append(i)
		f[i] = f[i] - 1
	i = i + 1
i = x - 1
while i >= 0:
	if f[i] > 0:
		ans.append(i)
	i = i - 1
print(len(ans))
i = 0
while i < len(ans):
	print(ans[i], end=' ')
	i = i + 1
sys.exit(0)


dp = [0 for i in range(5001)]
n = int(input())
l = list(map(int, input().split()))
mx = 0
k = []
d = []
c = 0
for i in l:
	dp[i] += 1
	mx = max(mx, i)
for i in range(1, len(dp)):
	if dp[i]:
		k.append(i)
		dp[i] -= 1
	if dp[i] and i != mx:
		d.append(i)
		dp[i] -= 1
print(len(k) + len(d))
print(' '.join((str(i) for i in k + d[::-1])))


m = int(input())
x = list(map(int, input().split()))
a = 5001 * [0]
x.sort()
ans = []
for i in range(m):
	a[x[i]] += 1
for k in range(len(a)):
	if a[k] > 0:
		ans.append(k)
		a[k] -= 1
for j in range(5000, 0, -1):
	if a[j] > 0 and j != max(x):
		ans.append(j)
print(len(ans))
print(*ans)


N = int(input())
arr = list(map(int, input().split()))
arr.sort()
left = [0]
right = []
flag = 0
local = 0
for i in range(0, len(arr)):
	if local < arr[i]:
		local = arr[i]
		flag = 1
		right.append(arr[i])
	elif flag == 1:
		left.append(arr[i])
		flag = 0
	else:
		continue
if right[len(right) - 1] == left[len(left) - 1]:
	right.pop()
print(len(left) + len(right) - 1)
for i in range(1, len(left)):
	print(left[i], end=' ')
for i in range(0, len(right)):
	print(right[len(right) - 1 - i], end=' ')


n = int(input())
pos = [int(x) for x in input().rstrip().split()]
pos.sort()
left = set()
right = set()
for i in range(len(pos)):
	if pos[i] != pos[-1] and pos[i] == pos[i + 1] and (not pos[i] in left):
		left.add(pos[i])
		right.add(pos[i])
	elif pos[i] not in left:
		left.add(pos[i])
print(len(right) + len(left))
for i in sorted(left):
	print(i, end=' ')
for i in sorted(right)[::-1]:
	print(i, end=' ')


import random
n = int(input())
mass = list(map(int, input().split()))
temp_mass = [0] * 50001
for elem in mass:
	temp_mass[elem] += 1
temp2_mass = []
for i in range(5001):
	if temp_mass[i] != 0:
		temp2_mass.append(i)
		temp_mass[i] -= 1
max_tmp = max(mass)
for i in range(5001, 0, -1):
	if temp_mass[i] != 0 and i != max_tmp:
		temp2_mass.append(i)
print(len(temp2_mass))
print(' '.join(map(str, temp2_mass)))


n = int(input())
array = list(map(int, input().split()))
array.sort()
up_set = set()
down_set = set()
for i in range(n):
	up_set.add(array[i])
	if array[i] == array[i - 1] and array[i] != array[-1]:
		down_set.add(array[i])
print(len(up_set) + len(down_set))
print(*list(sorted(up_set)), *reversed(list(sorted(down_set))))


from sys import stdin
inFile = stdin
tokens = []
tokens_next = 0

def next_str():
	global tokens, tokens_next
	while tokens_next >= len(tokens):
		tokens = inFile.readline().split()
		tokens_next = 0
	tokens_next += 1
	return tokens[tokens_next - 1]

def nextInt():
	return int(next_str())
n = nextInt()
c = [0] * 5001
for i in range(n):
	c[nextInt()] += 1
res = list(filter(lambda x: c[x] > 0, range(1, 5001)))
res2 = list(filter(lambda x: c[x] > 1, range(1, 5001)))
if len(res2) > 0 and res2[-1] == res[-1]:
	del res2[-1]
res_all = res + list(reversed(res2))
print(len(res_all))
for i in res_all:
	print(i, end=' ')


import math
import itertools
import collections

def getdict(n):
	d = {}
	if type(n) is list:
		for i in n:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
	else:
		for i in range(n):
			t = ii()
			if t in d:
				d[t] += 1
			else:
				d[t] = 1
	return d

def cdiv(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

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

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

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def wr(arr):
	return ' '.join(map(str, arr))

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def revn(n):
	m = 0
	while n > 0:
		m = m * 10 + n % 10
		n = n // 10
	return m
n = ii()
b = li()
t = [0] * 5001
(ans, ext) = ([], [])
for i in range(n):
	t[b[i]] += 1
for i in range(5001):
	if t[i] == 1:
		ans.append(i)
	elif t[i] > 1:
		ans.append(i)
		ext.append(i)
ext.reverse()
if len(ext) == 0 or ans[-1] == ext[0]:
	ans.extend(ext[1:])
else:
	ans.extend(ext)
print(len(ans))
print(wr(ans))


n = int(input())
a = list(map(int, input().split()))
a.sort()
r = [a[0]]
b = []
for i in range(1, n):
	if a[i] > r[-1]:
		r.append(a[i])
	else:
		b.append(a[i])
for i in range(len(b) - 1, -1, -1):
	if b[i] < r[-1]:
		r.append(b[i])
print(len(r))
for i in r:
	print(i, end=' ')


n = int(input())
quant = [0] * 5001
numbers = list(map(int, input().split()))
res = []
m = max(numbers)
for i in range(n):
	quant[numbers[i]] += 1
i = 1
while i < m:
	if quant[i] > 0:
		res.append(i)
		quant[i] -= 1
	i += 1
res.append(m)
i = m - 1
while i > 0:
	if quant[i] > 0:
		res.append(i)
		quant[i] -= 1
	i -= 1
print(len(res))
print(*res)


from sys import stdin, stdout

def solve(c, n):
	c = list(sorted(c))
	root = Root()
	root.value = c.pop()
	left_bound = root
	right_bound = root
	while len(c) > 0:
		val = c.pop()
		node = Node()
		node.value = val
		if left_bound.value > val:
			node.prev = left_bound
			if left_bound == root:
				root.left = node
			else:
				left_bound.next = node
			left_bound = node
		elif right_bound.value > val:
			node.prev = right_bound
			if right_bound == root:
				root.right = node
			else:
				right_bound.next = node
			right_bound = node
	res_len = 0
	res_str = ''
	p = left_bound
	while p is not root:
		res_str += str(p.value) + ' '
		res_len += 1
		p = p.prev
	res_str += str(p.value) + ' '
	res_len += 1
	p = root.right
	while p is not None:
		res_str += str(p.value) + ' '
		res_len += 1
		p = p.next
	return (res_len, res_str)

class Node:
	value = None
	next = None
	prev = None

class Root:
	value = None
	right = None
	left = None

def __starting_point():
	n_s = stdin.readline()
	cards_s = stdin.readline().split(' ')
	n = int(n_s)
	cards = [int(c) for c in cards_s]
	root = solve(cards, n)
	print('{}\n{}'.format(root[0], root[1]))
__starting_point()


m = int(input())
b = list(map(int, input().split()))
b.sort()
cnt = 0
inc = []
des = []
x = []
maxx = max(b)
c = 0
for i in range(0, m):
	if i % 2 == 0:
		if b[i] not in inc:
			inc.append(b[i])
			cnt += 1
	elif b[i] not in des:
		des.append(b[i])
		cnt += 1
des.reverse()
x = inc + des
k = x.count(maxx)
if k > 1:
	cnt -= 1
	x.remove(maxx)
print(cnt)
print(*x)


n = int(input())
lst = [int(i) for i in input().split()]
(count, result) = ([0] * (10 ** 5 + 1), list())
for elem in lst:
	count[elem] += 1
for i in range(10 ** 5 + 1):
	if count[i]:
		result.append(i)
		count[i] -= 1
if len(result) != n:
	for i in reversed(range(10 ** 5 + 1)):
		if count[i] and result[-1] != i:
			result.append(i)
print(len(result))
print(*result)


import math
import os
import random
import re
import sys
import functools
from operator import itemgetter, attrgetter
from collections import Counter
m = int(input())
b = list(map(int, input().split()))
s = sorted(set(b))
d = dict()
r = []
for i in b:
	d[i] = d.get(i, 0) + 1
for k in s[:-1]:
	if d[k] > 1:
		r.append(k)
print('{0}\n{1} {2}'.format(len(s) + len(r), ' '.join(map(str, s)), ' '.join(map(str, r[::-1]))))


m = int(input())
b = [int(i) for i in input().split()]
dd = []
ans = []
ccc = dict()
for i in b:
	if i in ccc:
		ccc[i] += 1
	else:
		ccc[i] = 1
for i in set(b):
	dd.append((i, ccc[i]))
dd.sort(key=lambda x: x[0])
for i in range(len(dd) - 1):
	if dd[i][1] > 1:
		ans.append(dd[i][0])
ans.append(dd[-1][0])
for i in range(len(dd) - 2, -1, -1):
	ans.append(dd[i][0])
print(len(ans))
print(*ans)


n = int(input())
d = {}
for i in map(int, input().split()):
	if i not in d:
		d[i] = 0
	d[i] += 1
a = []
for i in sorted(d):
	d[i] -= 1
	a.append(i)
	if d[i] == 0:
		d.pop(i)
b = []
for i in sorted(d):
	d[i] -= 1
	b.append(i)
	if d[i] == 0:
		d.pop(i)
if not b:
	print(len(a) + len(b))
	print(*reversed(a))
else:
	if b[-1] == a[-1]:
		a.pop()
	print(len(a) + len(b))
	print(*b, *reversed(a))


import sys
import math
input = sys.stdin.readline
m = int(input())
l = sorted([int(i) for i in input().split()], reverse=True)
c = 1
l1 = [l[0]]
l2 = []
c1 = 0
c2 = 1
for i in range(1, m):
	if l[i] != l[i - 1]:
		if c2 == 1:
			c2 = 0
		c1 = 0
		c += 1
		l1.append(l[i])
	elif c1 == 0 and c2 == 0:
		l2.append(l[i])
		c += 1
		c1 = 1
l1 = l2[::-1] + l1
print(c)
print(*l1)


n = int(input())
a = list(map(int, input().split()))
count = [0] * (10 ** 5 + 1)
for i in a:
	count[i] += 1
ans = []
for i in range(10 ** 5 + 1):
	if count[i]:
		ans.append(i)
		count[i] -= 1
if len(ans) != n:
	for i in reversed(range(10 ** 5 + 1)):
		if count[i] and ans[-1] != i:
			ans.append(i)
print(len(ans))
print(*ans)


def heapSort(li):

	def downHeap(li, k, n):
		new_elem = li[k]
		while 2 * k + 1 < n:
			child = 2 * k + 1
			if child + 1 < n and li[child] < li[child + 1]:
				child += 1
			if new_elem >= li[child]:
				break
			li[k] = li[child]
			k = child
		li[k] = new_elem
	size = len(li)
	for i in range(size // 2 - 1, -1, -1):
		downHeap(li, i, size)
	for i in range(size - 1, 0, -1):
		temp = li[i]
		li[i] = li[0]
		li[0] = temp
		downHeap(li, 0, i)
	return li
n = input()
sqc1 = [int(i) for i in input().split(' ')]
sqc2 = [str(i) for i in heapSort(list(set(sqc1)))]
for i in sqc2:
	sqc1.remove(int(i))
sqc1 = [str(i) for i in heapSort(list(set(sqc1)))]
sqc2 = sqc2[::-1]
if len(sqc1) == 0:
	print(len(sqc2))
	print(' '.join(sqc2).strip())
elif sqc1[-1] == sqc2[0]:
	print(len(sqc1) + len(sqc2) - 1)
	print((' '.join(sqc1[:-1]) + ' ' + ' '.join(sqc2)).strip())
else:
	print(len(sqc1) + len(sqc2))
	print((' '.join(sqc1) + ' ' + ' '.join(sqc2)).strip())


import sys
from itertools import groupby
lines = []
for Line in sys.stdin:
	lines.append(Line.rstrip('\n'))
n = int(lines[0])
del lines[0]
cards = list(map(int, lines[0].split()))
cards = [list(g) for (k, g) in groupby(sorted(cards)[::-1])]
r = []
for i in range(len(cards)):
	r += [cards[i][0]]
	cards[i].pop(0)
cards = cards[::-1]
r2 = []
for i in range(len(cards)):
	if cards[i] != []:
		r2 += [cards[i][0]]
if r2 != [] and r != [] and (r[0] == r2[-1]):
	r.pop(0)
print(len(r2 + r))
print(*r2 + r)


n = int(input())
l = [int(x) for x in input().split()]
d = {}
for x in l:
	if x not in d:
		d[x] = 1
	else:
		d[x] += 1
H = {}
for x in d:
	H[x] = min(2, d[x])
ans = []
for x in H:
	ans.append(x)
	H[x] -= 1
ans1 = []
for x in H:
	if H[x] > 0:
		ans1.append(x)
ans.sort()
ans1.sort()
if len(ans1) > 0 and ans[-1] == ans1[-1]:
	ans1.pop()
ans = ans + ans1[::-1]
print(len(ans))
print(*ans)


read = lambda : map(int, input().split())
m = int(input())
b = list(read())
n = 5010
c = [0] * n
for i in b:
	c[i] += 1
(a1, a2) = ([], [])
for i in range(n):
	if c[i] >= 1:
		a1.append(i)
	if c[i] >= 2:
		a2.append(i)
if a1 and a2 and (a1[-1] == a2[-1]):
	a2.pop()
ans = a1 + a2[::-1]
print(len(ans))
print(*ans)


from collections import Counter, OrderedDict
for _ in range(1):
	n = int(input())
	arr = list(map(int, input().split()))
	hashMap = Counter(arr)
	maxx = max(arr)
	for i in hashMap:
		if hashMap[i] > 2:
			hashMap[i] = 2
	hashMap[maxx] = 1
	total = 0
	for i in hashMap:
		total += hashMap[i]
	output = []
	hashMap = OrderedDict(sorted(hashMap.items()))
	for i in hashMap:
		if hashMap[i] != 0:
			output.append(i)
			hashMap[i] -= 1
	hashMap = OrderedDict(sorted(hashMap.items(), reverse=True))
	for i in hashMap:
		if hashMap[i] != 0:
			output.append(i)
			hashMap[i] -= 1
	output = list(map(str, output))
	print(total)
	print(' '.join(output))


n = int(input())
a = list(map(int, input().split()))
ans = []
ans1 = []
for i in range(0, len(a)):
	if a[i] not in ans:
		ans.append(a[i])
	elif a[i] not in ans1:
		ans1.append(a[i])
	else:
		pass
ans.sort()
ans1.sort()
if len(ans1) > 0:
	if ans[-1] == ans1[-1]:
		ans1.pop(-1)
print(len(ans) + len(ans1))
print(*ans + ans1[::-1])


import sys
import math
import bisect

def solve(A):
	n = len(A)
	d = dict()
	for a in A:
		if a not in d:
			d[a] = 0
		d[a] += 1
	B = []
	C = []
	for k in sorted(d.keys()):
		if d[k] == 1:
			B.append(k)
		elif d[k] >= 2:
			B.append(k)
			C.append(k)
	if len(B) and len(C) and (B[-1] == C[-1]):
		C.pop()
	return B + C[::-1]

def main():
	n = int(input())
	A = list(map(int, input().split()))
	B = solve(A)
	print(len(B))
	print(' '.join(list((str(a) for a in B))))
main()


n = int(input())
s = sorted(map(int, input().split()))
m = 5001
c = [0] * m
for i in range(n):
	c[s[i]] += 1
c[s[-1]] = 1
a = [i for i in range(m) if c[i] > 0] + [i for i in range(m) if c[i] > 1][::-1]
print(len(a))
print(' '.join(map(str, a)))


n = int(input())
mass = list(map(int, input().split()))
mass.sort()
new_mass = []
new_mass1 = []
for i in range(0, n):
	if i == 0:
		new_mass.append(mass[0])
	elif mass[i] == mass[i - 1]:
		if len(new_mass1) == 0 or new_mass1[-1] != mass[i]:
			new_mass1.append(mass[i])
	else:
		new_mass.append(mass[i])
if len(new_mass1) == 0:
	ukaz = -1
else:
	ukaz = len(new_mass1) - 1
	while ukaz >= 0 and new_mass[-1] <= new_mass1[ukaz]:
		ukaz -= 1
if ukaz < 0:
	if len(new_mass) > len(new_mass1):
		print(len(new_mass))
		print(*new_mass)
	else:
		print(len(new_mass1))
		print(*new_mass1)
else:
	print(len(new_mass) + ukaz + 1)
	temp = new_mass + new_mass1[ukaz::-1]
	print(*temp)


n = int(input())
cards = list(map(int, input().split()))
cards.sort()
stair = []
stair_rev = []
stair.append(cards[0])
cards.pop(0)
a = 0
while a < len(cards):
	if cards[a] > stair[-1]:
		stair.append(cards[a])
		cards.pop(a)
	else:
		a += 1
tmpmax = stair[-1]
cards.sort(reverse=True)
a = 0
while a < len(cards):
	if cards[a] < tmpmax:
		tmpmax = cards[a]
		stair_rev.append(cards[a])
		cards.pop(a)
	else:
		a += 1
stair += stair_rev
print(len(stair))
s = ''
for d in stair[0:-1]:
	s += str(d) + ' '
s += str(stair[-1])
print(s)


input()
A = map(int, input().split())
arr = [0] * 5001
for a in A:
	arr[a] += 1
ans = list()
last_i = 0
for i in range(1, len(arr)):
	if arr[i] > 0:
		ans.append(i)
		arr[i] -= 1
		last_i = i
for i in range(last_i - 1, 0, -1):
	if arr[i] > 0:
		ans.append(i)
print(len(ans))
print(' '.join(map(str, ans)))


N = int(input())
A = list(map(int, input().split()))
X = max(A)
Ar = [0] * (X + 1)
for a in A:
	Ar[a] += 1
	Ar[a] = min(Ar[a], 2)
Ar[-1] = min(Ar[-1], 1)
print(sum(Ar))
for (i, x) in enumerate(Ar):
	if x > 0:
		print(i, end=' ')
		Ar[i] -= 1
for i in range(len(Ar) - 2, -1, -1):
	if Ar[i] > 0:
		print(i, end=' ')


n = int(input())
karts = list(map(int, input().split()))
output = []
output2 = []
karts.sort()
repeat = 0
for i in range(n - 1, 0, -1):
	if karts[i] == karts[i - 1]:
		repeat += 1
		if repeat == 1:
			output2.append(karts[i])
	else:
		repeat = 0
		output.append(karts[i])
output.append(karts[0])
if len(output2) > 0 and output[0] == output2[0]:
	output2.pop(0)
print(len(output2) + len(output))
for i in range(len(output2) - 1, -1, -1):
	print(output2[i], end=' ')
print(*output)


n = int(input())
a = list(map(int, input().split()))
a.sort()
b = []
for i in range(n - 2):
	if not (a[i] == a[i + 1] and a[i] == a[i + 2]):
		b.append(a[i])
b.append(a[n - 2])
b.append(a[n - 1])
if b[-1] == b[-2]:
	b = b[:len(b) - 1]
print(len(b))
b1 = []
b2 = []
for i in range(len(b)):
	if i % 2 == 0:
		b2.append(b[i])
	else:
		b1.append(b[i])
for i in b1:
	print(i, end=' ')
b2.reverse()
for i in b2:
	print(i, end=' ')


import collections as cl
(n, b) = (int(input()), list(sorted(cl.Counter(map(int, input().split())).items())))
(pref, post) = ([], [])
for i in range(len(b) - 1):
	pref.append(b[i][0])
	if b[i][1] > 1:
		post.append(b[i][0])
print(len(pref) + len(post) + 1)
print(*pref, b[-1][0], *reversed(post))


m = int(input())
l = list(map(int, input().split()))
l.sort()
d = {}
for i in l:
	if i in d:
		d[i] += 1
	else:
		d[i] = 1
m = max(l)
p = []
a = []
for i in d:
	if i != m and d[i] > 1:
		p.append(i)
		a.append(i)
	else:
		a.append(i)
a.sort(reverse=True)
print(len(a) + len(p))
print(*p, *a)


input()
a = list(map(int, input().split()))
b = [0] * 5000
for x in a:
	b[x - 1] += 1
r = ''
count = 0
max = 0
for i in range(5000):
	if b[i] > 0:
		count += 1
		max = i
		r += str(i + 1) + ' '
		b[i] -= 1
for i in range(max - 1, -1, -1):
	if b[i] > 0:
		count += 1
		r += str(i + 1) + ' '
print(count)
print(r)


n = int(input())
l = list(map(int, input().split()))
ans = []
b = []
for i in range(len(l)):
	if l[i] not in ans:
		ans.append(l[i])
	elif l[i] not in b:
		b.append(l[i])
ans = sorted(ans)
b = sorted(b)
if len(b) > 0:
	if ans[-1] == b[-1]:
		b.pop(-1)
print(len(ans) + len(b))
print(*ans + b[::-1])


from collections import Counter
n = int(input())
x = list(map(int, input().split()))
cnt = Counter(x)
y = sorted(cnt)
z = []
for i in y:
	if cnt[i] > 0:
		z.append(i)
		cnt[i] -= 1
for i in range(len(y) - 2, -1, -1):
	if cnt[y[i]] > 0:
		z.append(y[i])
print(len(z))
z.reverse()
for i in z:
	print(i, end=' ')


m = int(input())
a = map(int, input().split())

def seryozha_and_ladder(a):
	a = sorted(a, key=lambda x: -x)
	prev_i = a[0]
	cnt = 2
	first_part = [a[0]]
	second_part = []
	for i in a:
		if i == prev_i:
			cnt += 1
			if cnt == 2:
				second_part.append(i)
		else:
			first_part.append(i)
			cnt = 1
			prev_i = i
	return first_part[::-1] + second_part
res = seryozha_and_ladder(a)
print(len(res))
print(' '.join(map(str, res)))


from itertools import chain

def main():
	input()
	(lb, la) = ([], [])
	a = b = -1
	it = iter(sorted(map(int, input().split())))
	try:
		while True:
			c = next(it)
			if a != c:
				a = c
				la.append(a)
			c = next(it)
			if b != c:
				b = c
				lb.append(b)
			pass
	except StopIteration:
		if a == b:
			del lb[-1]
	print(len(la) + len(lb))
	print(' '.join(map(str, chain(la, lb[::-1]))))

def __starting_point():
	main()
__starting_point()


n = int(input())
t = list(map(int, input().split()))
t.sort()
f = t[::-1]
x = []
u = []
while True:
	if len(f) > 0:
		if len(u) == 0:
			u.append(f.pop())
		elif len(f) > 0:
			if f[-1] > u[-1]:
				u.append(f.pop())
			elif len(x) > 0:
				if f[-1] > x[-1]:
					x.append(f.pop())
				else:
					f.pop()
			else:
				x.append(f.pop())
		else:
			break
	else:
		break
if len(x) > 0:
	if x[-1] < u[-1]:
		pass
	else:
		x.pop()
print(len(u + x[::-1]))
print(*u + x[::-1])


n = int(input())
a = list(map(int, input().split()))
v = [0] * 5000
for i in range(n):
	if v[a[i] - 1] < 2:
		v[a[i] - 1] += 1
if v[max(a) - 1] == 2:
	v[max(a) - 1] = 1
o = []
for i in range(5000):
	if v[i] != 0:
		o.append(i + 1)
for i in range(4999, -1, -1):
	if v[i] == 2:
		o.append(i + 1)
print(len(o))
print(*o)


from collections import Counter
n = int(input())
l = Counter((int(x) for x in input().split()))
a = []
b = []
for (x, y) in list(l.items()):
	a.append(x)
	if y > 1:
		b.append(x)
a.sort()
b.sort()
if len(b) and a[-1] == b[-1]:
	b = b[:-1]
ans = a + b[::-1]
print(len(ans))
print(' '.join((str(x) for x in ans)))


__author__ = 'maaanu'

def start():
	m = int(input())
	cards = [int(x) for x in input().split(' ')]
	cards.sort()
	max_cards = len(cards)
	last_card = cards.pop()
	table = [last_card]
	for val in reversed(cards):
		if val < last_card:
			last_card = val
			table.append(last_card)
		elif val < table[0]:
			table.insert(0, val)
		else:
			max_cards -= 1
	print(max_cards)
	print(''.join([str(number) + ' ' for number in table]))
start()


n = int(input())
ip = input().split(' ')
ip2 = list(map(int, ip))
ip2 = sorted(ip2)
leader = max(ip2)
lower = set()
upper = set()
for x in ip2:
	if x < leader and x not in lower:
		lower.add(x)
	elif x < leader and x not in upper:
		upper.add(x)
lower.add(leader)
lower = sorted(lower)
print(len(lower) + len(upper))
upper = sorted(upper, reverse=True)
res = lower + upper
for y in range(len(res)):
	if y != len(res) - 1:
		print(res[y], end=' ')
	else:
		print(res[y])


n = int(input())
d = {}
l = list(map(int, input().split()))
for i in range(n):
	if l[i] in d:
		d[l[i]] = d[l[i]] + 1
	else:
		d[l[i]] = 1
k = list(set(l))
m = sorted(k)
k.sort(reverse=True)
l = []
for i in k:
	if i == k[0]:
		continue
	elif d[i] > 1:
		l.append(i)
k = m + l
print(len(k))
print(*k)


import sys
m = int(sys.stdin.readline())
b = [int(x) for x in sys.stdin.readline().split(' ')]
b.sort()
l1 = []
l2 = []
last = b[0]
rep = 1
l1.append(last)
for i in range(1, len(b)):
	if b[i] == last:
		rep += 1
	else:
		last = b[i]
		rep = 1
	if rep == 1:
		l1.append(last)
	elif rep == 2:
		l2.append(last)
if l2 and l2[-1] == l1[-1]:
	l2.pop()
l2.reverse()
print(len(l1) + len(l2))
print(' '.join([str(x) for x in l1]), end='')
if l2:
	print(' ', end='')
	print(' '.join([str(x) for x in l2]))
else:
	print()


input()
list = [0] * 5001
for i in map(int, input().split()):
	list[i] += 1
a = [i for i in range(len(list)) if list[i]]
b = [i for i in a[:-1] if list[i] > 1]
print(len(a) + len(b))
print(' '.join(map(str, a + b[::-1])))


n = int(input())
A = list(map(int, input().split()))
A.sort()
if len(A) > 1:
	B = [A[0], A[1]]
else:
	B = [A[0]]
for i in range(n - 2):
	if A[i] != A[i + 2]:
		B.append(A[i + 2])
if len(A) > 1 and A[-1] == A[-2]:
	B.pop()
print(len(B))
(P, Q) = ([], [])
for i in range(len(B) - 1, -1, -1):
	if i % 2:
		P.append(B[i])
	else:
		Q.append(B[i])
print(*P[::-1], *Q)


m = int(input())
b = list(map(int, input().split()))
d = dict()
for i in b:
	if i in d:
		d[i] += 1
	else:
		d[i] = 1
t = list(d.keys())
t.sort()
i = len(t) - 2
while i >= 0:
	if d[t[i]] > 1:
		t.append(t[i])
	i -= 1
print(len(t))
print(' '.join(map(str, t)))


from sys import stdout, stdin, setrecursionlimit
from io import BytesIO, IOBase
from collections import *
from itertools import *
from random import *
from bisect import *
from string import *
from queue import *
from heapq import *
from math import *
from re import *
from os import *

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 = read(self._fd, max(fstat(self._fd).st_size, 8192))
			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 = read(self._fd, max(fstat(self._fd).st_size, 8192))
			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:
			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')
(stdin, stdout) = (IOWrapper(stdin), IOWrapper(stdout))
(graph, mod, szzz) = ({}, 10 ** 9 + 7, lambda : sorted(zzz()))

def getStr():
	return input()

def getInt():
	return int(input())

def listStr():
	return list(input())

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

def isInt(s):
	return '0' <= s[0] <= '9'

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

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

def output(answer, end='\n'):
	stdout.write(str(answer) + end)

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)
dx = [-1, 1, 0, 0, 1, -1, 1, -1]
dy = [0, 0, 1, -1, 1, -1, -1, 1]
daysInMounth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
n = getInt()
arr = szzz()
cnt = Counter(arr)
set1 = set()
set2 = set()
for i in arr:
	if i not in set1:
		set1.add(i)
	elif i not in set2:
		set2.add(i)
a = sorted(set1)
b = sorted(set2, reverse=True)
new = list(a + b)
mx = max(new)
if new.count(mx) > 1:
	new.remove(mx)
print(len(new))
print(*new)


n = int(input())
a = list(map(int, input().split()))
m = max(a)
right = []
left = []
cnt = [0] * 5010
for i in range(n):
	cnt[a[i]] += 1
s = set(a)
n = len(s)
for i in s:
	if cnt[i] > 0 and i < m:
		right.append(i)
		cnt[i] -= 1
for i in s:
	if cnt[i] > 0 and i < m:
		left.append(i)
print(1 + len(left) + len(right))
left.sort()
for i in range(len(left)):
	print(left[i], end=' ')
print(m, end=' ')
right.sort(reverse=True)
for i in range(len(right)):
	print(right[i], end=' ')


n = int(input())
a = list(map(int, input().split()))
col = [0] * 5001
ans = []
do_max = []
posle_max = []
max = 0
for i in range(n):
	col[a[i]] += 1
	if a[i] > max:
		max = a[i]
for i in range(5001):
	if i == max:
		do_max.append(i)
	elif col[i] >= 2:
		do_max.append(i)
		posle_max.append(i)
	elif col[i] == 1:
		do_max.append(i)
print(len(do_max) + len(posle_max))
for i in range(len(do_max)):
	print(do_max[i], end=' ')
for i in range(len(posle_max) - 1, -1, -1):
	print(posle_max[i], end=' ')


a = int(input())
b = list(map(int, input().split()))
b.sort()
dupes = set()
for i in range(1, a):
	if b[i] == b[i - 1]:
		dupes.add(b[i])
c = sorted(list(set(b)))
d = sorted(list(dupes))
if len(d):
	if c[-1] == d[-1]:
		y = c[:-1] + d[::-1]
	else:
		y = c + d[::-1]
else:
	y = c
print(len(y))
print(' '.join(map(str, y)))


from sys import stdin
for line in stdin:
	N = list(map(int, line.split()))
	numbers = list(map(int, input().split()))
	numbers.sort(reverse=True)
	l = len(numbers)
	temp = []
	for number in numbers:
		if len(temp) == 0:
			temp.append(number)
		elif temp[-1] != number:
			temp.append(number)
		elif temp[0] != number:
			temp.insert(0, number)
	print(len(temp))
	i = 1
	printlen = len(temp)
	for e in temp:
		if i == printlen:
			print(e)
		else:
			print(e, end=' ')
		i += 1


input()
cards = list(map(int, input().split()))
cards2 = []
for card in sorted(cards):
	if len(cards2) == 0:
		cards2.append((card, 1))
	elif card == cards2[-1][0]:
		cards2[-1] = (card, cards2[-1][1] + 1)
	else:
		cards2.append((card, 1))
r = []
for card2 in cards2:
	r.append(card2[0])
for card2 in cards2[-2::-1]:
	if card2[1] >= 2:
		r.append(card2[0])
print(len(r))
print(*r)


m = int(input())
arr = list(map(int, input().split()))
freq = {}
for ele in arr:
	freq[ele] = freq.get(ele, 0) + 1
s = 0
first = []
last = []
mx = max(arr)
for key in freq.keys():
	if key != mx:
		s += min(freq[key], 2)
		if freq[key] >= 2:
			first += [key]
			last += [key]
		else:
			first += [key]
print(s + 1)
op = sorted(first) + [mx] + sorted(last, reverse=True)
print(' '.join(map(str, op)))


s = int(input())
c = list(map(int, input().split()))
c.sort()
g = 0
a = []
m = c[s - 1]
a.append(m)
i = 1
while c[s - i] == m and s - i >= 0:
	i += 1
for t in range(0, s - i + 1):
	if c[-i - t] != m:
		m = c[-i - t]
		g = 1
		a.append(m)
	elif g == 1:
		a.insert(0, m)
		g = 2
print(len(a))
print(*a)


from collections import Counter
n = int(input())
arr = list(map(int, input().split()))
straight = list(sorted(set(arr)))
counter = Counter(arr)
for v in straight:
	counter[v] -= 1
counter += Counter()
temp = [x for x in sorted(set(counter), reverse=True) if x < straight[-1]]
print(len(straight) + len(temp))
print(*straight, *temp)


n = int(input())
a = list(map(int, input().split()))
d = {}
r = []
l = []
m = max(a)
for i in a:
	if i in d:
		d[i] += 1
	else:
		d[i] = 1
for i in d:
	if i < m and d[i] > 0:
		d[i] -= 1
		r.append(i)
for i in d:
	if i < m and d[i] > 0:
		d[i] -= 1
		l.append(i)
r.sort(reverse=True)
l.sort()
ans = l + [m] + r
print(len(ans))
print(' '.join(map(str, ans)))


n = int(input())
arr = list(map(int, input().strip().split()))
maxele = -2 ** 31
d = dict()
for i in range(n):
	if arr[i] not in d:
		d[arr[i]] = 1
	else:
		d[arr[i]] += 1
	maxele = max(maxele, arr[i])
l = []
r = []
res = 1
for i in d:
	if i != maxele:
		if d[i] > 1:
			l.append(i)
			r.append(i)
			res += 2
		else:
			l.append(i)
			res += 1
l.sort()
r.sort(reverse=True)
print(res)
st = l + [maxele] + r
print(' '.join(map(str, st)))


n = int(input())
a = list(map(int, input().split()))
a = sorted(a)
used = [0] * 5002
for i in range(len(a)):
	used[a[i]] += 1
l = []
for i in range(len(used) - 1, -1, -1):
	if used[i] > 0:
		l.append(str(i))
		used[i] -= 1
left = []
max_el = int(l[0])
for i in range(len(used)):
	if i < max_el and used[i] > 0:
		left.append(str(i))
ans = left + l
print(len(ans))
print(' '.join(ans))


n = int(input())
a = list(map(int, input().split()))
r = max(a)
r1 = [r]
left = []
right = []
for i in range(n):
	if a[i] < r and a[i] not in left:
		left.append(a[i])
	elif a[i] < r and a[i] not in right:
		right.append(a[i])
	else:
		pass
left.sort()
right.sort(reverse=True)
m = left + r1 + right
print(len(left) + 1 + len(right))
print(*m)


import collections
n = int(input())
l = list(map(int, input().split()))
d = collections.Counter(l)
l1 = []
l2 = []
d1 = sorted(d)
l1 += d1
for i in range(len(d1) - 1):
	if d[d1[i]] > 1:
		l2.append(d1[i])
l1 += l2[::-1]
print(len(l1))
print(' '.join(map(str, l1)))


n = int(input())
arr = list(map(int, input().split()))
arr.sort(reverse=True)
arr2 = [arr[0]]
for i in range(1, n):
	if arr[i] < arr2[-1]:
		arr2.append(arr[i])
	elif arr[i] < arr2[0]:
		arr2.insert(0, arr[i])
print(len(arr2))
print(*arr2)


m = int(input())
a = sorted(map(int, input().split()))
if a[m - 2] == a[m - 1]:
	i = m - 2
	while a[i] == a[m - 1] and i != -1:
		i -= 1
	i += 1
	m -= len(a[i:m - 1])
	a = a[:i + 1]
bv = [a[0]]
bu = [a[0] - 1]
for i in range(1, m):
	if a[i] != bv[len(bv) - 1]:
		bv.append(a[i])
	elif a[i] != bu[len(bu) - 1]:
		bu.append(a[i])
	else:
		m -= 1
bu = sorted(bu[1:], reverse=True)
print(m)
print(*bv + bu)


import sys
import string
from collections import Counter, defaultdict
from math import fsum, sqrt, gcd, ceil, factorial
from itertools import combinations, permutations
flush = lambda : sys.stdout.flush
comb = lambda x, y: factorial(x) // factorial(y) // factorial(x - y)
ip = lambda : input()
ii = lambda : int(input())
r = lambda : map(int, input().split())
rr = lambda : list(r())
n = ii()
arr = rr()
x = Counter(arr)
m = max(arr)
ans = []
for i in range(1, m + 1):
	if x[i]:
		x[i] -= 1
		ans.append(i)
for i in range(m - 1, 0, -1):
	if x[i]:
		ans.append(i)
print(len(ans))
print(*ans)


n = int(input())
a = list(map(int, input().split()))
c = [0 for _ in range(10 ** 5 + 1)]
for i in a:
	c[i] += 1
ans = []
for i in range(10 ** 5 + 1):
	if c[i]:
		ans.append(i)
		c[i] -= 1
if len(ans) != n:
	for i in range(10 ** 5, -1, -1):
		if c[i] and ans[-1] != i:
			ans.append(i)
print(len(ans))
print(*ans)


a = int(input())
b = sorted(map(int, input().split()))
if len(set(b)) == a:
	print(a)
	print(*b)
else:
	(v, x) = ([], [])
	for i in b:
		if i not in v:
			v.append(i)
		elif i not in x:
			x.append(i)
	if x[-1] == v[-1]:
		print(len(x) + len(v) - 1)
		z = x[:-1] + v[::-1]
		print(*z)
	else:
		print(len(v) + len(x))
		print(*x + v[::-1])


from sys import stdin, stdout
from math import factorial as f
inp = stdin.readline
op = stdout.write
n = int(inp())
a = list(map(int, inp().split()))
a.sort()
if len(set(a)) == n:
	op(str(len(a)) + '\n')
	a = [str(i) for i in a]
	op(' '.join(a[::-1]) + '\n')
else:
	temp = float('inf')
	i = 0
	rev = []
	while i < len(a):
		if a[i] == temp:
			temp = a.pop(i)
			rev.append(temp)
		else:
			temp = a[i]
			a[i] = str(a[i])
			i = i + 1
	rev = list(set(rev))
	rev.sort()
	if int(a[len(a) - 1]) == rev[len(rev) - 1]:
		rev.pop()
	if len(rev) == 0:
		temp = int(a.pop(0))
		rev.append(temp)
		rev.sort()
	rev = [str(i) for i in rev]
	op(str(len(a) + len(rev)) + '\n')
	op(' '.join(a) + ' ' + ' '.join(rev[::-1]))


a = int(input())
b = list(map(int, input().split()))
dic = {}
for i in b:
	try:
		dic[i] += 1
	except:
		dic[i] = 1
t = sorted(dic.keys())
tt = len(t)
r = 0
s = []
for i in range(tt - 1):
	s += [t[i]]
	dic[t[i]] -= 1
	r += 1
s += [t[-1]]
for i in range(2, tt + 1):
	i *= -1
	if dic[t[i]] > 0:
		s += [t[i]]
		r += 1
print(r + 1)
print(*s)


n = int(input())
a = list(map(int, input().split()))
a.sort()
otv = []
otv_1 = []
l = max(a)
kol = 0
pr = -1
for i in range(len(a)):
	if a[i] == l:
		otv.append(l)
		break
	if a[i] == pr:
		if kol == 1:
			otv_1.append(a[i])
			kol += 1
	else:
		pr = a[i]
		kol = 1
		otv.append(a[i])
otv_1 = otv_1[::-1]
for i in range(len(otv_1)):
	otv.append(otv_1[i])
print(len(otv))
print(*otv)


from sys import stdin, stdout
nmbr = lambda : int(stdin.readline())
lst = lambda : list(map(int, stdin.readline().split()))
for _ in range(1):
	n = nmbr()
	a = sorted(lst())
	used = [0] * n
	i = 0
	ans = []
	while i < n:
		if i == 0 or (not used[i] and a[i] > a[i - 1]):
			ans += [a[i]]
			used[i] = 1
		i += 1
	i = n - 2
	while i >= 0:
		if not used[i] and a[i] < a[i + 1]:
			ans += [a[i]]
		i -= 1
	print(len(ans))
	print(*ans)


n = int(input())
a = list(map(int, input().split()))
a.sort()
i = 0
rev = []
temp = '0'
hd = 0
while i < len(a):
	if a[i] == temp:
		temp = a.pop(i)
		if hd > 0 and (not rev[hd - 1] == temp):
			rev.append(temp)
			hd = hd + 1
		elif hd == 0:
			rev.append(temp)
			hd = hd + 1
	else:
		temp = a[i]
		i = i + 1
if len(rev) > 0 and a[len(a) - 1] == rev[len(rev) - 1]:
	rev.pop()
if len(rev) > 0:
	rev = [str(i) for i in rev]
a = [str(i) for i in a]
ans = a + rev[::-1]
print(str(len(a) + len(rev)))
print(' '.join(ans))


from heapq import heappop, heappush
n = int(input())
(a, b, c) = ([], [], [])
for i in input().split():
	heappush(a, int(i))
for i in range(n):
	m = str(heappop(a))
	if b and b[-1] == m:
		if not c or c[0] != m:
			c.insert(0, m)
	else:
		b += [m]
if c and b and (c[0] == b[-1]):
	c.pop(0)
print(len(b) + len(c))
print(' '.join(b + c))