#include #define rep(i,a,b) for(int i=a;icnt += d; if (dig == -1) { x->val = a; return; } update(x->c[(a&(1 << dig))>0], dig - 1, a, d); } void dump() { dump(root); } void dump(Node*& x) { printf("[%d,%d,%d] (", x, x->cnt, x->val); if (x->c[0]) printf("%d) (", x->c[0]); else printf("##) ("); if (x->c[1]) printf("%d)\n", x->c[1]); else printf("##)\n"); if (x->c[0]) dump(x->c[0]); if (x->c[1]) dump(x->c[1]); } // get the minimal value that (x xor a) : x is one of all numbers // warning!! returned value is (x xor a) int inf = INT_MAX / 2; int getMinXorA(Node* n, int dig, int a) { if (dig < 0) { assert(n != NULL); return n->val ^ a; } int d = (a & (1 << dig)) > 0; int dd = 1 - d; if (n->c[d]) if (n->c[d]->cnt) return getMinXorA(n->c[d], dig - 1, a); return getMinXorA(n->c[dd], dig - 1, a); } int getMinXorA(int a) { return getMinXorA(root, 31, a); } // get the number of the variables satisfying that (x xor a) < k int countStrictlyLessThanKXorA(Node* n, int dig, int a, int k) { if (dig < 0) return 0; int d = (a & (1 << dig)) > 0; int dd = 1 - d; int f = (k & (1 << dig)) > 0; int res = 0; if (f == 1) { if (n->c[dd]) res += countStrictlyLessThanKXorA(n->c[dd], dig - 1, a, k); if (n->c[d]) res += n->c[d]->cnt; } else { if (n->c[d]) res += countStrictlyLessThanKXorA(n->c[d], dig - 1, a, k); } return res; } int countStrictlyLessThanKXorA(int a, int k) { return countStrictlyLessThanKXorA(root, 31, a, k); } // get the number of the variables satisfying that k <= (x xor a) int countGreaterThanKXorA(Node* n, int dig, int a, int k) { if (!n) return 0; if (dig < 0) return n->cnt; int res = 0; if (!(k&(1 << dig)) && n->c[!(a&(1 << dig))]) res += n->c[!(a&(1 << dig))]->cnt; return res + countGreaterThanKXorA(n->c[((k^a)&(1 << dig))>0], dig - 1, a, k); } int countGreaterThanKXorA(int a, int k) { return countGreaterThanKXorA(root, 31, a, k); } }; /*---------------------------------------------------------------------------------------------------             ∧_∧       ∧_∧  (´<_` )  Welcome to My Coding Space!      ( ´_ゝ`) /  ⌒i     /   \    | |     /   / ̄ ̄ ̄ ̄/  |   __(__ニつ/  _/ .| .|____      \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ typedef long long ll; #define INF 1LL<<60 int N; ll P, H[201010]; ll dp[201010][2]; //--------------------------------------------------------------------------------------------------- void _main() { cin >> N >> P; rep(i, 0, N) cin >> H[i]; rep(i, 0, 201010) rep(dir, 0, 2) dp[i][dir] = INF; dp[0][0] = 0; rep(i, 0, N - 1) { // ->を伸ばす遷移 dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + max(H[i + 1] - H[i], 0LL)); // 今まで->だったのを<-とする遷移 dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + P); // <-を伸ばす遷移 dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + max(H[i] - H[i + 1], 0LL)); // 今まで<-だったのを->とする遷移 dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + P); } ll ans = min(dp[N - 1][0], dp[N - 1][1]); cout << ans << endl; }