Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque

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


def resolve():
    n = int(input())
    A = sorted(list(int(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()


Fixed submission:
import sys
from collections import deque

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


def resolve():
    n = int(input())
    A = sorted(list(int(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()

Buggy python submission (verdict: Runtime Error):
import sys
from collections import deque

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


def resolve():
    n = int(input())
    A = sorted(list(int(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[0])", abs(B[0] - A[0])]
            diff_right_mi = ["A.popleft()", "B.append(A[0])", abs(B[-1] - A[0])]

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

            L = [diff_left_mi, diff_right_mi, diff_left_ma, diff_right_ma]
            ma = 0
            for l in L:
                if ma < l[2]:
                    op1 = l[0]
                    op2 = l[1]
                    ma = l[2]
            eval(op2)
            eval(op1)

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

    print(res)


if __name__ == '__main__':
    resolve()


Fixed submission:
import sys
from collections import deque

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


def resolve():
    n = int(input())
    A = sorted(list(int(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()