Language: C++

#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <utility>
#include <algorithm>
#include <functional>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <limits>
#include <numeric>
#include <valarray>
#include <fstream>

using namespace std;
typedef unsigned int uint;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, LL> PP;
#define REP(i, a, n) for(LL i = (a), i##_max = (n); i < i##_max; ++i)
#define REM(i, a, n) for(LL i = (LL)(n) - 1, i##min = (a); i >= i##min; --i)
#define ALL(arr) (arr).begin(), (arr).end()
#define FLOAT fixed << setprecision(16)
#define SPEEDUP {cin.tie(NULL); ios::sync_with_stdio(false);}
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const string INFSTR = "\x7f";
const double EPS = 1.0e-9;

void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U>
istream& operator>>(istream& ist, pair<T, U>& right) { return ist >> right.first >> right.second; }
template <class T, class U>
ostream& operator<<(ostream& ost, const pair<T, U>& right) { return ost << right.first << ' ' << right.second; }
template <class T, class TCompatible, size_t N>
void Fill(T(&dest)[N], const TCompatible& val) { fill(dest, dest + N, val); }
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T(&dest)[M][N], const TCompatible& val) { for (int i = 0; i < M; ++i) Fill(dest[i], val); }
template<class T>
T Compare(T left, T right) { return left > right ? 1 : (left < right ? -1 : 0); }
istream& Ignore(istream& ist) { string s; ist >> s; return ist; }
bool Inside(int i, int j, int h, int w) { return i >= 0 && i < h && j >= 0 && j < w; }
template <class T>
T Next() { T buf; cin >> buf; return buf; }

#ifdef ONLY_MY_ENVIR
#include "IntMod.h"
#include "UnionFind.h"
#include "Graph.h"
#include "Range.h"
#include "Global.h"
#include "FlowSolver.h"
#include "Tree.h"
#include "SuffixArray.h"
#include "Geometry.h"
#include "Matrix.h"
#include "SegmentTree.h"
#include "SegmentTree2D.h"
#include "BIT.h"
#include "Rational.h"
#include "Position.h"
#include "Factorization.h"
#include "Math.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream& operator>> (istream& ist, __int128& val) { LL tmp;  ist >> tmp; val = tmp; return ist; }
ostream& operator<< (ostream& ost, __int128 val) { LL tmp = val; ost << tmp; return ost; }
#endif

#if 1234567891
#include <array>
#include <random>
#include <unordered_set>
#include <unordered_map>
template<typename T>
using PriorityQ = priority_queue<T, vector<T>, greater<T> >;
// template <class T>
// auto Is(const T& value) { return [value](const auto& comparand) -> bool { return comparand == value; }; }
#endif

struct MinimumPP {
	using Type = PP;
	static Type Identity() {
		return PP(INFLL, INFLL);
	}
	static Type Operate(const Type& left, const Type& right) {
		return left < right ? left : right;
	}
};


LL N, D;
LL A[200000];
SegmentTree<MinimumPP> Asc(18);
SegmentTree<MinimumPP> Dsc(18);


