Problem:
 Score : 400 points    Problem Statement You are given N integers; the i -th of them is A_i .
Find the maximum possible sum of the absolute differences between the adjacent elements after arranging these integers in a row in any order you like.      Constraints  2 \leq N \leq 10^5  1 \leq A_i \leq 10^9  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  A_1  :  A_N       Output Print the maximum possible sum of the absolute differences between the adjacent elements after arranging the given integers in a row in any order you like.        Sample Input 1 5
6
8
1
2
3      Sample Output 1 21  When the integers are arranged as 3,8,1,6,2 , the sum of the absolute differences between the adjacent elements is |3 - 8| + |8 - 1| + |1 - 6| + |6 - 2| = 21 . This is the maximum possible sum.       Sample Input 2 6
3
1
4
1
5
9      Sample Output 2 25       Sample Input 3 3
5
5
1      Sample Output 3 8

Solution (Python):
import os
import os
import sys
from collections import deque

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N = int(sys.stdin.readline())
A = [int(sys.stdin.readline()) for _ in range(N)]


def solve(A):
    if N % 2 == 0:
        ans = [-1] * N
        for i, a in enumerate(A[:N // 2]):
            ans[i * 2 + 1] = a
        for i, a in enumerate(A[N // 2:]):
            ans[i * 2] = a
    else:
        ans = deque()
        que = deque(A)
        while len(que) > 1:
            ans.append(que.popleft())
            ans.append(que.pop())
        ans.appendleft(que.pop())
        ans = list(ans)
    return np.abs(np.diff(ans)).sum()


A.sort()
a = solve(A)
A.reverse()
b = solve(A)
print(max(a, b))

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]

A.sort()

if N % 2 == 0:  
    small = A[:N//2]
    big = A[N//2:]
    ans = sum(big)*2 - sum(small)*2 - big[0] + small[-1]
    print(ans)
else:  
    small = A[:N//2]
    big = A[N//2+1:]
    midian = A[N//2]

    
    ans_big = sum(big)*2 - sum(small)*2 - big[0] + midian  
    
    ans_small = sum(big)*2 - sum(small)*2 - midian + small[-1]  
    print(max(ans_big, ans_small))

Solution (Python):
from collections import deque
N = int(input())
H = []
Q = deque()
for i in range(N):
  A = int(input())
  H.append(A)
H.sort()


if N%2 == 0:
  groupA = H[:N//2]
  groupB = H[N//2:]
  ans = 2*sum(groupB)-2*sum(groupA)-groupB[0]+groupA[-1]
else:
  groupA1 = H[:N//2]
  groupB1 = H[N//2:]
  groupA2 = H[:N//2+1]
  groupB2 = H[N//2+1:]
  ans1 = 2*sum(groupB1)-2*sum(groupA1)-groupB1[0]-groupB1[1]
  ans2 = 2*sum(groupB2)-2*sum(groupA2)+groupA2[-1]+groupA2[-2]
  ans = max(ans1,ans2)
print(ans)

Solution (Python):
n = int(input())
a = sorted([int(input()) for i in range(n)])
l = n//2

if n % 2 == 0:
    small = a[:l]
    big = a[l:]
    ans = sum(big) * 2 - sum(small) * 2  - big[0] + small[-1]
    print(ans)

else:
    small = a[:l]
    big = a[l+1:]
    ans = sum(big) * 2 - sum(small) * 2 - min(a[l] - small[-1], big[0] - a[l])
    print(ans)

Solution (Python):
N=int(input())
l=[]

for i in range(N):
    l.append(int(input()))

l=sorted(l)
i=0
sum=0
nr=0
nl=0
right=l.pop(0)
left=right

while len(l)>1:
    if i%4==0:
        nr=l.pop(-1)
        sum+=nr-right
        right=nr        
    elif i%4==1:
        nl=l.pop(-1)
        sum+=nl-left
        left=nl
    elif i%4==2:
        nr=l.pop(0)
        sum-=nr-right
        right=nr       
    elif i%4==3:
        nl=l.pop(0)
        sum-=nl-left
        left=nl
        
    i+=1



        
print(sum+max(abs(right-l[0]),abs(left-l[0])))

Solution (Python):
n = int(input())
a = sorted([int(input()) for i in range(n)])
l = n//2

if n % 2 == 0:
    small = a[:l]
    big = a[l:]
    ans = sum(big) * 2 - sum(small) * 2  - big[0] + small[-1]
    print(ans)

else:
    small = a[:l]
    big = a[l+1:]
    ans = sum(big) * 2 - sum(small) * 2 - min(a[l] - small[-1], big[0] - a[l])
    print(ans)

Solution (Python):
n = int(input())
a = sorted([int(input()) for i in range(n)])

if n % 2 == 0:
    small = a[:(n//2)]
    big = a[(n//2):]
    ans = sum(big) * 2 - sum(small) * 2  - big[0] + small[-1]
    print(ans)

else:
    small = a[:(n//2)]
    big = a[(n//2):]
    ans = sum(big) * 2 - sum(small) * 2  - big[0] -big[1]

    small = a[:(n//2)+1]
    big = a[(n//2)+1:]
    ans = max(ans, sum(big) * 2 - sum(small) * 2 + small[-1] + small[-2])
    print(ans)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
num = int(n/2)
a.sort(reverse = True)
x = []
y = []
if n%2 == 0:
    result = 0
    x = a[:num]
    y = a[num:]
    result += sum(x)*2 - x[-1]
    result -= sum(y)*2 - y[0]
    print(result)
else:
    x = a[:num]
    y = a[num+1:]
    mid = a[num]
    result_1 = int(sum(x)*2 + mid - x[-1] - sum(y)*2)
    result_2 = int(sum(x)*2 - sum(y)*2 -mid + y[0])
    print(max(result_1, result_2))

Solution (Python):
from collections import deque

n = int(input())
a = [0 for i in range(n)]
for i in range(n):
    a[i] = int(input())

a.sort()

d = deque()

lft = False
smll = False

d.append(a[0])
fst = 1
end = n-1

for i in range(n-1):
    if not(smll):
        if not(lft):
            d.appendleft(a[end])
            end -= 1
            lft = True
        else:
            d.append(a[end])
            end -= 1
            lft = False
            smll = True
    else:
        if not(lft):
            d.appendleft(a[fst])
            fst += 1
            lft = True
        else:
            d.append(a[fst])
            fst += 1
            lft = False
            smll = False

memo = d.pop()
ans = 0
for i in range(n-1):
    memo2 = d.pop()
    ans += abs(memo-memo2)
    memo = memo2
    


d.append(a[n-1])
fst = 0
end = n-2
lft = False
smll = True


for i in range(n-1):
    if not(smll):
        if not(lft):
            d.appendleft(a[end])
            end -= 1
            lft = True
        else:
            d.append(a[end])
            end -= 1
            lft = False
            smll = True
    else:
        if not(lft):
            d.appendleft(a[fst])
            fst += 1
            lft = True
        else:
            d.append(a[fst])
            fst += 1
            lft = False
            smll = False

memo3 = d.pop()
ans2 = 0
for i in range(n-1):
    memo4 = d.pop()
    ans2 += abs(memo3-memo4)
    memo3 = memo4
    
ans = max(ans,ans2)
    
print(ans)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
A.sort()


ans = 0
if N % 2 == 0:
  for i in range(N//2-1):
    ans -= A[i]*2
  ans += -A[N//2-1] + A[N//2]
  for i in range(N//2+1,N):
    ans += A[i]*2
else:
  ans1 = 0
  for i in range(N//2-1):
    ans1 -= A[i]*2
  ans1 += -A[N//2-1]-A[N//2]
  for i in range(N//2+1,N):
    ans1 += A[i]*2
  ans2 = 0
  for i in range(N//2):
    ans2 -= A[i]*2
  ans2 += A[N//2] + A[N//2+1]
  for i in range(N//2+2,N):
    ans2 += A[i]*2
  
  ans = max(ans1,ans2)
print(ans)

Solution (Python):
N = int(input())
A = sorted(int(input()) for _ in range(N))
if N % 2:
    B1 = A[:N // 2]
    C1 = A[N // 2:]
    c1 = 2 * sum(C1) - C1[0] - C1[1]
    b1 = 2 * sum(B1)
    B2 = A[:N // 2 + 1]
    C2 = A[N // 2 + 1:]
    c2 = 2 * sum(C2)
    b2 = 2 * sum(B2) - B2[-1] - B2[-2]
    print(max(c1 - b1, c2 - b2))
else:
    B = A[:N // 2]
    C = A[N // 2:]
    c = 2 * sum(C) - C[0]
    b = 2 * sum(B) - B[-1]
    print(c - b)

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]

a.sort(reverse = True)


keisuu = [0]*n
for i in range(n-1):
    if i % 2 == 0:
        keisuu[i] += 1
        keisuu[i+1] -= 1
    else:
        keisuu[i] -= 1
        keisuu[i+1] += 1
ans = 0
keisuu.sort(reverse = True)
for i in range(n):
    ans += a[i] * keisuu[i]


keisuu = [0]*n
for i in range(n-1):
    if i % 2 == 1:
        keisuu[i] += 1
        keisuu[i+1] -= 1
    else:
        keisuu[i] -= 1
        keisuu[i+1] += 1
keisuu.sort(reverse = True)
ans1 = 0
for i in range(n):
    ans1 += a[i] * keisuu[i]

print(max(ans, ans1))

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
a.sort()
m = n // 2 - 1
if n % 2 == 0:
    ans = sum(a[:m]) * -2 - a[m] + a[m+1] + sum(a[m+2:]) * 2
else:
    ans1 = sum(a[:m]) * -2 - (a[m] + a[m+1]) + sum(a[m+2:]) * 2
    ans2 = sum(a[:m+1]) * -2 + (a[m+1] + a[m+2]) + sum(a[m+3:]) * 2
    ans = max(ans1, ans2)
    
print(ans)

Solution (Python):

def main():
    N, *A = map(int, open(0).read().split())
    A.sort()
    if N & 1:
        low, high, mid = A[: N // 2], A[N // 2 + 1 :], A[N // 2]
        x = (sum(high) - sum(low)) * 2 - mid + low[-1]
        y = (sum(high) - sum(low)) * 2 + mid - high[0]
        ans = max(x, y)
    else:
        low, high = A[: N // 2], A[N // 2 :]
        ans = (sum(high) - sum(low)) * 2 - high[0] + low[-1]
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

def main():
    N, *A = map(int, open(0).read().split())
    A.sort()
    if N & 1:
        low, high, mid = A[: N // 2], A[N // 2 + 1 :], A[N // 2]
        x = (sum(high) - sum(low)) * 2 - mid + low[-1]
        y = (sum(high) - sum(low)) * 2 + mid - high[0]
        ans = max(x, y)
    else:
        low, high = A[: N // 2], A[N // 2 :]
        ans = (sum(high) - sum(low)) * 2 - high[0] + low[-1]
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

from collections import deque


def main():
    N, *A = map(int, open(0).read().split())
    A.sort()
    if N & 1:
        low, high, mid = A[: N // 2], A[N // 2 + 1 :], A[N // 2]
        x = (sum(high) - sum(low)) * 2 - mid + low[-1]
        y = (sum(high) - sum(low)) * 2 + mid - high[0]
        ans = max(x, y)
    else:
        low, high = A[: N // 2], A[N // 2 :]
        ans = (sum(high) - sum(low)) * 2 - high[0] + low[-1]
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):


import math

n = int(input())
num = [int(input()) for _ in range(n)]
num.sort()


def dif():
    ans = 0
    for i in range(1, n):
        ans += abs(b[i] - b[i - 1])
    return ans


b = [0]*n

if (n % 2 == 0):
    for i in range(n // 2):
        b[i * 2 + 1] = num[i]
        b[i * 2] = num[i + n // 2]
    
    print(dif())
    
    
    
    
else:
    
    
    for i in range(n // 2):
        b[i * 2 + 1] = num[i]
        b[i*2] = num[i+n//2+1]
    b[n - 1] = num[n // 2]
    temp = dif()
    
    for i in range(n // 2):
        b[i * 2 + 2] = num[i]
        b[i*2+1] = num[i+n//2+1]
    b[0] = num[n // 2]
    
    print(max(dif(), temp))

Solution (Python):
N = int(input())
A = sorted([int(input()) for _ in range(N)])

if N % 2 == 1:
    A11 = A[:N//2+1]
    A12 = A[N//2+1:]
    ans1 = sum(A12)*2-sum(A11)*2+A11[-1]+A11[-2]

    A21 = A[:N//2]
    A22 = A[N//2:]
    ans2 = sum(A22)*2-sum(A21)*2-A22[0]-A22[1]

    ans = max(ans1, ans2)
else:
    A1 = A[:N//2]
    A2 = A[N//2:]
    ans = sum(A2)*2-sum(A1)*2+A1[-1]-A2[0]


print(ans)

Solution (Python):
def main():

    N = int(input())
    A = []
    for _ in range(N):
        A.append(int(input()))
    A.sort()

    if N % 2 == 0:
        A1 = A[:N//2]
        A2 = A[N//2:][::-1]
        B1 = []
        B2 = []
        k, l = 0, 0
        for i in range(N):
            if i % 2 == 0:
                B1.append(A1[k])
                B2.append(A2[k])
                k += 1
            else:
                B1.append(A2[l])
                B2.append(A1[l])
                l += 1
        B1 = [B1[-1]] + B1[:-1]
        B2 = [B2[-1]] + B2[:-1]

        cand1, cand2 = 0, 0
        for i in range(1, N):
            cand1 += abs(B1[i-1]-B1[i])
            cand2 += abs(B2[i-1]-B2[i])
        return max(cand1, cand2)
    else:
        A3 = A[:N//2]
        A4 = A[N//2:][::-1]
        A5 = A[:(N+1)//2]
        A6 = A[(N+1)//2:][::-1]
        B3 = []
        B4 = []
        k, l = 0, 0
        for i in range(N):
            if i % 2 == 0:
                B3.append(A4[k])
                B4.append(A5[k])
                k += 1
            else:
                B3.append(A3[l])
                B4.append(A6[l])
                l += 1
        B3 = [B3[-1]] + B3[:-1]
        B4 = [B4[-1]] + B4[:-1]

        cand3, cand4 = 0, 0
        for i in range(1, N):
            cand3 += abs(B3[i-1]-B3[i])
            cand4 += abs(B4[i-1]-B4[i])
        return max(cand3, cand4)


if __name__ == '__main__':
    print(main())

Solution (Python):
N = int(input())
As = []
for i in range(N):
  A = int(input())
  As.append(A)
As.sort()
if N%2 == 0:
  r = 2*sum(As[N//2+1:])-2*sum(As[:N//2-1])+As[N//2]-As[N//2-1]
if N%2 == 1:
  r1 = 2*sum(As[N//2+1:])-2*sum(As[:N//2-1])-As[N//2]-As[N//2-1]
  As.reverse()
  r2 = -2*sum(As[N//2+1:])+2*sum(As[:N//2-1])+As[N//2]+As[N//2-1]
  r = max(r1, r2)
print(r)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]

A.sort()
ans = 0
if N%2==0:
    B1 = A[:N//2]
    B2 = A[N//2:]
    ans = 2*(sum(B2)-sum(B1)) + B1[-1] - B2[0]
else:
    B1 = A[:N//2]
    B2 = A[N//2:]
    p = 2*(sum(B2)-sum(B1)) - B2[0] - B2[1]

    C1 = A[:N//2+1]
    C2 = A[N//2+1:]
    q = 2*(sum(C2)-sum(C1)) + C1[-1] + C1[-2]
    ans = max(p, q)

print(ans)

Solution (Python):
from collections import deque
N = int(input())
A = [int(input()) for _ in range(N)] 

A.sort()
q = deque(A)


B = [0] * N


center = N // 2
minimum = q.popleft()
B[center] = minimum
left = center - 1
right = center + 1
cnt = 0
while q:
    if cnt % 4 == 0:
        num = q.pop()
        B[left] = num
        left -= 1
    elif cnt % 4 == 1:
        num = q.pop()
        B[right] = num
        right += 1
    elif cnt % 4 == 2:
        num = q.popleft()
        B[left] = num
        left -= 1
    elif cnt % 4 == 3:
        num = q.popleft()
        B[right] = num
        right += 1
    
    cnt += 1

ans_b = 0
for a,b in zip(B, B[1:]):
    ans_b += abs(a-b)


q = deque(A)

B = [0] * N

center = N // 2
maximum = q.pop()
B[center] = maximum

left = center - 1
right = center + 1
cnt = 0
while q:
    if cnt % 4 == 2:
        num = q.pop()
        B[left] = num
        left -= 1
    elif cnt % 4 == 3:
        num = q.pop()
        B[right] = num
        right += 1
    elif cnt % 4 == 0:
        num = q.popleft()
        B[left] = num
        left -= 1
    elif cnt % 4 == 1:
        num = q.popleft()
        B[right] = num
        right += 1
    
    cnt += 1


ans_c = 0
for a,b in zip(B, B[1:]):
    ans_c += abs(a-b)




print(max(ans_b, ans_c))

Solution (Python):
import sys,bisect as bs,collections as cl,heapq as hq
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return " ".join(map(str,x))
def max2(x): 
    return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

n = onem()
a = []
for i in range(n):
    a.append(onem())
a.sort()
bbb = a[n//2+(n%2):]
bbb.reverse()

bbbb = a[n//2:]

if len(bbb) >= 3:
    bbbb = [bbbb[0]] + bbbb[2:] + [bbbb[1]]


aaa = a[:n//2+(n%2)]
if len(aaa) >= 3:
    aaa = [aaa[-1]] + aaa[:-2] + [aaa[-2]]

aaaa = a[:n//2]

lll = []
llll = []

for i in range(n):
    if i % 2 == 0:
        lll.append(aaa[i//2])
        llll.append(bbbb[i//2])
    else:
        lll.append(bbb[i//2])
        llll.append(aaaa[i//2])
ans = 0
an = 0
for i in range(n):
    if i == 0:
        st = lll[0]
        stt = llll[0]
    else:
        ans += abs(lll[i] - st)
        st = lll[i]
        an += abs(llll[i] - stt)
        stt = llll[i]
print(max(ans,an))

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]

A.sort()

if N % 2 == 0:
    small = A[:N // 2]
    big = A[N//2:]
    x = 2 * sum(small) - small[-1]
    y = 2 * sum(big) - big[0]
    ans = y - x
else:
    small = A[:N // 2]
    big = A[N // 2 + 1:]
    mid = A[N // 2]

    left = 2 * sum(big) - 2 * sum(small) - mid + small[-1]
    right = 2 * sum(big) - 2 * sum(small) + mid - big[0]

    ans = max(left, right)

print(ans)

Solution (Python):
def calc_diffsum(li):
    ans = 0
    for ind,i in enumerate(li[:-1]):
        ans += abs(i-li[ind+1])
    return ans

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

A.sort()
li = []
li2 = []
for ind,i in enumerate(A):
    if(ind%2==1):
        li.append(A[-ind//2])
        li2.append(A[ind//2])
    else:
        li.append(A[ind//2])
        li2.append(A[-ind//2-1])

if(abs(li[-1]-li[0])>abs(li[-1]-li[-2])):
    li.insert(0,li[-1])
    del li[-1]
if(abs(li2[-1]-li2[0])>abs(li2[-1]-li2[-2])):
    li2.insert(0,li2[-1])
    del li2[-1]
print(max(calc_diffsum(li),calc_diffsum(li2)))

Solution (Python):
N=int(input())
a=sorted([int(input()) for i in range(N)])
r=[0]*N
r[1:-1:2]=a[:(N-1)//2]
r[-1]=a[(N-1)//2]
r[:-1:2]=a[(N-1)//2+1:]
A=list(reversed(a))
R=[0]*N
R[1:-1:2]=A[:(N-1)//2]
R[-1]=A[(N-1)//2]
R[:-1:2]=A[(N-1)//2+1:]
print(max(sum([abs(r[i]-r[i+1]) for i in range(N-1)]),sum([abs(R[i]-R[i+1]) for i in range(N-1)])))

Solution (Python):

N = int(input())
A = [int(input()) for i in range(N)]
A.sort(reverse=True)

p = 0
n = 0
if N%2 == 0:
    MAX = 0
    for i in range(N):
        if i < (N//2):
            if i == (N//2)-1:
                p+=A[i]
            else:
                p+=A[i]*2
        else:
            if i == (N//2):
                n+=A[i]
            else:
                n+=A[i]*2
    MAX = max(MAX,p-n)
else:
    MAX = 0
    for i in range(N):
        if i <= (N//2):
            if i == (N//2) or i == (N//2)-1:
                p+=A[i]
            else:
                p+=A[i]*2
        else:
            n+=A[i]*2
    MAX = max(MAX,p-n)
    p = 0
    n = 0
    for i in range(N):
        if i <= (N//2)-1:
            p+=A[i]*2
        else:
            if i == (N//2) or i == (N//2)+1:
                n+=A[i]
            else:
                n+=A[i]*2
    MAX = max(MAX,p-n)
        
print(MAX)

Solution (Python):
from collections import deque

n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))

stack = deque(sorted(a))
one = 0
right = stack.pop()
left = right
count = 0
while stack:
    if count % 2 == 0:
        if len(stack) >= 2:
            stack_right = stack.popleft()
            stack_left = stack.popleft()
            one += abs(stack_right - right) + abs(stack_left - left)
            right = stack_right
            left = stack_left
        else:
            stack_one = stack.pop()
            one += max(abs(stack_one - right), abs(stack_one - left))
    else:
        if len(stack) >= 2:
            stack_right = stack.pop()
            stack_left = stack.pop()
            one += abs(stack_right - right) + abs(stack_left - left)
            right = stack_right
            left = stack_left
        else:
            stack_one = stack.pop()
            one += max(abs(stack_one - right), abs(stack_one - left))
    count += 1
stack = deque(sorted(a))
two = 0
right = stack.popleft()
left = right
count = 0
while stack:
    if count % 2 == 1:
        if len(stack) >= 2:
            stack_right = stack.popleft()
            stack_left = stack.popleft()
            two += abs(stack_right - right) + abs(stack_left - left)
            right = stack_right
            left = stack_left
        else:
            stack_one = stack.pop()
            two += max(abs(stack_one - right), abs(stack_one - left))
    else:
        if len(stack) >= 2:
            stack_right = stack.pop()
            stack_left = stack.pop()
            two += abs(stack_right - right) + abs(stack_left - left)
            right = stack_right
            left = stack_left
        else:
            stack_one = stack.pop()
            two += max(abs(stack_one - right), abs(stack_one - left))
    count += 1
print(max(one, two))

Solution (Python):
N = int(input())
A = [0] * N
for i in range(N):
    A[i] = int(input())

sorted_A = sorted(A, reverse=True)

if N % 2 == 0:
    ans = sum(sorted_A[:(N - 2) // 2]) * 2 + sorted_A[(N - 2) // 2] - sorted_A[(N - 2) // 2 + 1] - sum(sorted_A[(N - 2) // 2 + 2:] * 2)
else:
    ans_a = sum(sorted_A[:(N - 2) // 2]) * 2 + sum(sorted_A[(N - 2) // 2: (N - 2) // 2 + 2]) - sum(sorted_A[(N - 2) // 2 + 2:]) * 2
    ans_b = sum(sorted_A[:(N - 1) // 2]) * 2 - sum(sorted_A[(N - 1) // 2: (N - 1) // 2 + 2]) - sum(sorted_A[(N - 1) // 2 + 2:]) * 2
    ans = max(ans_a, ans_b)
print(ans)

Solution (Python):
n=int(input())
a=sorted([int(input())for i in range(n)])
def f(x):
    p,q=1,n-2
    l,r=[a[0]],[a[n-1]]
    c=a[n-1]-a[0]
    t=0
    while p<=q:
        if t==x:
            r.append(a[q])
            c+=a[q]-l[0]
            q-=1
            l.pop(0)
        else:
            l.append(a[p])
            c+=r[0]-a[p]
            p+=1
            r.pop(0)
        t=(t+1)%2
    return c
print(max(f(0),f(1)))

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]
n=int(input())
a=[int(input()) for i in range(n)]
a.sort()
if n==2:
    print(a[1]-a[0])
    quit()

ans=0
for i in range((n-1)//2):
    ans-=a[i]
for i in range((n-2)//2):
    ans+=a[n-1-i]
ans*=2
if n%2!=0:
    ans+=a[n//2]+a[n//2+1]
else:
    ans+=a[n//2]-a[n//2-1]

ans2=0
for i in range((n-1)//2):
    ans2+=a[n-1-i]
for i in range((n-2)//2):
    ans2-=a[i]
ans2*=2
if n%2!=0:
    ans2-=a[n//2]+a[n//2-1]
else:
    ans2+=a[n//2]-a[n//2+1]




print(max(ans,ans2))

Solution (Python):



import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = [I() for _ in range(n)]
a.sort()
if n % 2:
    lower = a[:n//2]
    upper = a[n//2+1:]
    mid = a[n//2]
    x = 2*sum(upper) - 2*sum(lower) - mid + lower[-1]
    y = 2*sum(upper) - 2*sum(lower) + mid - upper[0]
    print(max(x,y))
else:
    lower = a[:n//2]
    upper = a[n//2:]
    x = 2*sum(upper) - upper[0]
    y = 2*sum(lower) - lower[-1]
    print(x-y)

Solution (Python):
n=int(input())
a=sorted([int(input()) for _ in [0]*n])

if n%2==0:
    print(2*sum(a[n//2+1:])-2*sum(a[:n//2-1])+a[n//2]-a[n//2-1])
else:
    b=abs(sum(a[n//2-1:n//2+1])+2*sum(a[:n//2-1])-2*sum(a[n//2+1:]))
    a.sort(reverse=True)
    c=abs(sum(a[n//2-1:n//2+1])+2*sum(a[:n//2-1])-2*sum(a[n//2+1:]))
    print(max(b,c))

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

A.sort()

if N%2 == 0:
    lower = A[:N//2]
    upper = A[N//2:]
    x = 2 * sum(upper) - upper[0]
    y = 2 * sum(lower) - lower[-1]
    answer = x - y
else:
    lower = A[:N//2]
    upper = A[N//2+1:]
    mid = A[N//2]
    x = 2 * sum(upper) - 2 * sum(lower) - mid + lower[-1]
    y = 2 * sum(upper) - 2 * sum(lower) + mid - upper[0]
    answer = max(x,y)
print(answer)

Solution (Python):

import sys
sys.setrecursionlimit(10**7)
import math
from collections import defaultdict


mod = 10**9 + 7


def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
  read_all = [tuple(map(int, input().split())) for _ in range(N)]
  return map(list,zip(*read_all))



from collections import deque

N = I()
tempA = [0]*N
for i in range(N):
  tempA[i] = I()
  
tempA.sort()
A = deque(maxlen=N)
for i in range(N):
  A.append(tempA[i])
A2 = tempA[:]


temp = A.popleft()
B = deque(maxlen=N)
B.append(temp)
for i in range(math.ceil((N-1)/2)):
  if i%2==0:
    temp = A.pop()
    B.appendleft(temp)
    if len(A)>0:
      temp = A.popleft()
      B.appendleft(temp)
  else:
    temp = A.pop()
    B.append(temp)
    if len(A)>0:
      temp = A.popleft()
      B.append(temp)

val1 = 0
val2 = val1
for i in range(N-1):
  val1 += abs(B[i]-B[i+1])

if N%2==1:
  val2 = val1-abs(A2[(N-1)//2]-A2[(N-1)//2+1])+abs(A2[(N-1)//2]-A2[(N-1)//2-1])
print(max(val1,val2))

Solution (Python):
from collections import deque

def solve(n, a):
    a = sorted(a)
    return max(f(n, a), f(n, a[::-1]))

def f(n, a):
    a = deque(a)
    b = deque([])
    for i in range(n):
        if (i-1) % 4 < 2:
            v = a.popleft()
        else:
            v = a.pop()
        if i % 2 == 0:
            b.appendleft(v)
        else:
            b.append(v)
    return sum([abs(b[i+1]-b[i]) for i in range(n-1)])

n = int(input())
a = [int(input()) for i in range(n)]
print(solve(n, a))

Solution (Python):
import sys
input = sys.stdin.readline
n = int(input().strip())
an = [int(input().strip()) for i in range(n)]
an.sort()

ans = an[-1] - an[0]
high = an[-1]
low = an[0]
c = n//2

if n % 2:
  for i in range(1,c):
    ans += high - an[i] + an[-i-1] - low
    high = an[-i-1]
    low = an[i]
  ans += max(high-an[c],an[c]-low)
  print(ans)
else:
  for i in range(1,c):
    ans += high - an[i] + an[-i-1] - low
    high = an[-i-1]
    low = an[i]
  print(ans)

Solution (Python):
N=int(input())
A=sorted(int(input()) for _ in [0]*N)

if N%2:
    a = A[:N//2]
    b = A[N//2+1:][::-1]
    m = A[N//2]
    if abs(m-a[-1])>abs(m-b[-1]):
        a,b = b,a
    B = [m]*N
    for i in range(N//2):
        B[2*i+1] = b[i]
        B[2*i+2] = a[i]
else:
    a = A[:N//2][::-1]
    b = A[N//2:][::-1]
    B = [0]*N
    for i in range(N//2):
        B[i*2] = a[i]
        B[i*2+1] = b[i]
ans = sum(abs(B[i]-B[i+1]) for i in range(N-1))
print(ans)

Solution (Python):
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))

Solution (Python):
import collections

n=int(input())
arr=[int(input()) for _ in range(n)]
arr=sorted(arr)
if n%2==1:
  q=collections.deque()
  q.append(arr[0])
  l=1
  r=n-1
  move=1
  while 1:
    q.appendleft(arr[r])
    r-=1
    q.append(arr[r])
    r-=1
    move+=2
    if move==n:
      break
    q.appendleft(arr[l])
    l+=1
    q.append(arr[l])
    l+=1
    move+=2
    if move==n:
      break
  q=list(q)
  ans1=0
  for i in range(n-1):
    ans1+=abs(q[i+1]-q[i])
  q=collections.deque()
  q.append(arr[-1])
  l=0
  r=n-2
  move=1
  while 1:
    q.appendleft(arr[l])
    l+=1
    q.append(arr[l])
    l+=1
    move+=2
    if move==n:
      break
    q.appendleft(arr[r])
    r-=1
    q.append(arr[r])
    r-=1
    move+=2
    if move==n:
      break
  q=list(q)
  ans2=0
  for i in range(n-1):
    ans2+=abs(q[i+1]-q[i])
  ans=max(ans1,ans2)
  print(ans)
else:
  q=collections.deque()
  q.append(arr[0])
  l=1
  r=n-1
  move=1
  while 1:
    if n-move==1:
      break
    q.appendleft(arr[r])
    r-=1
    q.append(arr[r])
    r-=1
    move+=2
    if n-move==1:
      break
    q.appendleft(arr[l])
    l+=1
    q.append(arr[l])
    l+=1
    move+=2
  q=list(q)
  ans1=0
  for i in range(n-2):
    ans1+=abs(q[i+1]-q[i])
  ans1+=max(abs(arr[l]-q[0]),abs(arr[l]-q[-1]))
  q=collections.deque()
  q.append(arr[-1])
  l=0
  r=n-2
  move=1
  while 1:
    if n-move==1:
      break
    q.appendleft(arr[l])
    l+=1
    q.append(arr[l])
    l+=1
    move+=2
    if n-move==1:
      break
    q.appendleft(arr[r])
    r-=1
    q.append(arr[r])
    r-=1
    move+=2
  q=list(q)
  ans2=0
  for i in range(n-2):
    ans2+=abs(q[i+1]-q[i])
  ans2+=max(abs(arr[r]-q[0]),abs(arr[r]-q[-1]))
  ans=max(ans1,ans2)
  print(ans)

Solution (Python):
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
a.sort()

if n == 2:
    print(a[1] - a[0])
    exit()

ans = 0
middles = [n // 2 - 1, n // 2, n // 2 + 1]
if middles.count(0):
    middles.remove(0)
if middles.count(n-1):
    middles.remove(n-1)
for mid in middles:
    l = 0; r = n-1
    val = abs(a[mid] - a[l])
    state = True 
    while l != r:
        val += abs(a[r] - a[l])
        if state:
            l += 1
            if l == mid:
                l += 1
            state = False
        else:
            r -= 1
            if r == mid:
                r -= 1
            state = True
    ans = max(ans, val)
for mid in middles:
    l = 0; r = n-1
    val = abs(a[mid] - a[r])
    state = True 
    while l != r:
        val += abs(a[r] - a[l])
        if state:
            r -= 1
            if r == mid:
                r -= 1
            state = False
        else:
            l += 1
            if l == mid:
                l += 1
            state = True
    ans = max(ans, val)
print(ans)

Solution (Python):
n=int(input())
a=[int(input()) for i in range(n)]
a.sort()
t=n//2
if(n%2==0):
    ans=a[t]-a[t-1]+(sum(a[t+1:])-sum(a[:t-1]))*2
else:
    a1=(sum(a[t+1:])-sum(a[:t-1]))*2-a[t]-a[t-1]
    a2=(sum(a[t+2:])-sum(a[:t]))*2+a[t]+a[t+1]
    ans=max(a1,a2)
print(ans)

Solution (Python):

"""
C - Align
https://atcoder.jp/contests/tenka1-2018-beginner/tasks/tenka1_2018_c

"""
import sys


from collections import deque

def solve(N, numbers):
    res = deque([numbers[0]])
    numbers = deque(numbers[1:])
    while numbers:
        bb = abs(numbers[-1] - res[-1])
        bf = abs(numbers[-1] - res[0])
        ff = abs(numbers[0] - res[0])
        fb = abs(numbers[0] - res[-1])
        t = sorted([[bb, 'bb'], [bf, 'bf'], [ff, 'ff'], [fb, 'fb']])
        if t[-1][1] == 'bb':
            res.append(numbers.pop())
        elif t[-1][1] == 'bf':
            res.appendleft(numbers.pop())
        elif t[-1][1] == 'ff':
            res.appendleft((numbers.popleft()))
        elif t[-1][1] == 'fb':
            res.append(numbers.popleft())

    res = list(res)
    return sum([abs(a-b) for a, b in zip(res, res[1:])])



def main(args):
    N = int(input())
    numbers = sorted([int(input()) for _ in range(N)])
    ans = solve(N, numbers)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):
N = int(input())
 
num_list = []
for _ in range(N):
    num_list.append(int(input()))
    
num_sorted = sorted(num_list)
 
def calc_diff(add, add2, diff, diff2, num_sorted):
    total = 0
    
    for _ in range(add2):
        tmp = num_sorted.pop()
        total += tmp * 2
        
    for _ in range(add):
        tmp = num_sorted.pop()
        total += tmp * 1
        
    for _ in range(diff):
        tmp = num_sorted.pop()
        total -= tmp * 1
            
    for _ in range(diff2):
        tmp = num_sorted.pop()
        total -= tmp * 2
        
    return total
  
import copy
 
def calc(N, num_sorted):
    
    diff2 = 0
    add2 = 0
    diff = 0
    add = 0
    
    if N % 2 == 0:
        add = 1
        diff = 1
        add2 = N // 2 - 1
        diff2 = N // 2 - 1
        
        total = calc_diff(add, add2, diff, diff2, copy.deepcopy(num_sorted))
 
        return total
            
    if N % 2 == 1:        
        add = 0
        diff = 2
        add2 =(N - 1) // 2
        diff2 = (N - 1) // 2 - 1
        
        totalB = calc_diff(add, add2, diff, diff2, copy.deepcopy(num_sorted))
        
        add = 2
        diff = 0
        add2 =(N - 1) // 2 - 1
        diff2 = (N - 1) // 2
        
        totalC = calc_diff(add, add2, diff, diff2, copy.deepcopy(num_sorted))
        
        return max(totalB, totalC)
      
print(calc(N, num_sorted))

Solution (Python):
n=int(input())
A=sorted([int(input()) for i in range(n)])
if n%2==0:
  ans=-2*sum(A[:n//2-1])-A[n//2-1]+A[n//2]+2*sum(A[n//2+1:])
  print(ans)
else:
  ans=-2*sum(A[:n//2])+A[n//2]+A[n//2+1]+2*sum(A[n//2+2:])
  ans=max(ans,-2*sum(A[:n//2-1])-A[n//2-1]-A[n//2]+2*sum(A[n//2+1:]))
  print(ans)