Language: C++

#include <numeric>
#include <iostream>
#include <queue>
#include <vector>
#include <cassert>
// https://atcoder.jp/contests/keyence2019/tasks/keyence2019_e
#define SHOW(...) static_cast<void>(0)
#define NDEBUG
//!=======================================================================================!//
//!   a88888b.                     dP       .88888.                             dP        !//
//!  d8'   '88                     88      d8'   '88                            88        !//
//!  88        .d8888b. .d8888b. d8888P    88        88d888b. .d8888b. 88d888b. 88d888b.  !//
//!  88        88'  '88 Y8ooooo.   88      88   YP88 88'  '88 88'  '88 88'  '88 88'  '88  !//
//!  Y8.   .88 88.  .88       88   88      Y8.   .88 88       88.  .88 88.  .88 88    88  !//
//!   Y88888P' '88888P' '88888P'   dP       '88888'  dP       '88888P8 88Y888P' dP    dP  !//
//!                                                                    88                 !//
//!                                                                    dP                 !//
//!=======================================================================================!//
template <typename T>
struct CostGraph
{
    CostGraph(const std::size_t v) : V{v}, edge(v), rev_edge(v) {}
    void addEdge(const std::size_t from, const std::size_t to, const T cost, const bool bi = false)
    {
        assert(from < V), assert(to < V);
        edge[from].push_back(Edge{to, cost}), rev_edge[to].push_back(Edge(from, cost));
        if (bi) { addEdge(to, from, cost, false); }
    }
    struct Edge
    {
        Edge(const std::size_t to, const T cost) : to{to}, cost{cost} {}
        const std::size_t to;
        const T cost;
        bool operator<(const Edge& e) const { return cost != e.cost ? cost < e.cost : to < e.to; }
    };
    const std::vector<Edge>& operator[](const std::size_t i) const { return assert(i < V), edge[i]; }
    friend std::ostream& operator<<(std::ostream& os, const CostGraph& g)
    {
        os << "[\n";
        for (std::size_t i = 0; i < g.V; i++) {
            for (const auto& e : g.edge[i]) { os << i << "->" << e.to << ":" << e.cost << "\n"; }
        }
        return (os << "]\n");
    }
    const std::size_t V;
    std::vector<std::vector<Edge>> edge, rev_edge;
};
template <typename T>
CostGraph<T> Voronoi(const CostGraph<T>& G, const std::vector<std::size_t>& S)
{
    using P = std::pair<std::size_t, std::size_t>;
    using PP = std::pair<T, P>;
    constexpr T INF = std::numeric_limits<T>::max();
    const std::size_t V = G.V, N = S.size();
    std::vector<T> D(V, INF);
    std::vector<std::size_t> from(V, V);
    std::priority_queue<PP, std::vector<PP>, std::greater<PP>> Q;
    for (std::size_t i = 0; i < N; i++) { Q.push({T{}, {S[i], S[i]}}); }
    CostGraph<T> ans(V);
    while (not Q.empty()) {
        const auto p = Q.top();
        Q.pop();
        const T d = p.first;
        const std::size_t s = p.second.first, f = p.second.second;
        if (D[s] <= d) { continue; }
        D[s] = d, from[s] = f;
        for (const auto& e : G[s]) {
            const std::size_t to = e.to;
            const T cost = e.cost;
            if (D[to] == INF) {
                Q.push({d + cost, {to, from[s]}});
            } else {
                if (from[s] != from[to]) { ans.addEdge(from[to], from[s], D[to] + D[s] + cost, true); }
            }
        }
    }
    return ans;
}
using ll = long long;
int main()
{
    std::size_t N;
    ll D;
    std::cin >> N >> D;
    std::vector<ll> A(N);
    for (std::size_t i = 0; i < N; i++) { std::cin >> A[i]; }
    CostGraph<ll> G(2 * N);
    for (std::size_t i = 0; i < N; i++) { G.addEdge(i, i + N, A[i], true); }
    for (std::size_t i = 0; i < N - 1; i++) { G.addEdge(i, i + 1, D, true); }
    std::vector<std::size_t> S(N);
    std::iota(S.begin(), S.end(), N);
    const auto T = Voronoi(G, S);
    ll ans = 0;
    for (std::size_t i = N; i < 2 * N; i++) {
        for (const auto& e : T[i]) { ans += e.cost; }
    }
    std::cout << ans / 2 << std::endl;
    return 0;
}


Compiler output:
a.cc: In function 'CostGraph<T> Voronoi(const CostGraph<T>&, const std::vector<long unsigned int>&)':
a.cc:53:28: error: 'numeric_limits' is not a member of 'std'
   53 |     constexpr T INF = std::numeric_limits<T>::max();
      |                            ^~~~~~~~~~~~~~
a.cc:53:44: error: expected primary-expression before '>' token
   53 |     constexpr T INF = std::numeric_limits<T>::max();
      |                                            ^
a.cc:53:47: error: '::max' has not been declared; did you mean 'std::max'?
   53 |     constexpr T INF = std::numeric_limits<T>::max();
      |                                               ^~~
      |                                               std::max
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:2:
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: 'std::max' declared here
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~