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())
nums = []
for _ in range(n):
    nums.append(int(input()))

sorted(nums)
min_num = -1
max_num = -1

# 最大値
ans_right = nums.pop()
# 最小値
ans_left = nums.pop(0
ans = abs(ans_right - ans_left)

while len(nums) > 2:
    num_max = nums[-1]
    num_min = nums[0]
    compare = [
        abs(num_max-ans_right),
        abs(num_max-ans_left),
        abs(num_min-ans_right),
        abs(num_min-ans_left)
    ]
    t = max(compare)
    c = compare.index(t)

    # 追加処理
    ans += t
    add_to_ans = num_max if c == 0 else num_min
    nums.remove(add_to_ans)
    if c % 2 == 0:
        ans_right = add_to_ans
    else:
        ans_left = add_to_ans

# add len(nums) == 1
print(ans + max(abs(nums[0]-ans_right),abs(nums[0]-ans_left)))

Submission status: Runtime Error

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

A.sort()
count = 0
for i in range(N):

    ans.append(A[0+i])
    count += 1
    if count == N:
        break

    ans.insert(0,A[end-i])
    count += 1
    if count == N:
        break

print((ans[1:]-ans[:-1]).sum())

Submission status: Runtime Error

Solution code:
n = int(input())
a = sorted([int(input()) for x in range(n)])
ans = 0
if n % 2 != 0:
    a1 = a[n // 2]
    del a[n // 2]
    for i in range(n - 1):
        ua = abs(a1 - a[0])
        ta =  abs(a1 - a[-1])
        if ua >= ta:
            ans += ua
            a1 = a[0]
            del a[0]
        else:
            ans += ta
            a1 = a[-1]
            del a[-1]
else:
    a1 = a[n // 2 - 1]
    A2 = a.copy()
    a2 = A2[n // 2]
    ans2 = 0
    del A2[n // 2]
    del a[n // 2 - 1]
    for i in range(n - 1):
        ua = abs(a1 - a[0])
        ta =  abs(a1 - a[-1])
        if ua >= ta:
            ans += ua
            a1 = a[0]
            del a[0]
        else:
            ans += ta
            a1 = a[-1]
            del a[-1]

    for i in range(n - 1):
        ua = abs(a2 - A2[0])
        ta =  abs(a2 - A2[-1])
        if ua >= ta:
            ans2 += ua
            a2 = A2[0]
            del A2[0]
        else:
            ans2 += ta
            a2 = A2[-1]
            del A2[-1]
    ans = max[ans , ans2]

print(ans)

Submission status: Runtime Error

Solution code:
#import numpy as np
import itertools
n = int(input())
a = []
for i in range(n):
    a.append(input())
# [d1, d2, d3, ..., dN]
a = [int(m) for m in a]
sum = []

a.sort()

count = 0
s = 0

for a in itertools.permutations(a):
    #a = [int(m) for m in a]
    print(a)
#print(np.diff(a, n=1, axis=-1))
    for j in range(n):
        if j != 0:
            sum[count] += abs(a[j] - a[j - 1])
    if sum[count] > sum[count - 1] and count > 0:
        s = sum[count]
    count += 1

Submission status: Runtime Error

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

ans_list = [0] * len(A)
if len(A)%2 == 0:
    low = A[0:len(A)//2]
    high = sorted(A[len(A)//2:len(A)], reverse=True)
    high = A[len(A)//2:len(A)]
else:
    low = A[0:len(A)//2+1]
    high = sorted(A[len(A)//2+1:len(A)], reverse=True)
    high = A[len(A)//2+1:len(A)] 
first = low.pop(0)

ans = 0
ans += abs(first - high[0])
ans += abs(first - high[1])

for i in range(len(low)):
    ans += abs(high[i] - low[i])

print(ans)

Submission status: Runtime Error

Solution code:
from itertools import zip_longest

n = int(input())
nums = [int(input()) for i in range(n)]
nums = sorted(nums)
mid = int(len(nums)/2)
smalls = nums[:mid]
larges = nums[mid:]
if len(smalls)>len(larges):
    if abs(smalls[-1]-smalls[-2])>abs(smalls[-1]-larges[0]):
        larges.append(smalls[-1])
        smalls.pop(-1)
elif len(larges) > len(smalls):
    if abs(larges[0]-larges[2])>abs(larges[0]-smalls[-1]):
        smalls.append(larges[0])
        larges.pop(0)
smalls=sorted(smalls)
larges=sorted(larges)

smalls_even = sorted(smalls[0::2],reverse=True)
smalls_odd = smalls[1::2]
larges_even = larges[0::2]
larges_odd = sorted(larges[1::2],reverse=True)

smalls_even.extend(smalls_odd)
larges_even.extend(larges_odd)
smalls = smalls_even
larges = larges_even
aligns = []

if len(smalls) > len(larges):
    for i,j in zip_longest(smalls,larges):
        if i != None:
            aligns.append(i)
        if j!=None:
            aligns.append(j)
else:
    for i,j in zip_longest(larges,smalls):
        if i != None:
            aligns.append(i)
        if j!=None:
            aligns.append(j)

sa = 0
before=0
for i in aligns:
    if before == 0:
        before = i
        continue
    sa += abs(i-before)
    before = i
print(sa)

Submission status: Runtime Error

Solution code:
#coding:utf-8
import copy
N = int(input())
A_list = []
for i in range(N):
    A_list.append(int(input()) )

def calculate_x(list):
    len_list = len(list)
    sum = 0
    for i in range(len_list-1):
        sum = sum + abs(list[i]-list[i+1])
    return sum

A_list = sorted(A_list)

if N % 2 ==0: #even
    answer_list = []
    A_min_list = A_list[:int(N/2)-1]
    A_max_list = A_list[-int(N/2)+1:]
    for max, min in zip(A_max_list, A_min_list):
        answer_list.append(max)
        answer_list.append(min)
    answer_list.append(A_list[int(N/2)]) #min of max_list
    answer_list.insert(0,A_list[int(N/2)-1]) #max of min_list
    tmp = calculate_x(answer_list)
    print(tmp)

else: #odd
    if N >=5:
    answer_list = []
    A_min_list = A_list[:int((N-1)/2)-1] #0,1
    A_max_list = A_list[-int((N-1)/2)+1:] #5,6
    x1 = A_list[int((N-1)/2)-1] #2
    x2 = A_list[int((N-1)/2)]#3
    x3 = A_list[int((N-1)/2)+1]#4
    for max, min in zip(A_max_list, A_min_list):
        answer_list.append(max)
        answer_list.append(min)

    answer_list2 = copy.deepcopy(answer_list)
    max = 0
    answer_list2.append(x1)
    answer_list2.insert(0,x2)
    answer_list2.insert(0,x3)
    tmp = calculate_x(answer_list2)
    if tmp > max:
        max = tmp

    answer_list2 = copy.deepcopy(answer_list)
    answer_list2.append(x1)
    answer_list2.insert(0,x3)
    answer_list2.insert(0,x2)
    tmp = calculate_x(answer_list2)
    if tmp > max:
        max = tmp

    answer_list2 = copy.deepcopy(answer_list)
    answer_list2.append(x2)
    answer_list2.insert(0,x1)
    answer_list2.insert(0,x3)
    tmp = calculate_x(answer_list2)
    if tmp > max:
        max = tmp

    answer_list2 = copy.deepcopy(answer_list)
    answer_list2.append(x2)
    answer_list2.insert(0,x3)
    answer_list2.insert(0,x1)
    tmp = calculate_x(answer_list2)
    if tmp > max:
        max = tmp

    answer_list2 = copy.deepcopy(answer_list)
    answer_list2.append(x3)
    answer_list2.insert(0,x1)
    answer_list2.insert(0,x2)
    tmp = calculate_x(answer_list2)
    if tmp > max:
        max = tmp

    answer_list2 = copy.deepcopy(answer_list)
    answer_list2.append(x3)
    answer_list2.insert(0,x2)
    answer_list2.insert(0,x1)
    tmp = calculate_x(answer_list2)
    if tmp > max:
        max = tmp



    answer_list3 = []
    A_min_list = A_list[:int((N-1)/2)-1] #0,1
    A_max_list = A_list[-int((N-1)/2)+1:] #5,6
    x1 = A_list[int((N-1)/2)-1] #2
    x2 = A_list[int((N-1)/2)]#3
    x3 = A_list[int((N-1)/2)+1]#4
    for max, min in zip(A_max_list, A_min_list):
        answer_list3.append(min)
        answer_list3.append(max)

    answer_list4 = copy.deepcopy(answer_list3)
    answer_list4.append(x1)
    answer_list4.insert(0,x2)
    answer_list4.insert(0,x3)
    tmp = calculate_x(answer_list4)
    if tmp > max:
        max = tmp

    answer_list4 = copy.deepcopy(answer_list3)
    answer_list4.append(x1)
    answer_list4.insert(0,x3)
    answer_list4.insert(0,x2)
    tmp = calculate_x(answer_list4)
    if tmp > max:
        max = tmp

    answer_list4 = copy.deepcopy(answer_list3)
    answer_list4.append(x2)
    answer_list4.insert(0,x1)
    answer_list4.insert(0,x3)
    tmp = calculate_x(answer_list4)
    if tmp > max:
        max = tmp

    answer_list4 = copy.deepcopy(answer_list3)
    answer_list4.append(x2)
    answer_list4.insert(0,x3)
    answer_list4.insert(0,x1)
    tmp = calculate_x(answer_list4)
    if tmp > max:
        max = tmp

    answer_list4 = copy.deepcopy(answer_list3)
    answer_list4.append(x3)
    answer_list4.insert(0,x1)
    answer_list4.insert(0,x2)
    tmp = calculate_x(answer_list4)
    if tmp > max:
        max = tmp

    answer_list4 = copy.deepcopy(answer_list3)
    answer_list4.append(x3)
    answer_list4.insert(0,x2)
    answer_list4.insert(0,x1)
    tmp = calculate_x(answer_list4)
    if tmp > max:
        max = tmp
    print(max)

Submission status: Runtime Error

Solution code:
a = int(input())

b = []

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

b.sort()

c = []

if a % 2 == 0:
  k = a//2
else:
  k = a//2+1

bb = b[0:k]

bbb = b[k:]
j = len(bb)-1
jj = len(bbb)
bbb.reverse()
i = 0
ii = 0
c.append(bb[-1])
bb.pop()
for llll in range(a-1):
    if i != j and ii != jj:
        p = abs(c[-1]-bb[i])
        pp = abs(c[-1]-bbb[ii])
        if max(abs(c[-1]-bb[i]),abs(c[-1]-bbb[ii])) == p:
            c.append(bb[i])
            c.append(bbb[ii])
            i += 1
            ii += 1
            llll += 1
        else:
            c.append(bbb[ii])
            c.append(bb[i])
            ii += 1
            i += 1
            llll += 1
su = 0
for i in range(a-1):
    su += abs(c[i]-c[i+1])
print(su)

Submission status: Runtime Error

Solution code:
N = int(input())
l=[int(input()) for i in range(N)]
l.sort()
res = 0
k = 0
o = 0
s = 0
t = 0
if N%2 == 0:
    a = l[:int(N/2)]
    a.reverse()
    b = l[int(N/2):]
    b.reverse()
    for i in range(N-1):
        res1 += b[k] - a[o]
        res2 += b[s] - a[t]
        if i%2 == 0:
            o += 1
            s += 1
        if i%2 == 1:
            k += 1
            t += 1
    res = max(res1, res2)
else:
    a = l[:int(N/2)]
    b = l[int(N/2)+1:]
    b.reverse()
    for i in range(N-2):
        res1 += b[k] - a[o]
        res2 += b[s] - a[t]
        if i%2 == 0:
            k += 1
            t += 1
        if i%2 == 1:
            o += 1
            s += 1
    res1 += max(abs(a[-1] - l[int(N/2)]), abs(b[0] - l[int(N/2)]))
    res2 += max(abs(a[-1] - l[int(N/2)]), abs(b[0] - l[int(N/2)]))
    res = max(res1, res2)
print(res)

Submission status: Runtime Error

Solution code:
n = int(input())
heights = [each_height for each_height in map(int, input().split())]
 
s2l = sorted(heights)
l2s = s2l[::-1]
 
results = []
pop_small = True
left = True
for i in range(n):
    if i == 0:
        results.append(s2l.pop())
    elif i == n-1:
        if pop_small:
            temp = l2s.pop()
            if abs(results[0] - temp) > abs(results[n-2] - temp):
                results.insert(0, temp)
            else:
                results.append(temp)
        else:
            temp = s2l.pop()
            if abs(results[0] - temp) > abs(results[n - 2] - temp):
                results.insert(0, temp)
            else:
                results.append(temp)
    elif pop_small:
        if left:
            results.insert(0, l2s.pop())
            left = False
        else:
            results.append(l2s.pop())
            left = True
            pop_small = False
    else:
        if left:
            results.insert(0, s2l.pop())
            left = False
        else:
            results.append(s2l.pop())
            left = True
            pop_small = True
 
print(sum([abs(results[i] - results[i+1]) for i in range(n-1)]))

Submission status: Runtime Error

Solution code:

N = int(input())
A = []


def solve(k, A):
    if abs(A[k] - A[0]) > abs(A[k] - A[-1]):
        b = abs(A[k] - A[0])
        del A[k]
        return _solve(True, A) + b
    elif abs(A[k] - A[0]) < abs(A[k] - A[-1]):
        b = abs(A[k] - A[-1])
        del A[k]
        return _solve(False, A) + b
    else:
        b_top = abs(A[k] - A[0])
        b_tail = abs(A[k] - A[-1])
        del A[k]
        temp = A[:]
        return max(_solve(True, temp) + b_top, _solve(False, A) + b_tail)
    return ans


def _solve(from_top, A):
    ans = 0
    pos = {"top": 0, "tail": N - 2}
    if from_top:
        next = "top"
    else:
        next = "tail"

    while True:
        ans += abs(A[pos["top"]] - A[pos["tail"]])
        if next == "top":
            if pos["top"] + 1 == pos["tail"]:
                return ans
            else:
                pos["top"] += 1
                next = "tail"
        else:
            if pos["top"] == pos["tail"] - 1:
                return ans
            else:
                pos["tail"] -= 1
                next = "top"


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

A.sort()
ans = 0
for i in range(N):
    temp = A[:]
    hoge = solve(i, temp)
    ans = max(hoge, ans)


print(ans)

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]

n = N-1 if N % 2 == 1 else N
ans = [0 for _ in range(n)]

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 ans[0] < ans[-1]:
        ans = [m[-1]] + ans
    else:
        ans = ans + [m[-1]]

print(ans)

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

print(S)

Submission status: Runtime Error

Solution code:
N = int(input().strip('\n'))
lis = []
for i in range(N):
    lis.append(int(input().strip('\n')))
lis = np.sort(np.array(lis))
mx_n = lis[-1]
ms_n = lis[0]
lis = lis[1:-1]
point = mx_n-ms_n
while lis.shape[0]>=2:
    mx = lis[-1]
    ms = lis[0]
    point += (mx_n-ms+mx-ms_n)
    mx_n = mx
    ms_n = ms
    lis = lis[1:-1]
if lis.shape[0]==1:
    point += max(abs(mx_n-lis[0]),abs(ms_n-lis[0]))
print(point)

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]

if N % 2 == 1:
    N -= 1
ans = [0 for _ in range(N)]

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 ans[0] < ans[-1]:
        ans = [m[-1]] + ans
    else:
        ans = ans + [m[-1]]

print(ans)

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

print(S)

Submission status: Runtime Error

Solution code:
n = int(input())
l = []
ans = 0
for i in range(n):
    l.append(int(input()))
m = sum(l) / n
large_l = []
small_l = []
for i in range(n):
    if l[i] > m:
        large_l.append(abs(l[i]-m))
    else:
        small_l.append(abs(l[i]-m))
large_l.sort()
small_l.sort()
if n % 2 == 1:
    if len(large_l) < len(small_l):
        ans += small_l[0] + small_l[1]
        for i in range(2, len(small_l)):
            ans += 2 * small_l[i]
        for i in range(len(large_l)):
            ans += 2 * large_l[i]
    else:
        ans += large_l[0] + large_l[1]
        for i in range(len(small_l)):
            ans += 2 * small_l[i]
        for i in range(2, len(large_l)):
            ans += 2 * large_l[i]
else:
    ans += large_l[0] + small_l[0]
    for i in range(1, len(small_l)):
        ans += 2 * small_l[i]
    for i in range(1, len(large_l)):
        ans += 2 * large_l[i]
print(int(ans))

Submission status: Runtime Error

Solution code:
def neighbor_sum(N, A):
    A_1 = [] 
    A_2 = []
    i = 0
    j = N-1
    change = True
    while i < j:
        if change == True:
            A_1.append(A[i])
            A_2.append(A[j])
            i += 1
            j -= 1
            change = False
        else:
            A_1.append(A[j])
            A_2.append(A[i])
            i += 1
            j -= 1
            change = True
        if N % 2 == 1:
            if change == True:
                A_1.append(A[N//2])
            else:
                A_2.append(A[N//2])
        return A_1[::-1] + A_2[:]

if __name__ == "__main__":
    N = int(input())
    A = [int(input()) for _ in range(N)]
    A.sort()
    diff = neighbor_sum(N, A)
    diff_sum = [abs(diff[i+1] - diff[i]) for i in range(N-1)]
    print(sum(diff_sum))

Submission status: Runtime Error

Solution code:
a = int(input())

b = []

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

b.sort()

c = []

if a % 2 == 0:
  k = a//2
else:
  k = a//2+1

bb = b[0:k]

bbb = b[k:]

j = len(bb)-1
jj = len(bbb)
bbb.reverse()
i = 0
ii = 0
c.append(bb[-1])
for llll in range(a-1):
    if i != j and ii != jj:
        p = abs(c[-1]-bb[i])
        pp = abs(c[-1]-bbb[ii])
        if max(abs(c[-1]-bb[i]),abs(c[-1]-bbb[ii])) == p:
            c.append(bb[i])
            c.append(bbb[ii])
            i += 1
            ii += 1
            llll += 1
        else:
            c.append(bbb[ii])
            c.append(bb[i])
            i += 1
            ii += 1
            llll += 1
    elif i == j:
        c.append(bbb[ii])
        ii += 1
    elif ii == jj:
        c.append(bb[i])
        i += 1

su = 0
for i in range(a-1):
    su += abs(c[i]-c[i+1])
print(su)

Submission status: Runtime Error

Solution code:
import math as mt
N = int(input())
A = [int(input()) for i in range(N)]
A.sort()
#print(A)
S = 0
P2 = mt.floor(N/2 - 1)
if N % 2 == 0:
    for i in range(0,P2,1):
        S += 2 * (A[N-i-1] - A[i])
    S += A[P2+1] - A[P2]
elif N % 4 == 1:
    for i in range(0,P2,1):
        S += 2 * (A[N-i-1] - A[i])
    S += 2 * A[N-1-P2] - A[P2] - A[P2+1]
else:
    for i in range(0,P2,1):
        S += 2 * (A[N-i-1] - A[i])
    S += A[N-P2-1] + A[N-P2] - 2 * A[P2]
print(S)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from collections import deque
n=int(input())
a=[]
for i in range(0,n):
  a.append(int(input()))
a.sort()

d = [a[0]]
a=a[1:]
b = deque(d)

f=True
while len(a)>0:
  la=len(a)-1
  lb=len(b)-1
  ln = abs(b[0]-a[0])
  lx = abs(b[0]-a[la])
  rn = abs(b[lb]-a[0])
  rx = abs(b[lb]-a[la])

  x = max(ln, max(lx, max(rn, rx)))
  
  if x == ln:
    b.insert(0,a[0])
    a=a[1:]
  elif x == lx:
    b.insert(0,a.pop())
  elif x == rn:
    b.append(a[0])
    a=a[1:]
  else:
    b.append(a.pop())
#print(b)

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

Submission status: Runtime Error

Solution code:
N = int(input())
l=[int(input()) for i in range(N)]
l.sort()
res1 = 0
res2 = 0
k = 0
o = 0
if N%2 == 0:
    a = l[:int(N/2)]
    a.reverse()
    b = l[int(N/2):]
    b.reverse()
    for i in range(N-1):
        res1 += b[k] - a[o]
        if i%2 == 0:
            o += 1
        if i%2 == 1:
            k += 1
            
    for i in range(N-1):
        res2 += b[k] - a[o]
        if i%2 == 0:
            k += 1
        if i%2 == 1:
            o += 1
            
res = max(res1, res2)       
else:
    a = l[:int(N/2)]
    b = l[int(N/2)+1:]
    b.reverse()
    for i in range(N-2):
        res1 += b[k] - a[o]
        if i%2 == 0:
            k += 1
        if i%2 == 1:
            o += 1
    res1 += max(abs(a[0] - l[int(N/2)]), abs(b[0] - l[int(N/2)]))
    for i in range(N-2):
        res2 += b[k] - a[o]
        if i%2 == 0:
            o += 1
        if i%2 == 1:
            k += 1
    res2 += max(abs(a[0] - l[int(N/2)]), abs(b[0] - l[int(N/2)]))
res = max(res1, res2)
print(res)

Submission status: Runtime Error

Solution code:
a = int(input())

b = []

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

b.sort()

c = []

if a % 2 == 0:
  k = a//2
else:
  k = a//2+1

bb = b[0:k]

bbb = b[k:]

j = len(bb)-1
jj = len(bbb)
bbb.reverse()
i = 0
ii = 0
c.append(bb[-1])
for llll in range(a-1):
    if i != j and ii != jj:
        p = abs(c[-1]-bb[i])
        pp = abs(c[-1]-bbb[ii])
        if max(abs(c[-1]-bb[i]),abs(c[-1]-bbb[ii])) == p:
            c.append(bb[i])
            c.append(bbb[ii])
            i += 1
            ii += 1
        else:
            c.append(bbb[ii])
            c.append(bb[i])
            i += 1
            ii += 1
    elif i == j:
        c.append(bbb[ii])
        ii += 1
    elif ii == jj:
        c.append(bb[i])
        i += 1

su = 0
for i in range(a-1):
    su += abs(c[i]-c[i+1])
print(su)

Submission status: Runtime Error

Solution code:
N = int(input())
lis=[]
for i in range(N):
    lis.append(int(input()))
lis.sort()
res=[lis.pop(0)]
 while True:
     res.append(lis.pop(-1))
     if len(lis)==0:break
     res.insert(0,lis.pop(-1))
     if len(lis)==0:break
     res.append(lis.pop(0))
     if len(lis)==0:break
     res.insert(0,lis.pop(0))
     if len(lis)==0:break
 
n=0
for i in range(N-1):
    n+=abs(res[i]-res[i+1])
print(res,n)

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]
#        print(N-1-i,i)
    su=su+max(2*alist[(N//2)+1]-alist[N//2]-alist[(N//2)-1],alist[(N//2)+1]+alist[N//2]-2*alist[(N//2)-1])
#    print(2*alist[(N//2)+1]-alist[N//2]-alist[(N//2)-1],alist[(N//2)+1]+alist[N//2]-2*alist[(N//2)-1])
print(su)

Submission status: Runtime Error

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

A.sort()

le = len(A) // 2

chiisaiA = A[:le]
ookiiA = A[le::][::-1]

result = [None]*(len(A))
result[1::2] = chiisaiA
result[::2] = ookiiA

res = []
for i in range(len(A)-1):
    res.append(abs(result[i] - result[i+1]))

res.append(abs(result[0] - result[len(A)-1]))
res.sort()

temp = sum(res) - res[0]

if le % 2 == 0:
  result = [None]*(len(A))
  result[1::2] = ookiiA
  result[::2] = chiisaiA

  res = []
  for i in range(len(A)-1):
    res.append(abs(result[i] - result[i+1]))

  res.append(abs(result[0] - result[len(A)-1]))
  res.sort()

print(min(temp, sum(res) - res[0]))

Submission status: Runtime Error

Solution code:
def t118C():
    N = int(input())
    A = [int(input()) for i in range(N)]
    A.sort()
    anslist = []
    ans = 0
    tmplist = []
    tmplist2 = []

    up = 0
    down = N - 1
    checklist = [0 for i in range(N)]
    while True:
        if checklist[up] == 1:
            break
        tmplist.append(up)
        checklist[up] = 1
        up += 1

        if checklist[down] == 1:
            break
        tmplist2.append(down)
        checklist[down] = 1
        down -= 1

        if checklist[down] == 1:
            break
        tmplist.append(down)
        checklist[down] = 1
        down -= 1

        if checklist[up] == 1:
            break
        tmplist2.append(up)
        checklist[up] = 1
        up += 1

    for i in range(len(tmplist)):
        tmplist[i] = A[tmplist[i]]

    for i in range(len(tmplist2)):
        tmplist2[i] = A[tmplist2[i]]

    #print(tmplist, tmplist2)
    for i in range(1, len(tmplist)):
        ans += abs(tmplist[i] - tmplist[i - 1])
    for i in range(1, len(tmplist2)):
        ans += abs(tmplist2[i] - tmplist2[i - 1])
    ans += abs(tmplist[0] - tmplist2[0])
    ans2 = ans
    if n >= 2:
        ans2 -= abs(tmplist[-1] - tmplist[-2])
        ans2 -= abs(tmplist2[-1] - tmplist2[-2])
        ans2 += abs(tmplist[-1] - tmplist2[-2])
        ans2 += abs(tmplist[-2] - tmplist2[-1])
    #print(ans2)
    print(max(ans, ans2))
t118C()

Submission status: Runtime Error

Solution code:
def neighbor_sum(N, A):
    A_1 = [] 
    A_2 = []
    i = 0
    j = N-1
    change = True
    while i < j:
        if change == True:
            A_1.append(A[i])
            A_2.append(A[j])
            i += 1
            j -= 1
            change = False
        else:
            A_1.append(A[j])
            A_2.append(A[i])
            i += 1
            j -= 1
            change = True
        return A_1[::-1] + A_2[:]

if __name__ == "__main__":
    N = int(input())
    A = [int(input()) for _ in range(N)]
    A.sort()
    diff = neighbor_sum(N, A)
    diff_sum = [abs(diff[i+1] - diff[i]) for i in range(N-1)]
    print(sum(diff_sum))

Submission status: Runtime Error

Solution code:
def t118C():
    N = int(input())
    A = [int(input()) for i in range(N)]
    A.sort()
    anslist = []
    ans = 0
    tmplist = []
    tmplist2 = []

    up = 0
    down = N - 1
    checklist = [0 for i in range(N)]
    while True:
        if checklist[up] == 1:
            break
        tmplist.append(up)
        checklist[up] = 1
        up += 1

        if checklist[down] == 1:
            break
        tmplist2.append(down)
        checklist[down] = 1
        down -= 1

        if checklist[down] == 1:
            break
        tmplist.append(down)
        checklist[down] = 1
        down -= 1

        if checklist[up] == 1:
            break
        tmplist2.append(up)
        checklist[up] = 1
        up += 1

    for i in range(len(tmplist)):
        tmplist[i] = A[tmplist[i]]

    for i in range(len(tmplist2)):
        tmplist2[i] = A[tmplist2[i]]

    #print(tmplist, tmplist2)
    for i in range(1, len(tmplist)):
        ans += abs(tmplist[i] - tmplist[i - 1])
    for i in range(1, len(tmplist2)):
        ans += abs(tmplist2[i] - tmplist2[i - 1])
    ans += abs(tmplist[0] - tmplist2[0])
    ans2 = ans
    ans2 -= abs(tmplist[-1] - tmplist[-2])
    ans2 -= abs(tmplist2[-1] - tmplist2[-2])
    ans2 += abs(tmplist[-1] - tmplist2[-2])
    ans2 += abs(tmplist[-2] - tmplist2[-1])
    #print(ans2)
    print(max(ans, ans2))
t118C()

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
B = sorted(A, reverse=False)
A.sort(reverse=True)


l_in = 0
r_in = len(A) - 1
ans = (A[l_in] - A[r_in]) + (A[l_in] - A[r_in-1])
hashi = [A[r_in], A[r_in-1]]
l_in += 1
r_in -= 2
while l_in != r_in and N != 3:
    l_max = max(abs(A[l_in] - hashi[0]), abs(A[l_in] - hashi[1]))
    r_max = max(abs(A[r_in] - hashi[0]), abs(A[r_in] - hashi[1]))
    if l_max > r_max:
        if abs(A[l_in] - hashi[0]) > abs(A[l_in] - hashi[1]):
            ans += abs(A[l_in] - hashi[0])
            hashi[0] = A[l_in]
        else:
            ans += abs(A[l_in] - hashi[1])
            hashi[1] = A[l_in]
        l_in += 1
    else:
        if abs(A[r_in] - hashi[0]) > abs(A[r_in] - hashi[1]):
            ans += abs(A[r_in] - hashi[0])
            hashi[0] = A[r_in]
        else:
            ans += abs(A[r_in] - hashi[1])
            hashi[1] = A[r_in]
        r_in -= 1
# last
if l_in == r_in:
    if abs(A[l_in] - hashi[0]) > abs(A[l_in] - hashi[1]):
        ans += abs(A[l_in] - hashi[0])
        hashi[0] = A[l_in]
    else:
        ans += abs(A[l_in] - hashi[1])
        hashi[1] = A[l_in]


l_in = 0
r_in = len(B) - 1
ansb = abs(B[l_in] - B[r_in]) + abs(B[l_in] - B[r_in - 1])
hashi = [B[r_in], B[r_in - 1]]
l_in += 1
r_in -= 2
while l_in != r_in and N != 3:
    l_max = max(abs(B[l_in] - hashi[0]), abs(B[l_in] - hashi[1]))
    r_max = max(abs(B[r_in] - hashi[0]), abs(B[r_in] - hashi[1]))
    if l_max > r_max:
        if abs(B[l_in] - hashi[0]) > abs(B[l_in] - hashi[1]):
            ansb += abs(B[l_in] - hashi[0])
            hashi[0] = B[l_in]
        else:
            ansb += abs(B[l_in] - hashi[1])
            hashi[1] = B[l_in]
        l_in += 1
    else:
        if abs(B[r_in] - hashi[0]) > abs(B[r_in] - hashi[1]):
            ansb += abs(B[r_in] - hashi[0])
            hashi[0] = B[r_in]
        else:
            ansb += abs(B[r_in] - hashi[1])
            hashi[1] = B[r_in]
        r_in -= 1
# last
if l_in == r_in:
    if abs(B[l_in] - hashi[0]) > abs(B[l_in] - hashi[1]):
        ansb += abs(B[l_in] - hashi[0])
        hashi[0] = B[l_in]
    else:
        ansb += abs(B[l_in] - hashi[1])
        hashi[1] = B[l_in]

print(max(ans, ansb))

Submission status: Runtime Error

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

ans_list = [0] * len(A)
mid = len(A)//2
if len(A)%2 == 0:
    low = A[0:len(A)//2]
    high = sorted(A[len(A)//2:len(A)], reverse=True)
    mid -= 1
else:
    low = A[0:len(A)//2+1]
    high = sorted(A[len(A)//2+1:len(A)], reverse=True)

ans_list[mid] = low.pop(0)

if len(high) >= 2:
    left = mid - 1
    right = mid + 1
    for i,h in enumerate(high):
        if i % 2 == 0:
            ans_list[right] = h
            right += 2
        else: 
            ans_list[left] = h
            left -= 2
elif len(low) == 1:
    ans_list[0] = low[0]

left = mid - 2
right = mid + 2

if len(low) >= 2:
    for i,l in enumerate(low):
        if i % 2 == 0:
            ans_list[right] = l
            right += 2
        else:
            ans_list[left] = l
            left -= 2
elif len(high) == 1:
    ans_list[2] = high[0]

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

Submission status: Runtime Error

Solution code:
def solve(pos):
    start = pos
    ans = 0
    a = 0
    b = N - 1
    for i in range(N-1):
        if a==start:
            a += 1
        elif b==start:
            b -= 1
        if abs(A[a]-A[pos])>=abs(A[b]-A[pos]):
            ans += abs(A[a]-A[pos])
            pos = a
            a += 1
        else:
            ans += abs(A[b]-A[pos])
            pos = b
            b -= 1
        #print(ans, a, b)
    #print(ans)
    return ans

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

if N<=10000:
    pos_list = list(range(N))
else:
    M = (a+b)/2
    for i in range(N-1):
        if A[i]<=M and M<A[i+1]:
            break
    a = max(0, i - 500)
    b = min(N - 1, i + 500)
    pos_list = list(range(a, b+1))
    if pos_list[0]!=0:
        pos_list = [0] + pos_list
    if pos_list[-1]!= N - 1:
        pos_list.append(N-1)
#print(pos_list)

ans = 0
for pos in pos_list:
    tmp = solve(pos)
    ans = max(ans, tmp)
print(ans)

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)
for i in range(len(ans)):
    if i == 0:
        ans[int(len(ans)/2)] = min(list)
        list.pop(list.index(min(list)))
    elif i == 1:
        ans[int(len(ans))-1] = min(list)
        list.pop(list.index(min(list)))
    elif i == 2:
        ans[0] = min(list)
        list.pop(list.index(min(list)))
for i in range(len(ans)):
    if ans[i] == 0:
        ans[i] = max(list)
        list.pop(list.index(max(list)))
total = 0
for j in range(len(ans)-1):
    total += abs(ans[j] - ans[j+1])
print(total)