Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())

a=[]

for i in range(n):
    b=int(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)




Fixed submission:
n=int(input())
l=[]
for i in range(n):
    l.append(int(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))

Buggy python submission (verdict: Runtime Error):
n=int(input())

a=[]

for i in range(n):
    b=int(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)




Fixed submission:
n=int(input())
l=[]
for i in range(n):
    l.append(int(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))

Buggy python submission (verdict: Runtime Error):
n=int(input())

a=[]

for i in range(n):
    b=int(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

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

ans.append(ans[0])     
num=0
num2=0
num3=0


for i in range(n):
    num+=abs(ans[i]-ans[i+1])

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




Fixed submission:
n=int(input())
l=[]
for i in range(n):
    l.append(int(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))

Buggy python submission (verdict: Runtime Error):
n=int(input())

a=[]

for i in range(n):
    b=int(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

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

ans.append(ans[0])     
num=0
num2=0
num3=0


for i in range(n-1):
    num+=abs(ans[i]-ans[i+1])

for i in range(n-1):
    num2+=abs(ans[i+1]-ans[i+2])

for i in range(n-1):
    if i==0:
        num3+=abs(ans[-2]-ans[0])
    else:
        num3+=abs(ans[i]-ans[i-1])
        
print(max(num,num2,num3))




Fixed submission:
n=int(input())
l=[]
for i in range(n):
    l.append(int(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))