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: Wrong Answer

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

A.sort()
align = [0] * N
flag = 0
count = 1
first = int(N / 2)
bigPoint = N - 1
smallPoint = 1
result = 0

align[first] += A[0]

if len(A) % 2 != 0:
  for j in range(int(N / 2)):
    if flag == 0:
      align[first - count] += A[bigPoint]
      align[first + count] += A[bigPoint - 1]
      if first - count == 0:
        break
      bigPoint -= 2
      count += 1
      flag = 1
    else:
      align[first - count] += A[smallPoint]
      align[first + count] += A[smallPoint + 1]
      if first - count == 0:
        break
      smallPoint += 2
      count += 1
      flag = 0
else:
  for k in range(int(N / 2)):
    if flag == 0:
      align[first - count] += A[bigPoint]
      if first - count == 0:
        break
      align[first + count] += A[bigPoint - 1]
      bigPoint -= 2
      count += 1
      flag = 1
    else:
      align[first - count] += A[smallPoint]
      if first - count == 0:
        break
      align[first + count] += A[smallPoint + 1]
      smallPoint += 2
      count += 1
      flag = 0
index = 0
while flag != 2:
  result += abs(align[index] - align[index + 1])
  if index + 1 == len(align) - 1:
    flag = 2
  else:
    index += 1  
      

print(result)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = []
flag=0
ans=0
anslist=[]
se=0
for i in range(N):
    A.append(int(input()))
A.sort()
if N%2==0:
    anslist.append(A[-1])
    del A[-1]
else:
    anslist.append(A[0])
    del A[0]
    setting=1
try:
    if N%2==0:
        while True:
            if flag==0:
                for i in range(2):
                    anslist.append(A[0])
                    anslist.reverse()
                    del A[0]
                flag=1
            else:
                for i in range(2):
                    anslist.append(A[-1])
                    anslist.reverse()
                    del A[-1]
                flag=0
    else:
        while True:
            if flag!=0:
                for i in range(2):
                    anslist.append(A[0])
                    anslist.reverse()
                    del A[0]
                flag=1
            else:
                for i in range(2):
                    anslist.append(A[-1])
                    anslist.reverse()
                    del A[-1]
                flag=0


except Exception as e:
    #print (anslist)
    flag=0
    while flag<N-1:
            ans=abs(anslist[flag]-anslist[flag+1])+ans
            flag=flag+1
    print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
num = [int(input()) for i in range(N)] 
num.sort(reverse=True) #降順

ans=0

if N % 2 == 0: #偶数の時　小大小大
  n = int(N/2)
  large = num[0:n]
  small = sorted(num[n:N]) #昇順
  if n >1:
    ans = sum(large) + sum(large[:n-1]) - sum(small) - sum(small[0:n-1])
  else:
    ans = sum(large) - sum(small)

else: #奇数の時　小大小大小
  n = int((N-1)/2)
  large = num[0:n]
  small = sorted(num[n:N])
  if n >1:
    ans = sum(large) *2 - sum(small) - sum(small[0:n-1])
  else:
    ans = sum(large) *2 - sum(small)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
iN = int(input())

aA = sorted([int(_) for _ in sys.stdin.readlines()])
aInvA = list(reversed(aA))

