Language: C++

#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>
#include <cassert>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#define ALL(v) begin(v),end(v)
#else
#define ALL(v) (v).begin(),(v).end()
#endif

using namespace std;


namespace{
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
	v.assign(a, vector<T>(b, t));
}
template <class T> inline T &chmin(T &x, const T &y){ return x = min(x, y); }
template <class T> inline T &chmax(T &x, const T &y){ return x = max(x, y); }
template <class F, class T>
void convert(const F &f, T &t){
	stringstream ss;
	ss << f;
	ss >> t;
}


#define REP(i,n) for(int i=0;i<int(n);++i)
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8

struct union_find{
	typedef unsigned size_type;
	vector<int> par;
	size_type grs;
	explicit union_find(size_type n = 0){ init(n); }
	void init(size_type n){
		par.assign(n, -1);
		grs = n;
	}
	size_type find(size_type x){
		if(par[x] < 0){ return x; }
		return par[x] = find(par[x]);
	}
	size_type size(size_type x){
		return -par[find(x)];
	}
	size_type groups(){
		return grs;
	}
	bool unite(size_type x, size_type y){
		x = find(x);
		y = find(y);
		if(x == y){ return false; }
		if(par[x] < par[y]){ swap(x, y); }
		par[y] += par[x];
		par[x] = y;
		--grs;
		return true;
	}
	bool same(size_type x, size_type y){
		return find(x) == find(y);
	}
	bool is_root(size_type x){
		return find(x) == x;
	}
};

void mainmain(){
	const LL INF = 1LL << 60;

	int n;
	LL d;
	scanf("%d%lld", &n, &d);
	vector<LL> as(n);
	for(int i = 0; i < n; ++i){ scanf("%lld", &as[i]); }

	union_find uf(n);
	LL ans = 0;

	vector<pll> dp1, dp2;
	vector<pll> top2;
	vector<tuple<LL,int,int>> es;
	const pll def(INF, -1);
	while(uf.groups() > 1){
		dp1.assign(n, def);
		dp2.assign(n, def);

		top2.clear();
		for(int i = n - 1; i >= 0; --i){
			int u = uf.find(i);
			for(cauto t : top2){
				if(t.second != u){
					dp1[i] = pll(t.first + as[i] - i * d, t.second);
					break;
				}
			}
			pll p(as[i] + i * d, u);
			top2.insert(lower_bound(ALL(top2), p), p);
			for(int j = 0; j < (int)top2.size(); ++j){
				for(int k = j + 1; k < (int)top2.size(); ++k){
					if(top2[j].second == top2[k].second){
						top2[k] = def;
					}
				}
			}
			top2.erase(remove(ALL(top2), def), top2.end());
			if(top2.size() > 2){ top2.pop_back(); }
		}
		top2.clear();
		for(int i = 0; i < n; ++i){
			int u = uf.find(i);
			for(cauto t : top2){
				if(t.second != u){
					dp2[i] = pll(t.first + as[i] + i * d, t.second);
					break;
				}
			}
			pll p(as[i] - i * d, u);
			top2.insert(lower_bound(ALL(top2), p), p);
			for(int j = 0; j < (int)top2.size(); ++j){
				for(int k = j + 1; k < (int)top2.size(); ++k){
					if(top2[j].second == top2[k].second){
						top2[k] = def;
					}
				}
			}
			top2.erase(remove(ALL(top2), def), top2.end());
			if(top2.size() > 2){ top2.pop_back(); }
		}

		es.assign(n, def);
		for(int i = 0; i < n; ++i){
			pll p = min(dp1[i], dp2[i]);
			int u = uf.find(i);
			chmin(es[u], p);
		}
		sort(ALL(es));
		for(int i = 0; i < n; ++i){
			cauto t = es[i];
			if(t.second >= 0){
				if(uf.unite(i, t.second)){
					ans += t.first;
				}
			}
		}
	}
	printf("%lld\n", ans);
}



}
int main() try{
//	ios::sync_with_stdio(false); cin.tie(0);
	cout << fixed << setprecision(10);
	cerr << fixed << setprecision(4);
	mainmain();
}
catch(...){}


