You want to arrange n integers a_1, a_2, ..., a_{n} in some order in a row. Let's define the value of an arrangement as the sum of differences between all pairs of adjacent integers.

More formally, let's denote some arrangement as a sequence of integers x_1, x_2, ..., x_{n}, where sequence x is a permutation of sequence a. The value of such an arrangement is (x_1 - x_2) + (x_2 - x_3) + ... + (x_{n} - 1 - x_{n}).

Find the largest possible value of an arrangement. Then, output the lexicographically smallest sequence x that corresponds to an arrangement of the largest possible value.


-----Input-----

The first line of the input contains integer n (2 ≤ n ≤ 100). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (|a_{i}| ≤ 1000).


-----Output-----

Print the required sequence x_1, x_2, ..., x_{n}. Sequence x should be the lexicographically smallest permutation of a that corresponds to an arrangement of the largest possible value.


-----Examples-----
Input
5
100 -100 50 0 -50

Output
100 -50 0 50 -100 



-----Note-----

In the sample test case, the value of the output arrangement is (100 - ( - 50)) + (( - 50) - 0) + (0 - 50) + (50 - ( - 100)) = 200. No other arrangement has a larger value, and among all arrangements with the value of 200, the output arrangement is the lexicographically smallest one.

Sequence x_1, x_2, ... , x_{p} is lexicographically smaller than sequence y_1, y_2, ... , y_{p} if there exists an integer r (0 ≤ r < p) such that x_1 = y_1, x_2 = y_2, ... , x_{r} = y_{r} and x_{r} + 1 < y_{r} + 1.

Source: codeforces | Difficulty: EASY
Tags: Sorting

Implementation

Constructive algorithms


Solutions:
n = int(input())
a = list(map(int, input().split()))
a.sort()
(a[0], a[-1]) = (a[-1], a[0])
for i in a:
	print(i, end=' ')


n = int(input())
nums = [int(x) for x in input().split()]
nums.sort()
max = nums[-1]
nums[-1] = nums[0]
nums[0] = max
print(' '.join((str(x) for x in nums)))


def solve(n, nums):
	nums.sort()
	ret = []
	ret.append(nums[-1])
	for i in range(1, n - 1):
		ret.append(nums[i])
	ret.append(nums[0])
	return ret
n = int(input())
nums = list(map(int, input().split()))
ret = solve(n, nums)
for i in range(n):
	print(ret[i], end=' ')
print()


n = int(input())
a = input().split(' ')
temp = []
for i in a:
	temp.append(int(i))
temp.sort()
a = temp[0]
temp[0] = temp[len(temp) - 1]
temp[len(temp) - 1] = a
x = []
for i in temp:
	x.append(str(i))
out = ' '
print(out.join(x))


N = int(input())
A = sorted(list(map(int, input().split())))
(A[0], A[-1]) = (A[-1], A[0])
for x in A:
	print(x, end=' ')


n = int(input())
a = list(map(int, input().split()))
e = sorted(a)
b = max(a)
c = min(a)
d = sorted(e[1:-1])
print(b, *d, c)


import sys
input = sys.stdin.readline

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def upper_bound(a, x, lo=0, hi=None):
	if hi == None:
		hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

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
	return prime
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

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

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
n = int(input())
a = int_array()
ans = [0] * n
mx = max(a)
mn = min(a)
ans[0] = mx
ans[n - 1] = mn
a.remove(mx)
a.remove(mn)
a.sort()
for i in range(1, n - 1):
	ans[i] = a[i - 1]
print(*ans)


from itertools import permutations
from sys import stdin, stdout

def get_ints():
	return map(int, stdin.readline().strip().split())

def get_strs():
	return stdin.readline().strip().split()

def f_print(s):
	stdout.write(str(s))

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

def solve(arr):
	(i, j) = (1, len(arr) - 2)
	while i < j:
		(arr[i], arr[j]) = (arr[j], arr[i])
		i += 1
		j -= 1
	return arr
