Language: C++

#include <cstdio>
#include <algorithm>
using namespace std;

typedef long long ll;
const int Maxn = 200000;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;

struct State {
	pair<ll, int> normal, root;
	State(){}
	State(ll a, int b, ll c, int d) {
		normal = make_pair(a, b);
		root = make_pair(c, d);
	}
};

int N;
ll X[Maxn + 5];
vector<pair<int, int> > G[Maxn + 5];
inline void addedge(int u, int v, int w) {
	G[u].push_back(make_pair(v, w));
	G[v].push_back(make_pair(u, w));
}

int fa[Maxn + 5];
int find(int x) {
	return x == fa[x] ? x : fa[x] = find(fa[x]);
}
bool unite(int u, int v) {
	int fu = find(u), fv = find(v);
	if(fu == fv) return false;
	fa[fu] = fv;
	return true;
}

int belong[Maxn + 5];

State f[Maxn + 5];
State min(State lhs, State rhs) {
	if(rhs.normal.first < lhs.normal.first) {
		if(rhs.normal.second != lhs.normal.second)
			lhs.root = lhs.normal;
		lhs.normal = rhs.normal;
		if(rhs.root.first < lhs.root.first)
			lhs.root = rhs.root;
	} else {
		if(rhs.normal.second != lhs.normal.second) {
			if(rhs.normal.first < lhs.root.first)
				lhs.root = rhs.normal;
		} else if(rhs.root.first < lhs.root.first)
			lhs.root = rhs.root;
	}
	return lhs;
}
void DFS1(int u, int fa) {
	f[u] = State(X[u], belong[u], INFLL, -1);
	for(int i = 0; i < (int)G[u].size(); i++) {
		int v = G[u][i].first, w = G[u][i].second;
		if(v == fa) continue;
		DFS1(v, u);
		State tmp = f[v];
		tmp.normal.first += w, tmp.root.first += w;
		f[u] = min(f[u], tmp);
	}
}
void DFS2(int u, int fa, State last) {
	f[u] = min(f[u], last);
	for(int i = 0; i < (int)G[u].size(); i++) {
		int v = G[u][i].first, w = G[u][i].second;
		if(v == fa) continue;
		State tmp = f[u];
		tmp.normal.first += w, tmp.root.first += w;
		DFS2(v, u, tmp);
	}
}

pair<ll, int> mn[Maxn + 5];
int rts[Maxn + 5];
int main() {
#ifdef LOACL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	scanf("%d", &N);
	for(int i = 1; i <= N; i++)
		scanf("%lld", &X[i]);
	for(int i = 1; i <= N; i++) {
		int u, v, w;
		scanf("%d %d %d", &u, &v, &w);
		addedge(u, v, w);
	}
	for(int i = 1; i <= N; i++)
		fa[i] = i;
	ll ans = 0;
	while(true) {
		int cnt = 0;
		for(int i = 1; i <= N; i++)
			if(fa[i] == i) {
				belong[i] = ++cnt;
				rts[belong[i]] = i;
			}
		if(cnt == 1) break;
		for(int i = 1; i <= N; i++)
			belong[i] = belong[find(i)];
		DFS1(1, 0), DFS2(1, 0, State(INFLL, -1, INFLL, -1));
		for(int i = 1; i <= cnt; i++)
			mn[i] = make_pair(INFLL, -1);
		for(int i = 1; i <= N; i++) {
			if(f[i].normal.second == belong[i])
				mn[belong[i]] = min(mn[belong[i]], make_pair(f[i].root.first + X[i], f[i].root.second));
			else mn[belong[i]] = min(mn[belong[i]], make_pair(f[i].normal.first + X[i], f[i].normal.second));
		}
		for(int i = 1; i <= cnt; i++)
			if(unite(rts[i], rts[mn[i].second]))
				ans += mn[i].first;
	}
	printf("%lld\n", ans);
	return 0;
}

Compiler output:
a.cc:21:1: error: 'vector' does not name a type
   21 | vector<pair<int, int> > G[Maxn + 5];
      | ^~~~~~
a.cc: In function 'void addedge(int, int, int)':
a.cc:23:9: error: 'G' was not declared in this scope
   23 |         G[u].push_back(make_pair(v, w));
      |         ^
a.cc: In function 'void DFS1(int, int)':
a.cc:59:33: error: 'G' was not declared in this scope
   59 |         for(int i = 0; i < (int)G[u].size(); i++) {
      |                                 ^
a.cc:64:37: error: 'w' was not declared in this scope
   64 |                 tmp.normal.first += w, tmp.root.first += w;
      |                                     ^
a.cc: In function 'void DFS2(int, int, State)':
a.cc:70:33: error: 'G' was not declared in this scope
   70 |         for(int i = 0; i < (int)G[u].size(); i++) {
      |                                 ^
a.cc:74:37: error: 'w' was not declared in this scope
   74 |                 tmp.normal.first += w, tmp.root.first += w;
      |                                     ^