import qualified Data.Vector.Unboxed as U
import qualified Data.ByteString.Char8 as C
import Data.List

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

    let dp = U.constructN n (\dp_ ->
            if U.null dp_ then 0
                else let i = U.length dp_
                         refh = U.slice (max 0 (i-k)) (min i k) h
                         refd = U.slice (max 0 (i-k)) (min i k) dp_
                    in U.minimum (
                        U.zipWith (\dc hc -> dc + abs(h U.! i - hc)) refd refh
                        )
            )

    print $ U.last dp

