Language: C++


#if 1
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <array>
#include <deque>
#include <algorithm>
#include <utility>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <numeric>
#include <assert.h>
#include <bitset>
#include <list>

auto& in = std::cin;
auto& out = std::cout;
#define all_range(C) std::begin(C), std::end(C)
const double PI = 3.141592653589793238462643383279502884197169399375105820974944;



#include <vector>

class union_find
{
private:
	std::vector<int>parent;
	std::vector<int>rank;
	std::vector<int>size;
public:
	union_find(int siz)
	{
		parent.resize(siz);
		rank.resize(siz);
		size.resize(siz);
		for (int i = 0; i < siz; ++i)
		{
			parent[i] = i;
			rank[i] = 0;
			size[i] = 1;
		}
	}
	void unite(int x, int y)
	{
		int xr = find(x);
		int yr = find(y);
		if (xr == yr) {
			return;
		}

		if (rank[xr] > rank[yr])
		{
			std::swap(xr, yr);
		}

		if (rank[xr] < rank[yr])
		{
		}
		else 
		{
			++rank[xr];
		}

		parent[yr] = xr;
		size[xr] += size[yr];
	}
	int find(int x)
	{
		if (parent[x] == x)
		{
			return x;
		}
		else
		{
			return parent[x] = find(parent[x]);
		}
	}
	bool is_same(int x, int y)
	{
		return find(x) == find(y);
	}
};


int32_t N;
int64_t D;
int64_t A_[200010];
const auto& A = (A_ + 10);
std::pair<int64_t, int32_t> lmin[200010];
std::pair<int64_t, int32_t> rmin[200010];

int main()
{
	using std::endl;
	in.sync_with_stdio(false);
	out.sync_with_stdio(false);
	in.tie(nullptr);
	out.tie(nullptr);

	in >> N>>D;
	for (size_t i = 0; i < N; i++)
	{
		in >> A[i];
	}
	A[-1] = 1000000000200000;
	A[N] = 1000000000200000;

	union_find uf(N);

	int64_t cost_sum = 0;
	for (size_t COUNT = 0; COUNT < 20; COUNT++)
	{
		fill_all(lmin, std::pair<int64_t, int32_t>{ 1000000000200000 , -1 });
		fill_all(rmin, std::pair<int64_t, int32_t>{ 1000000000200000, -1 });

		int32_t min1 = -1, min2 = -1;
		for (int32_t i = 0; i < N; i++)
		{
			if (min1 != -1 && !uf.is_same(min1, i)) {
				auto new_cost = A[min1] + (i - min1)*D + A[i];
				auto& lm = lmin[uf.find(i)];
				if (new_cost < lm.first) {
					lm.first = new_cost;
					lm.second = min1;
				}
			}
			else if (min2 != -1 && !uf.is_same(min2, i)) {
				auto new_cost = A[min2] + (i - min2)*D + A[i];
				auto& lm = lmin[uf.find(i)];
				if (new_cost < lm.first) {
					lm.first = new_cost;
					lm.second = min2;
				}
			}
			else {
				auto& lm = lmin[uf.find(i)];
				lm.first = 1000000000200000;
				lm.second = -1;
			}

			auto new_cost = A[i];
			auto old_cost1 = A[min1] + (i - min1)*D;
			auto old_cost2 = A[min2] + (i - min2)*D;

			if (new_cost <= old_cost1) {
				if (min1 == -1 || !uf.is_same(min1, i)) {
					min2 = min1;
				}
				min1 = i;
			}
			else if (new_cost <= old_cost2 && !uf.is_same(min1, i)) {
				min2 = i;
			}
		}
		min1 = N, min2 = N;
		for (int32_t i = N - 1; i >= 0; i--)
		{
			{
				auto& rm = rmin[uf.find(i)];
				if (min1 != N && !uf.is_same(min1, i)) {
					auto new_cost = A[min1] + (min1 - i)*D + A[i];
					if (new_cost < rm.first) {
						rm.first = new_cost;
						rm.second = min1;
					}
				}
				else if (min2 != N && !uf.is_same(min2, i)) {
					auto new_cost = A[min2] + (min2 - i)*D + A[i];
					if (new_cost < rm.first) {
						rm.first = new_cost;
						rm.second = min2;
					}
				}
				else {
					rm.first = 1000000000200000;
					rm.second = N;
				}
			}

			auto new_cost = A[i];
			auto old_cost1 = A[min1] + (min1 - i)*D;
			auto old_cost2 = A[min2] + (min2 - i)*D;

			if (new_cost <= old_cost1) {
				if (min1 == N || !uf.is_same(min1, i)) {
					min2 = min1;
				}
				min1 = i;
			}
			else if (new_cost <= old_cost2 && !uf.is_same(min1, i)) {
				min2 = i;
			}
		}


		for (int32_t i = 0; i < N; i++)
		{
			if (i != uf.find(i)) { continue; }
			if (lmin[i].second == -1 && rmin[i].second == N) { continue; }

			auto li = lmin[i].second;
			auto ri = rmin[i].second;

			auto lcost = lmin[i].first;
			auto rcost = rmin[i].first;

			if (rcost < lcost && !uf.is_same(i, ri)) {
				cost_sum += rcost;
				uf.unite(i, ri);
			}
			else if(li !=-1 && !uf.is_same(i, li)){
				cost_sum += lcost;
				uf.unite(i, li);
			}
		}
	}
	out << cost_sum << endl;

	return 0;
}
#endif


Compiler output:
a.cc: In function 'int main()':
a.cc:123:17: error: 'fill_all' was not declared in this scope
  123 |                 fill_all(lmin, std::pair<int64_t, int32_t>{ 1000000000200000 , -1 });
      |                 ^~~~~~~~