import Data.List main = do [_, p] <- map read . words <$> getLine h <- map read . words <$> getLine print $ solve p h solve :: Int -> [Int] -> Int solve p a = uncurry min . foldl' f (0, p) $ zip a (tail a) where f :: (Int, Int) -> (Int, Int) -> (Int, Int) f (dpR, dpL) (h0, h1) = (v0, v1) where r = max 0 (h1 - h0) l = max 0 (h0 - h1) v0 = minimum [dpR + r, dpR + p, dpL + p] v1 = minimum [dpL + l, dpL + p, dpR + p]