size = int(get_string())
arr = get_ints()
ans = solve(sorted(arr, reverse=True))
f_print(' '.join([str(i) for i in ans]))


a = int(input())
arr = list(map(int, input().split()))
if a == 2:
	print(*sorted(arr)[::-1])
else:
	arr = sorted(arr)[::-1]
	arr[1:-1] = sorted(arr[1:-1])
	print(*arr)


n = int(input())
arr = [int(i) for i in input().split(' ')]
arr.sort()
t = arr[0]
arr[0] = arr[n - 1]
arr[n - 1] = t
for x in arr:
	print(x, end=' ')


input()
a = sorted(input().split(), key=int)
print(a[-1], *a[1:-1], a[0])


n = int(input())
A = list(map(int, input().split()))
A.sort()
print(A[-1], end=' ')
for i in range(1, n - 1):
	print(A[i], end=' ')
print(A[0])


n = int(input())
a = list(map(int, input().split()))
a.sort()
temp = a[0]
a[0] = a[n - 1]
a[n - 1] = temp
for i in range(n):
	if i != n - 1:
		print(a[i], end=' ')
	else:
		print(a[n - 1])


n = int(input())
arr = list(map(int, input().split()))
arr = sorted(arr)
(arr[0], arr[n - 1]) = (arr[n - 1], arr[0])
print(*arr)


n = int(input())
x = sorted((int(x) for x in input().split()))
(x[0], x[-1]) = (x[-1], x[0])
print(' '.join(map(str, x)))


from sys import stdin
n = int(stdin.readline().rstrip())
l = list(map(int, stdin.readline().split()))
l.sort()
(l[n - 1], l[0]) = (l[0], l[n - 1])
for i in l:
	print(str(i) + ' ', end='')


def go():
	n = int(input())
	a = list(sorted((int(i) for i in input().split(' '))))
	(a[0], a[n - 1]) = (a[n - 1], a[0])
	return ' '.join((str(i) for i in a))
print(go())


n = int(input())
a = list(map(int, input().split()))
a = sorted(a)
(a[0], a[-1]) = (a[-1], a[0])
print(*a)


n = int(input())
arr = [int(x) for x in input().split(' ')]
arr = sorted(arr, reverse=True)
print(arr[0], end=' ')
for item in arr[1:-1][::-1]:
	print(item, end=' ')
print(arr[-1])


n = int(input())
s = list(map(int, input().split()))
s.sort()
x = s[-1]
s[-1] = s[0]
s[0] = x
for i in s:
	print(i, end=' ')


n = int(input())
l = list(map(int, input().split()))
ans = []
l = sorted(l)
ans = [l[-1]] + l[1:n - 1] + [l[0]]
print(*ans)


n = int(input())
a = list(map(int, input().split()))
a = sorted(a)
(a[0], a[-1]) = (a[-1], a[0])
print(' '.join(map(str, a)))


n = int(input())
lst = sorted(map(int, input().split()))
lst[1:-1] = lst[-2:0:-1]
print(*tuple(reversed(lst)))


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

def si():
	return input()

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

def li():
	return list(mi())
import math
n = ii()
a = li()
a.sort()
(a[0], a[-1]) = (a[-1], a[0])
print(*a)


import math

def na():
	n = int(input())
	b = [int(x) for x in input().split()]
	return (n, b)

def nab():
	n = int(input())
	b = [int(x) for x in input().split()]
	c = [int(x) for x in input().split()]
	return (n, b, c)

def dv():
	(n, m) = map(int, input().split())
	return (n, m)

def dva():
	(n, m) = map(int, input().split())
	b = [int(x) for x in input().split()]
	return (n, m, b)

def nm():
	n = int(input())
	b = [int(x) for x in input().split()]
	m = int(input())
	c = [int(x) for x in input().split()]
	return (n, b, m, c)

def dvs():
	n = int(input())
	m = int(input())
	return (n, m)
(n, a) = na()
a = sorted(a)
t = a[-1]
a[-1] = a[0]
a[0] = t
print(*a)


