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 collections
import copy
N = int(input())
As = [int(input()) for _ in range(N)]
As.sort()
As = collections.deque(As)
small1 = collections.deque()
small2 = collections.deque()
big1 = collections.deque()
big2 = collections.deque()
A1 = copy.copy(As)
A2 = copy.copy(As)
s = A1.popleft()
b = A2.pop()
small1.append(s)
small2.append(s)
big1.append(b)
big2.append(b)
cnt1 = 0
cnt2 = 0
while A1:
    if cnt1%4 == 0:
        b = A1.pop()
        small1.append(b)
        small2.appendleft(b)
        cnt1 += 1
    elif  cnt1%4 == 1:
        b = A1.pop()
        small2.append(b)
        small1.appendleft(b)
        cnt1 += 1
    elif cnt1%4 == 2:
        s = A1.popleft()
        small1.append(s)
        small2.appendleft(s)
        cnt1 += 1
    else:
        s = A1.popleft()
        small2.append(s)
        small1.appendleft(s)
        cnt1 += 1
while A2:
    if cnt2%4 == 0:
        s = A2.popleft()
        big1.append(s)
        big2.appendleft(s)
        cnt2 += 1
    elif  cnt2%4 == 1:
        s = A2.popleft()
        big2.append(s)
        big1.appendleft(s)
        cnt2 += 1
    elif cnt2%4 == 2:
        b = A2.pop()
        big1.append(b)
        big2.appendleft(b)
        cnt2 += 1
    else:
        b = A2.pop()
        big2.append(b)
        big1.appendleft(b)
        cnt2 += 1
anss1 = 0
anss2 = 0
ansb1 = 0
ansb2 = 0
for i in range(N-1):
    anss1 += abs(small1[i+1]-small1[i])
    anss2 += abs(small2[i+1]-small2[i])
    ansb1 += abs(big1[i+1]-big1[i])
    ansb2 += abs(big2[i+1]-big2[i])
print(max(anss1, anss2, ansb1, ansb2))

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

Solution (Python):
n,*a=map(int,open(0).read().split())
a.sort()
d=len(a)//2
flag=0
if len(a)%2==1:
    b=a[:d+1]
    c=a[d+1:]
    flag=1
else:
    b=a[:d]
    c=a[d:]
l=[]
for i in range(d):
    l.append(c[i])
    l.append(b[i])
if flag:
    if abs(l[-1]-b[-1])>abs(l[0]-b[-1]):
        l.append(b[-1])
    else:
        l.insert(0,b[-1])
print(sum(abs(l[i+1]-l[i])for i in range(n-1)))

Solution (Python):
from collections import*
n,*a=map(int,open(0).read().split())
a.sort()
d=len(a)//2
flag=0
if len(a)%2==1:
    b=a[:d+1]
    c=a[d+1:]
    flag=1
else:
    b=a[:d]
    c=a[d:]
l=[]
for i in range(d):
    l.append(c[i])
    l.append(b[i])
if flag:
    if abs(l[-1]-b[-1])>abs(l[0]-b[-1]):
        l.append(b[-1])
    else:
        l.insert(0,b[-1])
print(sum(abs(l[i+1]-l[i])for i in range(n-1)))

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

A.sort(reverse=True)

