N, P = gets.split.map(&:to_i) H = gets.split.map(&:to_i) dp = Array.new(N) { Array.new(2, Float::INFINITY) } dp[0][0] = 0 dp[0][1] = 0 1.upto(N - 1) do |i| bh = H[i - 1] h = H[i] cost1 = dp[i - 1].min + P cost2 = dp[i - 1][0] + [0, h - bh].max cost3 = dp[i - 1][1] + [0, bh - h].max dp[i][0] = [cost1, cost2].min if i > 1 dp[i][1] = [cost1, cost3].min else dp[i][1] = cost1 end end puts dp[N - 1].min