import qualified Data.Vector.Unboxed as U
import qualified Data.Vector.Mutable as M
import qualified Data.ByteString.Char8 as C
import Data.List
import Control.Monad

r :: Int -> IO (U.Vector Int)
r n = U.unfoldrN n (C.readInt . C.dropWhile (==' ')) <$> C.getLine

r' :: IO [Int]
r'  =   unfoldr    (C.readInt . C.dropWhile (==' ')) <$> C.getLine

main = do
    [n,k] <- r'
    h <- r n

    dp <- M.new n
    M.write dp 0 0

    step <- M.new k
    forM_ [1..n-1] (\i -> do
        forM_ [0..min k i - 1] (\j -> do
            rd <- M.read dp (i-j-1)
            M.write step j $ rd + abs( h U.! i  -  h U.! (i-j-1) )
            )
        minss <- mins $ M.slice 0 (min i k) step
        M.write dp i minss 
        )

    res <- M.read dp (n-1)
    print res


mins v = minimum <$> toList v
toList v = mapM (M.read v) [0..M.length v-1]

