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.head $ foldl' proc (UV.cons 0 $ UV.replicate (k-1) inf) [1..n-1] where
    proc v0 i = UV.zipWith p (UV.drop i vh) (UV.snoc (UV.tail v0) inf)
        where p h = min (UV.head v0 + h # (vh UV.! (i-1)))
