#include using namespace std; using pii = pair; using ll = long long; #define rep(i, j) for(int i=0; i < (int)(j); i++) #define repeat(i, j, k) for(int i = (j); i < (int)(k); i++) #define all(v) v.begin(),v.end() #define debug(x) cerr << #x << " : " << x << endl template bool set_min(T &a, const T &b) { return a > b ? a = b, true : false; } template bool set_max(T &a, const T &b) { return a < b ? a = b, true : false; } // vector template istream& operator >> (istream &is , vector &v) { for(T &a : v) is >> a; return is; } template ostream& operator << (ostream &os , const vector &v) { for(const T &t : v) os << "\t" << t; return os << endl; } // pair template ostream& operator << (ostream &os , const pair &v) { return os << "<" << v.first << ", " << v.second << ">"; } const int INF = 1 << 30; const ll INFL = 1LL << 60; class Solver { public: bool solve() { ll N, P; cin >> N >> P; vector H(N); cin >> H; auto gezan = [&] (int &i) { ll sum = 0; while(i + 1 < N and H[i] >= H[i + 1]) { sum += H[i] - H[i + 1]; i++; } return sum; }; auto tozan = [&] (int &i) { ll sum = 0; while(i + 1 < N and H[i] <= H[i + 1]) { sum += H[i + 1] - H[i]; i++; } return sum; }; int i = 0; vector> arr; gezan(i); while(i + 1 < N) { arr.push_back(make_pair(tozan(i), arr.size())); arr.push_back(make_pair(gezan(i), arr.size())); } if(arr.size() > 0 and arr.back().first == 0) arr.pop_back(); sort(all(arr)); vector used(N); ll ans = 0; rep(i, arr.size()) { ll cost = arr[i].first; int idx = arr[i].second; if(used[idx]) continue; used[idx] = true; if(idx + 1 < N) used[idx + 1] = true; if(idx > 0) used[idx - 1] = true; ans += min(P, cost); } cout << ans << endl; return 0; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); Solver s; s.solve(); return 0; }