CodeEff/ECCO huggingface dataset:
# Original solution
def main():
    N = int(eval(input()))
    A = [int(eval(input())) for i in range(N)]
    A.sort()
    from collections import deque

    def make1():
        d = deque(A)
        ret = deque([d.popleft()])
        for i in range((N-1)//2):
            if i & 1:
                j = d.popleft()
                k = d.popleft()
            else:
                j = d.pop()
                k = d.pop()
            if abs(ret[0] - j) < abs(ret[-1] - j):
                ret.appendleft(j)
                ret.append(k)
            else:
                ret.append(j)
                ret.appendleft(k)
        if len(d) == 1:
            j = d.pop()
            if abs(ret[0] - j) < abs(ret[-1] - j):
                ret.append(j)
            else:
                ret.appendleft(j)
        ans = 0
        for i in range(1, N):
            ans += abs(ret[i-1] - ret[i])
        return ans

    def make2():
        d = deque(A)
        ret = deque([d.pop()])
        for i in range((N-1)//2):
            if i & 1:
                j = d.pop()
                k = d.pop()
            else:
                j = d.popleft()
                k = d.popleft()
            if abs(ret[0] - j) < abs(ret[-1] - j):
                ret.appendleft(j)
                ret.append(k)
            else:
                ret.append(j)
                ret.appendleft(k)
        if len(d) == 1:
            j = d.pop()
            if abs(ret[0] - j) < abs(ret[-1] - j):
                ret.append(j)
            else:
                ret.appendleft(j)
        ans = 0
        for i in range(1, N):
            ans += abs(ret[i-1] - ret[i])
        return ans

    print((max(make1(), make2())))


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    N = int(eval(input()))
    A = [int(eval(input())) for i in range(N)]
    A.sort()
    le, ri = A[-1], A[-1]
    # print(le, ri)
    from collections import deque
    A = deque(A[:N-1])
    ans = 0
    for i in range(N-1):
        cur = [abs(A[0] - le), abs(A[-1] - ri),
               abs(A[-1] - le), abs(A[0] - ri)]
        #print(le, ri, cur)
        if max(cur) == cur[0]:
            c = A.popleft()
            ans += abs(c - le)
            le = c
        elif max(cur) == cur[1]:
            c = A.pop()
            ans += abs(c - ri)
            ri = c
        elif max(cur) == cur[2]:
            c = A.pop()
            ans += abs(c - le)
            le = c
        else:
            c = A.popleft()
            ans += abs(c - ri)
            ri = c
    print(ans)


if __name__ == '__main__':
    main()
# Original solution
import itertools

n = int(eval(input()))

a = []
for i in range(n):
    a.append(int(eval(input())))

p = list(itertools.permutations(a))
vs = []
rv = 0
for i in range(len(p)):
    s = 0
    for ii in range(len(p[i])):
        if (ii>0):
            s += abs(p[i][ii]-p[i][ii-1])
    if s > rv:
        rv = s
print(rv)


# Optimized solution
import itertools

def getNext(tv, ta):
    maxv = abs(max(ta)-tv)
    minv = abs(tv-min(ta))
    if (maxv > minv):
        return max(ta)
    else:
        return min(ta)

n = int(eval(input()))

a = []
for i in range(n):
    a.append(int(eval(input())))

vs = []
for i in range(len(a)):
    s = 0
    top = a[i]
    fv = top
    c = list(a)
    c.remove(fv)
    while(c):
        v = getNext(fv, c)   
#        print(fv,v)
        s = s + abs(fv-v)
        c.remove(v)
        fv = v
#    print("s is" + str(s))
    vs.append(s)

print((max(vs)))
# Original solution
import collections
N = int(eval(input()))
A = []
for i in range(N):
    A.append(int(eval(input())))
A.sort()

s = collections.deque([A[0]])
i = 1
j = N-1
while i <= j:
    xl = max(abs(s[0]-A[j]), abs(s[-1]-A[j]))
    xs = max(abs(s[0]-A[i]), abs(s[-1]-A[i]))
    if xl > xs:
        x = A[j]
        j -= 1
    else:
        x = A[i]
        i += 1
    if abs(s[0]-x) > abs(s[-1]-x):
        s.appendleft(x)
    else:
        s.append(x)
assert len(s) == N
ans = 0
for i in range(1, N):
    ans += abs(s[i] - s[i-1])
print(ans)


# Optimized solution
import sys

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

n = ini()
a = [ini() for _ in range(n)]
a.sort()


def solve():
    i = 0
    j = n - 1
    is_odd = n % 2 == 1
    a0 = a[i]
    a1 = a[j]
    s = a[j] - a[i]
    i += 1
    j -= 1
    while i < j:
        s += a[j] - a0
        s += a1 - a[i]
        a0 = a[i]
        a1 = a[j]
        i += 1
        j -= 1
    if is_odd and i == j:
        s += max(a1 - a[i], a[i] - a0)
    return s


print(solve())
# Original solution
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)]
A.sort()

