Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = sorted([int(input()) for i in range(N)])
ans1 = [A[N-1]]
ans2 = [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.insert(0, A[f])
            f += 1

        ans2.append(A[b2])
        b2 -= 1
        if f2 <= b2:
            ans2.insert(0, A[b2])
            b2 -= 1
    else:
        ans1.append(A[b])
        b -= 1
        if f <= b:
            ans1.insert(0, A[b])
            b -= 1

        ans2.append(A[f2])
        f2 += 1
        if f2 <= b2:
            ans2.insert(0, A[f2])
            f2 += 1            
    s = not s


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

print(max(a1,a2))

Fixed submission:
N = int(input())
A = sorted([int(input()) 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 = sum([i*j for i,j in zip(a, A)])
s2 = sum([i*j for i,j in zip(b, A)])
print(max(s1,s2))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = sorted([int(input()) for i in range(N)])
ans1 = [A[N-1]]
ans2 = [A[0]]

f = 0
b = N - 2
s = True
while f <= b:
    if s:
        ans1.append(A[f])
        f += 1
        if f <= b:
            #ans1 = [A[f]] + ans1
            ans1.insert(0, A[f])
            f += 1
    else:
        ans1.append(A[b])
        b -= 1
        if f <= b:
            ans1.insert(0, A[b])
            #ans1 = [A[b]] + ans1
            b -= 1
    s = not s

f = 1
b = N - 1
s = True
while f <= b:
    if s:
        ans2.append(A[b])
        b -= 1
        if f <= b:
            ans2.insert(0, A[b])
            #ans2 = [A[b]] + ans2
            b -= 1
    else:
        ans2.append(A[f])
        f += 1
        if f <= b:
            ans2.insert(0, A[f])
            #ans2 = [A[f]] + ans2
            f += 1
    s = not s

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

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

print(max(a1,a2))

Fixed submission:
N = int(input())
A = sorted([int(input()) 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 = sum([i*j for i,j in zip(a, A)])
s2 = sum([i*j for i,j in zip(b, A)])
print(max(s1,s2))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = sorted([int(input()) for i in range(N)])
ans1 = [A[N-1]]
ans2 = [A[0]]

f = 0
b = N - 2
s = True
while f <= b:
    if s:
        ans1 = ans1 + [A[f]]
        f += 1
        if f <= b:
            ans1 = [A[f]] + ans1
            f += 1
    else:
        ans1 = ans1 + [A[b]]
        b -= 1
        if f <= b:
            ans1 = [A[b]] + ans1
            b -= 1
    s = not s

f = 1
b = N - 1
s = True
while f <= b:
    if s:
        ans2 = ans2 + [A[b]]
        b -= 1
        if f <= b:
            ans2 = [A[b]] + ans2
            b -= 1
    else:
        ans2 = ans2 + [A[f]]
        f += 1
        if f <= b:
            ans2 = [A[f]] + ans2
            f += 1
    s = not s

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

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

print(max(a1,a2))

Fixed submission:
N = int(input())
A = sorted([int(input()) 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 = sum([i*j for i,j in zip(a, A)])
s2 = sum([i*j for i,j in zip(b, A)])
print(max(s1,s2))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = sorted([int(input()) for i in range(N)])
ans1 = [A[N-1]]
ans2 = [A[0]]

f = 0
b = N - 2
s = True
while f <= b:
    if s:
        ans1 = ans1 + [A[f]]
        f += 1
        if f <= b:
            ans1 = [A[f]] + ans1
            f += 1
    else:
        ans1 = ans1 + [A[b]]
        b -= 1
        if f <= b:
            ans1 = [A[b]] + ans1
            b -= 1
    s = not s

f = 1
b = N - 1
s = True
while f <= b:
    if s:
        ans2 = ans2 + [A[b]]
        b -= 1
        if f <= b:
            ans2 = [A[b]] + ans2
            b -= 1
    else:
        ans2 = ans2 + [A[f]]
        f += 1
        if f <= b:
            ans2 = [A[f]] + ans2
            f += 1
    s = not s

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

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

print(max(a1,a2))

Fixed submission:
N = int(input())
A = sorted([int(input()) 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 = sum([i*j for i,j in zip(a, A)])
s2 = sum([i*j for i,j in zip(b, A)])
print(max(s1,s2))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = sorted([int(input()) for i in range(N)])
ans1 = [A[N-1]]
ans2 = [A[0]]

f = 0
b = N - 2
s = True
while f <= b:
    if s:
        ans1 = ans1 + [A[f]]

        f += 1
        if f <= b:
            ans1 = [A[f]] + ans1
            f += 1
    else:
        ans1 = ans1 + [A[b]]
        b -= 1
        if f <= b:
            ans1 = [A[b]] + ans1
            b -= 1
    s = not s

f = 1
b = N - 1
s = True
while f <= b:
    if s:
        
        ans2 = ans2 + [A[b]]
        b -= 1
        if f <= b:
            ans2 = [A[b]] + ans2
            b -= 1
    else:
        ans2 = ans2 + [A[f]]
        f += 1
        if f <= b:
            ans2 = [A[f]] + ans2
            f += 1

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

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

print(max(a1,a2))

Fixed submission:
N = int(input())
A = sorted([int(input()) 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 = sum([i*j for i,j in zip(a, A)])
s2 = sum([i*j for i,j in zip(b, A)])
print(max(s1,s2))