#include using namespace std; typedef signed long long ll; #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x<(to);x++) #define FORR(x,arr) for(auto& x:arr) #define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) //------------------------------------------------------- int N; ll P,H[202020]; ll sc[202020]; int nex[202020]; void solve() { int i,j,k,l,r,x,y; string s; cin>>N>>P; FOR(i,N) cin>>H[i]; nex[N-1]=N-1; for(i=N-2;i>=0;i--) { if(H[i]<=H[i+1]) nex[i]=nex[i+1]; else nex[i]=i; } FOR(i,N) sc[i]=1LL<<60; ll ret=0; int cur=0; sc[0]=0; while(cur=H[cur+1]) { sc[cur+1]=min(sc[cur],sc[cur+1]); } else { sc[cur+1]=min(sc[cur+1],sc[cur]+H[cur+1]-H[cur]); sc[cur+1]=min(sc[cur+1],sc[cur]+P); sc[nex[cur]]=min(sc[nex[cur]],sc[cur]+2*P); if(nex[cur]==N-1) sc[nex[cur]]=min(sc[nex[cur]],sc[cur]+P); } cur++; } cout<