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())
l=[]

for i in range(N):
    l.append(int(input()))

l=sorted(l)
i=0
sum=0
nr=0
nl=0
right=l.pop(0)
left=right

while l:
    if i%4==0:
        nr=l.pop(-1)
        sum+=nr-right
        right=nr        
    elif i%4==1:
        nl=l.pop(-1)
        sum+=nl-left
        left=nl
    elif i%4==2:
        nr=l.pop(0)
        sum-=nr-right
        right=nr       
    elif i%4==3:
        nl=l.pop(0)
        sum-=nl-left
        left=nl
        
    i+=1
        
print(sum)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=sorted([int(input()) for i in range(N)])
r=[0]*N
for i in range(N//2):
    r[i*2+1]=A[i]
if N%2:
    r[-1]=A[N//2]
for i in range(N//2):
    r[i*2]=A[i+N//2+N%2]
print(sum([abs(r[i]-r[i+1]) for i in range(N-1)]))

Submission status: Wrong Answer

Solution code:
#C問題
N = int(input())
A = [int(input()) for i in range(N)]
A.sort(reverse=True)

p = 0
n = 0
if N%2 == 0:
    MAX = 0
    for i in range(N):
        if i < (N//2):
            if i == (N//2)-1:
                p+=A[i]
            else:
                p+=A[i]*2
        else:
            if i == (N//2):
                n+=A[i]
            else:
                n+=A[i]*2
    MAX = max(MAX,p-n)
    p = 0
    n = 0
    for i in range(N):
        if i < (N//2)-1:
            if i == (N//2)-2:
                p+=A[i]
            else:
                p+=A[i]*2
        else:
            if i == (N//2)-1:
                n+=A[i]
            else:
                n+=A[i]*2
    MAX = max(MAX,p-n)
else:
    MAX = 0
    for i in range(N):
        if i <= (N//2):
            if i == (N//2) or i == (N//2)-1:
                p+=A[i]
            else:
                p+=A[i]*2
        else:
            if i == (N//2)+1 and N >=5:
                n+=A[i]
            else:
                n+=A[i]*2
    MAX = max(MAX,p-n)
    p = 0
    n = 0
    for i in range(N):
        if i <= (N//2)-1:
            if i == (N//2)-1 and N >= 5:
                p+=A[i]
            else:
                p+=A[i]*2
        else:
            if i == (N//2) or i == (N//2)+1:
                n+=A[i]
            else:
                n+=A[i]*2
    MAX = max(MAX,p-n)
        
print(MAX)

Submission status: Wrong Answer

Solution code:
#C問題
N = int(input())
A = [int(input()) for i in range(N)]
A.sort(reverse=True)

p = 0
n = 0
if N%2 == 0:
    for i in range(N):
        if i < (N//2):
            if i == (N//2)-1:
                p+=A[i]
            else:
                p+=A[i]*2
        else:
            if i == (N//2):
                n+=A[i]
            else:
                n+=A[i]*2
else:
    for i in range(N):
        if i <= (N//2):
            if i == (N//2) or i == (N//2)-1:
                p+=A[i]
            else:
                p+=A[i]*2
        else:
            if i == (N//2)+1 and N >=5:
                n+=A[i]
            else:
                n+=A[i]*2
                
print(p-n)

Submission status: Wrong Answer

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

sorted_A = sorted(A, reverse=True)

if N % 2 == 0:
    ans = sum(sorted_A[:(N - 2) // 2]) * 2 + sorted_A[(N - 2) // 2] - sorted_A[(N - 2) // 2 + 1] - sum(sorted_A[-((N - 2) // 2):] * 2)
else:
    ans_a = sum(sorted_A[:(N - 2) // 2]) * 2 + sum(sorted_A[(N - 2) // 2: (N - 2) // 2 + 2]) - sum(sorted_A[-((N - 1) // 2):]) * 2
    ans_b = sum(sorted_A[:(N - 1) // 2]) * 2 - sum(sorted_A[(N - 1) // 2: (N - 1) // 2 + 2]) - sum(sorted_A[-((N - 2) // 2):]) * 2
    ans = max(ans_a, ans_b)
print(ans)

Submission status: Wrong Answer

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

sorted_A = sorted(A, reverse=True)

if N == 2:
    ans = sorted_A[1] - sorted_A[0]
elif N % 2 == 0:
    ans = sum(sorted_A[0:(N - 2) // 2]) * 2 + sorted_A[(N - 2) // 2] - sorted_A[(N - 2) // 2 + 1] - sum(sorted_A[- (N - 2) // 2: N] * 2)
else:
    ans_a = sum(sorted_A[:(N - 2) // 2]) * 2 + sum(sorted_A[(N - 2) // 2: (N - 2) // 2 + 2]) - sum(sorted_A[-((N - 1) // 2):]) * 2
    ans_b = sum(sorted_A[:(N - 1) // 2]) * 2 - sum(sorted_A[(N - 1) // 2: (N - 1) // 2 + 2]) - sum(sorted_A[-((N - 2) // 2):]) * 2
    ans = max(ans_a, ans_b)
print(ans)

Submission status: Wrong Answer

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

sorted_A = sorted(A, reverse=True)

if N % 2 == 0:
    ans = sum(sorted_A[0:(N - 2) // 2]) * 2 + sorted_A[(N - 2) // 2] - sorted_A[(N - 2) // 2 + 1] - sum(sorted_A[- (N - 2) // 2: N] * 2)
else:
    ans_a = sum(sorted_A[:(N - 2) // 2]) * 2 + sum(sorted_A[(N - 2) // 2: (N - 2) // 2 + 2]) - sum(sorted_A[-((N - 1) // 2):]) * 2
    ans_b = sum(sorted_A[:(N - 1) // 2]) * 2 - sum(sorted_A[(N - 1) // 2: (N - 1) // 2 + 2]) - sum(sorted_A[-((N - 2) // 2):]) * 2
    ans = max(ans_a, ans_b)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=sorted([int(input())for i in range(n)])
def f(x):
    p,q=1,n-2
    l,r=[a[0]],[a[n-1]]
    c=a[n-1]-a[0]
    t=0
    while p<=q:
        if t==x:
            r.append(a[q])
            c+=a[q]-l[0]
            q-=1
            l.pop()
        else:
            l.append(a[p])
            c+=r[0]-a[p]
            p+=1
            r.pop()
        t=(t+1)%2
    return c
print(max(f(0),f(1)))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

A.sort()

A

if N%2 == 0:
    lower = A[:N//2]
    upper = A[N//2:]
    x = 2 * sum(upper) - upper[0]
    y = 2 * sum(lower) - lower[-1]
    answer = x - y
else:
    lower = A[:N//2]
    upper = A[N//2+1:]
    mid = A[N//2]
    x = 2 * sum(upper) - 2 * sum(lower) - mid + lower[-1]
    y = 2 * sum(upper) - 2 * sum(lower) + mid - upper[-1]
    answer = max(x,y)
print(answer)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

A.sort()

nums = deque()
next = ""
for i in range(N):
    if i == 0:
        nums.append(A[i])
    else:
        if i % 2 == 0:
            idx = i // 2
            next = "big"
        else:
            idx = N - 1 - (i // 2)
            next = "small"

        if abs(A[idx] - nums[0]) > abs(A[idx] - nums[-1]):
            nums.appendleft(A[idx])
        elif abs(A[idx] - nums[0]) < abs(A[idx] - nums[-1]):
            nums.append(A[idx])
        else:
            if next == "big":
                if nums[0] >= nums[-1]:
                    nums.appendleft(A[idx])
                else:
                    nums.append(A[idx])
            else:
                if nums[0] >= nums[-1]:
                    nums.append(A[idx])
                else:
                    nums.appendleft(A[idx])



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

print(ans)

Submission status: Wrong Answer

Solution code:
#### import ####
import sys
sys.setrecursionlimit(10**7)
import math
from collections import defaultdict

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
  read_all = [tuple(map(int, input().split())) for _ in range(N)]
  return map(list,zip(*read_all))

#################

#TLEテスト
N = I()
A = [0]*N
for i in range(N):
  A[i] = I()
  
A.sort()
A2 = A[:]

Submission status: Wrong Answer

Solution code:
#### import ####
import sys
sys.setrecursionlimit(10**7)
import math
from collections import defaultdict

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
  read_all = [tuple(map(int, input().split())) for _ in range(N)]
  return map(list,zip(*read_all))

#################

#TLEテスト
N = I()
A = [0]*N
for i in range(N):
  A[i] = I()
  
A.sort()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
A.sort()
ans = []
total = 0
if N%2 == 1:
    ans.append(A.pop(0))
    cur = 0
    while len(A) != 0:
        if cur%4 == 0:
            ans = [A.pop(-1)] + ans
            cur += 1
        elif cur%4 == 1:
            ans.append(A.pop(-1))
            cur += 1
        elif cur%4 == 2:
            ans = [A.pop(0)] + ans
            cur += 1
        else:
            ans.append(A.pop(0))
            cur += 1
else:
    cur = 0
    while len(A) != 0:
        if cur%4 == 0:
            ans = [A.pop(0)] + ans
            cur += 1
        elif cur%4 == 1:
            ans.append(A.pop(-1))
            cur += 1
        elif cur%4 == 2:
            ans.append(A.pop(0))
            cur += 1
        else:
            ans = [A.pop(-1)] + ans
            cur += 1
for x,y in zip(ans, ans[1:]):
    total += abs(x-y)
print(total)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from collections import deque
INF = float("inf")


def solve(N: int, A: "List[int]"):
    A.sort()
    q = deque(A)

    s = 0
    ans = deque([])
    ans.append(q.popleft())
    counter = 0
    while True:
        # print(ans, counter)
        if counter % 2 == 0:
            a = q.pop()
        else:
            a = q.popleft()

        mae = abs(ans[0]-a)
        ushiro = abs(ans[-1]-a)
        # print(mae, ushiro)
        if mae > ushiro:
            s += mae
            ans.appendleft(a)
        else:
            s += ushiro
            ans.append(a)

        counter += 1
        if len(q) == 0:
            break
    # print(ans)
    print(s)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import collections

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

Submission status: Wrong Answer

Solution code:
import collections

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

Submission status: Wrong Answer

Solution code:
import collections

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

Submission status: Wrong Answer

Solution code:
n=int(input())
arr=[int(input()) for _ in range(n)]
arr=sorted(arr)
ans=0
l=1
r=n-3
lnum=arr[-1]
rnum=arr[-2]
if n==2:
  print(abs(arr[0]-arr[1]))
elif n==3:
  print((arr[-1]-arr[0])+(arr[-2]-arr[0]))
elif n==4:
  print((arr[-1]-arr[1])+(arr[-1]-arr[0])+(arr[-2]-arr[0]))
else:
  ans=(arr[-1]-arr[0])+(arr[-2]-arr[0])
  while 1:
    if r-l==-1:
      break
    if arr[r]-min(lnum,rnum)>=max(lnum,rnum)-arr[l]:
      if lnum==min(lnum,rnum):
        ans+=arr[r]-lnum
        lnum=arr[r]
        r-=1
      else:
        ans+=arr[r]-rnum
        rnum=arr[r]
        r-=1
    else:
      if lnum==max(lnum,rnum):
        ans+=lnum-arr[l]
        lnum=arr[l]
        l+=1
      else:
        ans+=rnum-arr[l]
        rnum=arr[l]
        l+=1
  print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
arr=[int(input()) for _ in range(n)]
arr=sorted(arr)
ans=0
l=1
r=n-3
lnum=arr[-1]
rnum=arr[-2]
if n==2:
  print(abs(arr[0]-arr[1]))
else:
  ans=(arr[-1]-arr[0])+(arr[-2]-arr[0])
  while 1:
    if r-l==-1:
      break
    if arr[r]-min(lnum,rnum)>=max(lnum,rnum)-arr[l]:
      if lnum==min(lnum,rnum):
        ans+=arr[r]-lnum
        lnum=arr[r]
        r-=1
      else:
        ans+=arr[r]-rnum
        rnum=arr[r]
        r-=1
    else:
      if lnum==max(lnum,rnum):
        ans+=lnum-arr[l]
        lnum=arr[l]
        l+=1
      else:
        ans+=rnum-arr[l]
        rnum=arr[l]
        l+=1
  print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
arr=[int(input()) for _ in range(n)]
arr=sorted(arr)
ans=0
l=1
r=n-3
lnum=arr[-1]
rnum=arr[-2]
if n==2:
  print(abs(arr[0]-arr[1]))
else:
  ans=(arr[-1]-arr[0])+(arr[-2]-arr[0])
  while 1:
    if r-l==-1:
      break
    if arr[r]-min(lnum,rnum)>max(lnum,rnum)-arr[l]:
      if lnum==min(lnum,rnum):
        ans+=arr[r]-lnum
        lnum=arr[r]
        r-=1
      else:
        ans+=arr[r]-rnum
        rnum=arr[r]
        r-=1
    else:
      if lnum==max(lnum,rnum):
        ans+=lnum-arr[l]
        lnum=arr[l]
        l+=1
      else:
        ans+=rnum-arr[l]
        rnum=arr[l]
        l+=1
  print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
arr=[int(input()) for _ in range(n)]
arr=sorted(arr)
ans=0
l=1
r=n-3
lnum=arr[-1]
rnum=arr[-2]
flag=0
if n==2:
  print(abs(arr[0]-arr[1]))
else:
  ans=(arr[-1]-arr[0])+(arr[-2]-arr[0])
  while 1:
    if r-l==-1:
      break
    if flag==0:
      if abs(lnum-arr[l])>abs(rnum-arr[l]):
        ans+=abs(lnum-arr[l])
        lnum=arr[l]
      else:
        ans+=abs(rnum-arr[l])
        rnum=arr[l]
      l+=1
      flag=1
    else:
      if abs(rnum-arr[r])>abs(lnum-arr[r]):
        ans+=abs(arr[r]-rnum)
        lnrum=arr[r]
      else:
        ans+=abs(arr[r]-lnum)
        lnum=arr[r]
      r-=1
      flag=0
  print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

num_list = []
for _ in range(N):
    num_list.append(int(input()))
    
num_sorted = sorted(num_list)

def calc_diff(add, add2, diff, diff2, num_sorted):
    total = 0
    
    for _ in range(add2):
        tmp = num_sorted.pop()
        total += tmp * 2
        
    for _ in range(add):
        tmp = num_sorted.pop()
        total += tmp * 1
        
    for _ in range(diff):
        tmp = num_sorted.pop()
        total -= tmp * 1
            
    for _ in range(diff2):
        tmp = num_sorted.pop()
        total -= tmp * 2
        
    return total
  
import copy

def calc(N, num_sorted):
    
    diff2 = 0
    add2 = 0
    diff = 0
    add = 0
    
    if N % 2 == 0:
        add = 1
        diff = 1
        add2 = N // 2 - 1
        diff2 = N // 2 - 1
        
        total = calc_diff(add, add2, diff, diff2, copy.deepcopy(num_sorted))

        return total
            
    if N % 2 == 1:        
        add = 0
        diff = 2
        add2 =(N - 1) // 2
        diff2 = (N - 1) // 2 - 1
        
        totalB = calc_diff(add, add2, diff, diff2, copy.deepcopy(num_sorted))
        
        add = 2
        diff = 0
        add2 =(N - 1) // 2 - 1
        diff2 = (N - 1) // 2
        
        totalC = calc_diff(add, add2, diff, diff2, copy.deepcopy(num_sorted))
        
        return max(totalB, totalC)
      
calc(N, num_sorted)

Submission status: Wrong Answer

Solution code:
import collections
import copy
N = int(input())
As = [int(input()) for _ in range(N)]
As.sort()
As = collections.deque(As)
small1 = collections.deque()
small2 = collections.deque()
big1 = collections.deque()
big2 = collections.deque()
A1 = copy.copy(As)
A2 = copy.copy(As)
s = A1.popleft()
b = A2.pop()
small1.append(s)
small2.append(s)
big1.append(b)
big2.append(b)
cnt1 = 0
cnt2 = 0
while A1:
    if cnt1%4 == 0:
        b = A1.pop()
        small1.append(b)
        small2.appendleft(b)
        cnt1 += 1
    elif  cnt1%4 == 1:
        b = A1.pop()
        small2.append(b)
        small1.appendleft(b)
        cnt1 += 1
    elif cnt1%4 == 2:
        s = A1.popleft()
        small1.append(s)
        small2.appendleft(s)
        cnt1 += 1
    else:
        s = A1.popleft()
        small2.append(s)
        small1.appendleft(s)
        cnt1 += 1
while A2:
    if cnt2%4 == 0:
        s = A2.popleft()
        big1.append(s)
        big2.appendleft(s)
        cnt2 += 1
    elif  cnt2%4 == 1:
        s = A2.popleft()
        big2.append(s)
        big1.appendleft(s)
        cnt2 += 1
    elif cnt2%4 == 2:
        b = A2.pop()
        big1.append(b)
        big2.appendleft(b)
        cnt2 += 1
    else:
        b = A2.pop()
        big2.append(b)
        big1.appendleft(b)
        cnt2 += 1
anss1 = 0
anss2 = 0
ansb1 = 0
ansb2 = 0
print(small1)
print(small2)
print(big1)
print(big2)
for i in range(N-1):
    anss1 += abs(small1[i+1]-small1[i])
    anss2 += abs(small2[i+1]-small2[i])
    ansb1 += abs(big1[i+1]-big1[i])
    ansb2 += abs(big2[i+1]-big2[i])
print(max(anss1, anss2, ansb1, ansb2))

Submission status: Wrong Answer

Solution code:
import collections
import copy
N = int(input())
As = [int(input()) for _ in range(N)]
As.sort()
As = collections.deque(As)
small1 = collections.deque()
small2 = collections.deque()
big1 = collections.deque()
big2 = collections.deque()
A1 = copy.copy(As)
A2 = copy.copy(As)
s = A1.popleft()
b = A2.pop()
small1.append(s)
small2.append(s)
big1.append(b)
big2.append(b)
cnt1 = 0
cnt2 = 0
while A1:
    if cnt1%4 == 0:
        b = A1.pop()
        small1.append(b)
        small2.appendleft(b)
        cnt1 += 1
    elif  cnt1%4 == 1:
        b = A1.pop()
        small2.append(b)
        small1.appendleft(b)
        cnt1 += 1
    elif cnt1%4 == 2:
        s = A1.popleft()
        small1.append(s)
        small2.appendleft(s)
        cnt1 += 1
    else:
        s = A1.popleft()
        small2.append(s)
        small1.appendleft(s)
        cnt1 += 1
while A2:
    if cnt2%4 == 0:
        s = A2.popleft()
        big1.append(s)
        big2.appendleft(s)
        cnt2 += 1
    elif  cnt2%4 == 1:
        s = A2.popleft()
        big2.append(s)
        big1.appendleft(s)
        cnt2 += 1
    elif cnt2%4 == 2:
        b = A2.pop()
        big1.append(s)
        big2.appendleft(s)
        cnt2 += 1
    else:
        b = A2.pop()
        big2.append(s)
        big1.appendleft(s)
        cnt2 += 1
anss1 = 0
anss2 = 0
ansb1 = 0
ansb2 = 0
for i in range(N-1):
    anss1 += abs(small1[i+1]-small1[i])
    anss2 += abs(small2[i+1]-small2[i])
    ansb1 += abs(big1[i+1]-big1[i])
    ansb2 += abs(big2[i+1]-big2[i])
print(max(anss1, anss2, ansb1, ansb2))

Submission status: Wrong Answer

Solution code:
from collections import*
n,*a=map(int,open(0).read().split())
a.sort()
d=len(a)//2
flag=0
if len(a)%2==1:
    b=a[:d+1]
    c=a[d+1:]
    flag=1
else:
    b=a[:d]
    c=a[d:]
l=[]
for i in range(d):
    l.append(c[i])
    l.append(b[i])
if flag:
    l.insert(0,b[-1])
#print(l)
print(sum(abs(l[i+1]-l[i])for i in range(n-1)))

Submission status: Wrong Answer

Solution code:
from collections import*
n,*a=map(int,open(0).read().split())
a.sort()
print(a)
d=len(a)//2
flag=0
if len(a)%2==1:
    b=a[:d+1]
    c=a[d+1:]
    flag=1
else:
    b=a[:d]
    c=a[d:]
print(b,c)
l=[]
for i in range(d):
    l.append(c[i])
    l.append(b[i])
if flag:
    l.insert(0,b[-1])
#print(l)
print(sum(abs(l[i+1]-l[i])for i in range(n-1)))

Submission status: Wrong Answer

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

A.sort(reverse=True)

def p1(l):
    if N%2 == 1:
        ans = 0
        ans += sum(l[:(N-3)//2])*2
        ans += sum(l[(N-3)//2:(N-3)//2+2])
        ans += sum(l[(N-3)//2+2:])*(-2)
        return ans
    else:
        ans = 0
        ans += sum(l[:(N-2)//2])*2
        ans += sum(l[(N-2)//2:(N-2)//2+1])
        ans += sum(l[(N-2)//2+1:(N-2)//2+2])*(-1)
        ans += sum(l[(N-2)//2+2:])*(-2)
        return ans

def p2(l):
    if N%2 == 1:
        ans = 0
        ans += sum(l[:(N-1)//2])*2
        ans += sum(l[(N-1)//2:(N-1)//2+1])*(-1)
        ans += sum(l[(N-1)//2+1:])*(-2)
        return ans
    else:
        ans = 0
        ans += sum(l[:(N-2)//2])*2
        ans += sum(l[(N-2)//2:(N-2)//2+1])
        ans += sum(l[(N-2)//2+1:(N-2)//2+2])*(-1)
        ans += sum(l[(N-2)//2+2:])*(-2)
        return ans
print(max(p1(A),p2(A)))

Submission status: Wrong Answer

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

A.sort(reverse=True)

def p1(l):
    if N%2 == 1:
        ans = 0
        ans += sum(l[:(N-3)//2])*2
        ans += sum(l[(N-3)//2:(N-3)//2+2])
        ans += sum(l[(N-3)//2+2:])*(-2)
        return ans
    else:
        ans = 0
        ans += sum(l[:(N-2)//2])*2
        ans += sum(l[(N-2)//2:(N-2)//2+1])
        ans += sum(l[(N-2)//2+1:(N-2)//2+2])*(-1)
        ans += sum(l[(N-2)//2+2:])*(-2)
        return ans

def p2(l):
    if N%2 == 1:
        ans = 0
        ans += sum(l[:(N-3)//2])*2
        ans += sum(l[(N-3)//2:(N-3)//2+1])*(-1)
        ans += sum(l[(N-3)//2+1:])*(-2)
        return ans
    else:
        ans = 0
        ans += sum(l[:(N-2)//2])*2
        ans += sum(l[(N-2)//2:(N-2)//2+1])
        ans += sum(l[(N-2)//2+1:(N-2)//2+2])*(-1)
        ans += sum(l[(N-2)//2+2:])*(-2)
        return ans

print(max(p1(A),p2(A)))

Submission status: Wrong Answer

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

def sumd(l):
    a=0
    s=l.pop()
    while l:
        e=l.pop()
        a+=abs(s-e)
        s=e
    return a

Submission status: Wrong Answer

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

    a_list = []

    for _ in range(n):
        a_list.append(int(input()))

    a_list = sorted(a_list)

    left = right = a_list[0]

    min_no = 1
    max_no = n - 1

    ans_sum = 0

    for i in range(1, n + 1):

        div_4 = i % 4

        if div_4 == 1:
            ans_sum += a_list[max_no] - left
            left = a_list[max_no]
            max_no -= 1

        elif i % 4 == 2:
            ans_sum += a_list[max_no] - right
            right = a_list[max_no]
            max_no -= 1

        elif div_4 == 3:
            ans_sum += right - a_list[min_no]
            right = a_list[min_no]
            min_no += 1

        else:
            ans_sum += left - a_list[min_no]
            left = a_list[min_no]
            min_no += 1

    print(ans_sum)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

    a_list = []

    for _ in range(n):
        a_list.append(int(input()))

    a_list = sorted(a_list)

    left = right = a_list[0]

    min_no = 1
    max_no = n - 1

    ans_sum = 0

    for i in range(1, n):

        div_4 = i % 4

        if div_4 == 1:
            ans_sum += a_list[max_no] - left
            left = a_list[max_no]
            max_no -= 1

        elif i % 4 == 2:
            ans_sum += a_list[max_no] - right
            right = a_list[max_no]
            max_no -= 1

        elif div_4 == 3:
            ans_sum += right - a_list[min_no]
            right = a_list[min_no]
            min_no += 1

        else:
            ans_sum += left - a_list[min_no]
            left = a_list[min_no]
            min_no += 1

    print(ans_sum)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

    a_list = []

    for _ in range(n):
        a_list.append(int(input()))

    a_list = sorted(a_list)

    left = a_list[0]

    right = a_list[0]

    min_no = 1
    max_no = len(a_list) - 1

    ans_sum = 0

    for i in range(1, len(a_list) + 1):

        div_4 = i % 4

        if div_4 == 1:
            ans_sum += abs(a_list[max_no] - left)
            left = a_list[max_no]
            max_no -= 1

        elif i % 4 == 2:
            ans_sum += abs(right - a_list[max_no])
            right = a_list[max_no]
            max_no -= 1

        elif div_4 == 3:
            ans_sum += abs(right - a_list[min_no])
            right = a_list[min_no]
            min_no += 1

        else:
            ans_sum += abs(a_list[min_no] - left)
            left = a_list[min_no]
            min_no += 1

    print(ans_sum)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

    a_list = []

    for _ in range(n):
        a_list.append(int(input()))

    a_list = sorted(a_list)

    ans_list = [a_list[0]]

    min_no = 1
    max_no = len(a_list) - 1

    for i in range(1, len(a_list) + 1):

        div_4 = i % 4

        if div_4 == 1:
            ans_list = [a_list[max_no]] + ans_list
            max_no -= 1

        elif i % 4 == 2:
            ans_list = ans_list + [a_list[max_no]]
            max_no -= 1

        elif div_4 == 3:
            ans_list = ans_list + [a_list[min_no]]
            min_no += 1

        else:
            ans_list = [a_list[min_no]] + ans_list
            min_no += 1

    ans_sum = sum([abs(a - b) for a, b in zip(ans_list, ans_list[1:])])

    print(ans_sum)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

    a_list = []

    for _ in range(n):
        a_list.append(int(input()))

    a_list = sorted(a_list)

    ans_list = [a_list[0]]

    min_no = 1
    max_no = len(a_list) - 1

    for i in range(1, len(a_list) + 1):

        div_4 = i % 4

        if div_4 == 1:
            ans_list = [a_list[max_no]] + ans_list
            max_no -= 1

        elif i % 4 == 2:
            ans_list = ans_list + [a_list[max_no]]
            max_no -= 1

        elif div_4 == 4:
            ans_list = [a_list[min_no]] + ans_list
            min_no += 1

        elif div_4 == 3:
            ans_list = ans_list + [a_list[min_no]]
            min_no += 1

    ans_sum = 0

    for i in range(len(ans_list)):

        if i == 0:
            continue

        ans_sum += abs(ans_list[i-1] - ans_list[i])

    print(ans_sum)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

    a_list = []

    for _ in range(n):
        a_list.append(int(input()))

    ans_list = [min(a_list)]

    a_list.remove(min(a_list))

    for i in range(len(a_list)):

        if i % 4 == 2:
            ans_list = [min(a_list)] + ans_list
            a_list.remove(min(a_list))

        elif i % 4 == 3:
            ans_list = ans_list + [min(a_list)]
            a_list.remove(min(a_list))

        elif i % 4 == 0:
            ans_list = [max(a_list)] + ans_list
            a_list.remove(max(a_list))

        elif i % 4 == 1:
            ans_list = ans_list + [max(a_list)]
            a_list.remove(max(a_list))

    ans_sum = 0

    for i in range(len(ans_list)):

        if i == 0:
            continue

        ans_sum += abs(ans_list[i-1] - ans_list[i])

    print(ans_sum)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n=int(input()) 
a = [] 
for i in range(n): 
    a.append(int(input())) 
b = sorted(a)
c = [0 for i in range(len(b))]
if len(b)%2==0:
    c[0] = b[int(len(b)/2-1)]
    c[-1] = b[int(len(b)/2)]
    for i in range(int(len(b)/2-1)):
        c[2*i+2]=b[i]
        c[2*i+1]=b[-i-1]
else:
    c[0] = b[int(len(b)//2-1)]
    c[-1] = b[int(len(b)//2)]
    for i in range(int(len(b)//2-1)):
        c[2*i+2]=b[i]
    for i in range(int(len(b)//2)):
        c[2*i+1]=b[-i-1]
ans=0
for i in range(len(b)-1):
    ans = ans + abs(c[i]-c[i+1])
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
A=sorted([int(input()) for i in range(n)])
if n%2==0:
  m=n//2
  ans=A[m]+2*sum(A[m+1:])-2*sum(A[:m-1])-A[m-1]
else:
  m=n//2
  ans=A[m+1]+2*sum(A[m+2:])-2*sum(A[:m-1])-A[m-1]+A[m]
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
A=sorted([int(input()) for i in range(n)])
if n%2==0:
  m=n//2
  ans=A[m]+2*sum(A[m+1:])-2*sum(A[:m-1])-A[m-1]
else:
  m=n//2
  ans=A[m]+2*sum(A[m+1:])-2*sum(A[:m-1])+A[m-1]
print(ans)