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 (gfortran v4.8.4) (judge verdict: Time Limit Exceeded)

program align
  implicit none
  integer              :: n, i
  integer, allocatable :: a(:)
  integer(8)           :: m1, m2, m3, m4
  read(*,*) n
  allocate(a(n))
  do i = 1, n
     read(*,*) a(i)
  end do
  if (n .eq. 2) then
     write(*,'(i0)') abs(a(1)-a(2))
     stop
  end if
  call quickSort(a)
  m1 = 0
  m2 = 0
  m3 = 0
  m4 = 0
  if (mod(n,2) .eq. 1) then
     do i = 1, n/2
        m1 = m1 + abs(a(i)-a(n+1-i))
        m2 = m2 + abs(a(n+1-i)-a(i))
     end do
     do i = 1, n/2-1
        m1 = m1 + abs(a(n+1-i)-a(i+1))
        m2 = m2 + abs(a(i)-a(n-i))
     end do
     m1 = m1 + abs(a(1)-a(n/2+1))
     m2 = m2 + abs(a(n)-a(n/2+1))
  else
     do i = 1, n/2-1
        m1 = m1 + abs(a(i)-a(n+1-i))
        m2 = m2 + abs(a(n+1-i)-a(i))
     end do
     if (n .gt. 4) then
        do i = 1, n/2-2
           m1 = m1 + abs(a(n+1-i)-a(i+1))
           m2 = m2 + abs(a(i)-a(n-i))
        end do
     end if
     m3 = m1 + abs(a(n/2)-a(1)) + abs(a(n/2+2)-a(n/2+1))
     m4 = m2 + abs(a(n/2)-a(n)) + abs(a(n/2-1)-a(n/2+1))
     m1 = m1 + abs(a(n/2+1)-a(1)) + abs(a(n/2+2)-a(n/2))
     m2 = m2 + abs(a(n/2+1)-a(n)) + abs(a(n/2-1)-a(n/2))
     m1 = max(m1,m3)
     m2 = max(m2,m4)
  end if
  write(*,'(i0)') max(m1,m2)
  deallocate(a)
  stop
contains
  recursive subroutine quickSort(a)
    implicit none
    integer, intent(inout) :: a(:)
    integer                :: n, i, p, top, btm
    n = size(a)
    if (n == 1) return
    if (n == 2) then
       if (a(1) > a(2)) call swap(a(1),a(2))
       return
    end if
    do i = 1, n-1
       if (a(i) > a(i+1)) call swap(a(i),a(i+1))
    end do
    do i = n-1, 2, -1
       if (a(i) < a(i-1)) call swap(a(i),a(i-1))
    end do
    if (n == 3) return
    p = (a(1)+a(n))/2
    btm = 2
    top = n-1
    do while (btm < top)
       if (a(btm) <= p) btm = btm + 1
       if (a(top) > p) top = top - 1
       if (a(btm) > p .and. a(top) <= p) then
          call swap(a(btm),a(top))
          btm = btm + 1
          top = top - 1
       end if
    end do

    call quickSort(a(top:n-1))
    call quickSort(a(2:btm))
    
    return
  end subroutine quickSort

  subroutine swap(a,b)
    implicit none
    integer, intent(inout) :: a, b
    integer                :: c
    c = a
    a = b
    b = c
    return
  end subroutine swap
end program align