#include #define show(x) cerr << #x << " = " << x << endl using namespace std; using ll = long long; template ostream& operator<<(ostream& os, const vector& v) { os << "sz:" << v.size() << "\n["; for (const auto& p : v) { os << p << ","; } os << "]\n"; return os; } template ostream& operator<<(ostream& os, const pair& p) { os << "(" << p.first << "," << p.second << ")"; return os; } constexpr ll MOD = (ll)1e9 + 7LL; template constexpr T INF = numeric_limits::max() / 64; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; ll P; cin >> N >> P; vector H(N); for (int i = 0; i < N; i++) { cin >> H[i]; } vector point; bool falling = true; for (int i = 0; i < N - 1; i++) { if (falling and H[i + 1] > H[i]) { point.push_back(H[i]); falling = false; } else if ((not falling) and H[i + 1] < H[i]) { point.push_back(H[i]); falling = true; } } point.push_back(H[N - 1]); const int size = point.size(); ll sum = 0; for (int i = 0; i < size - 1; i++) { if (i % 2 == 0) { sum += min(P, point[i + 1] - point[i]); } } cout << sum << endl; return 0; }