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

inf = (maxBound::Int) `div` 2

f n k vh = UV.last $ UV.constructN n m where
    m v0
        | i == 0 = 0
        | otherwise = minimum $ map (\j->((vh UV.! i) # (vh UV.! j)) + v0 UV.! j) [(max 0 (i-k))..i-1]
        where i = UV.length v0
