import Control.Monad
import Data.List
import Data.Maybe
import qualified Data.ByteString.Char8 as B

main = do
    n<-readLn::IO Int
    as<-replicateM n $ fst.fromJust.B.readInt <$> B.getLine ::IO[Int]
    print $ sol n as
--
sol n as = re [a] [] u v
    where
        (a:u') = sort as
        u = take (n-1-(n-1)`div`2) u'
        v = take ((n-1)`div`2) $ sortBy (flip compare) as

re lv rv [] [] = cal 0 (lv++reverse rv)
re [l] [] [u] [] = abs (l-u)
re [l] [] [u] [v] = abs(l-u) + abs(l-v)
re lv@(l:_) rv@(r:_) [u] [] = cal (max (abs(u-l)) (abs(u-r))) (lv++reverse rv)
re lv@(l:_) rv@(r:_) [u] [v] = cal c (lv++reverse rv)
    where
        c = max (abs(l-u)+abs(r-v)) (abs(l-v)+abs(r-u))
re lv rv (u1:u2:u) (v1:v2:v) = re (u1:v1:lv) (u2:v2:rv) u v

cal s [_] = s
cal s (a:b:v) = cal (s+abs(a-b)) (b:v)
