CodeEff/ECCO huggingface dataset:
# Original solution
h = []
N = int(eval(input()))

for i in range(N):
    v = int(eval(input()))
    h.append(v)
h.sort()
hh = h.copy()

ans = 0
t = h.pop(-1)
t1 = t
pm = 0
while 1:
    t2 = h.pop(pm)
    ans += abs(t - t2)
    if len(h) == 0:
        break
    t = t2

    t3 = h.pop(pm)
    ans += abs(t1 - t3)
    if len(h) == 0:
        break
    t1 = t3

    if pm==0:
        pm = -1
    else:
        pm = 0

ans1 = 0
h = hh
t = h.pop(0)
t1 = t
pm = -1

while 1:
    t2 = h.pop(pm)
    ans1 += abs(t - t2)
    if len(h) == 0:
        break
    t = t2

    t3 = h.pop(pm)
    ans1 += abs(t1 - t3)
    if len(h) == 0:
        break
    t1 = t3
    if pm == 0:
        pm = -1
    else:
        pm = 0


print((max(ans, ans1)))

# Optimized solution

import sys

sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

# def main():

    # N = int(input())

    # for i in range(N):
    #     v = int(input())
    #     h.append(v)
def main():
    N, *h = list(map(int, read().split()))
    h.sort()
    hh = h.copy()

    ans = 0
    t = h.pop(-1)
    t1 = t
    pm = 0
    while 1:
        t2 = h.pop(pm)
        ans += abs(t - t2)
        if len(h) == 0:
            break
        t = t2

        t3 = h.pop(pm)
        ans += abs(t1 - t3)
        if len(h) == 0:
            break
        t1 = t3

        if pm==0:
            pm = -1
        else:
            pm = 0

    ans1 = 0
    h = hh
    t = h.pop(0)
    t1 = t
    pm = -1

    while 1:
        t2 = h.pop(pm)
        ans1 += abs(t - t2)
        if len(h) == 0:
            break
        t = t2

        t3 = h.pop(pm)
        ans1 += abs(t1 - t3)
        if len(h) == 0:
            break
        t1 = t3
        if pm == 0:
            pm = -1
        else:
            pm = 0

    print((max(ans, ans1)))

if __name__ == "__main__":
    main()
# Original solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from collections import deque
from math import ceil