t = int(input())
a = list(map(int, input().split()))
a.sort()
(a[-1], a[0]) = (a[0], a[-1])
print(*a)


n = int(input())
a = list(map(int, input().split()))
max_index = a.index(max(a))
min_index = a.index(min(a))
if min_index == max_index:
	for i in range(len(a)):
		print(a[i], end=' ')
else:
	b = []
	for i in range(len(a)):
		if i != max_index and i != min_index:
			b.append(a[i])
	b.sort()
	print(a[max_index], end=' ')
	for i in range(len(b)):
		print(b[i], end=' ')
	print(a[min_index], end=' ')


import sys
input = sys.stdin
line = input.readline()
n = int(line)
line = input.readline()
d = list(map(int, line.split(' ')))
maxn = maxc = max(d)
minn = minc = min(d)
ans = []
for num in d:
	if num == maxc:
		maxc = None
	elif num == minc:
		minc = None
	else:
		ans.append(num)
ans.sort()
print(maxn, end=' ')
for num in ans:
	print(num, end=' ')
print(minn)


n = int(input())
a = list(map(int, input().split()))
a.sort()
print(a[n - 1], *a[1:n - 1], a[0])


numOfInts = int(input())
input1 = list(map(int, input().split(' ')))
input1.sort()
(input1[0], input1[-1]) = (input1[-1], input1[0])
answer = ''
for i in range(0, numOfInts):
	answer += str(input1[i])
	if i < numOfInts - 1:
		answer += ' '
print(answer)


a = int(input())
b = [int(x) for x in input().split(' ')]
c = sorted(b)
d = c[0]
c[0] = c[-1]
c[-1] = d
for i in range(len(c)):
	print(c[i], end=' ')


n = int(input())
a = list(map(int, input().split()))
mi = min(a)
ma = max(a)
ans = []
ans.append(ma)
a.sort()
print(ma, *a[1:len(a) - 1], end=' ')
print(mi)


input()
a = sorted(map(int, input().split()))
t = a[-1]
a[-1] = a[0]
a[0] = t
print(*a)


n = int(input())
a = sorted([int(_) for _ in input().split()])
print(a[n - 1], *a[1:n - 1], a[0])


N = int(input())
num = []
i = 0
IN = input().split()
while i < N:
	num.append(int(IN[i]))
	i = i + 1
num.sort()
(num[0], num[len(num) - 1]) = (num[len(num) - 1], num[0])
for i in num:
	print(i, end=' ')


def check(n, a):
	first = max(a)
	last = min(a)
	string = str()
	a.remove(first)
	a.remove(last)
	a.sort()
	a = [first] + a + [last]
	for i in a:
		string += str(i) + ' '
	return string.rstrip()
n = int(input().rstrip())
a = list(map(int, input().rstrip().split()))
print(check(n, a))


while 1:
	try:
		n = int(input())
		a = list(map(int, input().split()))
		a.sort()
		print(a[n - 1], end=' ')
		for i in range(1, n - 1):
			print(a[i], end=' ')
		print(a[0])
	except EOFError:
		break


n = int(input())
k = map(int, input().split())
mylist = sorted(list(k))
mymax = mylist[-1]
mymin = mylist[0]
mylist[0] = mymax
mylist[-1] = mymin
outlist = []
for i in range(n):
	outlist.append(mylist[i])
out = ' '.join((str(i) for i in outlist))
print(out)


n = int(input())
arr = list((int(x) for x in input().split(' ')))
mi = min(arr)
ma = max(arr)
arr.remove(mi)
arr.remove(ma)
arr.sort()
print(' '.join(map(str, [ma] + arr + [mi])))


n = int(input())
A = list(map(int, input().split()))
x = max(A)
A.pop(A.index(max(A)))
y = min(A)
A.sort()
A.pop(A.index(min(A)))
print(' '.join(list(map(str, [x] + A + [y]))))


n = int(input())
arr = list(map(int, input().split()))
arr.sort()
ans = list()
ans.append(arr[-1])
ans.extend(arr[1:n - 1])
ans.append(arr[0])
print(*ans)


