main = print . solve1 . map read . drop 1 . words =<< getContents

maxVal = 1000000000

solve1 :: [Integer] -> Integer
solve1 [] = maxVal
solve1 xs = min (solve2 xs) (solve1 $ tail xs)

solve2 :: [Integer] -> Integer
solve2 [] = maxVal
solve2 (x:[]) = abs x
solve2 (x:xs) = minimum . map (abs . flip (-) x) . scanr (+) 0 $ xs
