import Data.List

main :: IO ()
main = do
  n <- read <$> getLine :: IO Int
  ns <- map read . words <$> getLine :: IO [Int]
  print $ solve ns

solve :: [Int] -> Int
solve ns = foldl (\acc a -> acc + (snd a - fst a)) 0 lss
  where
    ls = sort ns
    lss = zip ls (tail ls)