I = input
I()
a = sorted(map(int, I().split()))
print(a[-1], ' '.join(map(str, a[1:-1])), a[0])


n = int(input())
l = list(map(int, input().split()))
l.sort()
l2 = l[:len(l) // 2]
l1 = l[len(l) // 2:]
print(l1[-1], *l2[1:], *l1[:-1], l2[0])


n = input()
a = input().split(' ')
ans = sorted(a, key=int)
print(ans[-1], ' '.join(ans[1:-1]), ans[0])


from sys import stdin as sin

def aint():
	return int(sin.readline())

def amap():
	return map(int, sin.readline().split())

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

def astr():
	return str(sin.readline().split())
n = int(input())
l = alist()
l.sort()
a = l.pop(0)
b = l.pop(-1)
l = [b] + l + [a]
print(*l, sep=' ')


def __starting_point():
	inp = input()
	inp = input()
	arr = inp.split(' ')
	Li = []
	Ls = []
	for a in arr:
		Li.append(int(a))
	Li.sort()
	fst = Li.pop(0)
	lst = Li.pop(-1)
	ans = str(lst)
	for x in Li:
		Ls.append(str(x))
	for s in Ls:
		ans += ' ' + s
	ans += ' ' + str(fst)
	print(ans)
__starting_point()


from collections import Counter
n = int(input())
l = list(map(int, input().split()))
l.sort()
(l[0], l[-1]) = (l[-1], l[0])
for i in l:
	print(i, end=' ')


n = int(input())
a = list(map(int, input().split()))
a.sort()
(a[0], a[-1]) = (a[-1], a[0])
print(' '.join([str(i) for i in a]))


import os
import sys
from io import BytesIO, IOBase
input = sys.stdin.readline

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())

def main():
	try:
		n = inp()
		a = inlt()
		b = a.pop(a.index(max(a)))
		c = a.pop(a.index(min(a)))
		print(*[b] + sorted(a) + [c], sep=' ')
	except Exception as e:
		print(e)
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())
a = [int(i) for i in input().split()]
index = a.index(max(a))
(a[0], a[index]) = (a[index], a[0])
index = a.index(min(a))
(a[-1], a[index]) = (a[index], a[-1])
a = [a[0]] + sorted(a[1:-1]) + [a[-1]]
print(' '.join([str(i) for i in a]))


import sys
from functools import reduce
from collections import Counter
import time
import datetime
import math

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

def sip():
	return input()

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

def mips():
	return map(str, sys.stdin.readline().split())

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

def matip(n, m):
	lst = []
	for i in range(n):
		arr = lip()
		lst.insert(i, arr)
	return lst

def factors(n):
	return list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def minJumps(arr, n):
	jumps = [0 for i in range(n)]
	if n == 0 or arr[0] == 0:
		return float('inf')
	jumps[0] = 0
	for i in range(1, n):
		jumps[i] = float('inf')
		for j in range(i):
			if i <= j + arr[j] and jumps[j] != float('inf'):
				jumps[i] = min(jumps[i], jumps[j] + 1)
				break
	return jumps[n - 1]

def dic(arr):
	return Counter(arr)

def check_prime(n):
	if n < 2:
		return False
	for i in range(2, int(n ** 0.5) + 1, 2):
		if n % i == 0:
			return False
	return True
n = ip()
arr = lip()
arr.sort()
(arr[0], arr[n - 1]) = (arr[n - 1], arr[0])
print(*arr)


n = int(input())
A = list(map(int, input().split()))
A.sort()
(A[0], A[-1]) = (A[-1], A[0])
print(' '.join(list(map(str, A))))


n = int(input())
c = list(map(int, input().split()))
a = []
for i in range(0, len(c)):
	a.append(c[i])
a.sort()
d = a[0]
a[0] = a[n - 1]
a[n - 1] = d
for i in range(0, n):
	print(a[i], end=' ')


