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 lv rv u (a:b:r) = re2 (n-2) (a:lv) (b:rv) u r

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 rv (a:b:u) r = re (n-2) (a:lv) (b:rv) u r


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