n = int(readline())
a = sorted([int(readline()) for i in range(n)])
c = deque(a[:ceil(n / 2)])
d = deque(a[ceil(n / 2):])
b = deque(a[n // 2:])
a = deque(a[:n // 2])
check_1 = deque([a.popleft()])
check_2 = deque([d.pop()])
for i in range(n // 2):
    if i % 2 == 0:
        check_1.appendleft(b.pop())
        check_2.appendleft(c.popleft())
        if len(b) == 0:
            if len(a) != 0:
                check_1.append(a.popleft())
                check_2.append(d.pop())
            break
        check_1.append(b.pop())
        check_2.append(c.popleft())
    else:
        check_1.appendleft(a.popleft())
        check_2.appendleft(d.pop())
        if len(a) == 0:
            if len(b) != 0:
                check_1.append(b.pop())
                check_2.append(c.popleft())
            break
        check_1.append(a.popleft())
        check_2.append(d.pop())
cnt_1 = 0
cnt_2 = 0
for i in range(1, n):
    cnt_1 += abs(check_1[i - 1] - check_1[i])
    cnt_2 += abs(check_2[i - 1] - check_2[i])
print((max(cnt_1, cnt_2)))


# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, *a = list(map(int, read().split()))
a.sort()
a1 = 2 * (sum(a[(n + 1) // 2:]) - sum(a[:(n + 1) // 2]))
a2 = 2 * (sum(a[n // 2:]) - sum(a[:n // 2]))
if n % 2 == 0:
    v = a[n // 2] - a[n // 2 - 1]
    a1 -= v
    a2 -= v
else:
    a1 += a[n // 2 - 1] + a[n // 2]
    a2 -= a[n // 2 + 1] + a[n // 2]
print((max(a1, a2)))
# Original solution
from collections import deque

def main():
    N = int(eval(input()))
    A = deque(sorted((int(eval(input())) for i in range(N))))
    dq = deque()
    dq.append(A.popleft())
    for _ in range(N-1):
        a_first = A[0]
        a_last = A[-1]
        dq_first = dq[0]
        dq_last = dq[-1]

        ff = abs(a_first-dq_first)
        fl = abs(a_first-dq_last)
        lf = abs(a_last-dq_first)
        ll = abs(a_last-dq_last)

        comp_list = [ff, fl, lf, ll]
        max_idx = comp_list.index(max(comp_list))
        if max_idx == 0:
            dq.appendleft(A.popleft())
        elif max_idx == 1:
            dq.append(A.popleft())
        elif max_idx == 2:
            dq.appendleft(A.pop())
        else:
            dq.append(A.pop())

    abs_sum = sum([abs(dq[i] - dq[i-1]) for i in range(1, N)])
    print(abs_sum)
if __name__ == "__main__":
    main()

# Optimized solution
from collections import deque

def main():
    N = int(eval(input()))
    A = deque(sorted((int(eval(input())) for i in range(N))))
    dq = deque()
    dq.append(A.popleft())
    ans = 0
    for _ in range(N-1):
        a_first = A[0]
        a_last = A[-1]
        dq_first = dq[0]
        dq_last = dq[-1]

        ff = abs(a_first-dq_first)
        fl = abs(a_first-dq_last)
        lf = abs(a_last-dq_first)
        ll = abs(a_last-dq_last)

        comp_list = [ff, fl, lf, ll]
        max_idx = comp_list.index(max(comp_list))
        if max_idx == 0:
            ans += ff
            dq.appendleft(A.popleft())
        elif max_idx == 1:
            ans += fl
            dq.append(A.popleft())
        elif max_idx == 2:
            ans += lf
            dq.appendleft(A.pop())
        else:
            ans += ll
            dq.append(A.pop())

    print(ans)
if __name__ == "__main__":
    main()
# Original solution
# encoding: utf-8
import math

N = int(eval(input()))
A = [int(eval(input())) for i in range(N)]

A_up = sorted(A)
A_down = list(A_up[::-1])

rslt = []
idx_up = 0
idx_down = 0
for i in range(N):
    if i % 4 == 0:
        rslt = [A_up[idx_up]] + rslt
        idx_up += 1
    elif i % 4 == 1:
        rslt = rslt + [A_down[idx_down]]
        idx_down += 1
    elif i % 4 == 2:
        rslt = rslt + [A_up[idx_up]]
        idx_up += 1
    else:
        rslt = [A_down[idx_down]] + rslt
        idx_down += 1

# exception (N == 3, may be related with perfect graph ?)
if N == 3:
    if A_up[1] - A_up[0] > A_up[2] - A_up[1]:
        rslt = [A_up[1], A_up[0], A_up[2]]
    else:
        rslt = [A_up[1], A_up[2], A_up[0]]
        
ans = 0
prev = rslt[0]
for val in rslt[1:]:
    ans += (val - prev) if (val - prev) > 0 else (prev - val)
    prev = val

# print(rslt)
print(ans)

# Optimized solution
# encoding: utf-8
import math, sys

N = int(eval(input()))
A = [int(eval(input())) for i in range(N)]

A_up = sorted(A)
A_down = list(A_up[::-1])

# magic-hand-like order
# 1 2 3
# |X X  4 <- this node is taken last
# 7 6 5

ans = 0
for i in range(N // 2):
    if i == 0:
        ans += (A_down[0] - A_up[0])
    else:
        ans += (A_down[i - 1] - A_up[i])
        ans += (A_down[i] - A_up[i - 1])

if N % 2:
    ans += max((A_down[N // 2 - 1] - A_down[N // 2], A_up[N // 2] - A_up[N // 2 - 1]))

print(ans)
# Original solution
import sys
def ii(): return int(sys.stdin.readline())
def mi(): return list(map(int, sys.stdin.readline().split()))
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value

N = ii()
A = sorted([ii() for _ in range(N)])
'''
if N > 2:
    ans += A[-1] * 2 - A[0] - A[1]
    l_ind = 2
    r_ind = N-2
    while True:
        if r_ind - 2 >= l_ind:
'''
if N==2:
    print((A[1]-A[0]))
#elif N%2:
else:
    '''
    ans_1 = A[N-1]*2 - A[0] - A[1]
    ans_1 += sum(A[N//2:N-1])*2 - sum(A[:N//2])*2 - A[N//2] - A[N//2-1]
    ans_2 = A[N-1] + A[N-2] - A[0]*2
    ans_2 += sum(A[N//2+1:])*2 - sum(A[1:N//2+1])*2 - A[N//2] - A[N//2+1]
    print(ans_1, ans_2)
    '''
    ans_1 = A[N-1]*2 - A[0] - A[1]
    tmp = A[0] + A[1]
    l_ind = 1
    r_ind = N-1
    while True:
        r_ind -= 2
        if N%2 and l_ind > r_ind:
            break
        if (not N%2) and r_ind == l_ind:
            ans_1 += A[r_ind+1] - A[r_ind-1]
            break
        ans_1 += A[r_ind] + A[r_ind+1] - tmp
        tmp = A[r_ind] + A[r_ind+1]
        l_ind += 2
        if N%2 and l_ind > r_ind:
            break
        if (not N%2) and r_ind == l_ind:
            ans_1 += A[r_ind+1] - A[r_ind-1]
            break
        ans_1 += tmp - (A[l_ind] + A[l_ind-1])
        tmp = A[l_ind] + A[l_ind-1]

    ans_2 = A[N-1] + A[N-2] - A[0]*2
    tmp = A[N-1] + A[N-2]
    l_ind = 0
    r_ind = N-2
    while True:
        l_ind += 2
        if N%2 and l_ind > r_ind:
            break
        if (not N%2) and r_ind == l_ind:
            ans_2 += A[r_ind+1] - A[r_ind-1]
            break
        ans_2 += tmp - (A[l_ind] + A[l_ind-1])
        tmp = A[l_ind] + A[l_ind-1]
        r_ind -= 2
        if N%2 and l_ind > r_ind:
            break
        if (not N%2) and r_ind == l_ind:
            ans_2 += A[r_ind+1] - A[r_ind-1]
            break
        ans_2 += A[r_ind] + A[r_ind+1] - tmp
        tmp = A[r_ind] + A[r_ind+1]

    print((max(ans_1, ans_2)))

# Optimized solution
import sys
def ii(): return int(sys.stdin.readline())
def mi(): return list(map(int, sys.stdin.readline().split()))
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value

N = ii()
A = sorted([ii() for _ in range(N)])

if N % 2:
    ans_1 = sum(A[N//2+1:])*2 - sum(A[:N//2+1])*2
    ans_1 += A[N//2-1] + A[N//2]
    ans_2 = sum(A[N//2:])*2 - sum(A[:N//2])*2
    ans_2 -= A[N//2] + A[N//2+1]
    print((max(ans_1, ans_2)))
else:
    ans = sum(A[N//2:])*2 - sum(A[:N//2])*2
    ans -= A[N//2] - A[N//2-1]
    print(ans)
# Original solution
def solve(pos):
    start = pos
    ans = 0
    a = 0
    b = N - 1
    for i in range(N-1):
        if a==start:
            a += 1
        elif b==start:
            b -= 1
        if abs(A[a]-A[pos])>=abs(A[b]-A[pos]):
            ans += abs(A[a]-A[pos])
            pos = a
            a += 1
        else:
            ans += abs(A[b]-A[pos])
            pos = b
            b -= 1
        #print(ans, a, b)
    #print(ans)
    return ans

N = int(eval(input()))
A = []
for i in range(N):
    A.append(int(eval(input())))
A.sort()

if N<=10000:
    pos_list = list(range(N))
else:
    M = (A[0] + A[-1])/2
    for i in range(N-1):
        if A[i]<=M and M<A[i+1]:
            break
    a = max(0, i - 50)
    b = min(N - 1, i + 50)
    pos_list = list(range(a, b+1))
    if pos_list[0]!=0:
        pos_list = [0] + pos_list
    if pos_list[-1]!= N - 1:
        pos_list.append(N-1)
#print(pos_list)

ans = 0
for pos in pos_list:
    tmp = solve(pos)
    ans = max(ans, tmp)
print(ans)


# Optimized solution
N = int(eval(input()))
A = []
for i in range(N):
    A.append(int(eval(input())))
A.sort()

PLUS = A[int((N+1)//2):]
MINUS = A[:int((N+1)//2)]
#print(PLUS, MINUS)
ans = sum(PLUS)*2 - sum(MINUS)*2
if N%2==0:
    ans += MINUS[-1] - PLUS[0]
else:
    ans += sum(MINUS[-2:])

PLUS = A[int(N//2):]
MINUS = A[:int(N//2)]
#print(PLUS, MINUS)
ans2 = sum(PLUS)*2 - sum(MINUS)*2
if N%2==0:
    ans2 += MINUS[-1] - PLUS[0]
else:
    ans2 -= sum(PLUS[:2])

print((max(ans, ans2)))
# Original solution
N=int(eval(input()))
A=[]
for i in range(N):A.append(int(eval(input())))
A=sorted(A)
B=[0]*N
if N%2==0:
    for i in range(N):
        if i==N//2-1:
            B[0]=A[i]
        elif i<N//2-1:
            B[i*2+2]=A[i]
        else:
            B[-(i-N//2)*2-1]=A[i]
else:
    for i in range(N):
        if i==N//2:
            B[0]=A[i]
        elif i<N//2:
            B[i*2+2]=A[i]
        else:
            B[-(i-N//2)*2]=A[i]
r=0
for i in range(1,N):
    r += abs(B[i-1]-B[i])
A=A[::-1]
B=[0]*N
if N%2==0:
    for i in range(N):
        if i==N//2-1:
            B[0]=A[i]
        elif i<N//2-1:
            B[i*2+2]=A[i]
        else:
            B[-(i-N//2)*2-1]=A[i]
else:
    for i in range(N):
        if i==N//2:
            B[0]=A[i]
        elif i<N//2:
            B[i*2+2]=A[i]
        else:
            B[-(i-N//2)*2]=A[i]
s=0
for i in range(1,N):
    s += abs(B[i-1]-B[i])
print((max(r,s)))

# Optimized solution
N=int(eval(input()))
A=sorted(int(eval(input()))for _ in [0]*N)
n=N//2
print((A[n]+2*sum(A[n+1:])-2*sum(A[:n-1])-A[n-1]if N%2==0 else max(A[n]+A[n+1]+2*sum(A[n+2:])-2*sum(A[:n]),2*sum(A[n+1:])-2*sum(A[:n-1])-A[n-1]-A[n])))
# Original solution
from collections import deque
n = int(eval(input()))
A = sorted(int(eval(input()))for _ in range(n))
L = 1
R = n-1
B = deque([A[0]])
prev_L = A[0]
prev_R = A[0]
while L <= R:
    x, y = A[L], A[R]
    if abs(min(prev_L, prev_R)-y) < abs(max(prev_L, prev_R)-x):
        if prev_L < prev_R:
            B.append(x)
            prev_R = x
        else:
            B.appendleft(x)
            prev_L = x
        L += 1
    else:
        if prev_L < prev_R:
            B.appendleft(y)
            prev_L = y
        else:
            B.append(y)
            prev_R = y
        R -= 1

B = list(B)
ans = 0
for x, y in zip(B, B[1:]):
    ans += abs(x-y)
print(ans)

# Optimized solution
n = int(eval(input()))
A = sorted(int(eval(input()))for _ in range(n))

if n % 2 == 0:
    low = A[:n//2]
    high = A[n//2:]
    ans = abs(2*sum(high)-high[0]-(2*sum(low)-low[-1]))
else:
    low = A[:n//2]
    high = A[n//2+1:]
    mid = A[n//2]
    ans = abs(2*sum(high)-high[0]-(2*sum(low)-low[-1]))
    ans += max(high[0]-mid, mid-low[-1])
print(ans)
# Original solution
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
A = sorted(A)

ans1 = 0
l = 0
r = N-1
for i in range(N-2):
    if i%2 == 0:
        ans1 += 2 * A[r]
        r -= 1
    else:
        ans1 += -2 * A[l]
        l += 1
if N%2 == 0:
    ans1 -= min(A[l], A[r])
    ans1 += max(A[l], A[r])
else:
    ans1 -= (A[l] + A[r])

ans2 = 0
l = 0
r = N-1
for i in range(N-2):
    if i%2 == 1:
        ans2 += 2 * A[r]
        r -= 1
    else:
        ans2 += -2 * A[l]
        l += 1
if N%2 == 0:
    ans2 -= min(A[l], A[r])
    ans2 += max(A[l], A[r])
else:
    ans2 += (A[l] + A[r])

print((max(ans1,ans2)))


# Optimized solution
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
def main(N, A):
    A.sort()
    B = [A[i // 2] if i & 1 == 0 else A[-1 - i // 2] for i in range(N)]
    C = [A[i // 2] if i & 1 == 1 else A[-1 - i // 2] for i in range(N)]

    print((max(f(B), f(C))))

def f(L):
    res = 0
    for i in range(len(L) - 1):
        res += abs(L[i] - L[i - 1])
    return res

if __name__ == '__main__':
    input = sys.stdin.readline
    N = int(eval(input()))
    A = [int(eval(input())) for _ in range(N)]
    main(N, A)
# Original solution
def calc_score(li):
    prev = li[0]
    ret = 0
    for el in li:
        ret += abs(el - prev)
        prev = el
    return ret


N, *A = list(map(int, open(0).read().split()))
A = sorted(A)
if N % 2:
    ans = 0
    li = [0] * N
    li[0] = A[N // 2]
    for i in range(N // 2):
        li[i * 2 + 1] = A[i + N // 2 + 1]
        li[i * 2 + 2] = A[i]
    ans = calc_score(li)
    li[N - 1] = A[N // 2]
    for i in range(N // 2):
        li[i * 2] = A[i + N // 2 + 1]
        li[i * 2 + 1] = A[i]
    print((max(ans, calc_score(li))))
else:
    li = [0] * N
    for i in range(N // 2):
        li[i * 2] = A[i + N // 2]
        li[i * 2 + 1] = A[i]
    print((calc_score(li)))


# Optimized solution
N, *A = list(map(int, open(0).read().split()))
M = N // 2
A.sort()
ans = 0
if N % 2:
    for i in range(1, M):
        ans += A[i + M + 1] - A[i - 1]
        ans += A[i + M + 1] - A[i]
    ans += A[M + 1] - A[0]
    ans = max(A[M + 1] - A[M] + ans, ans + A[M] - A[M - 1])
else:
    ans += A[M] - A[0]
    for i in range(1, M):
        ans += A[i + M] - A[i - 1]
        ans += A[i + M] - A[i]
print(ans)
# Original solution
from itertools import permutations
n = int(eval(input()))
A = [int(eval(input())) for _ in range(n)]
def f(x):
    cnt = 0
    for i in range(len(x) - 1):
        cnt += abs(x[i] - x[i + 1])
    return cnt
ans = []
for B in permutations(A):
    ans.append((f(B), B))
ans.sort(reverse=True)
print((ans[0][0]))


# Optimized solution
def solve():
    n = int(eval(input()))
    a = sorted(int(eval(input())) for _ in range(n))
    c0 = n // 2 - 1
    c1 = n // 2
    c2 = n // 2 + 1
    ans = 0
    if n & 1:
        for i in range(c0):
            ans -= a[i] * 2
        for i in range(c2 + 1, n):
            ans += a[i] * 2
        ans += -a[c0] + a[c2] + max(-a[c0] + a[c1], - a[c1] + a[c2])
    else:
        for i in range(c0):
            ans -= a[i] * 2
        for i in range(c1 + 1, n):
            ans += a[i] * 2
        ans += -a[c0] + a[c1]
    return ans
print((solve()))
# Original solution
def solve():
    n = int(eval(input()))
    a = sorted(int(eval(input())) for _ in range(n))
    c0 = n // 2 - 1
    c1 = n // 2
    c2 = n // 2 + 1
    ans = 0
    if n & 1:
        for i in range(c0):
            ans -= a[i] * 2
        for i in range(c2 + 1, n):
            ans += a[i] * 2
        ans += -a[c0] + a[c2] + max(-a[c0] + a[c1], - a[c1] + a[c2])
    else:
        for i in range(c0):
            ans -= a[i] * 2
        for i in range(c1 + 1, n):
            ans += a[i] * 2
        ans += -a[c0] + a[c1]
    return ans
print((solve()))


# Optimized solution
import sys
readline = sys.stdin.readline
def solve():
    n = int(eval(input()))
    a = sorted(int(readline()) for _ in range(n))
    c0 = n // 2 - 1
    c1 = n // 2
    c2 = n // 2 + 1
    ans = 0
    if n & 1:
        ans = sum(a[c2 + 1:]) * 2 - sum(a[:c0]) * 2 - a[c0] + a[c2] + max(-a[c0] + a[c1], - a[c1] + a[c2])
    else:
        ans = sum(a[c1 + 1:]) * 2 - sum(a[:c0]) * 2 - a[c0] + a[c1]
    return ans
print((solve()))
# Original solution
N=int(eval(input()))

A=[int(eval(input())) for i in range(N)]

A.sort()

ans=[A[0],A[-1]]
l=1
r=-2
n=[0]*4
ansn=abs(A[0]-A[-1])
for i in range(N-2):
    n[0]=abs(A[l]-ans[0])
    n[1]=abs(A[l]-ans[-1])
    n[2]=abs(A[r]-ans[0])
    n[3]=abs(A[r]-ans[-1])
    if n[0]==max(n):
        ansn+=n[0]
        ans.insert(0,A[l])
        l+=1
    elif n[1]==max(n):
        ansn+=n[1]
        ans.insert(len(ans),A[l])
        l+=1
    elif n[2]==max(n):
        ansn+=n[2]
        ans.insert(0,A[r])
        r-=1
    elif n[3]==max(n):
        ansn+=n[3]
        ans.insert(len(ans),A[r])
        r-=1

print(ansn)

# Optimized solution
N=int(eval(input()))

A=[int(eval(input())) for i in range(N)]

A.sort()

ansl=A[0]
ansr=A[-1]
l=1
r=-2
n=[0]*4
ansn=abs(A[0]-A[-1])
for i in range(N-2):
    n[0]=abs(A[l]-ansl)
    n[1]=abs(A[l]-ansr)
    n[2]=abs(A[r]-ansl)
    n[3]=abs(A[r]-ansr)
    if n[0]==max(n):
        ansn+=n[0]
        ansl=A[l]
        l+=1
    elif n[1]==max(n):
        ansn+=n[1]
        ansr=A[l]
        l+=1
    elif n[2]==max(n):
        ansn+=n[2]
        ansl=A[r]
        r-=1
    elif n[3]==max(n):
        ansn+=n[3]
        ansr=A[r]
        r-=1

print(ansn)
# Original solution
import collections

N=int(eval(input()))

A=[int(eval(input())) for i in range(N)]

A.sort()

ans=[A[0],A[-1]]
ans=collections.deque(ans)
l=1
r=-2
n=[0]*4
ansn=abs(A[0]-A[-1])
for i in range(N-2):
    n[0]=abs(A[l]-ans[0])
    n[1]=abs(A[l]-ans[-1])
    n[2]=abs(A[r]-ans[0])
    n[3]=abs(A[r]-ans[-1])
    if n[0]==max(n):
        ansn+=n[0]
        ans.appendleft(A[l])
        l+=1
    elif n[1]==max(n):
        ansn+=n[1]
        ans.append(A[l])
        l+=1
    elif n[2]==max(n):
        ansn+=n[2]
        ans.appendleft(A[r])
        r-=1
    elif n[3]==max(n):
        ansn+=n[3]
        ans.append(A[r])
        r-=1

print(ansn)

# Optimized solution
N=int(eval(input()))
A=[int(eval(input())) for i in range(N)]

A.sort()

k1=[]
if N%2==1:
    k1.extend([1,1])
    k1.extend([-2]*(N//2))
    k1.extend([2]*(N-N//2-2))
    k1.sort()
    sum1=0
    sum2=0
    for i in range(N):
        sum1+=A[i]*k1[i]
        sum2+=(-1)*A[i]*k1[-1-i]
    print((max(sum1,sum2)))
else:
    k1.extend([-1,1])
    k1.extend([-2]*((N-2)//2))
    k1.extend([2]*((N-2)//2))
    k1.sort()
    sum1=0
    sum2=0
    for i in range(N):
        sum1+=A[i]*k1[i]
        sum2+=(-1)*A[i]*k1[-1-i]
    print((max(sum1,sum2)))
# Original solution
def c_align(N, A):
    from collections import deque
    if N == 2:
        return abs(A[0] - A[1])
    tmp = sorted(A)

    seq = tmp[:]
    old_check = seq.pop(-1 + N // 2) if N % 2 == 0 else seq.pop(N // 2)
    current_check = -1
    seq = deque(seq)
    ans1 = 0
    turn = 0
    while seq:
        current_check = seq.pop() if turn % 2 == 0 else seq.popleft()
        ans1 += abs(current_check - old_check)
        old_check = current_check
        turn += 1

    seq = tmp[:]
    old_check = seq.pop(1 + N // 2) if N % 2 == 0 else seq.pop(N // 2)
    current_check = -1
    seq = deque(seq)
    ans2 = 0
    turn = 0
    while seq:
        current_check = seq.popleft() if turn % 2 == 0 else seq.pop()
        ans2 += abs(current_check - old_check)
        old_check = current_check
        turn += 1

    ans = max(ans1, ans2)
    return ans

N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
print((c_align(N, A)))

# Optimized solution
def c_align(N, A):
    A.sort()
    seq = [0] * N
    if N % 2 == 0:
        for k in range(N - 2, -1, -2):
            seq[k] = A.pop()
        for k in range(N - 1, 0, -2):
            seq[k] = A.pop()
        ans = 0
        for k in range(N - 1):
            ans += abs(seq[k] - seq[k + 1])
    else:
        tmp = A[:]
        for k in range(N - 3, -1, -2):
            seq[k] = tmp.pop()
        seq[N - 1] = tmp.pop()
        for k in range(N - 2, 0, -2):
            seq[k] = tmp.pop()

        seq2 = [0] * N
        tmp = A[:]
        for k in range(1, N - 1, 2):
            seq2[k] = tmp.pop()
        seq2[N - 1] = tmp.pop()
        for k in range(0, N - 2, 2):
            seq2[k] = tmp.pop()

        ans1, ans2 = 0, 0
        for k in range(N - 1):
            ans1 += abs(seq[k] - seq[k + 1])
        for k in range(N - 1):
            ans2 += abs(seq2[k] - seq2[k + 1])
        ans = max(ans1, ans2)
    return ans

N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
print((c_align(N, A)))
# Original solution
N=int(eval(input()))
num=[int(eval(input())) for i in range(N)]
num.sort()
jun=[]
sig=[i for i in range(1,N+1)]
if N%2!=0: #kisu
  manaka=int(N/2)
  if abs(num[manaka]-num[0])<abs(num[manaka]-num[N-1]):
    jun.append(manaka)
    for i in range(int((N-1)/2)):
      jun.append(max(sig)-1)
      sig.remove(max(sig))
      jun.append(min(sig)-1)
      sig.remove(min(sig))
  else:
    jun.append(manaka)
    for i in range(int((N-1)/2)):
      jun.append(min(sig)-1)
      sig.remove(min(sig))
      jun.append(max(sig)-1)
      sig.remove(max(sig))
else:
  manaka=int(N/2)
  jun.append(manaka)
  for i in range(manaka-1):
    jun.append(min(sig)-1)
    sig.remove(min(sig))
    jun.append(max(sig)-1)
    sig.remove(max(sig))
  jun.append(sig[0]-1)
delta=[abs(num[jun[i]]-num[jun[i+1]]) for i in range(N-1)]
print((sum(delta)))

# Optimized solution
N=int(eval(input()))
num=[int(eval(input())) for i in range(N)]
num.sort()

if N%2!=0: #kisu
  manaka=int(N/2)  
  sum_a=abs(num[manaka]-num[N-1])
  sum_a+=abs(num[N-1]-num[0])
  for i in range(int((N-1)/2)-1):
    sum_a+=abs(num[N-2-i]-num[i])
    sum_a+=abs(num[i+1]-num[N-2-i])
  sum_b=abs(num[manaka]-num[0])
  sum_b+=abs(num[0]-num[N-1])
  for i in range(int((N-1)/2)-1):
    sum_b+=abs(num[i+1]-num[N-1-i])
    sum_b+=abs(num[i+1]-num[N-2-i])
  print((max(sum_a,sum_b)))
else:
  manaka=int(N/2)
  sum=abs(num[manaka]-num[0])
  for i in range(manaka-1):
    sum+=abs(num[i]-num[N-1-i])
    sum+=abs(num[i+1]-num[N-1-i])
  print(sum)