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 merge_sort(n,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
  subroutine merge_sort(n,c)
    implicit none
    integer, intent(in)    :: n
    integer, intent(inout) :: c(n)
    integer                :: number, length, i, ubound
    number = n
    length = 1
    do while (number .gt. 1)
       do i = 1, number/2
          ubound = min(2*i*length,n)
          call merge(c(2*(i-1)*length+1:(2*i-1)*length),c((2*i-1)*length+1:ubound))
       end do
       length = 2*length
       number = (number+1)/2
    end do
    return
  end subroutine merge_sort

  subroutine merge(c1,c2)
    implicit none
    integer, intent(inout) :: c1(:), c2(:)
    integer                :: c(size(c1)+size(c2))
    integer                :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(c1)
    n2 = size(c2)
    do while (i1 .le. n1 .and. i2 .le. n2)
       if (c1(i1) .lt. c2(i2)) then
          c(i1+i2-1) = c1(i1)
          i1 = i1+1
       else
          c(i1+i2-1) = c2(i2)
          i2 = i2+1
       end if
    end do
    if (i1 .le. n1) then
       c(i1+i2-1:n1+n2) = c1(i1:n1)
    else if (i2 .le. n2) then
       c(i1+i2-1:n1+n2) = c2(i2:n2)
    end if
    c1 = c(1:n1)
    c2 = c(n1+1:n1+n2)
    return
  end subroutine merge

  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