const LL Huge = INFLL;
int main() {
	cin >> N >> D;
	REP(i, 0, N) {
		cin >> A[i];
		Asc.Update(i, PP(A[i] + i * D, i));
		Dsc.Update(i, PP(A[i] - i * D, i));
	}
	
	LL sum = 0;
	PriorityQ<pair<LL, PP>> Q;
	auto mn = Asc.Query(1, N);
	Q.push(make_pair(mn.first + A[0], PP(0, mn.second)));
	Asc.Update(0, PP(Huge, 0));
	Dsc.Update(0, PP(Huge, 0));

	int cnt = 1;
	while (cnt < N) {
		auto p = Q.top(); Q.pop();
		LL cost = p.first;
		int from = p.second.first;
		int to = p.second.second;

		if (Asc.Get(to).first >= Huge) {
			auto mn = Asc.Query(from + 1, N) - PP(D * from, 0);
			mn = min(mn, Dsc.Query(0, from) + PP(D * from, 0));

			Q.push(make_pair(mn.first + A[from], PP(from, mn.second)));
			continue;
		}

		Asc.Update(to, PP(Huge, to));
		Dsc.Update(to, PP(Huge, to));
		++cnt;
		sum += cost;

		auto mn2 = Asc.Query(from + 1, N) - PP(D * from, 0);
		mn2 = min(mn2, Dsc.Query(0, from) + PP(D * from, 0));
		Q.push(make_pair(mn2.first + A[from], PP(from, mn2.second)));

		auto mn = Asc.Query(to + 1, N) - PP(D * to, 0);
		mn = min(mn, Dsc.Query(0, to) + PP(D * to, 0));
		Q.push(make_pair(mn.first + A[to], PP(to, mn.second)));

	}
	cout << sum << endl;
	return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <utility>
#include <algorithm>
#include <functional>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <limits>
#include <numeric>
#include <valarray>
#include <fstream>

using namespace std;
typedef unsigned int uint;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, LL> PP;
#define REP(i, a, n) for(LL i = (a), i##_max = (n); i < i##_max; ++i)
#define REM(i, a, n) for(LL i = (LL)(n) - 1, i##min = (a); i >= i##min; --i)
#define ALL(arr) (arr).begin(), (arr).end()
#define FLOAT fixed << setprecision(16)
#define SPEEDUP {cin.tie(NULL); ios::sync_with_stdio(false);}
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const string INFSTR = "\x7f";
const double EPS = 1.0e-9;

void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U>
istream& operator>>(istream& ist, pair<T, U>& right) { return ist >> right.first >> right.second; }
template <class T, class U>
ostream& operator<<(ostream& ost, const pair<T, U>& right) { return ost << right.first << ' ' << right.second; }
template <class T, class TCompatible, size_t N>
void Fill(T(&dest)[N], const TCompatible& val) { fill(dest, dest + N, val); }
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T(&dest)[M][N], const TCompatible& val) { for (int i = 0; i < M; ++i) Fill(dest[i], val); }
template<class T>
T Compare(T left, T right) { return left > right ? 1 : (left < right ? -1 : 0); }
istream& Ignore(istream& ist) { string s; ist >> s; return ist; }
bool Inside(int i, int j, int h, int w) { return i >= 0 && i < h && j >= 0 && j < w; }
template <class T>
T Next() { T buf; cin >> buf; return buf; }

#ifdef ONLY_MY_ENVIR
#include "IntMod.h"
#include "UnionFind.h"
#include "Graph.h"
#include "Range.h"
#include "Global.h"
#include "FlowSolver.h"
#include "Tree.h"
#include "SuffixArray.h"
#include "Geometry.h"
#include "Matrix.h"
#include "SegmentTree.h"
#include "SegmentTree2D.h"
#include "BIT.h"
#include "Rational.h"
#include "Position.h"
#include "Factorization.h"
#include "Math.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream& operator>> (istream& ist, __int128& val) { LL tmp;  ist >> tmp; val = tmp; return ist; }
ostream& operator<< (ostream& ost, __int128 val) { LL tmp = val; ost << tmp; return ost; }
#endif

#if 1234567891
#include <array>
#include <random>
#include <unordered_set>
#include <unordered_map>
template<typename T>
using PriorityQ = priority_queue<T, vector<T>, greater<T> >;
// template <class T>
// auto Is(const T& value) { return [value](const auto& comparand) -> bool { return comparand == value; }; }
#endif

struct MinimumPP {
	using Type = PP;
	static Type Identity() {
		return PP(INFLL, INFLL);
	}
	static Type Operate(const Type& left, const Type& right) {
		return left < right ? left : right;
	}
};


LL N, D;
LL A[200000];
SegmentTree<MinimumPP> Asc(18);
SegmentTree<MinimumPP> Dsc(18);


