Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = []
for i in range(N):
	A.append(int(input()))
A.sort()
ary1 = [A[0]]
ary2 = [A[N-1]]
fst = 0
lst = N
fst_flg = False
sum1, sum2 = 0,0
while fst + 1 < lst:
	if fst_flg:
		fst_flg = False
		if fst + 2 != lst:
			fst += 1
			ary1 = [A[fst]] + ary1 + [A[fst+1]]
			ary2 = [A[N-1-fst]] + ary2 + [A[N-1-fst-1]]
			fst += 1
			l = len(ary1)
			sum1 += abs(ary1[0]-ary1[1]) + abs(ary1[l-1]-ary1[l-2])
			sum2 += abs(ary2[0]-ary2[1]) + abs(ary2[l-1]-ary2[l-2])
		else:
			fst += 1
			ary1 = [A[fst]] + ary1
			ary2 = [A[N-1-fst]] + ary2
			sum1 += abs(ary1[0]-ary1[1])
			sum2 += abs(ary2[0]-ary2[1])
	else:
		fst_flg = True
		if fst + 2 != lst:
			lst -= 1
			ary1 = [A[lst]] + ary1 + [A[lst-1]]
			ary2 = [A[N-1-lst]] + ary2 + [A[N-lst]]
			lst -= 1
			l = len(ary1)
			sum1 += abs(ary1[0]-ary1[1]) + abs(ary1[l-1]-ary1[l-2])
			sum2 += abs(ary2[0]-ary2[1]) + abs(ary2[l-1]-ary2[l-2])
		else:
			lst -= 1
			ary1 = [A[lst]] + ary1
			ary2 = [A[N-1-lst]] + ary2
			sum1 += abs(ary1[0]-ary1[1])
			sum2 += abs(ary2[0]-ary2[1])
print(max(sum1,sum2))

Fixed submission:
N = int(input())
A = []
for i in range(N):
	A.append(int(input()))
A.sort()
if N % 2 == 0:
	print(-2*sum(A[:N//2-1]) + 2*(sum(A[N//2+1:])) + A[N//2] - A[N//2-1])
	exit()

a = 2*sum(A[N//2+1:]) - 2*sum(A[:N//2-1]) - A[N//2] - A[N//2-1]
b = 2*sum(A[N//2+2:]) - 2*sum(A[:N//2]) + A[N//2] + A[N//2+1]
print(max(a,b))

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = []
for i in range(N):
	A.append(int(input()))
A.sort()
ary1 = [A[0]]
ary2 = [A[N-1]]
fst = 0
lst = N
fst_flg = False
sum1, sum2 = 0,0
while fst + 1 < lst:
	if fst_flg:
		fst_flg = False
		if fst + 2 != lst:
			fst += 1
			ary1 = [A[fst]] + ary1 + [A[fst+1]]
			ary2 = [A[N-1-fst]] + ary2 + [A[N-1-fst-1]]
			fst += 1
			l = len(ary1)
			sum1 += abs(ary1[0]-ary1[1]) + abs(ary1[l-1]-ary1[l-2])
			sum2 += abs(ary2[0]-ary2[1]) + abs(ary2[l-1]-ary2[l-2])
		else:
			fst += 1
			ary1 = [A[fst]] + ary1
			ary2 = [A[N-1-fst]] + ary2
			sum1 += abs(ary1[0]-ary1[1])
			sum2 += abs(ary2[0]-ary2[1])
	else:
		fst_flg = True
		if fst + 2 != lst:
			lst -= 1
			ary1 = [A[lst]] + ary1 + [A[lst-1]]
			ary2 = [A[N-1-lst]] + ary2 + [A[N-lst]]
			lst -= 1
			sum1 += abs(ary1[0]-ary1[1]) + abs(ary1[l-1]-ary1[l-2])
			sum2 += abs(ary2[0]-ary2[1]) + abs(ary2[l-1]-ary2[l-2])
		else:
			lst -= 1
			ary1 = [A[lst]] + ary1
			ary2 = [A[N-1-lst]] + ary2
			sum1 += abs(ary1[0]-ary1[1])
			sum2 += abs(ary2[0]-ary2[1])
print(max(sum1,sum2))

Fixed submission:
N = int(input())
A = []
for i in range(N):
	A.append(int(input()))
A.sort()
if N % 2 == 0:
	print(-2*sum(A[:N//2-1]) + 2*(sum(A[N//2+1:])) + A[N//2] - A[N//2-1])
	exit()

a = 2*sum(A[N//2+1:]) - 2*sum(A[:N//2-1]) - A[N//2] - A[N//2-1]
b = 2*sum(A[N//2+2:]) - 2*sum(A[:N//2]) + A[N//2] + A[N//2+1]
print(max(a,b))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = []
for i in range(N):
	A.append(int(input()))
A.sort()
ary1 = [A[0]]
ary2 = [A[N-1]]
fst = 0
lst = N
fst_flg = False
while fst + 1 < lst:
	if fst_flg:
		fst_flg = False
		if fst + 2 != lst:
			fst += 1
			ary1 = [A[fst]] + ary1 + [A[fst+1]]
			ary2 = [A[N-1-fst]] + ary2 + [A[N-1-fst-1]]
			fst += 1
		else:
			fst += 1
			ary1 = [A[fst]] + ary1
			ary2 = [A[N-1-fst]] + ary2
	else:
		fst_flg = True
		if fst + 2 != lst:
			lst -= 1
			ary1 = [A[lst]] + ary1 + [A[lst-1]]
			ary2 = [A[N-1-lst]] + ary2 + [A[N-lst]]
			lst -= 1
		else:
			lst -= 1
			ary1 = [A[lst]] + ary1
			ary2 = [A[N-1-lst]] + ary2
sum1,sum2=0,0
for i in range(N-1):
	sum1 += abs(ary1[i] - ary1[i+1])
	sum2 += abs(ary2[i] - ary2[i+1])
print(max(sum1,sum2))

Fixed submission:
N = int(input())
A = []
for i in range(N):
	A.append(int(input()))
A.sort()
if N % 2 == 0:
	print(-2*sum(A[:N//2-1]) + 2*(sum(A[N//2+1:])) + A[N//2] - A[N//2-1])
	exit()

a = 2*sum(A[N//2+1:]) - 2*sum(A[:N//2-1]) - A[N//2] - A[N//2-1]
b = 2*sum(A[N//2+2:]) - 2*sum(A[:N//2]) + A[N//2] + A[N//2+1]
print(max(a,b))