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



Sample input:
5
6
8
1
2
3


Sample output:
21

Submission status: Runtime Error

Solution code:
import sys
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value

N = ii()
A = sorted([ii() for _ in range(N)])
'''
if N > 2:
    ans += A[-1] * 2 - A[0] - A[1]
    l_ind = 2
    r_ind = N-2
    while True:
        if r_ind - 2 >= l_ind:
'''
#if N==3:
    #print(max(A[2]+A[1]-A[0]*2, A[2]*2-A[1]-A[0]))
if N%2:
    '''
    ans_1 = A[N-1]*2 - A[0] - A[1]
    ans_1 += sum(A[N//2:N-1])*2 - sum(A[:N//2])*2 - A[N//2] - A[N//2-1]
    ans_2 = A[N-1] + A[N-2] - A[0]*2
    ans_2 += sum(A[N//2+1:])*2 - sum(A[1:N//2+1])*2 - A[N//2] - A[N//2+1]
    print(ans_1, ans_2)
    '''
    ans_1 = A[N-1]*2 - A[0] - A[1]
    tmp = A[0] + A[1]
    l_ind = 1
    r_ind = N-1
    while True:
        r_ind -= 2
        if r_ind < l_ind:
            break
        ans_1 += A[r_ind] + A[r_ind+1] - tmp
        tmp = A[r_ind] + A[r_ind+1]
        l_ind += 2
        if l_ind > r_ind:
            break
        ans_1 += tmp - (A[l_ind] + A[l_ind-1])
        tmp = A[l_ind] + A[l_ind-1]
    #print(ans_1)

    ans_2 = A[N-1] + A[N-2] - A[0]*2
    tmp = A[N-1] + A[N-2]
    l_ind = 0
    r_ind = N-2
    while True:
        l_ind += 2
        if l_ind > r_ind:
            break
        ans_2 += tmp - (A[l_ind] + A[l_ind-1])
        tmp = A[l_ind] + A[l_ind-1]
        r_ind -= 2
        if r_ind < l_ind:
            break
        ans_2 += A[r_ind] + A[r_ind+1] - tmp
        tmp = A[r_ind] + A[r_ind+1]

    print(max(ans_1, ans_2))

else:
    ans_1 = A[N-1]*2 - A[0] - A[1]
    tmp = A[0] + A[1]
    l_ind = 1
    r_ind = N-1
    while True:
        r_ind -= 2
        if r_ind == l_ind:
            ans_1 += A[r_ind+1] - A[r_ind-1]
            break
        ans_1 += A[r_ind] + A[r_ind+1] - tmp
        tmp = A[r_ind] + A[r_ind+1]
        l_ind += 2
        if l_ind == r_ind:
            ans_1 += A[r_ind+1] - A[r_ind-1]
            break
        ans_1 += tmp - (A[l_ind] + A[l_ind-1])
        tmp = A[l_ind] + A[l_ind-1]

    ans_2 = A[N-1] + A[N-2] - A[0]*2
    tmp = A[N-1] + A[N-2]
    l_ind = 0
    r_ind = N-2
    while True:
        l_ind += 2
        if l_ind == r_ind:
            ans_2 += A[r_ind+1] - A[r_ind-1]
            break
        ans_2 += tmp - (A[l_ind] + A[l_ind-1])
        tmp = A[l_ind] + A[l_ind-1]
        r_ind -= 2
        if r_ind == l_ind:
            ans_2 += A[r_ind+1] - A[r_ind-1]
            break
        ans_2 += A[r_ind] + A[r_ind+1] - tmp
        tmp = A[r_ind] + A[r_ind+1]

    print(max(ans_1, ans_2))

Submission status: Runtime Error

Solution code:
n = int(input())
lis = []
for i in range(n):
    lis.append(int(input()))

lis.sort()

