import qualified Data.ByteString.Char8 as BC
import Data.Maybe

readIntsLine :: IO [Int]
readIntsLine = map (fst . fromJust . BC.readInt) . BC.words <$> BC.getLine

calc :: [(Int, Int)] -> Int -> [Int]
calc [] _ = []
calc ((ph, pc):phcs) h = cost : calc phcs h 
  where cost = pc + abs (ph - h)

solve :: [Int] -> [(Int, Int)] -> [Int]
solve [] _ = []
solve (h:hs) aphcs@((ph, pc):phcs) = cost : next
  where cost = minimum $ calc aphcs h
        next = solve hs $ phcs ++ [(h, cost)]

main :: IO ()
main = do
  [n, k] <- readIntsLine
  (h:hs) <- readIntsLine
  print . last . solve hs $ replicate k (h, 0)
