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 2 [a,b] = abs(a-b)
sol n as = re (n-1) [a] [] u v
    where
        (a:u) = sort as
        v = reverse u

re 0 lv rv _ _ = cal 0 (lv++reverse rv)
re 1 lv@(l:_) rv@(r:_) _ (a:_) = cal (max (abs(l-a)) (abs(r-a))) (lv++reverse rv)
re n [l] [] u (a:b:v) = re2 (n-2) [a,l] [b] u v
re n lv@(l:_) rv@(r:_) u (a:b:v)
    | (abs(l-a)+abs(r-b))>(abs(l-b)+abs(r-a)) = re2 (n-2) (a:lv) (b:rv) u v
    | otherwise = re2 (n-2) (b:lv) (a:rv) u v

re2 0 lv rv _ _ = cal 0 (lv++reverse rv)
re2 1 lv@(l:_) rv@(r:_) (a:_) _ = cal (max (abs(l-a)) (abs(r-a))) (lv++reverse rv)
re2 n lv@(l:_) rv@(r:_) (a:b:u) v
    | (abs(l-a)+abs(r-b))>(abs(l-b)+abs(r-a)) = re (n-2) (a:lv) (b:rv) u v
    | otherwise = re (n-2) (b:lv) (a:rv) u v

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