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

a#b = abs$a-b

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

inf = 10^18
f n k h = UV.last $ UV.foldl' proc (UV.singleton 0) $ UV.enumFromTo (1::Int) (n-1) where
    proc v0 dhi
        | dhi < k = UV.snoc v0 $ UV.minimum $ UV.zipWith m v0 h
        | otherwise = UV.tail $ UV.snoc v0 $ UV.minimum $ UV.zipWith m v0 $ UV.slice (dhi-k) k h
        where
            m sv sh = sv+sh# (h UV.! dhi)