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