const LL Huge = INFLL;
int main() {
	cin >> N >> D;
	REP(i, 0, N) {
		cin >> A[i];
		Asc.Update(i, PP(A[i] + i * D, i));
		Dsc.Update(i, PP(A[i] - i * D, i));
	}
	
	LL sum = 0;
	PriorityQ<pair<LL, PP>> Q;
	auto mn = Asc.Query(1, N);
	Q.push(make_pair(mn.first + A[0], PP(0, mn.second)));
	Asc.Update(0, PP(Huge, 0));
	Dsc.Update(0, PP(Huge, 0));

	int cnt = 1;
	while (cnt < N) {
		auto p = Q.top(); Q.pop();
		LL cost = p.first;
		int from = p.second.first;
		int to = p.second.second;

		if (Asc.Get(to).first >= Huge) {
			auto mn = Asc.Query(from + 1, N) - PP(D * from, 0);
			mn = min(mn, Dsc.Query(0, from) + PP(D * from, 0));

			Q.push(make_pair(mn.first + A[from], PP(from, mn.second)));
			continue;
		}

		Asc.Update(to, PP(Huge, to));
		Dsc.Update(to, PP(Huge, to));
		++cnt;
		sum += cost;

		auto mn2 = Asc.Query(from + 1, N) - PP(D * from, 0);
		mn2 = min(mn2, Dsc.Query(0, from) + PP(D * from, 0));
		Q.push(make_pair(mn2.first + A[from], PP(from, mn2.second)));

		auto mn = Asc.Query(to + 1, N) - PP(D * to, 0);
		mn = min(mn, Dsc.Query(0, to) + PP(D * to, 0));
		Q.push(make_pair(mn.first + A[to], PP(to, mn.second)));

	}
	cout << sum << endl;
	return 0;
}

Compiler output:
a.cc:110:1: error: 'SegmentTree' does not name a type
  110 | SegmentTree<MinimumPP> Asc(18);
      | ^~~~~~~~~~~
a.cc:111:1: error: 'SegmentTree' does not name a type
  111 | SegmentTree<MinimumPP> Dsc(18);
      | ^~~~~~~~~~~
a.cc: In function 'int main()':
a.cc:119:17: error: 'Asc' was not declared in this scope
  119 |                 Asc.Update(i, PP(A[i] + i * D, i));
      |                 ^~~
a.cc:120:17: error: 'Dsc' was not declared in this scope
  120 |                 Dsc.Update(i, PP(A[i] - i * D, i));
      |                 ^~~
a.cc:125:19: error: 'Asc' was not declared in this scope
  125 |         auto mn = Asc.Query(1, N);
      |                   ^~~
a.cc:128:9: error: 'Dsc' was not declared in this scope
  128 |         Dsc.Update(0, PP(Huge, 0));
      |         ^~~
a.cc: At global scope:
a.cc:199:11: error: redefinition of 'const int INF'
  199 | const int INF = 0x3FFFFFFF;
      |           ^~~
a.cc:37:11: note: 'const int INF' previously defined here
   37 | const int INF = 0x3FFFFFFF;
      |           ^~~
a.cc:200:10: error: redefinition of 'const LL INFLL'
  200 | const LL INFLL = 0x3FFFFFFF3FFFFFFF;
      |          ^~~~~
a.cc:38:10: note: 'const LL INFLL' previously defined here
   38 | const LL INFLL = 0x3FFFFFFF3FFFFFFF;
      |          ^~~~~
a.cc:201:14: error: redefinition of 'const double INFD'
  201 | const double INFD = 1.0e+308;
      |              ^~~~
a.cc:39:14: note: 'const double INFD' previously defined here
   39 | const double INFD = 1.0e+308;
      |              ^~~~
a.cc:202:14: error: redefinition of 'const std::string INFSTR'
  202 | const string INFSTR = "\x7f";
      |              ^~~~~~
a.cc:40:14: note: 'const std::string INFSTR' previously declared here
   40 | const string INFSTR = "\x7f";
      |              ^~~~~~
a.cc:203:14: error: redefinition of 'const double EPS'
  203 | const double EPS = 1.0e-9;
      |              ^~~
a.cc:41:14: note: 'const double EPS' previously defined here
   41 | const double EPS = 1.0e-9;
      |              ^~~
