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 itertools
import numpy as np
data_num = int(input()) 
data = [int(input()) for i in range(data_num)] 



data.sort()
npdata=np.array(data)

parts1=[1]
parts2=[2 for i in range(data_num-2)]
parts1.extend(parts2)
parts1.append(1)
npparts1=np.array(parts1)

parts0=[(-1)**(i+1) for i in range(data_num)]
npparts0=np.array(parts0)

y=npparts1*npparts0

y=y.tolist()
y.sort()

y=np.array(y)
z1=sum(data*y)

parts01=[(-1)**i for i in range(data_num)]
npparts0=np.array(parts01)

y=npparts1*npparts0

y=y.tolist()
y.sort()
y=np.array(y)
z2=sum(data*y)

print(max(z1,z2))

Solution (Python):
n = int(input())
A = [int(input()) for _ in range(n)]
A.sort()
if n%2==1:
    a = 2*sum(A[n-n//2:]) - sum(A[n-n//2-2:n-n//2]) - 2*sum(A[:n-n//2-2])
    b = -2*sum(A[:n//2]) + sum(A[n//2:n//2+2]) + 2*sum(A[n//2+2:])
    print(max(a,b))
else: print(-2*sum(A[:(n-2)//2])-A[(n-2)//2]+A[(n-2)//2+1]+2*sum(A[(n-2)//2+2:]))

Solution (Python):
n=int(input());A=[int(input()) for _ in range(n)];A.sort()
if n%2==1:print(max(2*sum(A[n-n//2:])-sum(A[n-n//2-2:n-n//2])-2*sum(A[:n-n//2-2]),-2*sum(A[:n//2])+sum(A[n//2:n//2+2])+2*sum(A[n//2+2:])))
else:print(-2*sum(A[:(n-2)//2])-A[(n-2)//2]+A[(n-2)//2+1]+2*sum(A[(n-2)//2+2:]))

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

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

if(len(a)==2):
    print(abs(a[0]-a[1]))
elif(len(a)==3):
    print(max(a[2] - a[1] + a[2] - a[0], a[1]-a[0] + a[2]-a[0]))
else:
    
    def append2big(id1, id2):
        global bBeg
        global bEnd
        global s
        if (a[id1] - bBeg + a[id2] - bEnd < a[id2] - bBeg + a[id1] - bEnd):
            id1, id2 = id2, id1
        s+=a[id1] - bBeg + a[id2] - bEnd
        bBeg = a[id1]
        bEnd = a[id2]
    def append2small(id1, id2):
        global bBeg
        global bEnd
        global s
        if (bBeg - a[id1] + bEnd - a[id2] < bBeg - a[id2] + bEnd - a[id1]):
            id1, id2 = id2, id1
        s+=bBeg - a[id1] + bEnd - a[id2]
        bBeg = a[id1]
        bEnd = a[id2]
    def append1small(id):
        global bBeg
        global bEnd
        global s
        if (bBeg - a[id] > bEnd - a[id]):
            s+=bBeg-a[id]
            bBeg = a[id]
        else:
            s+=bEnd-a[id]
            bEnd = a[id]
    def append1big(id):
        global bBeg
        global bEnd
        global s
        if (a[id] - bBeg > a[id] - bEnd):
            s+=a[id]-bBeg
            bBeg = a[id]
        else:
            s+=a[id]-bEnd
            bEnd = a[id]
    
    s = a[N-2]-a[0] + a[N-1] - a[0]
    bBeg = a[N-2]
    bEnd = a[N-1]
    l = 0
    r = N-2
    cur = 0
    while(l<r-1):
        if(cur==0):
            if(l+2<r):
                append2small(l+1, l+2)
                l+=2
            elif(l+1<r):
                append1small(l+1)
                l+=1
            cur = 1-cur
        else:
            if(r-2>l):
                append2big(r-1, r-2)
                r-=2
            elif(r-1>l):
                append1big(r-1)
                r-=1
            cur = 1- cur
    r1 = s
    
    s = a[N - 1] - a[0] + a[N - 1] - a[1]
    bBeg = a[0]
    bEnd = a[1]
    l = 1
    r = N - 1
    cur = 1
    while (l < r - 1):
        if (cur == 0):
            if (l + 2 < r):
                append2small(l + 1, l + 2)
                l += 2
            elif (l + 1 < r):
                append1small(l + 1)
                l += 1
            cur = 1 - cur
        else:
            if (r - 2 > l):
                append2big(r - 1, r - 2)
                r -= 2
            elif (r - 1 > l):
                append1big(r - 1)
                r -= 1
            cur = 1 - cur
    r2 = s

    print(max(r1, r2))

Solution (Python):


N = int(input())
a = [int(input()) for i in range(N)]
a.sort()

sum1 = a[N-1] - a[0]
sum2 = sum1
i = 0
while True:
    if i+1 == N-i-1:
        break
    sum1 += a[N-i-1] - a[i+1]
    if i+1 == N-i-2:
        break
    sum1 += a[N-i-2] - a[i]
    i += 1

i = 0
while True:
    if i == N-i-2:
        break
    sum2 += a[N-i-2] - a[i]
    if i+1 == N-i-2:
        break
    sum2 += a[N-i-1] - a[i+1]
    i += 1

max = sum1 if sum1 >= sum2 else sum2
print(max)

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

a.sort()

c0 = [0] * n
for i in range(n-1):
  if i%2:
    c0[i] += 1
    c0[i+1] += -1
  else:
    c0[i+1] += 1
    c0[i] += -1
    
c0.sort()

s0 = sum(i*j for i, j in zip(a, c0))
s1 = sum(-i*j for i, j in zip(a, c0[::-1]))

print(max(s0, s1))

Solution (Python):
N=int(input())
A=[]
for i in range(N):
    A.append(int(input()))
A.sort()
z=N//2
if not N%2==0:
    An=sum(A[z+2:])*2 - sum(A[:z-1])*2 - A[z-1] + A[z+1] + max(-A[z-1]+A[z], -A[z] + A[z+1])
else:
    An=sum(A[z+1:])*2-sum(A[:z-1])*2-A[z-1]+A[z]
print(An)

Solution (Python):
n = int(input())
lst = [int(input()) for _ in range(n)]
lst.sort()
center = n // 2
if n % 2:
  higher = lst[center + 1:]
  lower = lst[:center]
  ans = (sum(higher) - sum(lower)) * 2 - higher[0] + lower[-1] + max(lst[center] - lower[-1], higher[0] - lst[center])
else:
  higher = lst[center:]
  lower = lst[:center]
  ans = (sum(higher) - sum(lower)) * 2 - higher[0] + lower[-1]

print(ans)

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

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

Solution (Python):
n = input()
arr = []
for i in range(n):
    arr.append(input())
arr.sort()
if n % 2 == 0:
    arr1 = arr[0:n/2]
    arr2 = arr[n/2:n]
    n1 = arr1[-1]
    n2 = arr2[0]
    arr1.remove(n1)
    arr2.remove(n2)
    print (sum(arr2)-sum(arr1)) * 2 + n2 - n1
else:
    arr1 = arr[0:n/2]
    arr2 = arr[n/2+1:n]
    n1 = arr1[-1]
    n2 = arr2[0]
    n3 = arr[n/2]
    arr1.remove(n1)
    arr2.remove(n2)
    print (sum(arr2)-sum(arr1)) * 2 + n2 - n1 + max(n2-n3,n3-n1)

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


ans=0
head=0
tail=n-1

tmp=n//2
for i in range(n-1):
    if(i%2==0):
        ans+=abs(data[tmp] - data[head])
        tmp=head
        head+=1
    else:
        ans+=abs(data[tmp] - data[tail])
        tmp=tail
        tail-=1

if(i%2==1):
    tmp_ans=ans
    ans=0
    head=0
    tail=n-1

    tmp=n//2
    for i in range(n-1):
        if(i%2==1):
            ans+=abs(data[tmp] - data[head])
            tmp=head
            head+=1
        else:
            ans+=abs(data[tmp] - data[tail])
            tmp=tail
            tail-=1
    ans=max([tmp_ans,ans])

print(ans)

Solution (Python):
all_num = int(input())
nums = []
for i in range(all_num):
    nums.append( int(input()) )
	
nums = sorted(nums)

sort_nums = []
loop_num = int(all_num/2)
for i in range( loop_num ):
    if i % 2 == 0:
        sort_nums.append(nums[i])
        sort_nums.insert(0,nums[-(i+1)])
    else:
        sort_nums.insert(0,nums[i])
        sort_nums.append(nums[-(i+1)])
    
if len( nums ) % 2 == 1:
    center_index = int(len(nums)/2)
    center_num = nums[center_index]
    if abs(center_num - sort_nums[0]) > abs(center_num - sort_nums[-1]):
        sort_nums.insert(0,center_num)
    else :
        sort_nums.append(center_num)


total = 0
for i in range( len(sort_nums)-1 ):
    total = total + abs(sort_nums[i] - sort_nums[i+1])

print(total)

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

a = [1, -1]
listA = [0 for x in range(N)]
listB = [0 for x in range(N)]
for i in range(1, N):
    listA[i] += a[i % 2]
    
    listA[i - 1] += a[~(i % 2)]

    listB[i - 1] += a[i % 2]
    listB[i] += a[~(i % 2)]

listA.sort()
listB.sort()
listA = [x * y for (x, y) in zip(listA, A)]
listB = [x * y for (x, y) in zip(listB, A)]

ans = max(sum(listA), sum(listB))

print(ans)

Solution (Python):
N = int(input())

L = []
for i in range(N):
    n = int(input())
    L.append(n)
    
L = sorted(L)
ans = []

if N % 2 == 0:
    A1 = L[0: N //2]
    A2 = L[N // 2:]
    ans.append(2 * sum(A2[1:]) + A2[0] - (2 * sum(A1[:-1]) + A1[-1]))
    
    A1 = L[0: N // 2]
    A2 = L[N // 2:]
    ans.append(2 * sum(A2[1:]) + A2[0] - (2 * sum(A1[:-1]) + A1[-1]))
    
    print(max(ans))
    
else:
    A1 = L[0: N // 2 + 1]
    A2 = L[N // 2 + 1:]
    ans.append(2 * sum(A2) - (2 * sum(A1[0: -2]) + A1[-1] + A1[-2]))
    
    A1 = L[0: N // 2]
    A2 = L[N // 2:]
    
    ans.append((2 * sum(A2[2:]) + A2[0] + A2[1]) - 2 * sum(A1))
    print(max(ans))

Solution (Python):
import sys
readline = sys.stdin.readline
def solve():
    n = int(input())
    a = sorted(int(readline()) for _ in range(n))
    c0 = n // 2 - 1
    c1 = n // 2
    c2 = n // 2 + 1
    ans = 0
    if n & 1:
        ans = sum(a[c2 + 1:]) * 2 - sum(a[:c0]) * 2 - a[c0] + a[c2] + max(-a[c0] + a[c1], - a[c1] + a[c2])
    else:
        ans = sum(a[c1 + 1:]) * 2 - sum(a[:c0]) * 2 - a[c0] + a[c1]
    return ans
print(solve())