import Data.Array

makeDpTable :: (Array Int Int) -> Int -> Int -> Int -> [Int] -> [Int]
makeDpTable a b c k ans
 | b == 0    = makeDpTable a (b + 1) c k (0:ans)
 | b == c    = ans
 | otherwise = makeDpTable a (b + 1) c k newAns
  where newVal = minimum $ map (\x -> ((ans !! x) + abs((a ! b) - (a ! (b - (x + 1)))))) [0..(min (k - 1) (b - 1))]
        newAns = newVal:ans
 
main = do
 [n,k] <- map read . words <$> getLine :: IO [Int]
 hList <- map read . words <$> getLine :: IO [Int]
 print $ head $ makeDpTable (listArray (0, n - 1) hList) 0 n k []