import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto s = readln.split; auto N = s[0].to!int; auto P = s[1].to!long; auto H = readln.split.map!(to!long).array; long[] peaks; int p; while (p < N-1 && H[p] >= H[p+1]) ++p; peaks ~= p; if (p == N-1) { writeln(0); return; } bool up = true; while (p < N-1) { if (up && H[p] > H[p+1]) { peaks ~= p; up = false; } else if (!up && H[p] < H[p+1]) { peaks ~= p; up = true; } ++p; } if (up && H[$-1] >= H[$-2]) peaks ~= N-1; long ans = 0; p = 1; while (p < peaks.length) { if (H[peaks[p]] - H[peaks[p-1]] < P) ans += H[peaks[p]] - H[peaks[p-1]]; else ans += P; p += 2; } ans.writeln; }