import qualified Data.ByteString.Char8 as B
import Data.List
import qualified Data.Vector.Unboxed as UV

a#b = abs$a-b

main = do
    [n,k] <- map read.words<$>getLine
    h <- UV.unfoldr(B.readInt.B.dropWhile(<'!'))<$>B.getLine
    print $ f n k h

f n k vh = UV.head $ foldl' p (UV.singleton 0) [1..n-1] where
    p l0 i = UV.cons x $ UV.take k l0 where
        x = minimum $ map (\j-> (vh UV.! j) # (vh UV.! i) + l0 UV.! (i-1-j)) [i-1,i-2..(max 0 (i-k))]
