Language: C++

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define all(c) begin(c),end(c)

template<typename T1, typename T2>
inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }

template<typename T1, typename T2>
inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

//改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007


//ここから編集する
class UnionFind {
    vector<int> p;//p[i]はiの属する組織
public:
    UnionFind(int n) {
        p = vector<int>(n);
        for (int i = 0; i < n; i++) {
            p[i] = i;
        }
        return;
    }

/* xの属する集合を返す */
    int find(int x) {
        if (p[x] == x) return x;
        return p[x] = find(p[x]);
    }

/* yにxを統合する */
    void unite(int x, int y) {
        p[find(x)] = p[find(y)];
    }

/* xとyが属する集合が同じかを判定する */
    bool same(int x, int y) {
        return find(x) == find(y);
    }

};


class Solve {
public:
    int N;
    ll D;
    vector<ll> A;

    struct edge {
        int from;
        int to;
        ll cost;

        edge(int from, int to, ll cost) : from(from), to(to), cost(cost) {

        }
    };

    vector<edge> edges;

    void input() {
        cin >> N;
        cin >> D;
        A.resize(N);
        rep(i, 0, N) cin >> A[i];
    }

    void add_edge() {
        int pt = 0;
        ll ecost = A[pt] + D;

        for (int i = 1; i < N; ++i) {
            if (ecost < A[i - 1] + D) {
//                edges.emplace_back(pt, i, );
            } else {
                pt = i - 1;
                ecost = A[pt] + D;
            }
            edges.emplace_back(pt, i, A[i] + ecost);
            ecost += D;
        }

        pt = N - 1;
        ecost = A[pt] + D;

        for (int i = N - 2; i >= 0; --i) {
            if (ecost < A[i + 1] + D) {

            } else {
                pt = i + 1;
                ecost = A[pt] + D;
            }
            edges.emplace_back(pt, i, A[i] + ecost);
            ecost += D;
        }
    }


    void solve() {
        input();
        add_edge();

        UnionFind uf(N);

        sort(all(edges), [](edge a, edge b) {
            return a.cost < b.cost;
        });

        ll ans = 0ll;
        int cnt = 0;
        for (auto e : edges) {
            int a = e.from;
            int b = e.to;
            ll cost = e.cost;
            if (!uf.same(a, b)) {
                uf.unite(a, b);
                ans += cost;
                cnt++;
            }
        }

        assert(cnt == N - 1);

        cout << ans << endl;

    }
};


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    Solve().solve();


    return 0;
}


Compiler output:
a.cc: In member function 'void Solve::solve()':
a.cc:164:9: error: 'assert' was not declared in this scope
  164 |         assert(cnt == N - 1);
      |         ^~~~~~
a.cc:34:1: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
   33 | #include <climits>
  +++ |+#include <cassert>
   34 |