mid = lis[n-1]
front, back = [], []
flag = 1
for i in range((n-1)//2):
    if flag:
        front.append(lis[i])
        back.append(lis[i + 1])
        flag = 0
    else:
        front.append(lis[n - i - 1])
        back.append(lis[n - i - 2])
        flag = 1
if (n-1) % 2 == 1:
    front.append(lis[n//2-1])
ans_M = mid - front[0] + mid - back[0]
for i in range(1, len(front)):
    ans_M += abs(front[i] - front[i-1])
for i in range(1, len(back)):
    ans_M += abs(back[i] - back[i-1])

mid = lis[0]
front, back = [], []
flag = 1
for i in range((n-1)//2):
    if flag:
        front.append(lis[n - i - 1])
        back.append(lis[n - i - 2])
        flag = 0
    else:
        front.append(lis[i])
        back.append(lis[i + 1])
        flag = 1
if (n-1) % 2 == 1:
    front.append(lis[n//2-1])
ans_m = front[0] - mid + back[0] - mid
for i in range(1, len(front)):
    ans_m += abs(front[i] - front[i-1])
for i in range(1, len(back)):
    ans_m += abs(back[i] - back[i-1])

print(max(ans_M, ans_m))

Submission status: Runtime Error

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
a = []

for i in range(n):
    a.append(ni())

a.sort()
k = n//2

if n%2 == 0:
    ans = (sum(a[t+1:])-sum(a[:t-1]))*2+a[t]-a[t-1]
    print(ans)
else:
    x = 2*(sum(a[t+1:])-sum(a[:t-1])) - a[t-1] - a[t]
    y = 2*(sum(a[t+2:])-sum(a[:t])) + a[t+1] + a[t]
    print(max(x,y))

Submission status: Runtime Error

Solution code:
from collections import deque
import copy

def solve(idx, arr, lcur, rcur, isL):
  if len(idx) == len(arr):
    return sum([abs(arr[idx[i]]-arr[idx[i-1]]) for i in range(1, len(idx))])
  else:
    fL = copy.copy(idx)
    fR = copy.copy(idx)
    if isL:
      if lcur <= rcur:
        fL.appendleft(lcur)
        fR.append(lcur)
        lcur += 1
        if lcur <= rcur:
          fL.append(lcur)
          fR.appendleft(lcur)
          lcur += 1
    else:
      if lcur <= rcur:
        fL.appendleft(rcur)
        fR.append(rcur)
        rcur -= 1
        if lcur <= rcur:
          fL.append(rcur)
          fR.appendleft(rcur)
          rcur -= 1
    return max(solve(fL, arr, lcur, rcur, not isL), solve(fR, arr, lcur, rcur, not isL))

N = int(input())
A = sorted([int(input()) for _ in range(N)])
print(solve(deque([0]), A, 1, N-1, False))

Submission status: Runtime Error

Solution code:
#Tenka1 Programmer Beginner Contest-C Align
"""
リストを並び替えて隣り合う要素の差の最大を出力
"""
import sys
from collections import deque
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n = int(readline())
lst1 = []
for i in range(n):
    lst1.append(int(readline()))

d = deque()

lst1.sort()
lst2 = lst1[:n//2] #小さい方
lst1 = lst1[n//2:] #大きい方
large = deque(lst1)
small = deque(lst2)
#2又に分かれるまで手動
res1 = large.pop()
res2 = small.popleft()
def func(x):
    global res1,res2
    if abs(res1-x) > abs(res2-x):
        res = abs(res1-x)
        res1 = x
        return res
    else:
        res = abs(res2-x)
        res2 = x
        return res

ans = res1-res2
ans += func(small.popleft())
while True:
    if not len(large):
        pass
    else:
        ans += func(large.pop())
    if not len(large):
        pass
    else:
        ans += func(large.pop())
    if not len(small):
        pass
    else:
        ans += func(small.popleft())
    if not len(small):
        pass
    else:
        ans += func(small.popleft())
    if not (len(large) or len(small)):
        print(ans)
        exit()

Submission status: Runtime Error

Solution code:
N  = int (input())
As = [int(input()) for _ in range(N)]
# print(As)
As.sort()
if N == 1:
    print(0)

#小さい方から配置
else:
    bottom_ind = 1
    upper_ind = N - 1
    
    l = r = As[0]
    
    MA = 1
    MI = 0
    mode = MA
    
    ans1 = 0
    while True:
        if mode == MA: #上から取る
            next1 = As[upper_ind]
            next2 = As[upper_ind -1]
            ans1 += max(abs(next1 - l ) + abs(next2 - r ),abs(next2 - r ) + abs(next1 - l ))
            r =  next1
            l = next2
            m = upper_ind  - 2
            upper_ind -= 2
            mode = MI
    
        else:
            next1 = As[bottom_ind]
            next2 = As[bottom_ind + 1]
            ans1 += max(abs(next1 - l) + abs(next2 - r), abs(next2 - r) + abs(next1 - l))
            m = bottom_ind + 2
            bottom_ind+= 2
            r = next1
            l = next2
            mode = MA #下から取る
        if bottom_ind >= upper_ind:
            break
    if N % 2 == 0:
        # m = As[N//2]
        m = As[m]
        ans1 += max( abs(m - l) ,abs(m - r))
    
    
    #大きい方から配置
    
    bottom_ind = 0
    upper_ind = N - 2
    
    l = r = As[N - 1]
    
    
    mode = MI
    
    ans2 = 0
    while True:
        if mode == MA: #上から取る
            next1 = As[upper_ind]
            next2 = As[upper_ind -1]
            ans2 += max(abs(next1 - l ) + abs(next2 - r ),abs(next2 - r ) + abs(next1 - l ))
            r =  next1
            l = next2
            m = upper_ind  - 2
            upper_ind -= 2
            mode = MI
    
        else:
            next1 = As[bottom_ind]
            next2 = As[bottom_ind + 1]
            ans2 += max(abs(next1 - l) + abs(next2 - r), abs(next2 - r) + abs(next1 - l))
            m = bottom_ind + 2
            bottom_ind+= 2
            r = next1
            l = next2
            mode = MA #下から取る
        if bottom_ind >= upper_ind:
            break
    if N % 2 == 0:
        # m = As[N//2]
        m = As[m]
        ans2 += max( abs(m - l) ,abs(m - r))
    
    
    
    
    
    print(max(ans1,ans2))

Submission status: Runtime Error

Solution code:
N  = int (input())
As = [int(input()) for _ in range(N)]
# print(As)
As.sort()

#小さい方から配置

bottom_ind = 1
upper_ind = N - 1

l = r = As[0]

MA = 1
MI = 0
mode = MA

ans1 = 0
while True:
    if mode == MA: #上から取る
        next1 = As[upper_ind]
        next2 = As[upper_ind -1]
        ans1 += max(abs(next1 - l ) + abs(next2 - r ),abs(next2 - r ) + abs(next1 - l ))
        r =  next1
        l = next2
        m = upper_ind  - 2
        upper_ind -= 2
        mode = MI

    else:
        next1 = As[bottom_ind]
        next2 = As[bottom_ind + 1]
        ans1 += max(abs(next1 - l) + abs(next2 - r), abs(next2 - r) + abs(next1 - l))
        m = bottom_ind + 2
        bottom_ind+= 2
        r = next1
        l = next2
        mode = MA #下から取る
    if bottom_ind >= upper_ind:
        break
if N % 2 == 0:
    # m = As[N//2]
    m = As[m]
    ans1 += max( abs(m - l) ,abs(m - r))


#大きい方から配置

bottom_ind = 0
upper_ind = N - 2

l = r = As[N - 1]


mode = MI

ans2 = 0
while True:
    if mode == MA: #上から取る
        next1 = As[upper_ind]
        next2 = As[upper_ind -1]
        ans2 += max(abs(next1 - l ) + abs(next2 - r ),abs(next2 - r ) + abs(next1 - l ))
        r =  next1
        l = next2
        m = upper_ind  - 2
        upper_ind -= 2
        mode = MI

    else:
        next1 = As[bottom_ind]
        next2 = As[bottom_ind + 1]
        ans2 += max(abs(next1 - l) + abs(next2 - r), abs(next2 - r) + abs(next1 - l))
        m = bottom_ind + 2
        bottom_ind+= 2
        r = next1
        l = next2
        mode = MA #下から取る
    if bottom_ind >= upper_ind:
        break
if N % 2 == 0:
    # m = As[N//2]
    m = As[m]
    ans2 += max( abs(m - l) ,abs(m - r))





print(max(ans1,ans2))

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
A.sort()
if N & 1:
    ans1 = 2*sum(A[N//2:])-A[N//2]-A[N//2+1]-2*sum(A[:N//2])
    ans2 = 2*sum(A[N//2+1:])-2*sum(A[:N//2+1])+A[N//2]+A[N//2-1]
else:
    ans1 = 2*sum(A[N//2:])-A[N//2]-2*sum(A[:N//2])+A[N//2-1]
    ans2 = 2*sum(A[N//2+1:])-A[N//2+1]-2*sum(A[:N//2+1])+A[N//2]
print(max(ans1, ans2))

Submission status: Runtime Error

Solution code:
from collections import deque
N = int(input())
A = [int(input()) for i in range(N)]
A.sort()
if N % 4 == 1:
    print(2*sum(A[N//2+1:])-2*sum(A[:N//2+1])+A[N//2-1]+A[N//2])
elif N % 4 == 3:
    print(2*sum(A[N//2:])-2*sum(A[:N//2])-A[N//2]-A[N//2+1])
else:
    print(2*sum(A[N//2:])-2*sum(A[:N//2])-A[N//2+1]+A[N//2])

Submission status: Runtime Error

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
A = []
for i in range(N):
    A.append(int(input()))

A.sort()


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

Submission status: Runtime Error

Solution code:
N = int(input())
Alist = sorted([int(input()) for _ in range(N)])[::-1]
ans = 0
if N == 2:
    print(abs[Alist[1] - Alist[0]])
    exit()
if N == 3:
    print(Alist[0] + Alist[1] - 2 * Alist[2])
    exit()
add = (N-1)//2
minus = N - add - 2
for i in range(N):
    A = Alist[i]
    if i < add:
        ans += 2 * A
    else:
        if i == add:
            if N % 2 == 0:
                ans += A
            else:
                ans -= A
        elif i == add + 1:
            ans -= A
        else:
            ans -= 2 * A
print(ans)

Submission status: Runtime Error

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

c = A.pop(0)
l = A.pop()
r = A.pop()
ans = (l-c) + (r-c)

cond = 0
while A:
	if cond % 4 == 0:
		tmp = A.pop(0)
		ans += l - tmp
		l = tmp
		cond += 1 
	elif cond % 4 == 1:
		tmp = A.pop(0)
		ans += r - tmp
		r = tmp
		cond += 1
	elif cond % 4 == 2:
		tmp = A.pop()
		ans += tmp - l
		l = tmp
		cond += 1
	else:
		tmp = A.pop()
		ans += tmp - r
		r = tmp
		cond += 1

print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
a.sort()
x=[a[0]]
from collections import deque
d=deque(range(1,n))
c=0
if n%2==1:
	while len(d)>=1:
		if c%2==0:
			x.insert(0,a[d.pop()])
			x.append(a[d.pop()])
			c+=1
		else:
			x.insert(0,a[d.popleft()])
			x.append(a[d.popleft()]) 
			c+=1
else:
	while len(d)>=1:
		if c%2==0:
			x.insert(0,a[d.pop()])
			if len(d)>=1:
				x.append(a[d.pop()])
			else:
				x=x
			c+=1
		else:
			x.insert(0,a[d.popleft()])
			x.append(a[d.popleft()])
			c+=1
#print(x)
cnt=0
for i in range(n-1):
  cnt+=abs(x[i]-x[i+1])
print(cnt)

Submission status: Runtime Error

Solution code:
from collections import deque
import sys
input = sys.stdin.readline


def solve(N, A, fromR=True):
    res = 0
    L, R = None, None

    while A:
        fromR = 1 - fromR

        if L is None:
            L = A.pop() if fromR else A.popleft()
            R = L
            continue

        if len(A) > 1:
            if fromR:
                x = A.pop()
                y = A.pop()
            else:
                x = A.popleft()
                y = A.popleft()
            res += abs((R + L) - (x + y))
            R, L = x, y
            continue

        if len(A) == 1:
            x = A.pop()
            res += max(abs(R - x), abs(L - x))
            continue

    return res


if __name__ == "__main__":
    N = int(input())
    A = [int(input()) for _ in range(N)]
    A = deque(sorted(A))

    ans1 = solve(N, A.copy(), 0)
    ans2 = solve(N, A, 1)
    ans = max(ans1, ans2)
    print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
n=int(input())
A=deque(sorted([int(input()) for i in range(n)]))
B=deque([A.popleft()])

flag,ans=1,0
while len(A)!=0:
    if flag==1:L,R=A.pop(),A.pop()
    else:L,R=A.popleft(),A.popleft()

    if abs(L-B[0])+abs(R-B[-1])>abs(L-B[-1])+abs(R-B[0]):
        ans +=abs(L-B[0])+abs(R-B[-1])
        B.append(R)
        B.appendleft(L)
    else:
        ans +=abs(R-B[0])+abs(L-B[-1])
        B.append(L)
        B.appendleft(R)

    if len(A)==1:
        ans +=max(abs(B[0]-A[0]),abs(B[-1]-A[0]))
        break

    flag=flag*-1

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
n=int(input())
A=deque(sorted([int(input()) for i in range(n)]))
B=deque([A.popleft()])

flag,ans=1,0
while len(A)!=0:
    if flag==1:L,R=A.pop(),A.pop()
    else:L,R=A.popleft(),A.popleft()

    if abs(L-B[0])>abs(R-B[-1]):
        ans +=abs(L-B[0])+abs(R-B[-1])
        B.append(R)
        B.appendleft(L)
    else:
        ans +=abs(R-B[0])+abs(L-B[-1])
        B.append(L)
        B.appendleft(R)

    if len(A)==1:
        ans +=max(abs(B[0]-A[0]),abs(B[-1]-A[0]))
        break

    flag=flag*-1

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
n=int(input())
A=deque(sorted([int(input()) for i in range(n)]))
B=deque([A.popleft()])

flag,ans=1,0
while len(A)!=0:
    if flag==1:L,R=A.pop(),A.pop()
    elif flag==-1:L,R=A.popleft(),A.popleft()

    if abs(L-B[0])>abs(R-B[-1]):
        ans +=abs(L-B[0])+abs(R-B[-1])
        B.append(R)
        B.appendleft(L)
    else:
        ans +=abs(R-B[0])+abs(L-B[-1])
        B.append(L)
        B.appendleft(R)

    if len(A)==1:
        ans +=max(abs(B[0]-A[0]),abs(B[-1]-A[0]))
        break

    flag=flag*-1

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    A = sorted(A)
    B = sorted(A,reverse=True)
    ans = 0
    
    ls1 = [A[0]]
    ls2 = [A[0]]

    if N%2==1:
        for i in range((N-1)//2):
            if i%2==0:
                ls1.append(B[i])
                ls2.append(B[i+1])
                ans += abs(ls1[-1]-ls1[-2])+abs(ls2[-1]-ls2[-2])
            else:
                ls1.append(A[i])
                ls2.append(A[i+1])
                ans += abs(ls1[-1]-ls1[-2])+abs(ls2[-1]-ls2[-2])
    else:
        for i in range(N//2-1):
            if i%2==0:
                ls1.append(B[i])
                ls2.append(B[i+1])
                ans += abs(ls1[-1]-ls1[-2])+abs(ls2[-1]-ls2[-2])
            else:
                ls1.append(A[i])
                ls2.append(A[i+1])
                ans += abs(ls1[-1]-ls1[-2])+abs(ls2[-1]-ls2[-2])
        ans += A[i+1]
    print(ans) 

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# coding: utf-8
N = int(input())
A = []
for _ in range(N):
    A.append(int(input()))
A.sort()
L = []
if A[N//2] < A[N//2+1]:
    flag = True
else:
    flag = False
for i in range(N//2):
    if flag:
        L.append(A[N//2-1-i])
        L.append(A[-1-i])
    else:
        L.append(A[-1-i])
        L.append(A[N//2-1-i])
ans = 0
if N % 2 == 1:
    L.append(A[N//2])
for i in range(N-1):
    ans += abs(L[i] - L[i+1])
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n = int(input())
a = [int(readline()) for _ in range(n)]

a.sort()
deq = deque(a)

ans = 0

left, right = deq.popleft(), 0
cnt = 1


def comparison(left, right, x, y):
    if y == 0:
        return max(abs(left - x), abs(right - x))

    if right == 0:
        return abs(left*2 - x - y)
    else:
        return abs(left+right-x-y)


while deq:
    if len(deq) == 1:
        x = deq.pop()
        y = 0
    else:
        if cnt:
            x = deq.pop()
            y = deq.pop()
        else:
            x = deq.popleft()
            y = deq.popleft()

    ans += comparison(left, right, x, y)
    left, right = x, y
    cnt = 1-cnt

print(ans)

Submission status: Runtime Error

Solution code:
import itertools

n=int(input())
a=list(int(input()) for i in range(n))
b=[]
a.sort()

ans=0

if n%2==1:
    b.append(a[n//2])
    for i in range(n//2):
        if a[n//2]-a[0]<=a[-1]- a[n//2]:
            b.append(a[-(i+1)])
            b.append(a[i])
        else:
            b.append(a[i])
            b.append(a[-(i+1)])
            
        
    for i in range(len(b)-1):
        ans+=abs(b[i+1]-b[i])
    print(ans)


else:
    b.append(a[n//2-1])
    b.append(a[n//2+1])
    
    if n==2:
        print(b[1]-b[0])
        exit()
    
    for i in range(n//2-2):
        b.append(a[i])
        b.append(a[-(i+1)])
    
    b.append(a[n//2-2])
    b.append(a[n//2])
    
    for i in range(len(b)-1):
        ans+=abs(b[i+1]-b[i])
    print(ans)

Submission status: Runtime Error

Solution code:
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(n[l] - small[-1], big[0] - n[l])
    print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
a = [int(input()) for i in range(N)]

a.sort()
i = 0
j = N-1
if N <= 2:
    print(abs(a[0] - a[1]))
else:
    ans = a[j] - a[0] + a[j] - a[1]
    i = 0
    j = N-2
    while i < j:
        ans += a[j] - a[i]
        i += 1
        j -= 1
    else:
        if i == j:
            if N // 2 % 2 == 1:
                ans += max(abs(a[j+2]-a[j]), abs(a[j+1]-a[j]))
            else:
                ans += max(abs(a[j]-a[j-1]), abs(a[j]-a[j-2]))
    i = 1
    j = N-1
    ans1 = a[j] - a[0] + a[j-1] - a[0]
    while i < j:
        ans1 += a[j] - a[i]
        i += 1
        j -= 1
    else:
        if i == j:
            if N // 2 % 2 == 0:
                ans1 += max(abs(a[j+2]-a[j]), abs(a[j+1]-a[j]))
            else:
                ans1 += max(abs(a[j]-a[j-1]), abs(a[j]-a[j-2]))
    print(max(ans, ans1))

Submission status: Runtime Error

Solution code:
import sys,bisect as bs,collections as cl,heapq as hq
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    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): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    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:]

bbbb = [bbbb[0]] + bbbb[2:] + [bbbb[1]]

aaa = a[:n//2+(n%2)]
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))

Submission status: Runtime Error

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
n=int(input())
a=[int(input()) for i in range(n)]
a.sort()
# b=sorted(a,reverse=True)
c=[0]*n
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(ans)
# print(ans2)

print(max(ans,ans2))

Submission status: Runtime Error

Solution code:
import collections

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

Submission status: Runtime Error

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

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)

Submission status: Runtime Error

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

ans = 0
middles = [n // 2 - 1, n // 2, n // 2 + 1]
middles.remove(0)
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)

Submission status: Runtime Error

Solution code:
import collections

n=int(input())
arr=[int(input()) for _ in range(n)]
arr=sorted(arr)
q=collections.deque()
q.append(arr[0])
l=0
r=0
flag1=0
flag2=0
if len(arr)%2==1:
  while 1:
    if flag1==0:
      r+=1
      if flag2==0:
        q.append(arr[-r])
        flag2=1
      else:
        q.appendleft(arr[-r])
        flag2=0
        flag1=1
    else:
      l+=1
      if flag2==0:
        q.append(arr[l])
        flag2=1
      else:
        q.appendleft(arr[l])
        flag2=0
        flag1=0
    if l+r+1==n:
      break
  q=list(q)
  ans=0
  for i in range(len(q)-1):
    ans+=abs(q[i]-q[i+1])
  print(ans)
else:
  while 1:
    if flag1==0:
      r+=1
      if flag2==0:
        q.append(arr[-r])
        flag2=1
      else:
        q.appendleft(arr[-r])
        flag2=0
        flag1=1
    else:
      l+=1
      if flag2==0:
        q.append(arr[l])
        flag2=1
      else:
        q.appendleft(arr[l])
        flag2=0
        flag1=0
    if l+r+2==n:
      q=list(q)
      lnum=q[0]
      rnum=q[-1]
      if flag1==0:
        r+=1
        if abs(lnum-arr[-r])>abs(rnum-arr[-r]):
          q=[arr[-r]]+q
        else:
          q=q+[arr[-r]]
      else:
        l+=1
        if abs(lnum-arr[l])>abs(rnum-arr[l]):
          q=[arr[l]]+q
        else:
          q=q+[arr[l]]
      break
  q=list(q)
  ans=0
  for i in range(len(q)-1):
    ans+=abs(q[i]-q[i+1])
  print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())

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

A.sort()
small = A[0:len(A)//2+1] if len(A)%2!=0 else A[0:len(A)//2]
small_front = small[-1]
if len(small)>1:
    small_back = small[-2]
    small = [small_front] + small[:-2] + [small_back]
large = A[len(A)//2+1:] if len(A)%2!=0 else A[len(A)//2:]
large_back = large[0]
if len(large)>1:
    large_front = large[1]
    large = [large_front] + large[2:] + [large_back]
    
#smallが前の時
ans = 0
for i in range(len(large)):
    ans += large[i] - small[i]
    if not i+1 >= len(small):
        ans += large[i] - small[i+1]

#smallとlargeの前後を交換
temp = small[0]
small[0] = small[-1]
small[1] = temp
temp = large[0]
large[0] = large[-1]
large[-1] = temp

#largeが前の時
ans2 = 0
for i in range(len(small)):
    if i >= len(large):
        break
    ans2 += large[i] - small[i]
    if not i+1 >= len(large):
        ans2 += large[i+1] - small[i]

ans = max(ans,ans2)
if len(large) == 1:
    ans3 = large[0] - small[0]
    ans3 += abs(small[0] - small[1])
    ans4 = large[0] - small[0]
    ans4 += large[0] - small[0]
    ans = max(ans3,ans4, ans)


print(ans)

Submission status: Runtime Error

Solution code:
def getInt(): return int(input())
def getIntList(): return [int(x) for x in input().split()]
def zeros(n): return [0]*n

INF = 10**18

class Debug():
    def __init__(self):
        self.debug = True
 
    def off(self):
        self.debug = False
 
    def dmp(self, x, cmt=''):
        if self.debug:
            if cmt != '':
                w = cmt + ': ' + str(x)
            else:
                w = str(x)
            print(w)
        return x


def prob():
    d = Debug()
    d.off()
    N = getInt()
    A = zeros(N)
    for i in range(N):
        A[i] = getInt()
    d.dmp((N,A), "N,A")
    A.sort()
    a = zeros(N)
    a[0] = A[N//2]
    k = 1
    if a[0]-A[0] >= A[-1]-a[0]:
        from0 = True
    else:
        from0 = False
    for i in range(N//2):
        d.dmp((k), "k")
        if from0:
            a[k] = A[i]
            if i*2+1 < N:
                a[k+1] = A[-i-1]
        else:
            a[k] = A[-i-1]
            if i*2+1 < N-1:
                a[k+1] = A[i]
        k += 2
    if N % 2 == 0:
        a[-1] = A[N//2+1]
    d.dmp((a), "a")
    dif = 0
    for i in range(N-1):
        dif += abs(a[i]-a[i+1])
    print(dif)

ans = prob()

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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