def p1(l):
    if N%2 == 1:
        ans = 0
        ans += sum(l[:(N-3)//2])*2
        ans += sum(l[(N-3)//2:(N-3)//2+2])
        ans += sum(l[(N-3)//2+2:])*(-2)
        return ans
    else:
        ans = 0
        ans += sum(l[:(N-2)//2])*2
        ans += sum(l[(N-2)//2:(N-2)//2+1])
        ans += sum(l[(N-2)//2+1:(N-2)//2+2])*(-1)
        ans += sum(l[(N-2)//2+2:])*(-2)
        return ans

def p2(l):
    if N%2 == 1:
        ans = 0
        ans += sum(l[:(N-1)//2])*2
        ans += sum(l[(N-1)//2:(N-1)//2+2])*(-1)
        ans += sum(l[(N-1)//2+2:])*(-2)
        return ans
    else:
        ans = 0
        ans += sum(l[:(N-2)//2])*2
        ans += sum(l[(N-2)//2:(N-2)//2+1])
        ans += sum(l[(N-2)//2+1:(N-2)//2+2])*(-1)
        ans += sum(l[(N-2)//2+2:])*(-2)
        return ans
print(max(p1(A),p2(A)))

Solution (Python):
n = int(input())
A = sorted(int(input()) for i in range(n))
a = [0]*(n)
b = [0]*(n)
c = 1

for i in range(n-1):
    a[i] += -c
    a[i+1] += c
    b[i] += c
    b[i+1] += -c
    c *= -1
a.sort()
b.sort()
sum1,sum2 = 0,0
for i in range(n):
    sum1 += a[i] *A[i]
    sum2 += b[i] * A[i]
print(max(sum1,sum2))

Solution (Python):
from collections import deque


def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    A.sort()
    ans = 0
    B = deque(A)
    edge = [B.popleft()] * 2
    while len(B):
        a = abs(edge[0] - B[0])
        b = abs(edge[0] - B[len(B) - 1])
        c = abs(edge[1] - B[0])
        d = abs(edge[1] - B[len(B) - 1])
        if max(a, b, c, d) == a:
            edge[0] = B.popleft()
            ans += a
        elif max(a, b, c, d) == b:
            edge[0] = B.pop()
            ans += b
        elif max(a, b, c, d) == c:
            edge[1] = B.popleft()
            ans += c
        else:
            edge[1] = B.pop()
            ans += d
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
a=[]
n=int(input())
for i in range(n):a.append(int(input()))
a.sort()
if len(a)%2==0:
    b=[a.pop(len(a)//2-1)]
    while len(a)!=1:
        b.append(a.pop(-1))
        b.append(a.pop(0))
    b.append(a.pop(-1))
else:
    b=[a.pop(len(a)//2)]
    if abs(b[0]-a[len(a)//2])>abs(b[0]-a[len(a)//2-1]):
        while len(a)!=0:
            b.append(a.pop(-1))
            b.append(a.pop(0))
    else:
        while len(a)!=0:
            b.append(a.pop(0))
            b.append(a.pop(-1))
ans=0
for i in range(n-1):ans+=abs(b[i]-b[i+1])
print(ans)

Solution (Python):
from itertools import chain

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

A.sort()

m = N // 2
L, R = A[:m], A[m:]

def sumOfDiff(A):
    """隣の要素との差の合計"""
    return sum(abs(a - b) for a, b in zip(A, A[1:]))

def merge(L, R):
    """[L[0], R[0], L[1], R[1], ...]"""
    return list(chain.from_iterable(zip(L, R)))

if N % 2 == 0:
    
    s = sumOfDiff(merge(R, L))
else:
    L.reverse()
    B1 = merge(L, R[1:]) + [R[0]]  
    B2 = merge(R[1:], L) + [R[0]]  
    s = max(sumOfDiff(B1), sumOfDiff(B2))

print(s)

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

p1 = []
p2 = []
for i in range(n):
    x = 2
    if i == 0 or i == n-1:
        x = 1
    if i % 2 == 0:
        p1.append(x)
        p2.append(-x)
    else:
        p1.append(-x)
        p2.append(x)
p1.sort()
p2.sort()


ans1 = 0
ans2 = 0
for i in range(n):
    ans1 += l[i] * p1[i]
    ans2 += l[i] * p2[i]
    

print(max(ans1, ans2))

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

sort_values = []
med = int(N/2) if N % 2 == 1 else int(N/2)-1
prev = values[med]
A = values[:med]
B = list(reversed(values[med+1:]))
BAsum = 0
for i in range(med+1):
  if i >= len(B):
    break
  now = B[i]
  BAsum += abs(now-prev)
  prev = now
  if i >= len(A):
    break
  now = A[i]
  BAsum += abs(now-prev)
  prev = now

med = int(N/2)
prev = values[med]
A = values[:med]
B = list(reversed(values[med+1:]))
ABsum = 0
for i in range(med+1):
  if i >= len(A):
    break
  now = A[i]
  ABsum += abs(now-prev)
  prev = now
  if i >= len(B):
    break
  now = B[i]
  ABsum += abs(now-prev)
  prev = now  


print(max([BAsum, ABsum]))

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

A.sort()
if N % 2 == 0:
    print(-2*sum(A[:N//2-1]) - A[N//2-1] + A[N//2] + 2*sum(A[N//2+1:]))
else:
    m = max(-2*A[N//2-1] + A[N//2] + A[N//2+1], - A[N//2-1] - A[N//2] + 2*A[N//2+1])
    print(-2*sum(A[:N//2-1]) + m + 2*sum(A[N//2+2:]))

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

A.sort()
if N % 2 == 0:
    print(-2*sum(A[:N//2-1]) - A[N//2-1] + A[N//2] + 2*sum(A[N//2+1:]))
else:
    print(-2*sum(A[:N//2-1])
          + max(-2*A[N//2-1] + A[N//2] + A[N//2+1], - A[N//2-1] - A[N//2] + 2*A[N//2+1])
          + 2*sum(A[N//2+2:]))

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
a.sort()
p1 = []
p2 = []
for i in range(n):
    x = 2
    if i == 0 or i == n-1:
        x = 1
    if i % 2 == 0:
        p1.append(x)
        p2.append(-x)
    else:
        p1.append(-x)
        p2.append(x)
p1.sort()
p2.sort()

ans1 = 0
ans2 = 0
for i in range(n):
    ans1 += a[i]*p1[i]
    ans2 += a[i]*p2[i]

print(max(ans1, ans2))

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

A.sort()

if N % 2 == 1:
    w = -2*sum(A[:N//2]) +A[N//2] +A[N//2 + 1] + 2*sum(A[N//2 + 2:])
    m = -2*sum(A[:N//2-1]) - A[N//2 - 1] - A[N//2] + 2*sum(A[N//2+1:])
    print(max(w, m))
else:
    print(-2*sum(A[:N//2-1]) - A[N//2-1] + A[N//2] + 2*sum(A[N//2+1:]))

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

A.sort()
temp = 0

if N % 2 == 1:
    w = -2*sum(A[:N//2]) +A[N//2] +A[N//2 + 1] + 2*sum(A[N//2 + 2:])
    m = -2*sum(A[:N//2-1]) - A[N//2 - 1] - A[N//2] + 2*sum(A[N//2+1:])
    print(max(w, m))
else:
    print(-2*sum(A[:N//2-1]) - A[N//2-1] + A[N//2] + 2*sum(A[N//2+1:]))

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
a.sort()
ress = []
for i in range(2):
    if i == 0:
        res = 0
        b = a[n//2:]
        c = a[:n//2]
        res += 2*sum(b)
        res -= 2*sum(c)
        res -= b[0]
        if n%2 == 1:
            res -= b[1]
        else:
            res += c[-1]
        ress.append(res)
    if i==1 and n%2==1:
        res = 0
        b = a[n//2+1:]
        c = a[:n//2+1]
        res += 2*sum(b)
        res -= 2*sum(c)
        res += c[-1]+c[-2]
        ress.append(res)
print(max(ress))

Solution (Python):


def main():
    num = int(input())
    data_list = [int(input()) for i in range(num)]
    data_list.sort()


    if num % 2 == 1:
        hikukazu = 0
        for i in range(num // 2 + 1):
            hikukazu += 2 * data_list[i]
        hikukazu -= data_list[num // 2] + data_list[num // 2 - 1]

        tasukazu = 0
        for i in range(num // 2 + 1, num):
            tasukazu += 2 * data_list[i]
    else:
        hikukazu = 0
        for i in range(num // 2):
            hikukazu += 2 * data_list[i]
        hikukazu -= data_list[num // 2 - 1]

        tasukazu = 0
        for i in range(num // 2, num):
            tasukazu += 2 * data_list[i]
        tasukazu -= data_list[num // 2]

    a = tasukazu - hikukazu

    if num % 2 == 1:
        hikukazu = 0
        for i in range(num // 2):
            hikukazu += 2 * data_list[i]

        tasukazu = 0
        for i in range(num // 2, num):
            tasukazu += 2 * data_list[i]
        tasukazu -= data_list[num // 2] + data_list[num // 2 + 1]
    else:
        hikukazu = 0
        for i in range(num // 2):
            hikukazu += 2 * data_list[i]
        hikukazu -= data_list[num // 2 - 1]

        tasukazu = 0
        for i in range(num // 2, num):
            tasukazu += 2 * data_list[i]
        tasukazu -= data_list[num // 2]

    b = tasukazu - hikukazu

    
    print(max(a, b))


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
alist = []
for i in range(n):
    alist.append(int(input()))
alist.sort()
lar = alist[n//2:]
sma = alist[:n//2]
if n%2 == 0:
    ans = lar[0]-sma[-1]
    ans += 2*(sum(lar[1:])-sum(sma[:-1]))
    print(ans)
else:
    ans1 = lar[0]+lar[1]
    ans1 += 2*(sum(lar[2:])-sum(sma))
    ans2 = -sma[-1]-lar[0]
    ans2 += 2*(sum(lar[1:])-sum(sma[:-1]))
    print(max(ans1,ans2))

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

A.sort()

ans = A[len(A) - 1] - A[0]
small = 0
large = len(A) - 1
while (True):

    if large - small == 1:
        break
    elif large - small == 2:
        ans += max(A[large] - A[large - 1], A[small + 1] - A[small])
        break
    ans += A[large - 1] - A[small] + A[large] - A[small + 1]
    small += 1
    large -= 1

print(ans)

Solution (Python):
n = int(input())
a = []
for i in range(n):
    a += [int(input())]
a.sort()
n2 = n // 2

if n % 2:
    ans1 = (sum(a[n2 + 1:]) - sum(a[:n2 + 1])) * 2 + a[n2] + a[n2 - 1]
    ans2 = (sum(a[n2:]) - sum(a[:n2])) * 2 - a[n2] - a[n2 + 1]
    ans = max(ans1, ans2)
else:
    ans = (sum(a[n2:]) - sum(a[:n2])) * 2 - a[n2] + a[n2 - 1]

print(ans)

Solution (Python):
import bisect
from collections import deque
from collections import defaultdict
from fractions import gcd

from math import sqrt
from math import ceil
from itertools import permutations
from heapq import *

N=int(input())
A=[]
B=[]
for _ in range(N):
    a=int(input())
    A.append(a)
    B.append(a)
A.sort()
B.sort()
A=deque(A)
B=deque(B)
L=deque()
L.append(A.popleft())
num=0
while(A):
    if num==0:
        L.appendleft(A.pop())
        num+=1
    elif num==1:
        L.append(A.pop())
        num+=1
    elif num==2:
        L.appendleft(A.popleft())
        num+=1
    elif num==3:
        L.append(A.popleft())
        num=0
sum1=0
for i in range(N-1):
    sum1+=abs(L[i]-L[i+1])
R=deque()
R.append(B.pop())
num=2
while(B):
    if num==0:
        R.appendleft(B.pop())
        num+=1
    elif num==1:
        R.append(B.pop())
        num+=1
    elif num==2:
        R.appendleft(B.popleft())
        num+=1
    elif num==3:
        R.append(B.popleft())
        num=0
sum2=0
for i in range(N-1):
    sum2+=abs(R[i]-R[i+1])

print(max(sum1,sum2))

Solution (Python):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime,random
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())

N = int(input())
aa = [int(input()) for _ in range(N)]
aa.sort()

if N%2 == 0:
    ans = (sum(aa[N//2:]) - sum(aa[:N//2]))*2 - (aa[N//2]-aa[N//2-1])
    print(ans)
else:
    ans1 = (sum(aa[N//2:]) - sum(aa[:N//2]))*2 - (aa[N//2]+aa[N//2+1])
    ans2 = (sum(aa[(N//2+1):]) - sum(aa[:(N//2+1)]))*2 + (aa[N//2]+aa[N//2-1])
    print(max(ans1,ans2))

Solution (Python):
a = int(input())
k =[int(input()) for i in range(a)]
k.sort()
if a%2!=0:
  s1= 2*sum(k[a//2:]) - 2*sum(k[:a//2]) -k[a//2]-k[a//2+1]
  s2= 2*sum(k[a//2+1:]) - 2*sum(k[:a//2+1]) +k[a//2]+k[a//2-1]
  s = max(s1,s2)
else:
  s= 2*sum(k[a//2:]) - 2*sum(k[:a//2]) -k[a//2]+k[a//2-1]
print(s)

Solution (Python):
def main():
    N = int(input())
    A = [int(input()) for i in range(N)]
    A.sort()
    BIG, SMALL = -1, 0
    big_index = N-1
    small_index = 0

    left = A[big_index]
    left_sign = BIG
    right = A[big_index]
    right_sign = BIG
    big_index-=1
    result = 0
    for i in range(1, N):
        val1, val2 = 0, 0
        s = A[small_index]
        b = A[big_index]
        against1, against2 = 0, 0
        if left_sign == BIG:
            val1 = abs(left - s)
            against1 = SMALL
        else:
            val1 = abs(left - b)
            against1 = BIG
        if right_sign == BIG:
            val2 = abs(right - s)
            against2 = SMALL
        else:
            val2 = abs(right - b)
            against2 = BIG
        if val1 > val2:
            result += val1
            if against1 == BIG:
                left = b
                left_sign = against1
                
                big_index -= 1
            else:
                left = s
                left_sign = against1
                
                small_index += 1
        else:
            result += val2
            if against2 == BIG:
                right = b
                right_sign = against2
                
                big_index -= 1
            else:
                right = s
                right_sign = against2
                
                small_index += 1
    print(result)



if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
L = []
S = 0
K = 0
for i in range(N):
  L.append(int(input()))
L.sort()
if N == 2:
  print(abs(L[0]-L[1]))
elif N == 3:
  print(max(abs(L[2]+L[1]-2*L[0]),abs(2*L[2]-L[1]-L[0])))
elif N == 4:
  print(abs(2*L[3]+L[2]-L[1]-2*L[0]))
elif N%2 == 0:
  for i in range((N-2)//2):
    S += 2*(L[N-i-1]-L[i])
  S += L[N//2]-L[(N-2)//2]
  print(S)
else:
  for i in range((N-3)//2):
    S += 2*L[N-i-1]
  for j in range(N//2):
    S -= 2*L[j]
  S += L[N//2]+L[N//2+1]
  for k in range(N//2):
    K += L[N-k-1]*2
  for m in range((N-3)//2):
    K -= 2*L[m]
  K -= (L[N//2]+L[N//2-1])
  print(max(S,K))

Solution (Python):
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [long(input_raw[0])]
    A = []
    for i in range(N):
        input_raw = read_func().strip().split()
        A.append(long(input_raw[0]))

    A.sort()

    B = deque([])
    is_back = False
    is_append_left = True
    is_append_left_back = False

    for i in range(N):



            if is_back == False:
                x = A[i/2]
                if is_append_left == True:
                    B.appendleft(x)
                    is_append_left = False
                else:
                    B.append(x)
                    is_append_left = True

            else:
                x = A[len(A) - 1 - i/2]
                if is_append_left_back == True:
                    B.appendleft(x)
                    is_append_left_back = False
                else:
                    B.append(x)
                    is_append_left_back = True
















            if is_back == False:
                is_back = True
            else:
                is_back = False
    score1 = 0L
    for i in range(N -1):
        score1 += abs(B[i] - B[i + 1])


    A.sort(reverse = True)

    B = deque([])
    is_back = False
    is_append_left = True
    is_append_left_back = False
    for i in range(N):



            if is_back == False:
                x = A[i/2]
                if is_append_left == True:
                    B.appendleft(x)
                    is_append_left = False
                else:
                    B.append(x)
                    is_append_left = True

            else:
                x = A[len(A) - 1 - i/2]
                if is_append_left_back == True:
                    B.appendleft(x)
                    is_append_left_back = False
                else:
                    B.append(x)
                    is_append_left_back = True
















            if is_back == False:
                is_back = True
            else:
                is_back = False
    score2 = 0L
    for i in range(N -1):
        score2 += abs(B[i] - B[i + 1])

    print max(score1, score2)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [long(input_raw[0])]
    A = []
    for i in range(N):
        input_raw = read_func().strip().split()
        A.append(long(input_raw[0]))

    A.sort()

    B = deque([])
    is_back = False
    is_append_left = True
    is_append_left_back = False

    for i in range(N):



            if is_back == False:
                x = A[i/2]
                if is_append_left == True:
                    B.appendleft(x)
                    is_append_left = False
                else:
                    B.append(x)
                    is_append_left = True

            else:
                x = A[len(A) - 1 - i/2]
                if is_append_left_back == True:
                    B.appendleft(x)
                    is_append_left_back = False
                else:
                    B.append(x)
                    is_append_left_back = True
















            if is_back == False:
                is_back = True
            else:
                is_back = False
    score1 = 0L
    for i in range(N -1):
        score1 += abs(B[i] - B[i + 1])


    A.sort(reverse = True)

    B = deque([])
    is_back = False
    is_append_left = True
    is_append_left_back = False
    for i in range(N):



            if is_back == False:
                x = A[i/2]
                if is_append_left == True:
                    B.appendleft(x)
                    is_append_left = False
                else:
                    B.append(x)
                    is_append_left = True

            else:
                x = A[len(A) - 1 - i/2]
                if is_append_left_back == True:
                    B.appendleft(x)
                    is_append_left_back = False
                else:
                    B.append(x)
                    is_append_left_back = True
















            if is_back == False:
                is_back = True
            else:
                is_back = False
    score2 = 0L
    for i in range(N -1):
        score2 += abs(B[i] - B[i + 1])

    print max(score1, score2)


if __name__ == '__main__':
    main()

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

A.sort(reverse=True)
if len(A)%2==0:
  print(A[len(A)//2-1]-A[len(A)//2]+2*sum(A[:len(A)//2-1])-2*sum(A[len(A)//2+1:]))
else:
  tmp1=-A[len(A)//2]-A[len(A)//2+1]+2*sum(A[:len(A)//2])-2*sum(A[len(A)//2+2:])
  tmp2=A[len(A)//2-1]+A[len(A)//2]+2*sum(A[:len(A)//2-1])-2*sum(A[len(A)//2+1:])
  print(tmp1 if tmp1>tmp2 else tmp2)

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

a_list.sort(reverse=True)

half = N // 2

ans1 = 0
ans2 = 0
for i in range(half):
    if i == half - 1:
        ans1 += a_list[i]
        ans1 -= a_list[-(i + 1)]
    else:
        ans1 += 2 * a_list[i]
        ans1 -= 2 * a_list[-(i + 1)]
ans2 = ans1
if N % 2 != 0:
    ans1 += a_list[half - 1]
    ans1 -= a_list[- (half + 1)]
    ans2 += a_list[half]
    ans2 -= a_list[- half]

print(max(ans1, ans2))

Solution (Python):


import sys
from collections import Counter, deque

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()


N = I()
A = [I() for _ in range(N)]

A.sort()
B = deque()

state = 1
for i in range(N // 2):
    if state:
        B.append(A[i])
        B.appendleft(A[-(i + 1)])
    else:
        B.append(A[-(i + 1)])
        B.appendleft(A[i])
    state ^= 1
if N % 2:
    median = A[N // 2]
    if abs(B[0] - median) < abs(B[-1] - median):
        B.append(median)
    else:
        B.appendleft(median)

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

Solution (Python):


import sys
from collections import Counter, deque

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()


N = I()
A = [I() for _ in range(N)]

A.sort()
B = deque()

state = 1
for i in range(N // 2):
    if state:
        B.append(A[i])
        B.appendleft(A[-(i + 1)])
    else:
        B.append(A[-(i + 1)])
        B.appendleft(A[i])
    state ^= 1
if N % 2:
    C = deque(B)
    B.append(A[N // 2])
    C.appendleft(A[N // 2])
else:
    C = A[:]

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

print(max(ans1, ans2))

Solution (Python):
from itertools import accumulate


class CoeffTable:
    def __init__(self, n):
        self.n = n
        if n == 2:
            (self.p2, self.p1, self.m1, self.m2) = (0, 1, 1, 0)
        if n % 2 == 0:
            self.p1 = self.m1 = 1
            self.p2 = self.m2 = (n - 1) // 2
        else:
            self.p1 = 0
            self.m1 = 2
            self.p2 = (n - 1) // 2
            self.m2 = (n - 2) // 2
    def reverse(self):
        (self.p2, self.p1, self.m1, self.m2) = (
            self.m2,
            self.m1,
            self.p1,
            self.p2,
        )

    def cumsum(self):
        return list(accumulate([self.p2, self.p1, self.m1, self.m2]))



def main(n, seq):
    seq = sorted(seq, reverse=True)
    coeff = CoeffTable(n)
    cm = coeff.cumsum()
    ans1 = (
        2 * sum(seq[0 : cm[0]])
        + 1 * sum(seq[cm[0] : cm[1]])
        - 1 * sum(seq[cm[1] : cm[2]])
        - 2 * sum(seq[cm[2] : cm[3]])
    )
    coeff.reverse()
    rcm = coeff.cumsum()
    ans2 = (
        2 * sum(seq[0 : rcm[0]])
        + 1 * sum(seq[rcm[0] : rcm[1]])
        - 1 * sum(seq[rcm[1] : rcm[2]])
        - 2 * sum(seq[rcm[2] : rcm[3]])
    )
    ans = max(ans1, ans2)
    print(ans)
    return


if __name__ == "__main__":
    n = int(input())
    sample = []
    for _ in range(n):
        sample.append(int(input()))
    main(n, sample)

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

A = sorted(A)

P1 = [0 for _ in range(N)]
for i in range(N):
    if i == 0:
        P1[0] = 1
    elif i%2 == 1:
        if i == N-1:
            P1[i] = -1
        else:
            P1[i] = -2
    else:
        if i == N - 1:
            P1[i] = 1
        else:
            P1[i] = 2

P1 = sorted(P1)

P2 = [0 for _ in range(N)]
for i in range(N):
    if i == 0:
        P2[0] = -1
    elif i%2 == 1:
        if i == N-1:
            P2[i] = 1
        else:
            P2[i] = 2
    else:
        if i == N - 1:
            P2[i] = -1
        else:
            P2[i] = -2

P2 = sorted(P2)

ans1 = 0
ans2 = 0
for i in range(N):
    ans1 += P1[i]*A[i]
    ans2 += P2[i]*A[i]

ans = max(ans1,ans2)

print(ans)

Solution (Python):
from collections import deque
n = int(input())

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

l = 0
r = len(A)-1

A = sorted(A)

for _ in range(n):
    if len(A_) == 0:
        A_.append(A[r])
        r-=1
        continue

    todo = "ll"
    bestVal = abs(A[l]-A_[0])
    if(abs(A[l]-A_[-1]) > bestVal):
        bestVal = abs(A[l]-A_[-1])
        todo = "lr"
    if(abs(A[r]-A_[-1]) > bestVal):
        bestVal = abs(A[r]-A_[-1])
        todo = "rr"
    if(abs(A[r]-A_[0]) > bestVal):
        bestVal = abs(A[r]-A_[0])
        todo = "rl"

    if todo == "ll":
        A_.appendleft(A[l])
        l+=1
    if todo == "lr":
        A_.append(A[l])
        l+=1
    if todo == "rr":
        A_.append(A[r])
        r-=1
    if todo == "rl":
        A_.appendleft(A[r])
        r-=1
ans = 0
for i in range(len(A_)-1):
    ans += abs(A_[i+1]-A_[i])
print(ans)

Solution (Python):
N = int(input())
As = []
for i in range(N):
    As.append(int(input()))
As.sort()
if N%2 == 0:
    s = 0
    for i in range(N):
        if i < N/2:
            s -= 2*As[i]
        else:
            s += 2*As[i]
    s = s + As[int(N/2)-1] -As[int(N/2)]
    print(s)
else:
    s1, s2 = 0, 0
    for i in range(N):
        if i < N//2 -1:
            s1 -= 2*As[i]
        elif N//2-1 <= i <=N//2 :
            s1 -= As[i]
        else:
            s1 +=  2*As[i]
        if i < N//2:
            s2 -= 2*As[i]
        elif N//2 <= i <= N//2 +1:
            s2 += As[i]
        else:
            s2 += 2*As[i]
    print(max(s1, s2))

Solution (Python):

N=input()
N=int(N)

As=[]
for i in range(N):
	As.append(int(input()))


As.sort()

l=[]
l.append(As[N//2])

s=0
b=0
for i in range(N-1):
	if(i%2==1):
		l.append(As[-b-1])
		b+=1
	else:
		l.append(As[s])
		s+=1
ans=0
for i in range(1,N):
	ans+=abs(l[i]-l[i-1])



l2=[]
l2.append(As[(N-1)//2])

s2=0
b2=0
for i in range(N-1):
	if(i%2==0):
		l2.append(As[-b2-1])
		b2+=1
	else:
		l2.append(As[s2])
		s2+=1
ans2=0
for i in range(1,N):
	ans2+=abs(l2[i]-l2[i-1])


print(max(ans,ans2))

Solution (Python):
n = int(input())
a = sorted(int(input()) for _ in range(n))
if n == 2:
    print(abs(a[0] - a[1]))
elif n%2 == 1:
    r1 = a[n//2] + a[n//2+1] + sum(a[n//2+2:])*2 - sum(a[:n//2])*2
    r2 = sum(a[n//2+1:])*2 - sum(a[:n//2-1])*2 - a[n//2-1] - a[n//2]
    print(max(r1, r2))
else:
    r1 = sum(a[n//2+1:])*2 + a[n//2] - sum(a[:n//2-1])*2 - a[n//2-1]
    print(r1)

Solution (Python):
from collections import *
a = sorted(int(input()) for _ in range(int(input())))
q = deque(a)
rq = deque([q.popleft()])
while q:
    rq.appendleft(q.pop())
    if q: 
        rq.append(q.pop())
    if q:
        rq.appendleft(q.popleft())
    if q: 
        rq.append(q.popleft())
r1 = sum(abs(i-j) for i, j in zip(rq, list(rq)[1:]))

q = deque(a[::-1])
rq = deque([q.popleft()])
while q:
    rq.appendleft(q.pop())
    if q: 
        rq.append(q.pop())
    if q:
        rq.appendleft(q.popleft())
    if q: 
        rq.append(q.popleft())
r2 = sum(abs(i-j) for i, j in zip(rq, list(rq)[1:]))
print(max(r1, r2))

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

p,q = a[0],a[-1]
res = b(p-q)
for i in range(n//2-1):
    
    res += b(p-a[n-2-i])
    res += b(q-a[i+1])
    p,q = a[i+1],a[n-2-i]
if n%2==1:
    c = a[n//2]
    res += max(b(p-c),b(q-c))
print(res)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**13
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n = I()
    a = sorted([I() for _ in range(n)])
    if n % 2 == 0:
        k = n // 2
        b = a[:k-1]
        c = a[k+1:]
        t = [0] * n
        t[0] = a[k]
        t[-1] = a[k-1]
        for i in range(k-1):
            t[i*2+1] = b[i]
            t[i*2+2] = c[i]
        r = 0
        for i in range(n-1):
            r += abs(t[i]-t[i+1])

        return r

    k = n // 2 + 1
    b = a[:k-1]
    c = a[k+1:]
    t = [0] * n
    t[0] = a[k]
    t[-1] = a[k-1]
    t[-2] = b[-1]
    for i in range(k-2):
        t[i*2+1] = b[i]
        t[i*2+2] = c[i]
    r = 0
    for i in range(n-1):
        r += abs(t[i]-t[i+1])

    b = a[:k-2]
    c = a[k:]
    t = [0] * n
    t[0] = a[k-1]
    t[-1] = a[k-2]
    t[-2] = c[-1]
    for i in range(k-2):
        t[i*2+1] = c[i]
        t[i*2+2] = b[i]
    tr = 0
    for i in range(n-1):
        tr += abs(t[i]-t[i+1])

    if r < tr:
        r = tr

    return r


print(main())