#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define REP(i,n) for(int i=0; i=b; --i) #define ALL(c) (c).begin(), (c).end() typedef long long ll; typedef vector VI; typedef vector VL; typedef vector VVL; typedef vector VVI; typedef pair P; typedef pair PL; int main() { int n; ll p; cin >> n >> p; VL h(n); REP(i,n) scanf("%lld", &h[i]); VL r(n); REP(i,n-1){ r[i+1] = r[i] + min(p, max(0LL, h[i+1] - h[i])); } VL l(n); FORR(i,n-2,0){ l[i] = l[i+1] + min(p, max(0LL, h[i] - h[i+1])); } REP(i,n-1){ r[i+1] = min(r[i+1], r[i] + min(p, max(0LL, h[i+1] - h[i]))); FOR(j,i+1,n-2){ r[j+1] = min(r[j+1], r[i] + l[i+1] - l[j] + 2*p); } r[n-1] = min(r[n-1], r[i] + l[i+1] - l[n-1] + p); } cout << r[n-1] << endl; return 0; }