{-# LANGUAGE RankNTypes #-}
    
import Control.Monad.State
import Control.Monad.ST
import Data.Array.ST
import Control.Arrow
import Control.Monad
import Data.List
import qualified Data.ByteString.Char8 as B
import qualified Data.Vector as V

main :: IO ()
main = readContents 2 >>= print . solve

solve :: [[Int]] -> Int
solve [[n, k], hs] = runST (initialize >>= evalMemo calc 0)
    where
        initialize :: ST s (Table s Int Int)
        initialize = newListArray (0, n - 1) $ repeat Nothing
        calc f i 
            | i < n - 1 = minimum <$> rs
            | otherwise = return 0
            where
                rs = mapM mf [(i + 1)..(min (n - 1) $ i + k)]
                mf j = (+ d) <$> f j
                    where
                        d = abs $ vs V.! i - vs V.! j

        vs = V.fromList hs

readLine :: IO [Int]
readLine = unfoldr (fmap (second (maybe B.empty snd . B.uncons)) . B.readInt) <$> B.getLine

readContents :: Int -> IO [[Int]]
readContents = flip replicateM readLine

type Table s a b = STArray s a (Maybe b)
type Memo s a b = a -> StateT (Table s a b) (ST s) b

evalMemo :: (Ix a) => (forall s. Memo s a b -> Memo s a b) -> a -> Table s a b -> ST s b
evalMemo f k = evalStateT (memoizeFix f k)

memoizeFix :: (Ix a) => (forall s. Memo s a b -> Memo s a b) -> forall s. Memo s a b
memoizeFix f k = get >>= (\arr -> lift (readArray arr k) >>= maybe (f (memoizeFix f) k >>= ((<$) <*> (lift . writeArray arr k . Just))) return)