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:
n=int(input())

a=[]

for i in range(n):
    b=int(input())
    a.append(b)
    
a.sort()

ans=["" for i in range(n)]

ans[n//2]=a[-1]
a.remove(a[-1])
l=(n//2)-1
r=(n//2)+1

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

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


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

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

Submission status: Runtime Error

Solution code:
from collections import deque
n = int(input())
nums =[]

for i in range(n):
  nums.append(int(input()))

nums.sort()
nums = deque(nums)
a = [nums.pop()]
a = deque(a)

while not len(nums) == 0:
  try:
    a.appendleft(nums.popleft())
    a.append(nums.popleft())
    a.appendleft(nums.pop())
    a.append(nums.pop())
  except IndexError:
    pass

tmp_1 = abs(a[-1] - a[-2])
tmp_2 = abs(a[-1] - a[0])

if tmp_1 < tmp_2:
  a.appendleft(0,a.pop())

sub = 0
for i in range(1, len(a)):
  sub += abs(a[i-1]-a[i])
print(sub)

Submission status: Runtime Error

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


def distance(p, Array):
    m = 0
    index = 0
    k = 0
    ai = 0
    for i in Array:
        k = abs(p-i)
        if m < k:
            m = k
            ai = i
    return Array.index(ai)


S = 0
for f in range(N):
    B = A[:]
    bi = B.pop(f)
    s = 0 
    #print(bi, "----")
    while B:
        j = distance(bi, B)

        # b[j] が bi から一番遠い
        bj = B.pop(j)
        s += abs(bi - bj)
     #   print(s, bi, bj)

        bi = bj
        
    
    if S < s:
        S = s

print(S)

Submission status: Runtime Error

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

s = int((N - 1) / 2)
m = A[0:s+1]
M = A[s+1:][::-1]

ans = [0 for _ in range(N)]

if (N - 7) % 4 == 0:
    flag = True

if (flag):
    li = s - 1
    ri = s + 1
else:
    li = s
    ri = s + 1

for i, (a, b) in enumerate(zip(m, M)):
    if i % 2 == 1:
        a, b = b, a
    ans[li] = a
    ans[ri] = b
    li -= 1
    ri += 1

if N % 2 == 1:
    if (flag):
        ans[-1] = m[-1]
    else:
        ans[0] = m[-1]

S = 0
for i, j in zip(ans[1:], ans[:-1]):
    S += abs(i - j)

print(S)

Submission status: Runtime Error

Solution code:
from collections import deque
n = int(input())
nums =[]

for i in range(n):
  nums.append(int(input()))

nums.sort()
nums = deque(nums)
a = [nums.pop()]
a = deque(a)

while not len(nums) == 0:
  try:
    a.appendleft(nums.popleft())
    a.append(nums.popleft())
    a.appendleft(nums.pop())
    a.append(nums.pop())
  except IndexError:
    pass

tmp_1 = abs(a[-1] - a[-2])
tmp_2 = abs(a[-1] - a[0])

if tmp_1 < tmp_2:
  a.insert(0,a.pop())

sub = 0
for i in range(1, len(a)):
  sub += abs(a[i-1]-a[i])
print(sub)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [0]*n

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

ans = 0
x = a[len(a)//2]
y = a[-1]
z = n-1 if len(a)%2 else n
if x == a[len(a)//2+1]:
    y = a[0]
    for i in range(z):
        ans += abs(x-y)
        if i%2:
            x = a[-(i//2+2)]
        else:
            y = a[i//2]

else:
    for i in range(z):
        ans += abs(x-y)
        if i%2:
            y = a[-(i//2+2)]
        else:
            x = a[i//2]

print(ans)

Submission status: Runtime Error

Solution code:
import  math
def getN():
    return int(input())


def getNM():
    return map(int, input().split())


def getList():
    return list(map(int, input().split()))

n = getN()
nums = []


for i in range(n):
  nums.append(getN())

half = int(math.floor(n/2))
nums.sort()
nums1 = nums[:half]
nums2 = nums[half:]
nums2[1], nums2[-1] = nums2[-1], nums2[1]

tmp = nums2[0]
diff = 0
diff2 = 0
for i in range(half):
    num = nums2[i]
    diff += abs(tmp-num)
    tmp = num

    num = nums1[i]
    diff += abs(tmp - num)
    tmp = num

if n%2 == 1:
    diff += abs(tmp-nums2[-1])

if n%2 == 1:
    nums1 = nums[:half+1]
    nums2 = nums[half+1:]
    nums1[0], nums1[-2] = nums1[-2], nums1[0]

    tmp = nums1[0]
    for i in range(half):
        num = nums1[i]
        diff2 += abs(tmp - num)
        tmp = num

        num = nums2[i]
        diff2 += abs(tmp - num)
        tmp = num

    diff2 += abs(tmp-nums1[-1])

#print(nums1)
#print(nums2)
print(max(diff, diff2))
"""
3 4 5 9
1 1 3
"""

Submission status: Runtime Error

Solution code:
N = int(input())
import sys
l = sys.stdin.readlines()
list = []
ans = []
for i in range(N):
    list.append(int(l[i]))
    ans.append(0)
print(list)

for i in range(len(ans)):
    if i == 0:
        ans[int(len(ans)/2)] = min(list)
        list.pop(list.index(min(list)))
        print(ans)
    elif i == 1:
        ans[int(len(ans))-1] = min(list)
        list.pop(list.index(min(list)))
        print(ans)
    elif i == 2:
        ans[0] = min(list)
        list.pop(list.index(min(list)))
        print(ans)

for i in range(len(ans)):
    if ans[i] == 0:
        ans[i] = max(list)
        list.pop(list.index(max(list)))
        print(ans)

total = 0
for j in range(len(ans)-1):
    total += abs(ans[j] - ans[j+1])
print(total)

Submission status: Runtime Error

Solution code:
from collections import deque
N = input()
A = []


res = []
begin = 0
end = len(A) - 1
res = deque()
res.append(A[begin])
res.append(A[end])
begin += 1
end -= 1

Submission status: Runtime Error

Solution code:
def cul(a1,a2,b1,b2):
    #print(a1,a2,b1,b2)
    global ans
    #print(abs(a1-b1),abs(a2-b2), abs(a1-b2),abs(a2-b1))
    plus=max(abs(a1-b1)+abs(a2-b2), abs(a1-b2)+abs(a2-b1))
    #print(plus)
    ans+=plus

n=int(input())

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

a.sort()
#print(a)

ans=0
ansli=[]
fp1=1
fp2=2
lp1=len(a)-1
lp2=len(a)-2
cul(a[0], a[0], a[lp1], a[lp2])

while 1:
    cul(a[fp1], a[fp2], a[lp1], a[lp2])
    lp2-=2
    lp1-=2
    if fp2>=lp2:
        #print("point", fp1,fp2,lp2,lp1)
        if fp1>=lp2:
            print(ans)
            break
        else:
            plus=max(abs(a[fp1]-a[lp1]), abs(a[fp1]-a[lp2]) )
            #print("1", plus)
            #print(a[fp1], a[lp1], a[lp2])
            ans+=plus
            print(ans)
            break
    cul(a[fp1], a[fp2], a[lp1], a[lp2])
    fp1+=2
    fp2+=2
    if fp2>=lp2:
        if fp1>=lp2:
                print(ans)
                break
        else:
            plus=max(abs(a[lp1]-a[fp1]), abs(a[lp1]-a[fp2]) )
            #print(plus)
            ans+=plus
            print(ans)
            break

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
A.sort()
leftA = A[:N//2+N%2]
rightA = A[N//2+N%2:]
rightA.sort(reverse=True)
tmp = leftA[0]
leftA[0] = leftA[1]
leftA[1] = tmp
ans = 0
if len(leftA) == len(rightA):
    for i in range(N//2):
        if i == 0:
            ans += abs(leftA[i] - rightA[i])
            lastA = rightA[i]
            continue
        ans += abs(lastA - leftA[i])
        if i == N//2-1:
            ans += max(abs(leftA[i] - rightA[i]), abs(leftA[0] - rightA[i]))
            continue
        ans += abs(leftA[i] - rightA[i])
        lastA = rightA[i]
else:
    for i in range(N//2+1):
        if i == 0:
            ans += abs(leftA[i] - rightA[i])
            lastA = rightA[i]
            continue
        ans += abs(lastA - leftA[i])
        if i == N//2:
            continue
        ans += abs(leftA[i] - rightA[i])
        lastA = rightA[i]
    ans2 = 0
    tmp = leftA.pop(-1)
    rightA.insert(0, tmp)
    for i in range(N//2):
        if i == 0:
            ans2 += abs(leftA[i] - rightA[i])
            lastA = rightA[i]
            continue
        ans2 += abs(lastA - leftA[i])
        lastA = rightA[i]
    ans2 += abs(rightA[-2] - rightA[-1])
    ans = max(ans, ans2)

print(ans)

Submission status: Runtime Error

Solution code:
c=abs(b[0]-b[-1])+abs(b[0]-b[-2])
x,y=b[-1],b[-2]
del b[-1],b[-1],b[0]
while b:
    if len(b)<2:
        c+=max(abs(x-b[0]),abs(y-b[0]))
        del b[0]
    else:
        if min(x,y)>b[-1]:
            c+=max(abs(x-b[0])+abs(y-b[1]),abs(x-b[1])+abs(y-b[0]))
            x,y=b[0],b[1]
            del b[0],b[0]
        else:
            c+=max(abs(x-b[-1])+abs(y-b[-2]),abs(x-b[-2])+abs(y-b[-1]))
            x,y=b[-1],b[-2]
            del b[-1],b[-1]
print(c)

Submission status: Runtime Error

Solution code:
import math

def gcd(a,b):
    """Compute the greatest common divisor of a and b"""
    while b > 0:
        a, b = b, a % b
    return a

def lcm(a, b):
    """Compute the lowest common multiple of a and b"""
    return a * b / gcd(a, b)


def best_alignment(lst, small):

	ans = 0

	p = []

	if(small):
		p.append(lst[-1])
		p.append(lst[-1])
		del lst[-1:]
	else:
		m = min(lst)
		p.append(lst[0])
		p.append(lst[0])
		lst.pop(0)


	while(len(lst)>0):

		print(small)
		print(lst)
		print(p)
		print(ans)
		print("")

		if(small):
			ans = ans + abs(max(p)-lst[0])
			if(len(lst)==1):
				break

			ans = ans + abs(min(p)-lst[1])

			p = []
			p.append(lst.pop(0))
			p.append(lst.pop(0))
			small = False
			continue
		else:
			ans = ans + abs(min(p)-lst[-1])
			if(len(lst)==1):
				break

			ans = ans + abs(max(p)-lst[-2])

			p = []
			p.append(lst[-1])
			p.append(lst[-2])
			del lst[-2:]
			small = True
			continue

	return ans	




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

A.sort()
A_=A.copy()

print(max(best_alignment(A, True), best_alignment(A_, False)))

Submission status: Runtime Error

Solution code:
n = int(input())
A = [int(input()) for _ in range(n)]
def f(x):
    cnt = 0
    for i in range(len(x) - 1):
        cnt += abs(x[i] - x[i + 1])
    return cnt
ans = []
for B in permutations(A):
    ans.append((f(B), B))
ans.sort(reverse=True)
print(ans[0][0])

Submission status: Runtime Error

Solution code:
n=int(input())

a=[]

for i in range(n):
    b=int(input())
    a.append(b)
    
a.sort()

ans=["" for i in range(n)]

ans[n//2]=a[-1]
a.remove(a[-1])
l=(n//2)-1
r=(n//2)+1

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

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


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

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

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

Submission status: Runtime Error

Solution code:
n,*a=map(int,open(0).read().split())
b=sorted(a)
c=abs(b[0]-b[-1])+abs(b[0]-b[-2])
x,y=b[-1],b[-2]
del b[-1],b[-1],b[0]
while b:
    if len(b)<2:
        c+=max(abs(x-b[0]),abs(y-b[0]))
        del b[0]
    else:
        if x+y>b[0]+b[1]:
            c+=max(abs(x-b[0])+abs(y-b[1]),abs(x-b[1])+abs(y-b[0]))
            x,y=b[0],b[1]
            del b[0],b[0]
        else:
            c+=max(abs(x-b[-1])+abs(y-b[-2]),abs(x-b[-2])+abs(y-b[-1]))
            x,y=b[-1],b[-2]
            del b[-1],b[-1]
print(c)

Submission status: Runtime Error

Solution code:
n = input()
n = int(n)
a = []
for i in range(n):
    t = int(input())
    a.append(t)
a.sort()
b = [0 for i in range(n)]
k = n // 2
l = k // 2
b[k] = a.pop(0)
if n % 2 == 1:
    for i in range(1,l+1):
        b[k - 2 * i] = a.pop(0)
        b[k - 2 * i + 1] = a.pop(-1)
        b[k + 2 * i - 1] = a.pop(-1)
        b[k + 2 * i] = a.pop(0)
    if k % 2 == 1:
        b[0] = a.pop(-1)
        b[n - 1] = a.pop(-1)
else:
    for i in range(1,l+1):
        b[k - 2 * i] = a.pop(0)
        b[k - 2 * i + 1] = a.pop(-1)
        b[k + 2 * i - 1] = a.pop(-1)
        b[k + 2 * i] = a.pop(0)
    if k % 2 == 0:
        b[0] = a.pop(0)
        b[1] = a.pop(-1)
        b[n - 1] = a.pop(-1)
    else:
        b[0] = a.pop(-1)
sum = 0
for i in range(n-1):
    sum += abs(b[i] - b[i + 1])
print(sum)

Submission status: Runtime Error

Solution code:
from collections import deque
N = input()
A = []
for i in range(N):
	a = input()
	A.append(a)

A = sorted(A)
res = []
begin = 0
end = len(A) - 1
res = deque()
res.append(A[begin])
res.append(A[end])
begin+= 1
end -= 1
for i in range(N - 2):
	l = [abs(res[0] - A[begin]), abs(res[0] - A[end]), abs(res[-1] - A[begin]), abs(res[-1] - A[end])]
	cand = l.list(max(l))
	if cand == 0:
		res.appendleft(A[begin])
		begin += 1
	elif cand == 1:
		res.appendleft(A[end])
		end -= 1
	elif cand == 2:
		res.append(A[begin])
		begin += 1
	elif cand == 3:
		res.append(A[end])
		end -= 1
sum_diff = 0
for i in range(N - 1):
	 sum_diff += abs(res[i] - res[i + 1])
print sum_diff

Submission status: Runtime Error

Solution code:
n = int(input())
a = [0]*n

for i in range(n):
    a[i] = int(input())
a.sort()
x = a[:len(a)//2]
y = a[len(a)//2:]
y.reverse()
x = [x[-1]]+x[:-1]
b = sum([[i,j] for i,j in zip(x,y)],[])
if len(x)%2:
    b = b + [x[-1]]
ans = 0
for i in range(n):
    if i < n-1:
        ans += abs(b[i]-b[i+1])
print(ans)

Submission status: Runtime Error

Solution code:
n,*a=map(int,open(0).read().split())
b=sorted(a)
c=abs(b[0]-b[-1])+abs(b[0]-b[-2])
x,y=b[-1],b[-2]
del b[-1],b[-1],b[0]
while b:
    if len(b)<2:
        c+=max(abs(x-b[0]),abs(y-b[0]))
        del b[0]
    else:
        if x+y>b[0]+b[1]:
            c+=max(abs(x-b[0])+abs(y-b[1]),abs(x-b[1]),abs(y-b[0]))
            x,y=b[0],b[1]
            del b[0],b[0]
        else:
            c+=max(abs(x-b[-1])+abs(y-b[-2]),abs(x-b[-2]),abs(y-b[-1]))
            x,y=b[-1],b[-2]
            del b[-1],b[-1]
print(c)

Submission status: Runtime Error

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

A.sort()
right = A[:int(len(A) / 2)]
left = A[int(len(A) / 2):]

max_list = []

if len(right) == 1:
    max_list = [left[0], right[0], left[1]]
else:

    while True:
        if right:
            max_list.append(right.pop())
        if left:
            max_list.append(left.pop())

        if len(right) == 0 and len(left) == 0:
            break

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

print(ans)

Submission status: Runtime Error

Solution code:
N=int(input().strip())
alist=[]
for i in range(N):
    alist.append(int(input().strip()))
alist.sort()
su=0
if N%2==0:
    for i in range((N//2)-1):
        su=su + 2*alist[N-1-i] - 2*alist[i]
    su=su+alist[(N//2)+1]-alist[N//2]
elif N%2==1:
    for i in range((N//2)-1):
        su=su + 2*alist[N-1-i] - 2*alist[i]
    su=su-alist[(N//2)-1]-alist[N//2]+2*(alist[(N//2)+1])
    
print(su)

Submission status: Runtime Error

Solution code:
import numpy as np
N = input()
A = []
for i in range(N):
	a = input()
	A.append(a)

A = sorted(A)
res = []
begin = 0
end = len(A) - 1
res = deque()
res.append(A[begin])
res.append(A[end])
begin+= 1
end -= 1
for i in range(N - 2):
	cand = np.argmax([abs(res[0] - A[begin]), abs(res[0] - A[end]), abs(res[-1] - A[begin]), abs(res[-1] - A[end])])
	if cand == 0:
		res.appendleft(A[begin])
		begin += 1
	elif cand == 1:
		res.appendleft(A[end])
		end -= 1
	elif cand == 2:
		res.append(A[begin])
		begin += 1
	elif cand == 3:
		res.append(A[end])
		end -= 1
sum_diff = 0
for i in range(N - 1):
	 sum_diff += abs(res[i] - res[i + 1])
print sum_diff

Submission status: Runtime Error

Solution code:
n = input()
n = int(n)
a = []
for i in range(n):
    t = int(input())
    a.append(t)
a.sort()
b = [0 for i in range(n)]
k = n // 2
l = k // 2
b[k] = a[0]
if n % 2 == 1:
    for i in range(1,l+1):
        b[k - 2 * i] = a[2 * i - 1]
        b[k - 2 * i + 1] = a[n - (2 * i - 2) - 1]
        b[k + 2 * i - 1] = a[n - (2 * i - 1) - 1]
        b[k + 2 * i] = a[2 * i]
    if k % 2 == 1:
        b[0] = a[n - (2 * l) - 1]
        b[n - 1] = a[n - (2 * l) - 2]
else:
    for i in range(1,l+1):
        b[k - 2 * i] = a[2 * i - 1]
        b[k - 2 * i + 1] = a[n - (2 * i - 2) - 1]
        b[k + 2 * i - 1] = a[n - (2 * i - 1) - 1]
        b[k + 2 * i] = a[2 * i]
    if k % 2 == 0:
        b[0] = a[2 * l + 1]
        b[1] = a[n - (2 * l) - 1]
        b[n - 1] = a[n - (2 * l) - 2]
    else:
        b[0] = a[n - (2 * l) - 1]
sum = 0
for i in range(n-1):
    sum += abs(b[i] - b[i + 1])
print(sum)

Submission status: Runtime Error

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

B=sorted(A)
#print(B[N-1]-B[0] + B[N-1]-B[0])
hN=int(N/2)
cnt=0
C=[]
if N %2 == 0:
    C.append(B[hN])
    for i in range(hN-1):
        C.append(B[N-1-i])
        C.append(B[i])
    C.append(B[hN+1])
else:
    C.append(B[hN])
    for i in range(hN):
        C.append(B[N-1-i])
        C.append(B[i])

#print(C)

for i in range(1,N):
    cnt+=abs(C[i]-C[i-1])

C=[]
cnt2=0
if N %2 == 0:
    C.append(B[hN-1])
    for i in range(hN):
        C.append(B[i])
        C.append(B[N-1-i])
    C.append(B[hN+1])
else:
    C.append(B[hN])
    for i in range(hN):
        C.append(B[i])
        C.append(B[N-1-i])

#print(C)
for i in range(1,N):
    cnt2+=abs(C[i]-C[i-1])

print(max(cnt,cnt2))

Submission status: Runtime Error

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
a.sort()# ソート
hashi = [a[0], a[-1]]
total = abs(a[0] - a[-1])

s = 1
e = n - 2
while True:
    max_diff = 0
    point = "s"
    h = "s"
    if max_diff < abs(hashi[0] - a[s]):
        max_diff = abs(hashi[0] - a[s])
        point = "s"
        h = "s"
    if max_diff < abs(hashi[0] - a[e]):
        max_diff = abs(hashi[0] - a[e])
        point = "e"
        h = "s"
    if max_diff < abs(hashi[1] - a[s]):
        max_diff = abs(hashi[1] - a[s])
        point = "s"
        h = "e"
    if max_diff < abs(hashi[1] - a[e]):
        max_diff = abs(hashi[1] - a[e])
        point = "e"
        h = "e"
    total += max_diff
    if h == "s":
        if point == "s":
            hashi[0] = a[s]
        if point == "e":
            hashi[0] = a[e]
    if h == "e":
        if point == "s":
            hashi[1] = a[s]
        if point == "e":
            hashi[1] = a[e]
    if point == "s":
        s += 1
    if point == "e":
        e -= 1
    if s - 1 == e:
        break
print(total)

Submission status: Runtime Error

Solution code:
import itertools
n = int(input())
array = []
for _ in range(n):
    array.append(int(input()))
b = []
for i in range(n-1):
    for j in range(i+1, n):
        b.append(abs(array[i]-array[j]))
c = []
for t in list(itertools.combinations(b, 4)):
    c.append(sum(t))
print(max(c))

Submission status: Runtime Error

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

A.sort()



# 8 1 6 2 3 -> 17
# 1 8 2 6 3 -> 21

center = A[N//2]
ans = 0
for a in A:
    ans += 2*abs(a-center)
ans -= min(center-A[N//2-1],A[N//2+1]-center)
print(ans)

Submission status: Runtime Error

Solution code:
def findbest(liste, i):
	lister = liste.copy()
	lister.remove(i)
	returnlister = [i]
	len(lister) = t
	while(t >= 2):
		if abs(i - lister[-1]) >= abs(i - lister[0]):
			returnlister.append(lister.pop(-1))
		else:
			returnlister.append(lister.pop(0))
		i = returnlister[-1]
	t -= 1
	returnlister.extend(lister)
	output = 0
	for j in range(len(returnlister) - 1):
		output += abs(returnlister[j] - returnlister[j + 1])
	
	return output
 
a = int(input())
numlist = []
for i in range(a):
	numlist.append(int(input()))
 
numlist = sorted(numlist)
possible = list(set(numlist))
maxer = 0
if len(possible) <= 100:
	for i in possible:
		maxer = max(findbest(numlist, i), maxer)
else:
	for i in possible[len(possible)//2 - 1: len(possible)//2 + 2]:
		maxer = max(findbest(numlist, i), maxer)
		
print (maxer)

Submission status: Runtime Error

Solution code:
N = int(input())
L = []
for i in range(N) :
    l = int(input())
    L.append(l)
    L2.append(l)
L = sorted(L)
L2 = sorted(L2)
ANS = []
for i in range(2) :
    ans = 0
    turn = i
    flag = 0
    B = []
    if i == 0 :
        while len(L) != 0 :
            if len(L) == 1 :
                r = L[0]
                L.pop(0)
            else :
                if turn == 0 :
                    p = L[-1]
                    if flag != 0 :
                        ans += abs(p-q)
                    else :
                        B.append(p)
                        flag = 1
                    L.pop(-1)
                    turn = 1
                else :
                    q = L[0]
                    if flag != 0 :
                        ans += abs(p-q)
                    else :
                        B.append(q)
                        flag = 1
                    L.pop(0)
                    turn = 0

        ans += abs(r-B[0])
        ANS.append(ans)
        
    else :
        while len(L2) != 0 :
            if len(L2) == 1 :
                r = L2[0]
                L2.pop(0)
            else :
                if turn == 0 :
                    p = L2[-1]
                    if flag != 0 :
                        ans += abs(p-q)
                    else :
                        B.append(p)
                        flag = 1
                    L2.pop(-1)
                    turn = 1
                else :
                    q = L2[0]
                    if flag != 0 :
                        ans += abs(p-q)
                    else :
                        B.append(q)
                        flag = 1
                    L2.pop(0)
                    turn = 0

        ans += abs(r-B[0])
        ANS.append(ans)
        
        
print(max(ANS))

Submission status: Runtime Error

Solution code:
N = int(input())

num = []
for i in range(N):
  num.append(int(input()))
  
num = sorted(num)
ans = [num[0]]
num.pop(0)

for i in range(len(num)-1):
  if i % 4 == 0:
    ans.append(num[-1])
    num.pop(-1)
  elif i % 4 == 1:
    ans = [num[-1], *ans]
    num.pop(-1)
  elif i % 4 == 2:
    ans.append(num[0])
    num.pop(0)
  else:
    ans = [num[0], *ans]
    num.pop(0)

answer = max(abs(num[0]-ans[0]), abs(num[0]-ans[-1]))
for i in range(len(ans)-1):
  answer += abs(ans[i] - ans[i+1])
  
print(answer)

Submission status: Runtime Error

Solution code:
def findbest(liste, i):
	lister = liste.copy()
	lister.remove(i)
	returnlister = [i]
	while(len(lister) >= 2):
		if abs(i - lister[-1]) >= abs(i - lister[0]):
			returnlister.append(lister.pop(-1))
		else:
			returnlister.append(lister.pop(0))
		i = returnlister[-1]
	returnlister.extend(lister)
	output = 0
	for j in range(len(returnlister) - 1):
		output += abs(returnlister[j] - returnlister[j + 1])
	
	return output
 
a = int(input())
numlist = []
for i in range(a):
	numlist.append(int(input()))
 
numlist = sorted(numlist)
possible = list(set(numlist))
maxer = 0
if len(possible) <= 100:
	for i in possible:
		maxer = max(findbest(numlist, i), maxer)
else:
	for i in possible[len(possible)//2 - 10, len(possible)//2 + 10]:
		numlist = templist
		maxer = max(findbest(numlist, i), maxer)
		
print (maxer)

Submission status: Runtime Error

Solution code:
def findbest(liste, i):
	lister = liste.copy()
	lister.remove(i)
	returnlister = [i]
	while(len(lister) >= 2):
		if abs(i - lister[-1]) >= abs(i - lister[0]):
			returnlister.append(lister.pop(-1))
		else:
			returnlister.append(lister.pop(0))
		i = returnlister[-1]
	returnlister.extend(lister)
	output = 0
	for j in range(len(returnlister) - 1):
		output += abs(returnlister[j] - returnlister[j + 1])
	
	return output

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

numlist = sorted(numlist)
possible = list(set(numlist))
maxer = 0
if len(possible) <= 100:
	for i in possible:
		maxer = max(findbest(numlist, i), maxer)
else:
	for i in possible[len(possible)//2 - 10, len(possible)//2 + 10]:
		numlist = templist
		maxer = max(findbest(numlist, i), maxer)
		
print (maxer)

Submission status: Runtime Error

Solution code:
def findbest(liste, i):
	lister = liste.copy()
	lister.remove(i)
	returnlister = [i]
	while(len(lister) >= 2):
		if abs(i - lister[-1]) >= abs(i - lister[0]):
			returnlister.append(lister.pop(-1))
		else:
			returnlister.append(lister.pop(0))
		i = returnlister[-1]
	returnlister.extend(lister)
	output = 0
	for j in range(len(returnlister) - 1):
		output += abs(returnlister[j] - returnlister[j + 1])
	
	return output

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

numlist = sorted(numlist)
possible = list(set(numlist))
maxer = 0
if len(possible) <= 100:
	for i in possible:
		maxer = max(findbest(numlist, i), maxer)
else:
	for i in possible[len(possible) - 10, len(possible) + 10]:
		numlist = templist
		maxer = max(findbest(numlist, i), maxer)
		
print (maxer)

Submission status: Runtime Error

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

s=[]
k=len(num)
while k>0:
    s=s+[num.pop(0)]
    if k=1:break
    s=[num.pop(-1)]+s
    if k=2:break
    s=s+[num.pop(-1)]
    if k=3:break
    s=[num.pop(0)]+s
    k-=4

S=0
for i in range(len(s)-1):
    S+=abs(s[i]-s[i+1])
print(S)