n = int(input())
a = [int(x) for x in input().split()]
a.sort()
a = [a[-1]] + a[1:-1] + [a[0]]
print(*a)


n = int(input())
a_list = list(map(int, input().split()))
a_list = sorted(a_list)
size = len(a_list)
(a_list[0], a_list[size - 1]) = (a_list[size - 1], a_list[0])
for i in range(len(a_list)):
	print(a_list[i], end=' ')


a = int(input())
b = [int(x) for x in input().split(' ')]
c = []
d = -1000
g = -1 * 10 ** 3
e = 0
f = []
for x in range(a):
	if b[x] > d:
		d = b[x]
for x in range(g, d + 1):
	for y in range(len(b)):
		if b[y] == x:
			c.append(b[y])
e = c[0]
f.append(d)
for x in range(1, len(c) - 1):
	f.append(c[x])
f.append(e)
for i in f:
	print(i, end=' ')


n = int(input())
k = map(int, input().split())
mylist = sorted(list(k))

def myswap(mylist):
	(mylist[-1], mylist[0]) = (mylist[0], mylist[-1])
	return mylist
mylist = myswap(mylist)
for i in range(n):
	print(mylist[i], end=' ')


n = int(input())
l = list(map(int, input().split()))
l.sort()
print(l[-1], *l[1:-1], l[0])


import sys

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

def iinput():
	return int(input())

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

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))
n = iinput()
a = list(map(int, input().split()))
a.sort()
(a[-1], a[0]) = (a[0], a[-1])
print(*a)


import io
import os
import math

def main():
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	if n == 1:
		print(a[0])
	else:
		(a[0], a[n - 1]) = (a[n - 1], a[0])
		print(*a)
main()


n = int(input())
s = input()
A = list(map(int, s.split()))
A.sort()
(A[0], A[len(A) - 1]) = (A[len(A) - 1], A[0])
for i in range(len(A)):
	print('%d ' % A[i], end='')


n = int(input())
a = [int(i) for i in input().split()]
a = sorted(a)
(a[0], a[-1]) = (a[-1], a[0])
print(*a)


n = int(input())
x = sorted([int(x) for x in input().split()])
(x[0], x[-1]) = (x[-1], x[0])
print(*x)


n = int(input())
A = [int(x) for x in input().split()]
p = max(A)
q = min(A)
A.remove(p)
A.remove(q)
print(p, end=' ')
A.sort()
for i in A:
	print(i, end=' ')
print(q)


n = int(input())
arr = sorted(map(int, input().split()))
(arr[0], arr[-1]) = (arr[-1], arr[0])
for i in arr:
	print(i, end=' ')


def mapit():
	temp = list(map(int, input().split()))
	return temp
n = int(input())
arr = mapit()
if n == 2:
	arr.sort(reverse=True)
	print(*arr)
else:
	mx = max(arr)
	mxidx = arr.index(mx)
	(arr[0], arr[mxidx]) = (mx, arr[0])
	mn = min(arr)
	mnidx = arr.index(mn)
	(arr[-1], arr[mnidx]) = (mn, arr[-1])
	arr[1:-1] = sorted(arr[1:-1])
	print(*arr)


input()
aa = list(map(int, input().split()))
(x, y) = (max(aa), min(aa))
aa.remove(x)
aa.remove(y)
aa.sort()
print(*[x] + aa + [y])


n = int(input())
l = []
l = list(map(int, input().split()))
l.sort()
(l[0], l[-1]) = (l[-1], l[0])
z = ' '.join(map(str, l))
print(z)


n = int(input())
l = list(map(int, input().split(' ')))
l.sort()
(l[0], l[len(l) - 1]) = (l[len(l) - 1], l[0])
for i in range(len(l)):
	print(l[i], end=' ')


def main():
	n = int(input())
	nums = sorted(list(map(int, input().split(' '))))
	temp = nums[0]
	nums[0] = nums[-1]
	nums[-1] = temp
	print(' '.join(map(str, nums)))
main()


