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

Solution in Fortran (GNU Fortran 9.2.1) (judge verdict: Wrong Answer)

program prob6
    implicit none
    integer :: N
    integer(8), allocatable :: A(:)
    integer(8) :: ans, tmp
    integer :: i
    read(*,*) N
    allocate(A(N))
    
    do i = 1, N
        read(*,*) A(i)
    end do

    call msort(A)

    ans = 0
    if(mod(N, 2) == 1)then
        do i = 1, N/2
            ans = ans - 2 * A(i)
        end do
        ans = ans + A(N/2+1)
        ans = ans + A(N/2+2)
        do i = N/2+3, N
            ans = ans + 2 * A(i)
        end do

        do i = 1, N/2 -1
            tmp = tmp - 2 * A(i)
        end do
        tmp = tmp - A(N/2)
        tmp = tmp - A(N/2+1)
        do i = N/2+2, N
            tmp = tmp + 2 * A(i)
        end do 
        ans = max(tmp, ans)       
    else
        do i = 1, N/2 -1
            ans = ans - 2 * A(i)
        end do
        ans = ans - A(N/2)
        ans = ans + A(N/2+1)
        do i = N/2+2, N
            ans = ans + 2 * A(i)
        end do               
    end if

    write(*,*) ans

    deallocate(A)
    stop
contains

  !
  ! swap two arguments
  !
subroutine swap(a, b)
    implicit none
    integer(8), intent(inout) :: a, b

    integer(8) :: c

    c = a
    a = b
    b = c

  end subroutine swap

  !
  ! merge sort
  !
  recursive subroutine msort(x)
    implicit none
    integer(8), intent(inout) :: x(:)

    integer(8) :: n, mid, i, j, k
    integer(8), allocatable :: tmp(:)
    
    n = size(x)
    
    if(n == 1) then
      return
    end if

    !
    ! 前半と後半に分けてソート
    ! 1~midとmid+1~nをそれぞれソート済みにする
    !
    mid = n / 2
    call msort(x(:mid))
    call msort(x(mid+1:))

    !
    ! tmpという配列にxを一時的に保管
    ! マージソートは外部ソート
    !
    allocate(tmp(n))
    tmp = x

    ! tmpの後半部分を左右反転
    do k = n, mid+1, -1
      tmp(k) = x(mid + n - k + 1)
    end do

    ! iは前半のリストのうちまだ見ていないものの最小値
    ! jは後半のリストのうちまだ見ていないものの最小値
    ! kは見た回数の合計
    i = 1
    j = n

    do k = 1, n
      !
      ! 一番左と一番右のうち小さい方をxに入れていく
      ! ここが等号つき不等号なので、安定ソートとなる
      !
      if(tmp(i) <= tmp(j)) then
        x(k) = tmp(i)
        i = i + 1
      else
        x(k) = tmp(j)
        j = j - 1
      end if
    end do
    
    deallocate(tmp)
    return
  end subroutine msort

end program prob6