import Control.Monad import Data.List import Data.Array import Data.Array.IO inf :: Int inf = 10^18 chmin :: IOUArray Int Int -> Int -> Int -> IO () chmin a k v = do u <- readArray a k when (u > v) (writeArray a k v) main :: IO () main = do n <- readLn :: IO Int a <- listArray (0, n - 1) . sort . map read . words <$> getLine :: IO (Array Int Int) dp <- newArray (0, n) inf :: IO (IOUArray Int Int) writeArray dp 0 0 forM_ [0..n-1] $ \i -> do v <- readArray dp i when (i + 2 <= n) $ chmin dp (i + 2) (v + a!(i+1) - a!i) when (i + 3 <= n) $ chmin dp (i + 3) (v + a!(i+2) - a!i) readArray dp n >>= print