#include #include #include using namespace std; // A large enough value for infinity, considering N * max_H_diff or (N-1)*P // Max N = 2e5, Max H_diff = 1e9 -> 2e14 // Max P = 1e9, (N-1)*P approx 2e5 * 1e9 = 2e14 const long long INF = 4e14 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; long long p; cin >> n >> p; vector h(n); for (int i = 0; i < n; ++i) { cin >> h[i]; } // Constraints state N >= 2. If N=1 was allowed, cost is 0. // if (n == 1) { // cout << 0 << endl; // return 0; // } // pre_walk_cost[i] = min cost to walk from location 0 to location i vector pre_walk_cost(n); pre_walk_cost[0] = 0; for (int i = 0; i < n - 1; ++i) { pre_walk_cost[i + 1] = pre_walk_cost[i] + max(0LL, h[i + 1] - h[i]); } // suf_walk_cost[i] = min cost to walk from location n-1 to location i (backwards) vector suf_walk_cost(n); suf_walk_cost[n - 1] = 0; for (int i = n - 2; i >= 0; --i) { suf_walk_cost[i] = suf_walk_cost[i + 1] + max(0LL, h[i] - h[i + 1]); } long long min_total_cost = INF; // Strategy 1: 0 teleports, walk 0 -> n-1 // All locations {0, ..., n-1} are visited. min_total_cost = pre_walk_cost[n - 1]; // Strategy 2: 1 teleport // Walk 0..k, teleport from k to n-1, then walk (n-1)..(k+1). // This covers locations {0, ..., k} and {k+1, ..., n-1}. // k is the 0-indexed end location of the first segment. for (int k = 0; k < n - 1; ++k) { long long current_strategy_cost = pre_walk_cost[k] + p + suf_walk_cost[k + 1]; min_total_cost = min(min_total_cost, current_strategy_cost); } // Strategy 3: N-1 teleports (to visit N-1 new locations after starting at loc 0) // Start at 0. Teleport to 1 (cost P), then from 1 to 2 (cost P), ..., then from n-2 to n-1 (cost P). // This requires (n-1) teleports. // (N-1)*P can be 0 if P=0. min_total_cost = min(min_total_cost, (long long)(n - 1) * p); cout << min_total_cost << endl; return 0; }