Compiler output:
a.cc: In function 'void {anonymous}::mainmain()':
a.cc:180:26: error: no matching function for call to 'std::vector<std::tuple<long long int, int, int> >::assign(int&, const {anonymous}::pll&)'
  180 |                 es.assign(n, def);
      |                 ~~~~~~~~~^~~~~~~~
In file included from /usr/include/c++/14/vector:66,
                 from a.cc:11:
/usr/include/c++/14/bits/stl_vector.h:828:9: note: candidate: 'template<class _InputIterator, class> void std::vector<_Tp, _Alloc>::assign(_InputIterator, _InputIterator) [with <template-parameter-2-2> = _InputIterator; _Tp = std::tuple<long long int, int, int>; _Alloc = std::allocator<std::tuple<long long int, int, int> >]'
  828 |         assign(_InputIterator __first, _InputIterator __last)
      |         ^~~~~~
/usr/include/c++/14/bits/stl_vector.h:828:9: note:   template argument deduction/substitution failed:
a.cc:180:26: note:   deduced conflicting types for parameter '_InputIterator' ('int' and 'std::pair<long long int, long long int>')
  180 |                 es.assign(n, def);
      |                 ~~~~~~~~~^~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:808:7: note: candidate: 'void std::vector<_Tp, _Alloc>::assign(size_type, const value_type&) [with _Tp = std::tuple<long long int, int, int>; _Alloc = std::allocator<std::tuple<long long int, int, int> >; size_type = long unsigned int; value_type = std::tuple<long long int, int, int>]'
  808 |       assign(size_type __n, const value_type& __val)
      |       ^~~~~~
/usr/include/c++/14/bits/stl_vector.h:808:47: note:   no known conversion for argument 2 from 'const {anonymous}::pll' {aka 'const std::pair<long long int, long long int>'} to 'const std::vector<std::tuple<long long int, int, int> >::value_type&' {aka 'const std::tuple<long long int, int, int>&'}
  808 |       assign(size_type __n, const value_type& __val)
      |                             ~~~~~~~~~~~~~~~~~~^~~~~
/usr/include/c++/14/bits/stl_vector.h:855:7: note: candidate: 'void std::vector<_Tp, _Alloc>::assign(std::initializer_list<_Tp>) [with _Tp = std::tuple<long long int, int, int>; _Alloc = std::allocator<std::tuple<long long int, int, int> >]'
  855 |       assign(initializer_list<value_type> __l)
      |       ^~~~~~
/usr/include/c++/14/bits/stl_vector.h:855:7: note:   candidate expects 1 argument, 2 provided
a.cc:184:30: error: no matching function for call to 'chmin(__gnu_cxx::__alloc_traits<std::allocator<std::tuple<long long int, int, int> >, std::tuple<long long int, int, int> >::value_type&, {anonymous}::pll&)'
  184 |                         chmin(es[u], p);
      |                         ~~~~~^~~~~~~~~~
a.cc:63:30: note: candidate: 'template<class T> T& {anonymous}::chmin(T&, const T&)'
   63 | template <class T> inline T &chmin(T &x, const T &y){ return x = min(x, y); }
      |                              ^~~~~
a.cc:63:30: note:   template argument deduction/substitution failed:
a.cc:184:30: note:   deduced conflicting types for parameter 'const T' ('std::tuple<long long int, int, int>' and '{anonymous}::pll' {aka 'std::pair<long long int, long long int>'})
  184 |                         chmin(es[u], p);
      |                         ~~~~~^~~~~~~~~~
a.cc:189:30: error: 'const class std::tuple<long long int, int, int>' has no member named 'second'
  189 |                         if(t.second >= 0){
      |                              ^~~~~~
a.cc:190:50: error: 'const class std::tuple<long long int, int, int>' has no member named 'second'
  190 |                                 if(uf.unite(i, t.second)){
      |                                                  ^~~~~~
a.cc:191:50: error: 'const class std::tuple<long long int, int, int>' has no member named 'first'
  191 |                                         ans += t.first;
      |                                                  ^~~~~