{-# LANGUAGE FlexibleContexts, OverloadedStrings #-} import Control.Applicative import Control.Monad import qualified Data.ByteString.Char8 as B import Data.Maybe (fromJust) import Text.Printf import Debug.Trace f ys y = foldl (\a b -> a + abs (y-b)) 0 ys main = do _ <- getInts xs <- getInts print $ tri (f xs) (-1000000010) 1000000010 -- util getInts :: IO [Integer] getInts = map (fst . fromJust . B.readInteger) . B.words <$> B.getLine tri f lb ub | ub-lb < 3 = minimum [f x | x <- [lb .. ub]] | otherwise = if y1 < y2 then tri f lb x2 else tri f x1 ub where (x1,x2) = ((lb*2 + ub) `div` 3, (lb + ub*2) `div` 3) (y1,y2) = (f x1, f x2)