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

f=1
if len(a)==2:
    print(abs(a[0]-a[1]))
    f=0

while 1 and f:
    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: Wrong Answer

Solution code:
n = int(input())
aa = [int(input()) for i in range(0, n)]
aa.sort()
ana = [0] * n
ana[0] = aa[n//2-1]
ana[n-1] = aa[n//2]
for i in range(0, (n//2)-1):
    ana[2+2*i] = aa[i]
    ana[1+2*i] = aa[i + (n//2)+1]
if n % 2 != 0:
    ana[n-2] = aa[n-1]

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

print(a)

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

    if abs(num_l[0] - num_l[len(num_l)-1]) + abs(num_l[0] - num_l[len(num_l)-2]) > abs(num_l[len(num_l)-1] - num_l[0]) + abs(num_l[len(num_l)-1] - num_l[1]):
        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_s = num_l.pop(0)
            tmp_f = 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)
        else:
            max_num += abs(s - tmp_f) + abs(f - tmp_s)

        f = tmp_f
        s = tmp_s

        flag = not flag

    print(max_num)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())

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

a.sort()
ans = 0

if n % 2 == 1:
    b = a[0]*2
    a.pop(0)
    c = 0
    for i in range(n//2):
        if i % 2 == 0:
            c += a.pop(-1)
            c += a.pop(-1)
            ans += abs(b-c)
            b = c
            c = 0
        else:
            c += a.pop(0)
            c += a.pop(0)
            ans += abs(b-c)
            b = c
            c = 0
elif n == 2:
    ans = abs(a[0]-a[1])
else:
    b = a[0]*2
    a.pop(0)
    c = 0
    d = 0
    e = 0
    for i in range(n//2-1):
        if i % 2 == 0:
            if i == n//2 -2:
                d = a[-1]
                e = a[-2]
            c += a.pop(-1)
            c += a.pop(-1)
            ans += abs(b-c)
            b = c
            c = 0
        else:
            if i == n//2 -2:
                d = a[0]
                e = a[1]
            c += a.pop(0)
            c += a.pop(0)
            ans += abs(b-c)
            b = c
            c = 0
    ans += max(abs(d-a[0]),abs(e-a[0]))

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
N = input()
A = [1,3,5,7]


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

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 - 1]
    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 += ta
        a1 = a[-1]
        del a[-1]
    else:
        ans += ua
        a1 = a[0]
        del a[0]

print(ans)

Submission status: Wrong Answer

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

a = 0
b = -1
ans = 0
last = 0
for i in range(N-2):
    ans += (A[a] - A[b])
    if i % 2 == 0:
        a += 1
        last = A[a]
    else:
        b -= 1
        last = A[b]
if A[0] - last > last - A[-1]:
    ans += (A[0] - last)
else:
    ans += (last - A[-1])
if N == 2:
    ans = A[0] - A[-1]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys,collections,math;sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

n = I()
ls = []
for i in range(n):
    ls.append(I())
ls.sort()
a,b = collections.deque(ls),collections.deque()
b.append(a.popleft())
while a:
    if a:b.appendleft(a.pop())
    if a:b.append(a.pop())
    if a:b.appendleft(a.popleft())
    if a:b.append(a.popleft())
t,ans = b.popleft(),0
for e in b:
    ans += abs(t-e)
    t = e
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(input()) for i in range(n)]
a.sort()
x=[]
y=[]
c=0
if n%2==1:
  for j in range(n//2):
    x.append(a[j])
  for k in range((n//2)+1,n):
    y.append(a[k])
else:
  for j in range((n//2)-1):
    x.append(a[j])
  for k in range((n//2),n):
    y.append(a[k])
y.reverse()
if n%2==1:
  for l in range(len(x)):
    c+=abs(x[l]-y[l])
  for m in range(len(x)-1):
    c+=abs(x[m]-y[m+1])
else:
  for l in range(len(x)):
    c+=abs(x[l]-y[l])
  for m in range(len(x)):
    c+=abs(x[m]-y[m+1])
if n%2==1:
  c+=max(abs(a[n//2]-y[0]),abs(a[n//2]-x[len(x)-1]))
else:
  c+=max(abs(a[(n//2)-1]-y[0]),abs(a[(n//2)-1]-y[len(y)-1]))
print(c)

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
a.sort()
ans = 0
l=0
r=0
if(len(a)%2==1):
    a1=a[int(len(a)/2)]
else:
    a1=a[int(len(a)/2)-1]
    a3=a[int(len(a)/2)]
if(len(a)%2==1):
    if(abs(a1-a[0])>abs(a1-a[-1])):
        for i in range(n-1):
            if(i%2==0):
                a2=a[l]
                r+=1
            else:
                a2=a[-(r+1)]
                l+=1
            ans+=abs(a1-a2)
            a1=a2
    else:
        for i in range(n-1):
            if(i%2==0):
                a2=a[-(r+1)]
                r+=1
            else:
                a2=a[l]
                l+=1
            ans+=abs(a1-a2)
            a1=a2
else:
    if(abs(a3-a[0])>abs(a1-a[-1])):
        a1=a3
        for i in range(n-1):
            if(i%2==0):
                a2=a[l]
                r+=1
            else:
                a2=a[-(r+1)]
                l+=1
            ans+=abs(a1-a2)
            a1=a2
    else:
        for i in range(n-1):
            if(i%2==0):
                a2=a[-(r+1)]
                r+=1
            else:
                a2=a[l]
                l+=1
            ans+=abs(a1-a2)
            a1=a2

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = []
for i in range(n):
    A.append(int(input()))
A.sort()
ans = 0
t = [A[0], A[0]]
for i in range(n):
    if (i%2==0):
        u = A[i//2]
    else:
        u = A[-1-i//2]
    if (abs(u-t[0])>abs(u-t[1])):
        ans+=abs(u-t[0])
        t[0] = u
    else:
        ans+=abs(u-t[1])
        t[1] = u        
print(ans)

Submission status: Wrong Answer

Solution code:
n =  int(input())
A = [0] * n
for i in range(n):
  A[i] = int(input())
A.sort()
s = A[n-1] - A[0]
if(n % 2 == 0):
  for i in range(n//2 - 1):
    s += A[n-i-1] - A[i+1]
    s += A[n-i-2] - A[i]
else:
  if(n % 4 == 1):
    for i in range(n//2 - 1):
      s += A[n-i-1] - A[i+1]
      s += A[n-i-2] - A[i]
    s += A[n//2+1] - A[n//2]
  else:
    for i in range(n//2 - 1):
      s += A[n-i-1] - A[i+1]
      s += A[n-i-2] - A[i]
    s += A[n//2] - A[n//2-1]
print(s)

Submission status: Wrong Answer

Solution code:
N=input()
N=int(N)
L=[]

def diff(L):
    total=0
    for i in range(len(L)-1):
        total+=abs(L[i+1]-L[i])
    return total
for i in range(N):
    A=input()
    L.append(int(A))
L.sort()
if len(L)>3:
    length=len(L)
    SL=L[:int(length/2)]
    LL=L[int(length/2):]
    result=[]
    result.append(SL[-1])
    SL.pop(-1)
    last=LL[0]
    LL.pop(0)
    while True:
        try:
            result.append(LL[-1])
            LL.pop(-1)
            result.append(SL[0])
            SL.pop(0)
        except:
            break
    result.append(last)
    print(diff(result))
elif len(L)==3:
    all=[diff([L[0],L[1],L[2]]),diff([L[0],L[2],L[1]]),diff([L[1],L[0],L[2]]),diff([L[1],L[2],L[0]]),diff([L[2],L[0],L[1]]),diff([L[2],L[1],L[0]])]
    print(max(all))
elif len(L)==2:
    print(abs(L[0]-L[1]))

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

        for i in range(len(arr_)):
            newarr = arr_[:i] + [cur] + arr_[i:]
            tmpdiff = diffarray(newarr)
            if max < tmpdiff:
                max = tmpdiff
                retarr = newarr

        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:
import numpy as np
from math import ceil

n = int(input())
w = []
for i in range(n):
    w.append(int(input()))
w = sorted(w)
l_fh = ceil(n/2)
w_fh = w[:l_fh]
w_sh = w[l_fh:]

for i in range(ceil(len(w_fh)/2)):
    w_fh.append(w_fh.pop(0))

w = w_fh + w_sh
w_ = []
if len(w) == 3:
    w_ = w
else:
    for i in range(n):
        if i % 2 ==0:
            w_.append(w[0])
            w = w[1:]
        else:
            w_.append(w[-1])
            w = w[:-1]
res = np.abs(np.diff(np.array(w_))).sum()
print(res)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

a.sort()



if len(a) % 2 == 1:
    diff_low = a[int(len(a) / 2)] - a[int(len(a) / 2 - 1)]
    diff_high = a[int(len(a) / 2 + 1)] - a[int(len(a) / 2)]
    if diff_high > diff_low:
        high_group = a[int(len(a) / 2 + 1):]
        low_group = a[:int(len(a) / 2 + 1)]
        low_max = low_group[-1]
        low_group = low_group[:-1]
        if len(a) == 3:
            high_min = -low_group[0]
            low_group = []
        elif (low_group[-1] - low_group[-2]) > (high_group[1] - high_group[0]):
            high_min = high_group[0]
            high_group = high_group[1:]
        else:
            high_min = -low_group[-1]
            low_group = low_group[:-1]
    else:
        high_group = a[int(len(a) / 2):]
        low_group = a[:int(len(a) / 2)]
        high_min = high_group[0]
        high_group = high_group[1:]
        if len(a) == 3:
            low_max = -high_group[0]
            high_group = []
        elif (low_group[-1] - low_group[-2]) > (high_group[1] - high_group[0]):
            low_max = high_group[0]
            high_group = high_group[1:]
        else:
            low_max = low_group[-1]
            low_group = low_group[:-1]
else:
    high_group = a[int(len(a) / 2):]
    low_group = a[:int(len(a) / 2)]
    high_min = high_group[0]
    high_group = high_group[1:]
    low_max = low_group[-1]
    low_group = low_group[:-1]

high_sum = sum(high_group) * 2 + high_min
low_sum = sum(low_group) * 2 + low_max
print(high_sum - low_sum)

Submission status: Wrong Answer

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

    if len(a) % 2 == 0:
        low = a[:len(a) // 2]
        high = sorted(a[len(a) // 2:], reverse=True)
        return 2 * (sum(high) - sum(low)) + max(low) - min(high)
    else:
        low = a[:len(a) // 2]
        high = sorted(a[len(a) // 2:], reverse=True)
        return 2 * (sum(high) - sum(low)) - max(high) - min(high)


print(tpbc_2018_c())

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
A.sort()
ans1 = []
ans2 = []
p = -1
if(N%2 == 0):
    ans1.append(A.pop(N//2-1))
    while(len(A)>0):
        ans1.append(A.pop(p))
        p = -1 * (1+p)
    print(np.sum(np.abs(np.diff(ans1))))
else:
    ans1.append(A.pop((N-1)//2-1))
    for _ in range((N-1)//2):
        a = A.pop(0)
        b = A.pop(-1)
        ans1.append(a)
        ans1.append(b)
        ans2.append(b)
        ans2.append(a)
    print(max(np.sum(np.abs(np.diff(ans1))),np.sum(np.abs(np.diff(ans2)))))

Submission status: Wrong Answer

Solution code:
from sys import stdin
il = lambda func=int: list(map(func, stdin.readline().rstrip().split()))
ils = lambda n, s="int(input())": [eval(s) for _ in range(n)]

n = int(input())
a = ils(n)
a = sorted(a)
#A = a[::-1]
l = []
#L = []

l.append(a[0])
for i in range(n//2):
    if i % 2 == 0:
        l.append(a[-1-i])
        l.insert(0, a[-2-i])
    else:
        l.append(a[i])
        l.insert(0, a[i+1])

# L.append(A[0])
# for i in range(n//2):
#     if i % 2 == 0:
#         L.append(A[-1-i])
#         L.insert(0, A[-2-i])
#     else:
#         L.append(A[i])
#         L.insert(0, A[i+1])

ANS = 0
ans = 0

tmp = l[0]
for i in l:
    ans += abs(i - tmp)
    tmp = i
#tmp = L[0]
#for i in L:
#    ANS += abs(i - tmp)
#    tmp = i

print(max(ans, ANS))

Submission status: Wrong Answer

Solution code:
n = int(input())
aa = [int(input()) for i in range(n)]
sa=sorted(aa)
aaa=[]
for i in range(n):
    if i%2 ==0:
        aaa.append(sa.pop(0))
    else:
        aaa.append(sa.pop(-1))
len_a = len(aaa)
aaa.extend(aaa)
aaa=aaa[len_a-1:len(aaa)-1]
# print(aaa)
ans=0
for i in range(n-1):
    ans += abs(aaa[i]- aaa[i+1])
print(ans)

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:
        if abs(A[0] - A2[0]) < abs(A[0] - A2[-1]):
            A2.append(A[0])
        else:
            A2.insert(0,A[0])
        break
    if i % 2 == 0:
        A2.append(A.pop(-1))
    else:
        A2.append(A.pop(0))
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 = [int(input()) for i in range(N)]

A_sorted = sorted(A)
pem_A = []
#print(A_sorted)

pem_A.append(A_sorted.pop(0))#center
#while(True):
for i in range(N):
  try:
    pem_A.insert(0,A_sorted.pop(-1))#left
    pem_A.append(A_sorted.pop(-1))#right
    pem_A.insert(0,A_sorted.pop(0))#left
    pem_A.append(A_sorted.pop(0))#right
  except:
    break
#print(pem_A)

import numpy as np

ans = np.sum(abs(np.diff(pem_A)))
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

# 入力値受け取り
N = int(input())
a = [int(input()) for i in range(N)]

a.sort()
a.reverse()

answer = 0

a_ave = sum(a) // len(a)
b = a.pop(len(a) // 2)
a_len = len(a)

if b <= a_ave:
    for i in range(a_len):
        if i % 2 == 0:
            c = a.pop(0)
            answer += c - b
        else:
            c = a.pop()
            answer += b - c 
        b = c

else:
    for i in range(a_len):
        if i % 2 != 0:
            c = a.pop(0)
            answer += c - b
        else:
            c = a.pop()
            answer += b - c 
        b = c

# 結果出力
print("{}".format(answer))

Submission status: Wrong Answer

Solution code:
#tenka1 C
import numpy as np
N = int(input())
a = np.sort(np.array([int(input()) for i in range(N)]))

max_a = np.max(a)
min_a = np.min(a)

max_kazu = np.sum(a==max_a)
min_kazu = np.sum(a==min_a)

route = []

i = 0
while(len(a)>1):
    if (max_kazu >= min_kazu):
        if(i %2 == 0):
            route.append(a[-1])
            a = a[:-1]
        else:
            route.append(a[0])
            a = a[1:]
    else:
        if(i %2 == 1):
            route.append(a[-1])
            a = a[:-1]
        else:
            route.append(a[0])
            a = a[1:]
    i+=1
#route = route[-1::-1]
if (abs(route[-1]-a[0]) >= abs(route[0]-a[0])):
    route.append(a[0])
else:
    route.insert(0, a[0])


all_sum = 0
for i in range(len(route)):
    if i == len(route)-1:
        break
    all_sum+= abs(route[i+1]-route[i])

print(all_sum)

Submission status: Wrong Answer

Solution code:
n = int(input())
lis = []
for i in range(n):
  lis.append(int(input()))
lis.sort()
ans = []
num = 0
if n % 2 == 0:
  lismin = lis[:n//2]
  lismax = lis[n//2:]
  for i in range(n//2):
    ans.append(lismax[i])
    ans.append(lismin[i])
  for i in range(n-1):
    num += abs(ans[i+1] - ans[i])
  print(num)
else:
  ave = sum(lis)/n
  lismin = lis[:n//2]
  lismax = lis[n//2+1:]
  lismax = lismax[::-1]
  if lis[(n//2)] >= ave:
    lismax.append(lis[n//2])
    ans.append(lismax.pop((n//2)-1))
    for i in range(n//2):
      ans.append(lismin[i])
      ans.append(lismax[i])
    for i in range(n-1):
      num += abs(ans[i+1] - ans[i])
    print(num)   
  else:
    lismin.append(lis[n//2])
    ans.append(lismin.pop((n//2)-1))
    for i in range(n//2):
      ans.append(lismax[i])
      ans.append(lismin[i])
    for i in range(n-1):
      num += abs(ans[i+1] - ans[i])
    print(num)

Submission status: Wrong Answer

Solution code:
from sys import stdin
il = lambda func=int: list(map(func, stdin.readline().rstrip().split()))
ils = lambda n, s="int(input())": [eval(s) for _ in range(n)]

n = int(input())
a = ils(n)
a = sorted(a)
A = a[::-1]
l = []
L = []

l.append(a[0])
for i in range(n//2):
    if i % 2 == 0:
        l.append(a[-1-i])
        l.insert(0, a[-2-i])
    else:
        l.append(a[i])
        l.insert(0, a[i+1])

# L.append(A[0])
# for i in range(n//2):
#     if i % 2 == 0:
#         L.append(A[-1-i])
#         L.insert(0, A[-2-i])
#     else:
#         L.append(A[i])
#         L.insert(0, A[i+1])

print(l)
ANS = 0
ans = 0

tmp = l[0]
for i in l:
    ans += abs(i - tmp)
    tmp = i
#tmp = L[0]
for i in L:
    ANS += abs(i - tmp)
    tmp = i

print(max(ans, ANS))

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())
A = deque(sorted(int(input()) for _ in range(n)))
ans = deque([A.popleft()])
def f(x):
    cnt = 0
    for i in range(len(x) - 1):
        cnt += abs(x[i] - x[i + 1])
    return cnt
for i in range((n - 1) // 2):
    if i & 1:
        ans.appendleft(A.popleft())
        ans.append(A.popleft())
    else:
        ans.appendleft(A.pop())
        ans.append(A.pop())
if A:
    print(f(ans) + max(abs(ans[0] - A[0]), abs(ans[-1] - A[0])))
else:
    print(f(ans))

Submission status: Wrong Answer

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

A.sort()

if len(A) == 3:
    print(A[2] + A[1] - 2*A[0])
    exit()
elif len(A) == 2:
    print(A[1] - A[0])
    exit()

sumnum = 0
m = len(A)//2
if len(A) % 2 == 0:
    sumnum = 2 * sum(A[m+1:]) - 2 * sum(A[:m - 1])
    sumnum -= - A[m] + A[m - 1]
else:
    sumnum = 2 * sum(A[m+1:]) - 2 * sum(A[:m - 1])
    sumnum -= A[m] + A[m - 1]

print(sumnum)

Submission status: Wrong Answer

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
        # N == odd 
    if N % 2 == 1:
        
        if (A[N//2] - A[0]) < (A[N-1] - A[N//2]):
            if change == True:
                A_1.append(A[N//2])
            else:
                A_2.append(A[N//2])
        else:
            if change == True:
                A_2.append(A[N//2])
            else:
                A_1.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: Wrong Answer

Solution code:
from collections import deque

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

A.sort()
array = deque([A[0]])
a_len = 1
count = 0
idx = 0
result = 0

while a_len < N:
    if count % 2 == 0:
        array.appendleft(A[-idx-1])
        array.append(A[-idx-2])
    else:
        array.append(A[idx+1])
        array.appendleft(A[idx+2])
        idx += 2
    count += 1
    a_len += 2

for i in range(N-1):
    result += abs(array[i] - array[i+1])

print(result)

Submission status: Wrong Answer

Solution code:
#tenka1 C
import numpy as np
N = int(input())
a = np.sort(np.array([int(input()) for i in range(N)]))

max_a = np.max(a)
min_a = np.min(a)

max_kazu = np.sum(a==max_a)
min_kazu = np.sum(a==min_a)

route = []

i = 0
while(len(a)>1):
    if (max_kazu >= min_kazu):
        if(i %2 == 0):
            route.append(a[-1])
            a = a[:-1]
        else:
            route.append(a[0])
            a = a[1:]
    else:
        if(i %2 == 1):
            route.append(a[-1])
            a = a[:-1]
        else:
            route.append(a[0])
            a = a[1:]
    i+=1
#route = route[-1::-1]
if (max_kazu >= min_kazu):
    route.append(a[0])
else:
    route.insert(0, a[0])


all_sum = 0
for i in range(len(route)):
    if i == len(route)-1:
        break
    all_sum+= abs(route[i+1]-route[i])

print(all_sum, route)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [None] * N

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

A.sort()

R_0 = 0
R_1 = 0

for i in range(N):
    if i == 0:
        B_L = A[i]
    elif i == 1:
        B_R = A[-i]
        R_0 += B_R - B_L
    else:
        if i % 2 == 0:
            b = A[int(i/2)]
        else:
            b = A[-int((i+1)/2)]
        if abs(B_R - b) >= abs(B_L - b):
            R_0 += abs(B_R - b)
            B_R = b
        else:
            R_0 += abs(B_L - b)
            B_L = b

for i in range(N):
    if i == 0:
        B_L = A[i]
    elif i == 1:
        B_R = A[-i]
        R_1 += B_R - B_L
    else:
        if i % 2 == 0:
            b = A[int(i/2)]
        else:
            b = A[-int((i+1)/2)]
        if abs(B_R - b) > abs(B_L - b):
            R_1 += abs(B_R - b)
            B_R = b
        else:
            R_1 += abs(B_L - b)
            B_L = b

print(max(R_0, R_1))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [0]*N
result = []
for i in range(N):
 a[i] = int(input())
a = sorted(a)
if N%2 != 0:
 a_0 =a[0]
 a = sorted(a[1:N],key=lambda x: -x)
 result.append(a_0)
 for j in range(N-1):
  if j %2 ==0:
   result.insert(0,a[j])
  else:
   result.append(a[j])
else:
 a_sub = sorted(a,key=lambda x: -x)
 for j in range(N//2):
  if j %2 ==0:
   result.insert(0,a_sub[j])
   result.append(a[j])
  else:
   result.insert(0,a[j])
   result.append(a_sub[j])
result_sum = 0
for k in range(N-1):
 result_sum = result_sum + abs(result[k] - result[k+1])
print(result_sum)

Submission status: Wrong Answer

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

#num = 6
#num = 3
#a = [3,1,4,1,5,9]
#a= [5,5,1]
#num = 5
#a = [6,8,1,2,3]


a.sort()

b = [0 for i in range(len(a))]
c = [0 for i in range(len(a))]

if num%2 == 1:
    for i in range(len(a)//2+1):
        if i == 0:
            b[len(a)//2] = a[i]
        elif i%2 == 0:
            b[len(a)//2+i] = a[i]
            b[len(a)//2-i] = a[i-1]
        elif i%2 == 1:
            b[len(a)//2+i] = a[len(a)-i-1]
            b[len(a)//2-i] = a[len(a)-i]
        #print(b)
else:
    for i in range(len(a)//2):
        if i == 0:
            b[len(a)//2] = a[i]
        elif i%2 == 0:
            b[len(a)//2+i] = a[i]
            b[len(a)//2-i] = a[i-1]
        elif i%2 == 1:
            b[len(a)//2+i] = a[len(a)-i-1]
            b[len(a)//2-i] = a[len(a)-i]
    b[0] = a[len(a)-len(a)//2]

if num%2 == 1:
    for i in range(len(a)//2+1):
        if i == 0:
            c[len(a)//2] = a[len(a)-1]
        elif i%2 == 0:
            c[len(a)//2+i] = a[len(a)-i]
            c[len(a)//2-i] = a[len(a)-i-1]
        elif i%2 == 1:
            c[len(a)//2+i] = a[i-1]
            c[len(a)//2-i] = a[i]
        #print(c)
else:
    for i in range(len(a)//2):
        if i == 0:
            c[len(a)//2] = a[len(a)-1]
        elif i%2 == 0:
            c[len(a)//2+i] = a[len(a)-i-1]
            c[len(a)//2-i] = a[len(a)-i]
        elif i%2 == 1:
            c[len(a)//2+i] = a[i]
            c[len(a)//2-i] = a[i-1]
    c[0] = a[len(a)-len(a)//2-1]

sum = 0
sum1 = 0
for i in range(len(a)-1):
    sum = sum + abs(b[i]-b[i+1])
    sum1 = sum1 + abs(c[i]-c[i+1])
print(max(sum, sum1))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
A = []
for _ in range(N):
    A.append(int(input()))

As = sorted(A)
Ar = sorted(A)
Ar.reverse()
#
# print(As)
# print(Ar)

cent = (N+1)//2 - 1
ans = [0 for _ in range(N)]
for i in range(cent+1):
    if i == 0:
        ans[cent] = As[0]
    elif i % 2 == 1:
        ans[cent+i] = Ar[i-1]
        ans[cent-i] = Ar[i]
    else:
        ans[cent+i] = As[i-1]
        ans[cent-i] = As[i]

if N % 2 == 0:
    if cent % 2 == 0:
        ans[-1] = Ar[cent]
    else:
        ans[-1] = As[cent]

# print(ans)

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

print(diff)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [None] * N

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

A.sort()

R = 0

for i in range(N):
    if i == 0:
        B_L = A[i]
    elif i == 1:
        B_R = A[-i]
        R += B_R - B_L
    else:
        if i % 2 == 0:
            b = A[int(i/2)]
        else:
            b = A[-int((i+1)/2)]
        if abs(B_R - b) >= abs(B_L - b):
            R += abs(B_R - b)
            B_R = b
        else:
            R += abs(B_L - b)
            B_L = b

print(R)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
a.sort()
ans = 0
l=0
r=0
if(len(a)%2==1):
    a1=a[int(len(a)/2)]
else:
    a1=a[int(len(a)/2)-1]
if(abs(a1-a[0])>abs(a1-a[-1])):
    for i in range(n-1):
        if(i%2==0):
            a2=a[l]
            r+=1
        else:
            a2=a[-(r+1)]
            l+=1
        ans+=abs(a1-a2)
        a1=a2
else:
    for i in range(n-1):
        if(i%2==0):
            a2=a[-(r+1)]
            r+=1
        else:
            a2=a[l]
            l+=1
        ans+=abs(a1-a2)
        a1=a2

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
a.sort()
a.reverse()
sum=0
if n%2==1:
    for j in range(n):
        if j+1<=n//2:
            sum+=a[j]*2
        elif j+1<=n//2+2:
            sum-=a[j]
        else:
            sum-=a[j]*2
else:
    for j in range(n):
        if j+1<=n//2-1:
            sum+=a[j]*2
        elif j+1==n//2:
            sum+=a[j]
        elif j+1==n//2+1:
            sum-=a[j]
        else:
            sum-=a[j]*2
print(sum)

Submission status: Wrong Answer

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

ans_list = []
loop_n = (n-1)//2 if n%2 == 1 else (n-2)//2
ans_list.append(a.pop(0))
ans = 0
for i in range(loop_n):
    if i%2 == 0:
        ans_list.append(a.pop(-1))
        ans_list.insert(0, a.pop(-1))
        ans += abs(ans_list[0] - ans_list[1]) + abs(ans_list[-1] - ans_list[-2])
    else:
        ans_list.insert(0, a.pop(0))
        ans_list.append(a.pop(0))
        ans += abs(ans_list[0] - ans_list[1]) + abs(ans_list[-1] - ans_list[-2])
if n%2 == 0:
    if abs(ans_list[0] - a[0]) >= abs(ans_list[-1] - a[0]):
        ans_list.insert(0, a.pop())
        ans += abs(ans_list[0] - ans_list[1])
    else:
        ans_list.append(a.pop())
        ans += abs(ans_list[-1] - ans_list[-2])

print(ans)