a.cc:205:6: error: redefinition of 'void YesNo(bool)'
  205 | void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
      |      ^~~~~
a.cc:43:6: note: 'void YesNo(bool)' previously defined here
   43 | void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
      |      ^~~~~
a.cc:206:6: error: redefinition of 'void YESNO(bool)'
  206 | void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
      |      ^~~~~
a.cc:44:6: note: 'void YESNO(bool)' previously defined here
   44 | void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
      |      ^~~~~
a.cc:208:10: error: redefinition of 'template<class T, class U> std::istream& operator>>(std::istream&, std::pair<_T1, _T2>&)'
  208 | istream& operator>>(istream& ist, pair<T, U>& right) { return ist >> right.first >> right.second; }
      |          ^~~~~~~~
a.cc:46:10: note: 'template<class T, class U> std::istream& operator>>(std::istream&, std::pair<_T1, _T2>&)' previously declared here
   46 | istream& operator>>(istream& ist, pair<T, U>& right) { return ist >> right.first >> right.second; }
      |          ^~~~~~~~
a.cc:210:10: error: redefinition of 'template<class T, class U> std::ostream& operator<<(std::ostream&, const std::pair<_T1, _T2>&)'
  210 | ostream& operator<<(ostream& ost, const pair<T, U>& right) { return ost << right.first << ' ' << right.second; }
      |          ^~~~~~~~
a.cc:48:10: note: 'template<class T, class U> std::ostream& operator<<(std::ostream&, const std::pair<_T1, _T2>&)' previously declared here
   48 | ostream& operator<<(ostream& ost, const pair<T, U>& right) { return ost << right.first << ' ' << right.second; }
      |          ^~~~~~~~
a.cc:212:6: error: redefinition of 'template<class T, class TCompatible, long unsigned int N> void Fill(T (&)[N], const TCompatible&)'
  212 | void Fill(T(&dest)[N], const TCompatible& val) { fill(dest, dest + N, val); }
      |      ^~~~
a.cc:50:6: note: 'template<class T, class TCompatible, long unsigned int N> void Fill(T (&)[N], const TCompatible&)' previously declared here
   50 | void Fill(T(&dest)[N], const TCompatible& val) { fill(dest, dest + N, val); }
      |      ^~~~
a.cc:214:6: error: redefinition of 'template<class T, class TCompatible, long unsigned int M, long unsigned int N> void Fill(T (&)[M][N], const TCompatible&)'
  214 | void Fill(T(&dest)[M][N], const TCompatible& val) { for (int i = 0; i < M; ++i) Fill(dest[i], val); }
      |      ^~~~
a.cc:52:6: note: 'template<class T, class TCompatible, long unsigned int M, long unsigned int N> void Fill(T (&)[M][N], const TCompatible&)' previously declared here
   52 | void Fill(T(&dest)[M][N], const TCompatible& val) { for (int i = 0; i < M; ++i) Fill(dest[i], val); }
      |      ^~~~
a.cc:216:3: error: redefinition of 'template<class T> T Compare(T, T)'
  216 | T Compare(T left, T right) { return left > right ? 1 : (left < right ? -1 : 0); }
      |   ^~~~~~~
a.cc:54:3: note: 'template<class T> T Compare(T, T)' previously declared here
   54 | T Compare(T left, T right) { return left > right ? 1 : (left < right ? -1 : 0); }
      |   ^~~~~~~
a.cc:217:10: error: redefinition of 'std::istream& Ignore(std::istream&)'
  217 | istream& Ignore(istream& ist) { string s; ist >> s; return ist; }
      |          ^~~~~~
a.cc:55:10: note: 'std::istream& Ignore(std::istream&)' previously defined here
   55 | istream& Ignore(istream& ist) { string s; ist >> s; return ist; }
      |          ^~~~~~
a.cc:218:6: error: redefinition of 'bool Inside(int, int, int, int)'
  218 | bool Inside(int i, int j, int h, int w) { return i >= 0 && i < h && j >= 0 && j < w; }
      |      ^~~~~~
