import           Control.Monad
import           Data.Array.ST
import           Data.Array.Unboxed

main :: IO ()
main = do
  [n,k] <- map read . words <$> getLine :: IO [Int]
  hs <- listArray (1,n) . map read . words <$> getLine :: IO (UArray Int Int)
  print $ solve hs n k ! n

solve :: UArray Int Int -> Int -> Int -> UArray Int Int
solve hs n k = runSTUArray $ do
  dp <- newArray (1,n) 0
  forM_ [1..n] $
    \x -> case x of
      1 -> writeArray dp 1 0
      _ -> minimum <$> forM [(max (x-k) 1)..(x-1)] (\y -> liftM2 (+) (readArray dp y) (return . abs $ hs!x - hs!y)) >>=  writeArray dp x
  return dp