def main():
	n = int(input())
	v = list(map(int, input().split()))
	v.sort()
	(v[0], v[-1]) = (v[-1], v[0])
	for v0 in v:
		print(v0, end=' ')
main()


def l(n, arr):
	arr = sorted(arr)
	(arr[0], arr[n - 1]) = (arr[n - 1], arr[0])
	for i in arr:
		print(i, end=' ')
n = int(input())
arr = list(map(int, input().split()))
l(n, arr)


import os, sys

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	n = int(input())
	li = list(map(int, input().split()))
	li.sort()
	(li[0], li[n - 1]) = (li[n - 1], li[0])
	print(*li)
main()


n = int(input())
seq = list(map(int, input().split()))
sorted_seq = sorted(seq)
print(max(sorted_seq))
for i in sorted_seq[1:-1]:
	print(i)
print(min(sorted_seq))


n = int(input())
arr = [int(x) for x in input().split()]
arr = sorted(arr, reverse=True)
for i in range(1, n // 2, 1):
	(arr[i], arr[n - i - 1]) = (arr[n - i - 1], arr[i])
for ele in arr:
	print(ele, end=' ')


n = int(input())
a = list(map(int, input().split()))
ma = max(a)
mi = min(a)
a.remove(ma)
a.remove(mi)
a.sort()
a.insert(0, ma)
a.append(mi)
print(*a)


n = int(input())
sez = [int(i) for i in input().split()]
sez.sort()
a = len(sez)
(sez[0], sez[-1]) = (sez[-1], sez[0])
b = ' '.join([str(i) for i in sez])
print(b)


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left

def L():
	return list(map(int, stdin.readline().split()))

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

def I():
	return int(stdin.readline())
P = 1000000007
n = I()
arr = L()
arr.sort()
(arr[0], arr[-1]) = (arr[-1], arr[0])
print(*arr)


n = int(input())
l = list(map(int, input().split()))
i1 = l.index(min(l))
i2 = l.index(max(l))
l.sort()
t = l[0]
l[0] = l[n - 1]
l[n - 1] = t
print(*l)


n = int(input())
A = list(map(int, input().split()))
Max = max(A)
Min = min(A)
ind1 = A.index(Max)
A.pop(ind1)
ind2 = A.index(Min)
A.pop(ind2)
if len(A) > 0:
	print(str(Max), ' '.join(map(str, sorted(A))), str(Min))
else:
	print(str(Max), str(Min))


n = int(input())
list1 = list(map(int, input().split()))
print(max(list1), ' '.join(map(str, sorted(list1)[1:-1])), min(list1))


n = int(input())
data = sorted(list(map(int, input().split())))
print(data[-1], end=' ')
print(*data[1:-1], end=' ')
print(data[0])


def main():
	n = int(input())
	arr = list(sorted(map(int, input().split())))
	(arr[0], arr[-1]) = (arr[-1], arr[0])
	print(*arr)
main()


n = int(input())
m = list(map(int, input().split()))
m.sort()
(m[m.index(min(m))], m[n - 1]) = (m[n - 1], m[m.index(min(m))])
(m[m.index(max(m))], m[0]) = (m[0], m[m.index(max(m))])
print(' '.join(map(str, m)))


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')

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

def array():
	return [int(i) for i in input().split()]
n = int(input())
a = sorted(array())
(a[0], a[-1]) = (a[-1], a[0])
print(*a)


import sys, threading, os.path
import collections, heapq, math, bisect
import string
from platform import python_version
import itertools
sys.setrecursionlimit(10 ** 6)
threading.stack_size(2 ** 27)

def main():
	if os.path.exists('input.txt'):
		input = open('input.txt', 'r')
	else:
		input = sys.stdin
	n = int(input.readline())
	lis = list(map(int, input.readline().split()))
	lis.sort()
	(lis[0], lis[n - 1]) = (lis[n - 1], lis[0])
	output = ' '.join(map(str, lis))
	if os.path.exists('output.txt'):
		open('output.txt', 'w').writelines(str(output))
	else:
		sys.stdout.write(str(output))
main()


def main():
	input()
	l = list(map(int, input().split()))
	(x, y) = (min(l), max(l))
	l.remove(x)
	l.remove(y)
	l.sort()
	l.append(x)
	print(y, *l)
main()


n = int(input())
A = list(map(int, input().split()))
A.sort()
(A[0], A[-1]) = (A[-1], A[0])
print(*A)


length = int(input())
arr = list(map(int, input().split()))
mx = arr.pop(arr.index(max(arr)))
mn = arr.pop(arr.index(min(arr)))
arr.sort()
print(mx, *arr, mn)


n = int(input())
l = [int(i) for i in input().split()]
l.sort()
(l[0], l[-1]) = (l[-1], l[0])
print(*l)


n = int(input())
L = sorted([int(i) for i in input().split()])
print(L[n - 1], end=' ')
for i in range(1, n - 1):
	print(L[i], end=' ')
print(L[0])


n = int(input())
v = [int(x) for x in input().strip().split()]
u = sorted(v, reverse=True)
k = [u[0]] + u[1:n - 1][::-1] + [u[-1]]
index = 0
index2 = 0
if k[0] > v[0]:
	print(' '.join([str(x) for x in k]))
else:
	for i in range(1, n - 1):
		if not k[i] == v[i]:
			m = 10 ** 9
			for j in range(i, n - 1):
				if k[j] > v[i] and m > k[j]:
					m = k[j]
					index = j
			index2 = i
			break
	a = k[index2]
	k[index2] = k[index]
	k[index] = a
	k = k[:index2 + 1] + sorted(k[index2 + 1:-1]) + k[-1:]
	print(' '.join([str(x) for x in k]))


n = int(input())
l = list(map(int, input().strip().split()))
l.sort()
l.reverse()
t = [a for a in l[1:n - 1]]
t.sort()
l = [l[0]] + t + [l[-1]]
print(*l)


n = input()
s = input()
a = s.split()
for i in range(len(a)):
	a[i] = int(a[i])
a.sort()
(a[0], a[len(a) - 1]) = (a[len(a) - 1], a[0])
for i in range(len(a) - 1):
	a[i] = str(a[i]) + ' '
a[len(a) - 1] = str(a[len(a) - 1])
print(''.join(a))


n = int(input())
a = [int(i) for i in input().split()]
a.sort()
temp = a[0]
a[0] = a[n - 1]
a[n - 1] = temp
print(' '.join([str(i) for i in a]))


n = int(input())
a = list(map(int, input().split()))
a.sort()
s = a[0]
a = a[1:]
print(a[n - 2], *a[:n - 2], end=' ')
print(s)


from sys import stdin, stdout
cin = stdin.readline
cout = stdout.write
mp = lambda : list(map(int, cin().split()))
(n,) = mp()
a = mp()
l1 = []
l2 = []
count = 0
l1.append(max(a))
a.remove(max(a))
l2.append(min(a))
a.remove(min(a))
while a:
	l1 += [min(a)]
	a.remove(min(a))
	if a:
		l2 += [max(a)]
		a.remove(max(a))
for v in l1:
	cout(str(v) + ' ')
l2.reverse()
for v in l2:
	cout(str(v) + ' ')


n = int(input())
arr = list(map(int, input().split()))
_max = max(arr)
arr.remove(_max)
flag = 1
if len(arr) == 0:
	print(' '.join(map(str, arr)))
	flag = 0
_min = min(arr)
arr.remove(_min)
arr.sort()
arr.insert(0, _max)
arr.append(_min)
if flag == 1:
	print(' '.join(map(str, arr)))


n = int(input())
d = list(map(int, input().strip().split(' ')))
min = min(d)
max = max(d)
d.remove(min)
d.remove(max)
d.sort()
d = list(map(str, d))
print(str(max) + ' ' + ' '.join(d) + ' ' + str(min))


n = input()
s = sorted(map(int, input().split()))
(s[0], s[-1]) = (s[-1], s[0])
print(*s)


import sys
from itertools import permutations
input = lambda : sys.stdin.readline().strip('\r\n')
n = int(input())
a = sorted(list(map(int, input().split())))
print(a[-1], *a[1:-1], a[0])


(n, seq) = (int(input()), list(map(int, input().split())))
(a0, an) = (seq.pop(seq.index(max(seq))), seq.pop(seq.index(min(seq))))
print(' '.join(map(str, [a0] + sorted(seq) + [an])))


import sys
import math
n = int(input())
an = list(map(int, input().split()))
an.sort()
(an[0], an[n - 1]) = (an[n - 1], an[0])
print(' '.join(list(map(str, an))))


n = int(input())
t = list(map(int, input().split()))
(a, b) = (max(t), min(t))
t.remove(a)
t.remove(b)
t.sort()
print(' '.join((str(i) for i in [a] + t + [b])))


n = int(input())
arr = list(map(int, input().split()))
pos = []
neg = []
zeros = []
for num in arr:
	if num > 0:
		pos.append(num)
	elif num < 0:
		neg.append(num)
	else:
		zeros.append(num)
i = 0
pos.sort()
neg.sort()
if not zeros:
	if not neg:
		res = [pos[-1]] + pos[1:-1] + [pos[0]]
	elif not pos:
		res = [neg[-1]] + neg[1:-1] + [neg[0]]
	else:
		res = [max(pos)] + neg[1:] + pos[:-1] + [min(neg)]
elif not (pos or neg):
	res = zeros
elif not pos:
	res = neg[1:] + zeros + [min(neg)]
elif not neg:
	res = [max(pos)] + zeros + pos[:-1]
else:
	res = [max(pos)] + neg[1:] + zeros + pos[:-1] + [min(neg)]
print(' '.join([str(num) for num in res]))


n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
b = a[:]
a.pop(0)
a.pop()
a.sort()
print(b[0], end=' ')
for i in a:
	print(i, end=' ')
print(b[-1])


n = int(input())
ar = list(map(int, input().split()))
ar.sort()
(ar[0], ar[-1]) = (ar[-1], ar[0])
print(*ar)


n = int(input())
A = list(map(int, input().split()))
B = []
mna = -1000000007
mxa = max(A)
B.append(mxa)
A.remove(mxa)
if A:
	mna = min(A)
	A.remove(mna)
if A:
	A.sort()
	B = B + A
if mna != -1000000007:
	B.append(mna)
print(' '.join(list(map(str, B))))


n = int(input())
lst1 = input().split()
lst2 = []
for i in lst1:
	lst2.append(int(i))
Max = max(lst2)
Min = min(lst2)
lst2.sort()
del lst2[n - 1]
del lst2[0]
print(Max, end=' ')
for i in lst2:
	print(i, end=' ')
print(Min)


input()
a = sorted(map(int, input().split()))
(a[0], a[-1]) = (a[-1], a[0])
for x in a:
	print(x, end=' ')


def minus(lst):
	a = sorted(lst)
	b = a[::-1]
	return [b[0]] + sorted(b[1:len(b) - 1]) + [b[-1]]
n = int(input())
c = [int(i) for i in input().split()]
print(*minus(c))


def main():
	input()
	a = sorted(input().split(), key=int)
	print(a[-1], ' '.join(a[1:-1]), a[0])
main()


S = sorted
M = lambda : map(int, input().split())
m = int(input())
a = S(list(M()))
print(a[-1], *a[1:-1], a[0])


a = int(input())
b = sorted(map(int, input().split()))
print(b[-1])
for i in range(1, a - 1):
	print(b[i])
print(b[0])


n = int(input())
a = input().split()
a_array = []
array2 = []
for i in range(n):
	a_array.append(int(a[i]))
a_array = sorted(a_array)
a = a_array[0]
a_array[0] = a_array[len(a_array) - 1]
a_array[len(a_array) - 1] = a
for i in range(len(a_array)):
	array2.append(a_array[i])
	print(array2[i], end=' ')