a.cc:56:6: note: 'bool Inside(int, int, int, int)' previously defined here
   56 | bool Inside(int i, int j, int h, int w) { return i >= 0 && i < h && j >= 0 && j < w; }
      |      ^~~~~~
a.cc:220:3: error: redefinition of 'template<class T> T Next()'
  220 | T Next() { T buf; cin >> buf; return buf; }
      |   ^~~~
a.cc:58:3: note: 'template<class T> T Next()' previously declared here
   58 | T Next() { T buf; cin >> buf; return buf; }
      |   ^~~~
a.cc:244:10: error: redefinition of 'std::istream& operator>>(std::istream&, __int128&)'
  244 | istream& operator>> (istream& ist, __int128& val) { LL tmp;  ist >> tmp; val = tmp; return ist; }
      |          ^~~~~~~~
a.cc:82:10: note: 'std::istream& operator>>(std::istream&, __int128&)' previously defined here
   82 | istream& operator>> (istream& ist, __int128& val) { LL tmp;  ist >> tmp; val = tmp; return ist; }
      |          ^~~~~~~~
a.cc:245:10: error: redefinition of 'std::ostream& operator<<(std::ostream&, __int128)'
  245 | ostream& operator<< (ostream& ost, __int128 val) { LL tmp = val; ost << tmp; return ost; }
      |          ^~~~~~~~
a.cc:83:10: note: 'std::ostream& operator<<(std::ostream&, __int128)' previously defined here
   83 | ostream& operator<< (ostream& ost, __int128 val) { LL tmp = val; ost << tmp; return ost; }
      |          ^~~~~~~~
a.cc:259:8: error: redefinition of 'struct MinimumPP'
  259 | struct MinimumPP {
      |        ^~~~~~~~~
a.cc:97:8: note: previous definition of 'struct MinimumPP'
   97 | struct MinimumPP {
      |        ^~~~~~~~~
a.cc:270:4: error: redefinition of 'LL N'
  270 | LL N, D;
      |    ^
a.cc:108:4: note: 'LL N' previously declared here
  108 | LL N, D;
      |    ^
a.cc:270:7: error: redefinition of 'LL D'
  270 | LL N, D;
      |       ^
a.cc:108:7: note: 'LL D' previously declared here
  108 | LL N, D;
      |       ^
a.cc:271:4: error: redefinition of 'LL A [200000]'
  271 | LL A[200000];
      |    ^
a.cc:109:4: note: 'LL A [200000]' previously declared here
  109 | LL A[200000];
      |    ^
a.cc:272:1: error: 'SegmentTree' does not name a type
  272 | SegmentTree<MinimumPP> Asc(18);
      | ^~~~~~~~~~~
a.cc:273:1: error: 'SegmentTree' does not name a type
  273 | SegmentTree<MinimumPP> Dsc(18);
      | ^~~~~~~~~~~
a.cc:276:10: error: redefinition of 'const LL Huge'
  276 | const LL Huge = INFLL;
      |          ^~~~
a.cc:114:10: note: 'const LL Huge' previously defined here
  114 | const LL Huge = INFLL;
      |          ^~~~
a.cc:277:5: error: redefinition of 'int main()'
  277 | int main() {
      |     ^~~~
a.cc:115:5: note: 'int main()' previously defined here
  115 | int main() {
      |     ^~~~
a.cc: In function 'int main()':
a.cc:281:17: error: 'Asc' was not declared in this scope
  281 |                 Asc.Update(i, PP(A[i] + i * D, i));
      |                 ^~~
a.cc:282:17: error: 'Dsc' was not declared in this scope
  282 |                 Dsc.Update(i, PP(A[i] - i * D, i));
      |                 ^~~
a.cc:287:19: error: 'Asc' was not declared in this scope
  287 |         auto mn = Asc.Query(1, N);
      |                   ^~~
a.cc:290:9: error: 'Dsc' was not declared in this scope
  290 |         Dsc.Update(0, PP(Huge, 0));
      |         ^~~