#include #define all(vec) vec.begin(), vec.end() using namespace std; using ll = long long; using P = pair; constexpr ll INF = (1LL << 30) - 1LL; constexpr ll LINF = (1LL << 60) - 1LL; constexpr ll MOD = 1e9 + 7; template void chmin(T &a, T b) { a = min(a, b); } template void chmax(T &a, T b) { a = max(a, b); } int main() { cin.tie(0); ios::sync_with_stdio(false); ll n, p; cin >> n >> p; vector h(n); for (int i = 0; i < n; i++) { cin >> h[i]; } vector dp0(n, LINF), dp1(n, LINF); dp0[1] = min(p, max(0LL, h[1] - h[0])), dp1[1] = p; for (int i = 2; i < n; i++) { chmin(dp0[i], min(dp0[i - 1], dp1[i - 1]) + p); chmin(dp1[i], min(dp1[i - 1], dp0[i - 1]) + p); chmin(dp0[i], dp0[i - 1] + max(0LL, h[i] - h[i - 1])); chmin(dp1[i], dp1[i - 1] + max(0LL, h[i - 1] - h[i])); } cout << min(dp0[n - 1], dp1[n - 1]) << endl; }