iDiff = aInvA[0] - aA[0]
for i in range(1,iN//2 + 1 ):
    D1 = aInvA[i] - aA[i-1]
    D2 = aInvA[i-1] - aA[i]
    if D1 :
        iDiff += D1
    if D2:
        iDiff += D2

print(iDiff)

Submission status: Wrong Answer

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

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

if n%2==1:
  k=int((n-1)/2)
  l=int(n/2)
  b[k]=a[0]
  for i in range(l):
    if (k-2*i-1)>=0:
      b[k-2*i-1]=a[-1-2*i]
    if (k+2*i+1)<=n-1:
      b[k+2*i+1]=a[-2-2*i]
    if (k-2*i-2)>=0:
      b[k-2*i-2]=a[2*i+1]
    if (k+2+2*i)<=n-1:
      b[k+2+2*i]=a[2*i+2]
else:
  k=int(n/2)-1
  b[k]=a[0]
  b[k+1]=a[-1]
  for i in range(k):
    if i%2==0:
      if (k+2+2*i)<=n-1:
        b[k+2+2*i]=a[1+2*1]
      if (k-1-2*i)>=0:
        b[k-1-2*i]=a[-2-2*i]
    else:
      if (k-2-2*i)>=0:
        b[k-2-2*i]=a[2+2*1]
      if (k+1+2*i)<=n-1:
        b[k+1+2*1]=a[-3-2*i]
sum=0      
for i in range(n-1):
  sum=sum+abs(b[i]-b[i+1])

print(sum)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

A.sort()
difference = 0
cnt = 0
flag = 0
while A:
    cnt += 1
    if N % 2 == 1:
        if cnt % 2 == 1 and flag == 0:
            A_min = min(A)
            A.remove(A_min)
            A_max1 = A[-1]
            A_max2 = A[-2]
            difference += A_max1 - A_min
            difference += A_max2 - A_min
            if N == 3:
                break
        else:
            A_max = max(A)
            A.remove(A_max)
            A_min = min(A)
            A.remove(A_min)
            difference += A_max - A_min
            flag += 1
            if flag == 2:
                flag = 0
    else:
        if cnt % 2 == 1 and flag == 0:
            A_min = min(A)
            A_max = max(A)
            difference += A_max - A_min
            if N == 2:
                break
        else:
            A_min2 = A[1]
            difference += A_max - A_min2
            A.remove(A_max)
            A_max2 = max(A)
            difference += A_max2 - A_min
            A.remove(A_min)
            flag += 1
            A_max = A_max2
            A_min = A_min2
            if flag == 2:
                flag = 0

print(difference)

Submission status: Wrong Answer

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

if n%2 != 0:
        m = int(n/2)
        x = 0
        y = 0
        z = max(a[2*m]-a[m], a[m]-a[0])
        for i in range(m):
                x += (a[2*m-i] - a[i])
        for i in range(m-1):
                y += (a[2*m-1-i] - a[i])
        print(x+y+z)
elif n%2 == 0:
        m = int(n/2)
        x = 0
        y = 0
        for i in range(m-1):
                x += (a[2*m-1-i] - a[i])
        for i in range(m-2):
                y += (a[2*m-2-i] - a[i])
        print(x+y+a[m]-a[m-2]+a[2*m-1]-a[m-1])

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
num = [int(input()) for i in range(N)] 
num.sort(reverse=True) #降順

ans=0

if N % 2 == 0: #偶数の時　小大小大
  n = int(N/2)
  large = num[0:n]
  small = sorted(num[n:N]) #昇順
  if n >1:
    ans = sum(large) + sum(large[:n-1]) - sum(small) - sum(small[1:])
  else:
    ans = sum(large) - sum(small)

else: #奇数の時　小大小大小
  n = int((N-1)/2)
  large = num[0:n]
  small = sorted(num[n:N])
  if n >1:
    ans = sum(large) *2 - sum(small) - sum(small[0:n-1])
  else:
    ans = sum(large) *2 - sum(small)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

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

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

Submission status: Wrong Answer

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

A = sorted(A)
res = 0
A2 = []
for i in range(len(A)):
    if len(A) == 1:
        A2.insert(0,A[0])
        break
    if i % 2 == 0:
        A2.append(A.pop(-1))
    else:
        A2.append(A.pop(0))
#print(A2)
for i in range(len(A2)-1):
    res += abs(A2[i] - A2[i+1])

print(res)

Submission status: Wrong Answer

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

A.sort()

B = [0 for _ in range(N // 2)]
C = [0 for _ in range(N // 2)]
for i in range(N // 2):
    B[i] = A[i]
for i in range(N // 2):
    C[i] = A[i + ((N + 1) // 2)]

D1 = [0 for _ in range(N)]
D2 = [0 for _ in range(N)]
if N % 2 == 1:
    D1[-1] = A[N // 2]
    D2[-1] = A[N // 2]
    N = N - 1

B2 = B.copy()
C2 = C.copy()

i = 0
while i < N:
    if i % 2 == 1:
        D1[i] = C.pop()
    else:
        D1[i] = B.pop()
    i += 1

i = 0
while i < N:
    if i % 2 == 1:
        D2[i] = B2.pop()
    else:
        D2[i] = C2.pop()
    i += 1

result1 = 0
result2 = 0
for i in range(len(D1) - 1):
    result1 += abs(D1[i + 1] - D1[i])
    result2 += abs(D2[i + 1] - D2[i])

print(max(result1, result2))

Submission status: Wrong Answer

Solution code:
def main():

    def diffarray(arr):
        sum = 0
        tmp = arr[0]
        for digit in arr[1:]:
            sum += abs(tmp - digit)
            tmp = digit
        return sum

    def insert(arr,max):
        #一番後ろからとって比較
        cur = arr[-1]
        arr_ = arr[:-1]
        retarr = arr

        basediff = diffarray(arr_)

        #print(arr)
        for i in range(len(arr_)):
            newarr = arr_[:i] + [cur] + arr_[i:]
            tmpdiff = 0
            if i != 0:
                tmpdiff = basediff + abs(cur - arr_[i-1]) - abs(arr[i-1]-arr[i]) + abs(cur - arr_[i])
            else:
                tmpdiff = basediff + abs(cur - arr_[i])

            if max < tmpdiff:
                max = tmpdiff
                retarr = newarr
            #print(max,i,tmpdiff,basediff)

        return max, retarr

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

    max_diff = diffarray(arr)


    for i in range(N):
        max_diff, arr = insert(arr,max_diff)


    print(max_diff)



    return


main()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = []
flag=0
ans=0
anslist=[]
se=0
for i in range(N):
    A.append(int(input()))
A.sort()
if N%2==0:
    anslist.append(A[-1])
    del A[-1]
else:
    anslist.append(A[0])
    del A[0]
    se=1
try:
    if se==0:
        while True:
            if flag==0:
                for i in range(2):
                    anslist.append(A[0])
                    anslist.reverse()
                    del A[0]
                flag=1
            else:
                for i in range(2):
                    anslist.append(A[-1])
                    anslist.reverse()
                    del A[-1]
                flag=0
    else:
        while True:
            if flag!=0:
                for i in range(2):
                    anslist.append(A[0])
                    anslist.reverse()
                    del A[0]
                flag=1
            else:
                for i in range(2):
                    anslist.append(A[-1])
                    anslist.reverse()
                    del A[-1]
                flag=0
 
 
except Exception as e:
    #print (anslist)
    flag=0
    while N-1>flag:
        ans=abs(anslist[flag]-anslist[flag+1])+ans
        flag=flag+1
    
    print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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 len(tmplist) >= 2 and len(tmplist2) >= 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])
    if N == 3:
        ans2 = 0
        ans2 += abs(A[0] - A[2])
        ans2 += abs(A[2] - A[1])
    #print(ans, ans2)
    print(max(ans, ans2))
t118C()

Submission status: Wrong Answer

Solution code:
def cul(low, high):
    high = high[::-1]
    lists = []
    if len(low) > len(high):
        low.insert(0, low[-1])
        low = low[:-1]
        lists.append(low[0])
        for _l, _h in zip(low[1:], high):
            lists.append(_h)
            lists.append(_l)
    elif len(low) < len(high):
        high.insert(0, high[-1])
        high = high[:-1]
        lists.append(high[0])
        for _l, _h in zip(low, high[1:]):
            lists.append(_l)
            lists.append(_h)
    else:
        for _l, _h in zip(low, high):
            lists.append(_l)
            lists.append(_h)
    print(lists)
    ans = 0
    for _p, _n in zip(lists, lists[1:]):
        ans += abs(_n - _p)
    return ans


n = int(input())
a = sorted([int(input()) for _ in range(n)])
if len(a) % 2 == 1:
    lower = a[:len(a) // 2 + 1]
    higher = a[len(a) // 2 + 1:]
    first = cul(lower, higher)
    lower = a[:len(a) // 2]
    higher = a[len(a) // 2:]
    second = cul(lower, higher)
    print(max(first, second))
else:
    lower = a[:len(a) // 2]
    higher = a[len(a) // 2:]
    print(cul(lower, higher))

Submission status: Wrong Answer

Solution code:
a=int(input())
b=[]
c=[]
for i in range(a):
    aa=int(input())
    b.append(aa)
    c.append(aa)
b.sort()
c.sort()
c.reverse()

d=[]
if a==3:
    d.append(b[2])
    d.append(b[0])
    d.append(b[1])
else:
    for i in range(a):
        if i==0:
            if a%2==0:
                d.append(b[int(a/2-1)])
            else:
                d.append(b[int((a-1)/2)])
        elif i%2==0:
            d.append(b[int(i/2-1)])
        elif i%2==1:
            d.append(c[int((i-1)/2)])
ans=0
for i in range(a-1):
    pre=d[i+1]-d[i]
    if pre < 0:
        pre=int(pre*-1)
    ans += pre
print(ans)

Submission status: Wrong Answer

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)]

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]]

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

print(S)

Submission status: Wrong Answer

Solution code:
a=int(input())
b=[]
c=[]
for i in range(a):
    aa=int(input())
    b.append(aa)
    c.append(aa)
b.sort()
c.sort()
c.reverse()

d=[]
if a==3:
    d.append(b[2])
    d.append(b[0])
    d.append(b[1])
else:
    for i in range(a):
        if i==0:
            if a%2==0:
                d.append(b[int(a/2-1)])
            else:
                d.append(b[int((a-1)/2)])
        elif i%2==0:
            d.append(b[int(i/2-1)])
        elif i%2==1:
            d.append(c[int((i-1)/2)])
print(d)
ans=0
for i in range(a-1):
    pre=d[i+1]-d[i]
    if pre < 0:
        pre=int(pre*-1)
    ans += pre
print(ans)

Submission status: Wrong Answer

Solution code:
import collections

N = int(input())

A = [0] * N
for i in range(N):
    A[i] = int(input())


A = sorted(A)

if N % 2 == 1:
    d1 = A[0:N // 2 + 1]
    u1 = A[N // 2 + 1:N + 1]
else:
    d1 = A[0:N // 2]
    u1 = A[N // 2:N + 1]

d1 = collections.deque(d1)
u1 = collections.deque(u1)
d1.rotate(1)
u1.rotate(-1)

sum1 = 0
i = 0
while (True):
    # print("{},{},{}".format(i, len(d1), len(u1)))
    if i >= len(d1) or i >= len(u1):
        break
    sum1 += abs(u1[i] - d1[i])

    if i+1 >= len(d1) or i >= len(u1):
        break
    sum1 += abs(u1[i] - d1[i + 1])
    i += 1

sum2 = 0
i = 0
while (True):
    # print("{},{},{}".format(i, len(d1), len(u1)))
    if i >= len(d1) or i >= len(u1):
        break
    sum2 += abs(u1[i] - d1[i])

    if i >= len(d1) or i+1 >= len(u1):
        break
    sum2 += abs(u1[i+1] - d1[i])
    i += 1


print(max(sum1, sum2))

Submission status: Wrong Answer

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 len(tmplist) >= 2 and len(tmplist2) >= 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: Wrong Answer

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

Submission status: Wrong Answer

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]
else:
    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]

print(ans)

Submission status: Wrong Answer

Solution code:
n,*a=map(int,open(0).read().split())
if n<3:
    print(abs(a[0]-a[1]))
    exit()
b=sorted(a)
d=b[::-1]
c=abs(b[0]-b[-1])+abs(b[0]-b[-2])
e=abs(d[0]-d[-1])+abs(d[0]-d[-2])
x,y=b[-1],b[-2]
v,w=d[-1],d[-2]
del b[-1],b[-1],b[0]
del d[-1],d[-1],d[0]
while b:
    if len(b)<2:
        c+=max(abs(x-b[0]),abs(y-b[0]))
        e+=max(abs(v-d[0]),abs(w-d[0]))
        del b[0]
        del d[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]
        if min(v,w)>=d[-1]:
            e+=max(abs(v-d[0])+abs(w-d[1]),abs(v-d[1])+abs(w-d[0]))
            v,w=d[0],d[1]
            del d[0],d[0]
        else:
            e+=max(abs(v-d[-1])+abs(w-d[-2]),abs(v-d[-2])+abs(w-d[-1]))
            v,w=d[-1],d[-2]
            del d[-1],d[-1]
print(max(c,e))

Submission status: Wrong Answer

Solution code:
def subs(l):
    ans = 0
    for _p, _n in zip(l, l[1:]):
        ans += abs(_n - _p)
    return ans


def cul(low, high):
    low.insert(0, lower[-1])
    low = low[:-1]
    high = high[::-1]
    lists = []
    if len(low) > len(high):
        lists.append(low[0])
        for _l, _h in zip(low[1:], high):
            lists.append(_h)
            lists.append(_l)
        return subs(lists)
    elif len(low) < len(high):
        lists.append(high[0])
        for _l, _h in zip(low, high[1:]):
            lists.append(_l)
            lists.append(_h)
        return subs(lists)
    else:
        for _l, _h in zip(low, high):
            lists.append(_l)
            lists.append(_h)
        first = subs(lists)
        lists = []
        for _l, _h in zip(low, high):
            lists.append(_l)
            lists.append(_h)
        second = subs(lists)
        return max(first, second)


n = int(input())
a = sorted([int(input()) for _ in range(n)])
if len(a) % 2 == 1:
    lower = a[:len(a) // 2 + 1]
    higher = a[len(a) // 2 + 1:]
    first = cul(lower, higher)
    lower = a[:len(a) // 2]
    higher = a[len(a) // 2:]
    second = cul(lower, higher)
    print(max(first, second))
else:
    lower = a[:len(a) // 2]
    higher = a[len(a) // 2:]
    print(cul(lower, higher))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [0] * n
b = []
for i in range(n):
    a[i] = int(input())
a.sort()
b.append(a.pop(0))
cnt = 0
res = 0
while True:
    if cnt % 2 == 0:
        b.insert(0, a.pop(-1))
        res += abs(b[0] - b[1])
        if len(a) == 0:
            break
        else:
            b.append(a.pop(-1))
            res += abs(b[-1] - b[-2])
    else:
        b.insert(0, a.pop(0))
        res += abs(b[0] - b[1])
        if len(a) == 0:
            break
        else:
            b.append(a.pop(0))
            res += abs(b[-1] - b[-2])
    if len(a) == 0:
        break
    cnt += 1
print(res)

Submission status: Wrong Answer

Solution code:
#print(tmp)#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]
    # print(A_min_list)
    A_max_list = A_list[-int(N/2)+1:]
    # print(A_max_list)
    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

    # print(answer_list)
    tmp = calculate_x(answer_list)
    print(tmp)

else: #odd
    answer_list = []
    A_min_list = A_list[:int((N-1)/2)-1] #0,1
    # print(A_min_list)
    A_max_list = A_list[-int((N-1)/2)+1:] #5,6
    # print(A_max_list)
    #6 patternどれか分からないので総当りする。小さい順に名前をつける。
    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)
    #print(tmp)
    #print(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)
    #print(tmp)
    #print(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)
    #print(tmp)
    #print(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)
    #print(tmp)
    #print(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)
    #print(tmp)
    #print(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)
    #print(tmp)
    #print(answer_list2)
    if tmp > max:
        max = tmp
    # print(max)



    answer_list3 = []
    A_min_list = A_list[:int((N-1)/2)-1] #0,1
    # print(A_min_list)
    A_max_list = A_list[-int((N-1)/2)+1:] #5,6
    # print(A_max_list)
    #6 patternどれか分からないので総当りする。小さい順に名前をつける。
    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)
    #print(tmp)
    #print(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)
    #print(tmp)
    #print(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)
    #print(tmp)
    #print(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)
    #print(tmp)
    #print(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)
    #print(tmp)
    #print(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)
    # print(tmp)
    #print(answer_list4)
    if tmp > max:
        max = tmp
    print(max)

Submission status: Wrong Answer

Solution code:
import math


def main():
    N = int(input())
    A = [int(input()) for j in range(N)]
    max = 0
    if len(A) == 2:
        print(math.fabs(A[0] - A[1]))
    elif len(A) == 3:
        A.sort()
        print(int(math.fabs(A[0] - A[1]) + math.fabs(A[0] - A[2])))
    else:
        if len(A) % 2 == 0:
            div = len(A) // 2
            A.sort()
            Af = A[0:div]
            Al = A[div:]
            Af.sort(reverse=True)
            Al.sort(reverse=True)
            result = []
            for i in range(len(Af)):
                result.append(Af[i])
                result.append(Al[i])
            print(int(calc(result)))
        else:
            div = (len(A) // 2) + 1
            A.sort()
            Af = A[0:div]
            Al = A[div:]
            Af.sort(reverse=True)
            Al.sort(reverse=True)

            Af[1], Af[-1] = Af[-1], Af[1]

            result = []
            for i in range(len(Al)):
                result.append(Af[i])
                result.append(Al[i])
            result.append(Af[-1])
            print(int(calc(result)))


def calc(A):
    result = 0
    for i in range(len(A)-1):
        result += math.fabs(A[i] - A[i+1])
    return result


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())

    num_l = list()
    for _ in range(n):
        num_l += [int(input())]

    num_l = sorted(num_l)

    num_1 = num_l[0]
    num_2 = num_l[1]
    num_3 = num_l[len(num_l)-2]
    num_4 = num_l[len(num_l)-1]

    if (num_3 - num_1) + (num_4 - num_1) > (num_4 - num_1) + (num_4 - num_2):
        f = num_l.pop(0)
        s = f
        flag = True
    else:
        f = num_l.pop(len(num_l)-1)
        s = f
        flag = False
    max_num = 0
    while(num_l):
        if len(num_l) == 1:
            if abs(f - num_l[0]) > abs(s - num_l[0]):
                max_num += abs(f - num_l[0])
            else:
                max_num += abs(s - num_l[0])
            break

        if flag:
            tmp_f = num_l.pop(len(num_l)-1)
            tmp_s = num_l.pop(len(num_l)-1)
        else:
            tmp_f = num_l.pop(0)
            tmp_s = num_l.pop(0)

        if abs(f - tmp_f) + abs(s - tmp_s) > abs(s - tmp_f) + abs(f - tmp_s):
            max_num += abs(f - tmp_f) + abs(s - tmp_s)
            f = tmp_f
            s = tmp_s
        else:
            max_num += abs(s - tmp_f) + abs(f - tmp_s)
            f = tmp_s
            s = tmp_f

        flag = not flag

    print(max_num)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

if n%2==1:
  k=int((n-1)/2)
  l=int(k/2)
  b[k]=a[0]
  for i in range(l):
    if (k-2*i-1)>=0:
      b[k-2*i-1]=a[-1-2*i]
    if (k+2*i+1)<=n-1:
      b[k+2*i+1]=a[-2-2*i]
    if (k-2*i-2)>=0:
      b[k-2*i-2]=a[2*i+1]
    if (k+2+2*i)<=n-1:
      b[k+2+2*i]=a[2*i+2]
else:
  k=int(n/2)-1
  b[k]=a[0]
  b[k+1]=a[-1]
  for i in range(k):
    if i%2==0:
      if (k+2+2*i)<=n-1:
        b[k+2+2*i]=a[1+2*1]
      if (k-1-2*i)>=0:
        b[k-1-2*i]=a[-2-2*i]
    else:
      if (k-2-2*i)>=0:
        b[k-2-2*i]=a[2+2*1]
      if (k+1+2*i)<=n-1:
        b[k+1+2*1]=a[-3-2*i]
sum=0      
for i in range(n-1):
  sum=sum+abs(b[i]-b[i+1])
print(sum)

Submission status: Wrong Answer

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

if n%2 != 0:
        m = int(n/2)
        x = 0
        y = 0
        z = max(a[2*m]-a[m], a[m]-a[0])
        for i in range(m):
                x += (a[2*m-i] - a[i])
        for i in range(m-1):
                y += (a[2*m-1-i] - a[i])
        print(x+y+z)
elif n%2 == 0:
        m = int(n/2)
        x = 0
        y = 0
        for i in range(m-1):
                x += (a[2*m-1-i] - a[i])
        for i in range(m-2):
                y += (a[2*m-2-i] - a[i])
        print(x+y+a[m]-a[m-2]+a[2*m-1]-a[m-1])