from collections import deque

n = int(eval(input()))

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

a.sort()

x = deque(a)

y = deque(a)





def align(a, fromLeft=1):

    if fromLeft:

        p = a.popleft()

    else:

        p = a.pop()

    q = p



    res = 0

    fromLeft = 1 - fromLeft

    while a:

        p1 = a.popleft() if fromLeft else a.pop()

        if a:

            q1 = a.popleft() if fromLeft else a.pop()

            res += abs(p - p1) + abs(q - q1)

            p = p1

            q = q1

        else:

            res += max(abs(p - p1), abs(q - p1))

        fromLeft = 1 - fromLeft

    return res





ans = max(align(x, 1), align(y, 0))

print(ans)

from collections import deque

import sys

input = sys.stdin.readline





def solve(N, A, fromR=True):

    res = 0

    L, R = None, None



    while A:

        fromR = 1 - fromR



        if L is None:

            L = A.pop() if fromR else A.popleft()

            R = L

            continue



        if len(A) > 1:

            if fromR:

                x = A.pop()

                y = A.pop()

            else:

                x = A.popleft()

                y = A.popleft()

            res += abs((R + L) - (x + y))

            R, L = x, y

            continue



        if len(A) == 1:

            x = A.pop()

            res += max(abs(R - x), abs(L - x))

            continue



    return res





if __name__ == "__main__":

    N = int(eval(input()))

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

    B = deque(A[::])

    A = deque(A)

    



    ans1 = solve(N, A, 0)

    ans2 = solve(N, B, 1)

    ans = max(ans1, ans2)

    print(ans)

from collections import deque

n = int(eval(input()))
a = [int(eval(input())) for _ in range(n)]
a.sort()
x = deque(a)
y = deque(a)


def align(a, fromLeft=1):
    if fromLeft:
        p = a.popleft()
    else:
        p = a.pop()
    q = p
    res = 0
    fromLeft = 1 - fromLeft
    while a:
        p1 = a.popleft() if fromLeft else a.pop()
        if a:
            q1 = a.popleft() if fromLeft else a.pop()
            res += abs(p - p1) + abs(q - q1)
            p = p1
            q = q1
        else:
            res += max(abs(p - p1), abs(q - p1))
        fromLeft = 1 - fromLeft
    return res


ans = max(align(x, 1), align(y, 0))
print(ans)

from collections import deque
import sys

input = sys.stdin.readline


def solve(N, A, fromR=True):
    res = 0
    L, R = None, None
    while A:
        fromR = 1 - fromR
        if L is None:
            L = A.pop() if fromR else A.popleft()
            R = L
            continue
        if len(A) > 1:
            if fromR:
                x = A.pop()
                y = A.pop()
            else:
                x = A.popleft()
                y = A.popleft()
            res += abs((R + L) - (x + y))
            R, L = x, y
            continue
        if len(A) == 1:
            x = A.pop()
            res += max(abs(R - x), abs(L - x))
            continue
    return res


if __name__ == "__main__":
    N = int(eval(input()))
    A = sorted([int(eval(input())) for _ in range(N)])
    B = deque(A[::])
    A = deque(A)
    ans1 = solve(N, A, 0)
    ans2 = solve(N, B, 1)
    ans = max(ans1, ans2)
    print(ans)

+import sys -n = int(eval(input())) -a = [int(eval(input())) for _ in range(n)] -a.sort() -x = deque(a) -y = deque(a) +input = sys.stdin.readline -def align(a, fromLeft=1): -    if fromLeft: -        p = a.popleft() -    else: -        p = a.pop() -    q = p +def solve(N, A, fromR=True): -    fromLeft = 1 - fromLeft -    while a: -        p1 = a.popleft() if fromLeft else a.pop() -        if a: -            q1 = a.popleft() if fromLeft else a.pop() -            res += abs(p - p1) + abs(q - q1) -            p = p1 -            q = q1 -        else: -            res += max(abs(p - p1), abs(q - p1)) -        fromLeft = 1 - fromLeft +    L, R = None, None +    while A: +        fromR = 1 - fromR +        if L is None: +            L = A.pop() if fromR else A.popleft() +            R = L +            continue +        if len(A) > 1: +            if fromR: +                x = A.pop() +                y = A.pop() +            else: +                x = A.popleft() +                y = A.popleft() +            res += abs((R + L) - (x + y)) +            R, L = x, y +            continue +        if len(A) == 1: +            x = A.pop() +            res += max(abs(R - x), abs(L - x)) +            continue -ans = max(align(x, 1), align(y, 0)) -print(ans) +if __name__ == "__main__": +    N = int(eval(input())) +    A = sorted([int(eval(input())) for _ in range(N)]) +    B = deque(A[::]) +    A = deque(A) +    ans1 = solve(N, A, 0) +    ans2 = solve(N, B, 1) +    ans = max(ans1, ans2) +    print(ans)