if N % 2 == 0:
    ans = 0
    for i in range(N // 2):
        ans -= 2 * A[i]
    for i in range(N // 2, N):
        ans += 2 * A[i]
    ans += A[N // 2 - 1]
    ans -= A[N // 2]
    print(ans)
else:
    ans1 = 0
    for i in range(N // 2):
        ans1 -= 2 * A[i]
    for i in range(N // 2, N):
        ans1 += 2 * A[i]
    ans1 -= A[N // 2]
    ans1 -= A[N // 2 + 1]

    ans2 = 0
    for i in range(N // 2 + 1):
        ans2 -= 2 * A[i]
    for i in range(N // 2 + 1, N):
        ans2 += 2 * A[i]
    ans2 += A[N // 2]
    ans2 += A[N // 2 - 1]
    print((max(ans1, ans2)))


# Optimized solution
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


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


def main():
    N = int(eval(input()))
    A = [int(eval(input())) for _ in range(N)]
    A.sort()

    if N % 2 == 0:
        ans = A[N // 2] - A[N // 2 - 1]
        for i in range(N // 2 - 1):
            ans -= 2 * A[i]
        for i in range(N // 2 + 1, N):
            ans += 2 * A[i]
    else:
        ans1 = -A[N // 2] - A[N // 2 - 1]
        for i in range(N // 2 - 1):
            ans1 -= 2 * A[i]
        for i in range(N // 2 + 1, N):
            ans1 += 2 * A[i]

        ans2 = A[N // 2] + A[N // 2 + 1]
        for i in range(N // 2):
            ans2 -= 2 * A[i]
        for i in range(N // 2 + 2, N):
            ans2 += 2 * A[i]

        ans = max(ans1, ans2)

    print(ans)


if __name__ == "__main__":
    main()
# Original solution
n=int(eval(input()))

a=[]

for i in range(n):
    b=int(eval(input()))
    a.append(b)
    
a.sort()

ans=["" for i in range(n)]

ans[n//2]=a[-1]
a.remove(a[-1])
l=(n//2)-1
r=(n//2)+1
num=0
for i in range(n-1):
    if i%4==0:
        ans[l]=a[0]
        a.remove(a[0])
        num+=abs(ans[l+1]-ans[l])
        l-=1
    elif i%4==1:
        ans[r]=a[0]
        a.remove(a[0])
        num+=abs(ans[r]-ans[r-1])
        r+=1
    elif i%4==2:
        ans[l]=a[-1]
        a.remove(a[-1])
        num+=abs(ans[l+1]-ans[l])
        l-=1
    elif i%4==3:
        ans[r]=a[-1]
        a.remove(a[-1])
        num+=abs(ans[r]-ans[r-1])
        r-=1

ans.append(ans[0]) 
num+=abs(ans[-1]-ans[-2])    

num-=min(abs(ans[0]-ans[1]),abs(ans[-1]-ans[-2]),abs(ans[-2]-ans[-3]))
        
print(num)




# Optimized solution
n=int(eval(input()))
l=[]
for i in range(n):
    l.append(int(eval(input())))
l.sort()
if n%2==0:
    ln=l[:n//2]
    lx=l[n//2:]
    print((2*(sum(lx)-sum(ln))-lx[0]+ln[-1]))
else:
    ln=l[:n//2]
    lm=l[n//2]
    lx=l[n//2+1:]
    p=sum(lx)
    q=sum(ln)
    print((max(2*(p-q)+ln[-1]-lm,2*(p-q)-lx[0]+lm)))
# Original solution
from collections import*
n,*a=list(map(int,open(0).read().split()))
a.sort()
d=len(a)//2
flag=0
if len(a)%2==1:
    b=a[:d+1]
    c=a[d+1:]
    flag=1
else:
    b=a[:d]
    c=a[d:]
l=[]
for i in range(d):
    l.append(c[i])
    l.append(b[i])
if flag:
    if abs(l[-1]-b[-1])>abs(l[0]-b[-1]):
        l.append(b[-1])
    else:
        l.insert(0,b[-1])
print((sum(abs(l[i+1]-l[i])for i in range(n-1))))


# Optimized solution
n,*a=list(map(int,open(0).read().split()))
a.sort()
d=len(a)//2
flag=0
if len(a)%2==1:
    b=a[:d+1]
    c=a[d+1:]
    flag=1
else:
    b=a[:d]
    c=a[d:]
l=[]
for i in range(d):
    l.append(c[i])
    l.append(b[i])
if flag:
    if abs(l[-1]-b[-1])>abs(l[0]-b[-1]):
        l.append(b[-1])
    else:
        l.insert(0,b[-1])
print((sum(abs(l[i+1]-l[i])for i in range(n-1))))
# Original solution
# https://atcoder.jp/contests/tenka1-2018-beginner/submissions/6799465
def tenka18beg_c():
    N = int(eval(input()))
    A = [int(eval(input())) for _ in range(N)]
    A.sort()

    if N % 2 == 0:
        lower = A[:N//2]
        upper = A[N//2:]
        ans = (2 * sum(upper) - upper[0]) - (2 * sum(lower) - lower[-1])
    else:
        lower = A[:N//2]
        upper = A[N//2 + 1:]
        mid = A[N//2]
        x = 2 * sum(upper) - 2 * sum(lower) - mid + lower[-1]  # 中央が +2* -1* -1* のパターン
        y = 2 * sum(upper) - 2 * sum(lower) + mid - upper[0]  # 中央が +1* +1* -2* のパターン
        ans = max(x, y)
    print(ans)

tenka18beg_c()

# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def tenka18beg_c():
    # https://atcoder.jp/contests/tenka1-2018-beginner/submissions/6799465
    inp = list(map(int, read().split()))
    N = inp[0]
    A = inp[1:]
    A.sort()

    if N % 2 == 0:
        lower = A[:N//2]
        upper = A[N//2:]
        ans = (2 * sum(upper) - upper[0]) - (2 * sum(lower) - lower[-1])
    else:
        lower = A[:N//2]
        upper = A[N//2 + 1:]
        mid = A[N//2]
        x = 2 * sum(upper) - 2 * sum(lower) - mid + lower[-1]  # 中央が +2* -1* -1* のパターン
        y = 2 * sum(upper) - 2 * sum(lower) + mid - upper[0]  # 中央が +1* +1* -2* のパターン
        ans = max(x, y)
    print(ans)

tenka18beg_c()
# Original solution
from sys import stdin
from collections import deque

N = int(stdin.readline())
A = sorted([int(stdin.readline()) for i in range(N)])
ans1 = deque([A[N-1]])
ans2 = deque([A[0]])

f = 0
f2 = 1
b = N - 2
b2 = N - 1
s = True
while f <= b:
    if s:
        ans1.append(A[f])
        f += 1
        if f <= b:
            ans1.appendleft(A[f])
            f += 1
        ans2.append(A[b2])
        b2 -= 1
        if f2 <= b2:
            ans2.appendleft(A[b2])
            b2 -= 1
    else:
        ans1.append(A[b])
        b -= 1
        if f <= b:
            ans1.appendleft(A[b])
            b -= 1
        ans2.append(A[f2])
        f2 += 1
        if f2 <= b2:
            ans2.appendleft(A[f2])
            f2 += 1
    s = not s

a1 = a2 = 0
ans1 = list(ans1)
ans2 = list(ans2)
for i in range(N-1):
    a1 += abs(ans1[i] - ans1[i+1])
    a2 += abs(ans2[i] - ans2[i+1])

print((max(a1, a2)))

# Optimized solution
from sys import stdin
N = int(stdin.readline())
A = sorted([int(stdin.readline()) for i in range(N)])

a = [0] * N
b = [0] * N
j = 1

for i in range(N-1):
    a[i] += -j
    a[i+1] += j
    b[i] += j
    b[i+1] += -j
    j = -j

a.sort()
b.sort()
s1 = s2 = 0
for i in range(N):
    s1 += a[i] * A[i]
    s2 += b[i] * A[i]
print((max(s1, s2)))
# Original solution
N = int(eval(input()))
A = [int(eval(input())) for _ in range(N)] 
 
A = sorted(A)
res = 0
A2 = []
for i in range(len(A)):
    if len(A) == 1:
        if abs(A[0] - A2[0]) < abs(A[0] - A2[-1]):
            A2.append(A[0])
        else:
            A2.insert(0,A[0])
        break
    if i % 2 == 0:
        if i == 0:
            A2.append(A.pop(-1))
        else:
            if abs(A[-1] - A2[0]) < abs(A[-1] - A2[-1]):
                A2.append(A.pop(-1))
            else:
                A2.insert(0,A.pop(-1))
    else:
        #A2.append(A.pop(0))
        if abs(A[0] - A2[0]) < abs(A[0] - A2[-1]):
            A2.append(A.pop(0))
        else:
            A2.insert(0,A.pop(0))
for i in range(len(A2)-1):
    res += abs(A2[i] - A2[i+1])

print(res)

# Optimized solution
N = int(eval(input()))
A_in = [int(eval(input())) for _ in range(N)] 
 
A = sorted(A_in)
res1 = 0
A2 = []
for i in range(len(A)):
    if len(A) == 1:
        if abs(A[0] - A2[0]) < abs(A[0] - A2[-1]):
            A2.append(A[0])
        else:
            A2.insert(0,A[0])
        break
    if i % 2 == 0:
        A2.append(A.pop(-1))
    else:
        A2.append(A.pop(0))
for i in range(len(A2)-1):
    res1 += abs(A2[i] - A2[i+1])

A = sorted(A_in,reverse=True)
res2 = 0
A2 = []
for i in range(len(A)):
    if len(A) == 1:
        if abs(A[0] - A2[0]) < abs(A[0] - A2[-1]):
            A2.append(A[0])
        else:
            A2.insert(0,A[0])
        break
    if i % 2 == 0:
        A2.append(A.pop(-1))
    else:
        A2.append(A.pop(0))
for i in range(len(A2)-1):
    res2 += abs(A2[i] - A2[i+1])

print((max(res1,res2)))
# Original solution
N = int(eval(input()))
A = []
for i in range(N):
    A.append(int(eval(input())))

A.sort()

if N % 2 == 1:
    w = -2*sum(A[:N//2]) +A[N//2] +A[N//2 + 1] + 2*sum(A[N//2 + 2:])
    m = -2*sum(A[:N//2-1]) - A[N//2 - 1] - A[N//2] + 2*sum(A[N//2+1:])
    print((max(w, m)))
else:
    print((-2*sum(A[:N//2-1]) - A[N//2-1] + A[N//2] + 2*sum(A[N//2+1:])))

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

A.sort()
if N % 2 == 0:
    print((-2*sum(A[:N//2-1]) - A[N//2-1] + A[N//2] + 2*sum(A[N//2+1:])))
else:
    print((-2*sum(A[:N//2-1])
          + max(-2*A[N//2-1] + A[N//2] + A[N//2+1], - A[N//2-1] - A[N//2] + 2*A[N//2+1])
          + 2*sum(A[N//2+2:])))
# Original solution
import sys
from collections import deque

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(eval(input()))
    A = sorted(list(int(eval(input())) for _ in range(n)))
    A = deque(A)
    B = deque([])

    for i in range(n):
        if i == 0:
            mi = A.popleft()
            B.append(mi)
        elif i == 1:
            ma = A.pop()
            B.append(ma)
        else:
            diff_left_mi = ["A.popleft()", "B.appendleft(a)", abs(B[0] - A[0])]
            diff_right_mi = ["A.popleft()", "B.append(a)", abs(B[-1] - A[0])]

            diff_left_ma = ["A.pop()", "B.appendleft(a)", abs(B[0] - A[-1])]
            diff_right_ma = ["A.pop()", "B.append(a)", abs(B[-1] - A[-1])]

            ma = 0
            for op1, op2, diff in [diff_left_mi, diff_right_mi, diff_left_ma, diff_right_ma]:
                if ma <= diff:
                    fomula1 = op1
                    fomula2 = op2
                    ma = diff

            a = eval(fomula1)
            eval(fomula2)

    res = 0
    for i in range(1, n):
        res += abs(B[i] - B[i - 1])

    print(res)


if __name__ == '__main__':
    resolve()


# Optimized solution
import sys
from collections import deque

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(eval(input()))
    A = sorted(list(int(eval(input())) for _ in range(n)))
    B = deque([])
    for i in range(n // 2):
        if i % 2 == 0:
            B.appendleft(A[i])
            B.append(A[-(i + 1)])
        else:
            B.append(A[i])
            B.appendleft(A[-(i + 1)])

    if n % 2 != 0:
        if abs(B[0] - A[n // 2]) < abs(B[-1] - A[n // 2]):
            B.append(A[n // 2])
        else:
            B.appendleft(A[n // 2])

    res = 0
    for i in range(1, n):
        res += abs(B[i] - B[i - 1])

    print(res)

if __name__ == '__main__':
    resolve()
# Original solution
from collections import deque
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]

a.sort()
a = deque(a)

b = deque([a.pop()])

ans = 0
for _ in range(n - 1):
    x = max(
        abs(a[0] - b[0]),
        abs(a[0] - b[-1]),
        abs(a[-1] - b[0]),
        abs(a[-1] - b[-1]),
    )
    if x == abs(a[0] - b[0]):
        b.appendleft(a.popleft())
    elif x == abs(a[0] - b[-1]):
        b.append(a.popleft())
    elif x == abs(a[-1] - b[0]):
        b.appendleft(a.pop())
    else:  # x == abs(a[-1] - b[-1]):
        b.append(a.pop())
    ans += x

print(ans)


# Optimized solution
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]

cand1 = [0] * n
cand2 = [0] * n
for i in range(n - 1):
    if i % 2 == 0:
        cand1[i] -= 1
        cand1[i + 1] += 1
        cand2[i] += 1
        cand2[i + 1] -= 1
    else:
        cand1[i] += 1
        cand1[i + 1] -= 1
        cand2[i] -= 1
        cand2[i + 1] += 1

cand1.sort()
cand2.sort()
a.sort()

ans = max(
    sum(a[i] * cand1[i] for i in range(n)),
    sum(a[i] * cand2[i] for i in range(n)),
)
print(ans)
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for i in range(n)]
_a = a[:]
_a.sort()
__a = a[:]
__a.sort(reverse=True)

i = 0
cand_1 = [_a.pop(-1)]
cand_2 = [__a.pop(-1)]
while len(_a) > 0:
    if i % 4 < 2:
        _b = _a.pop(0)
        __b = __a.pop(0)
    else:
        _b = _a.pop(-1)
        __b = __a.pop(-1)
    if i % 2 == 0:
        cand_1.append(_b)
        cand_2.append(__b)
    else:
        cand_1.insert(0, _b)
        cand_2.insert(0, __b)
    i += 1
res1 = 0
res2 = 0
for i in range(1, n):
    res1+= abs(cand_1[i]-cand_1[i-1])
    res2+= abs(cand_2[i]-cand_2[i-1])

print((max(res1, res2)))


# Optimized solution
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
a.sort(reverse=True)

if n % 2 == 0:
    res = 0
    for i in range(n // 2 - 1):
        res += a[i] * 2
    res += a[n // 2 - 1]
    res -= a[n // 2]
    for i in range(n // 2 + 1, n):
        res -= a[i] * 2
    print(res)
else:
    res1 = 0
    for i in range(n // 2 - 1):
        res1 += a[i] * 2
    res1 += a[n // 2 - 1] + a[n // 2]
    for i in range(n // 2 + 1, n):
        res1 -= a[i] * 2

    res2 = 0
    for i in range(n // 2):
        res2 += a[i] * 2
    res2 -= a[n // 2] + a[n // 2 + 1]
    for i in range(n // 2 + 2, n):
        res2 -= a[i] * 2
    print((max(res1, res2)))
# Original solution
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]

a = sorted(a)
l = len(a)

if n % 2 == 0:
    # 谷max, 山, 谷, ..., 山, 谷, 山min
    tani = sorted(a[:l // 2], reverse=True)
    yama =  a[l // 2 + 1:] + [a[l // 2]]
    ans = 0
    for i in range(l //2):
        ans += yama[i] - tani[i]
    for i in range(l //2 - 1):
        ans += yama[i] - tani[i + 1]
    print(ans)

else:
    # 谷max, 山, 谷, ..., 山, 谷max
    tani = sorted(a[:l // 2 + 1], reverse=True)
    tani = tani[1:] + [tani[0]]
    yama = a[l // 2 + 1:]
    t = 0
    for i in range(l // 2):
        t += yama[i] - tani[i]
    for i in range(l // 2):
        t += yama[i] - tani[i + 1]
    ans = t

    # 山min, 谷, 山, ..., 谷, 山min
    tani = sorted(a[:l // 2], reverse=True)
    yama = a[l // 2:]
    yama = yama[1:] + [yama[0]]
    t = 0
    for i in range(l // 2):
        t += yama[i] - tani[i]
    for i in range(l // 2):
        t += yama[i + 1] - tani[i]

    ans = max(ans, t)
    print(ans)

# Optimized solution
n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
a = sorted(a)
la = len(a)

if n % 2 == 0:
    p = la // 2
    ans = (sum(a[p:]) - sum(a[:p])) * 2 + a[p - 1] - a[p]
else:
    p = la // 2
    ans = (sum(a[p:]) - sum(a[:p])) * 2 - a[p] - a[p + 1]

    p = la // 2 + 1
    t = (sum(a[p:]) - sum(a[:p])) * 2 + a[p - 2] + a[p - 1]

    ans = max(ans, t)

print(ans)
# Original solution
# -*- coding: utf-8 -*-

import copy
from collections import deque

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

aN_sorted = sorted(aN)
aN_sorted = deque(aN_sorted)
aN2 = [0] * N
for i in range(N):
    if i % 2 == 0:
        aN2[i] = aN_sorted.popleft()
    else:
        aN2[i] = aN_sorted.pop()
aN_sorted2 = sorted(aN, reverse=True)
aN_sorted2 = deque(aN_sorted2)
aN3 = [0] * N
for i in range(N):
    if i % 2 == 0:
        aN3[i] = aN_sorted2.popleft()
    else:
        aN3[i] = aN_sorted2.pop()

ans = 0
for i in range(1,N):
    if i == N-1:
        ans += max(abs(aN2[i-1] - aN2[i]), abs(aN2[i] - aN2[0]))
    else:
        ans += abs(aN2[i-1] - aN2[i])
ans2 = 0
for i in range(1,N):
    if i == N-1:
        ans2 += max(abs(aN3[i-1] - aN3[i]), abs(aN3[i] - aN3[0]))
    else:
        ans2 += abs(aN3[i-1] - aN3[i])

print((max(ans, ans2)))

# Optimized solution
# -*- coding: utf-8 -*-

# 終了後、重複処理を整理した版

from collections import deque

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

# 受け取ったリストを前後から交互に並べていく
def changeList(_list):
    _list = deque(_list)
    res = [0] * N
    for i in range(N):
        if i % 2 == 0:
            res[i] = _list.popleft()
        else:
            res[i] = _list.pop()
    return res

# 受け取ったリストの隣り合う差の合計、最後だけ先頭とも比べて大きい方
def sumList(_list):
    ans = 0
    for i in range(1,N):
        if i == N-1:
            ans += max(abs(_list[i-1] - _list[i]), abs(_list[i] - _list[0]))
        else:
            ans += abs(_list[i-1] - _list[i])
    return ans

# 昇順と降順、両方試して良かった方を使う
aN2 = changeList(sorted(aN))
aN3 = changeList(sorted(aN, reverse=True))

print((max(sumList(aN2), sumList(aN3))))
# Original solution
import itertools
n=int(eval(input()))
x=[int(eval(input())) for i in range(n)]
listX = list(itertools.permutations(x, n))

listY = []
for j in range(len(listX)):
  y = 0
  for i in range(n-1):
    y = y + abs(listX[j][i]-listX[j][i+1])
  listY.append(y)

print((max(listY)))



# Optimized solution
n = int(eval(input()))
listx = [int(eval(input())) for i in range(n)]

listx.sort()
listy=[]

x=listx.pop(0)
listy.append(x)
x=listx.pop(-1)
listy.append(x)
listy.sort()

while len(listx)>=1:
    abs1=abs(listx[0]-listy[-1])
    abs2=abs(listx[-1]-listy[0])

    if abs1 < abs2:
        x=listx.pop(-1)
        listy.insert(0,x)
    else:
        x=listx.pop(0)
        listy.append(x)
    
    if listy[0] > listy[-1]:
        listy.reverse()
ans=0

for i in range(len(listy)-1):
    ans=ans+abs(listy[i]-listy[i+1])

print(ans)
# Original solution
from collections import deque
N = int(eval(input()))
A = deque(sorted([int(eval(input())) for _ in range(N)]))
B = deque([])
B.append(A.popleft())
cnt = 0
while A:
    a0 = A[0]
    a1 = A[-1]
    b0 = B[0]
    b1 = B[-1]
    c00 = abs(a0-b0)
    c01 = abs(a0-b1)
    c10 = abs(a1-b0)
    c11 = abs(a1-b1)
    cmax = max(c00,c01,c10,c11)
    if cmax == c00:
        B.appendleft(A.popleft())
    elif cmax == c01:
        B.append(A.popleft())
    elif cmax == c10:
        B.appendleft(A.pop())
    else:
        B.append(A.pop())
    cnt += cmax
print(cnt)

# Optimized solution
N = int(eval(input()))
A = sorted([int(eval(input())) for _ in range(N)])
if N%2==0:
    B = A[:N//2]
    C = A[N//2:]
    C = C[::-1]
    D1 = []
    D2 = []
    for i in range(N//2-1):
        D1.append(B[i])
        D1.append(C[i])
        D2.append(C[i])
        D2.append(B[i])
    D1.append(B[-1])
    D1.insert(0,C[-1])
    D2.append(C[-1])
    D2.insert(0,B[-1])
    cnt1 = 0
    cnt2 = 0
    for i in range(1,N):
        cnt1 += abs(D1[i]-D1[i-1])
        cnt2 += abs(D2[i]-D2[i-1])
    print((max(cnt1,cnt2)))
else:
    B = A[:N//2]
    C = A[N//2+1:]
    C = C[::-1]
    D1 = []
    D2 = []
    for i in range(N//2):
        D1.append(B[i])
        D1.append(C[i])
        D2.append(C[i])
        D2.append(B[i])
    cnt1 = 0
    cnt2 = 0
    for i in range(1,N-1):
        cnt1 += abs(D1[i]-D1[i-1])
        cnt2 += abs(D2[i]-D2[i-1])
    cnt1 += max(A[N//2]-D1[0],D1[-1]-A[N//2])
    cnt2 += max(D2[0]-A[N//2],A[N//2]-D2[-1])
    print((max(cnt1,cnt2)))