This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Pantyhose(black) + glasses = infinity
#include <bits/stdc++.h>
using namespace std;
 
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using ordered_set = tree<int64_t, null_type, less_equal<int64_t>, rb_tree_tag, tree_order_statistics_node_update>;
#define debug(x) cerr << #x << " = " << x << '\n';
#define BP() cerr << "OK!\n";
#define PR(A, n) {cerr << #A << " = "; for (int64_t _=1; _<=n; ++_) cerr << A[_] << ' '; cerr << '\n';}
#define PR0(A, n) {cerr << #A << " = "; for (int64_t _=0; _<n; ++_) cerr << A[_] << ' '; cerr << '\n';}
#define FILE_NAME "data"
const int64_t MAX_N = 100002;
const int64_t INF = 1e18;
struct edge {
	int64_t v, w;
	edge() {}
	edge(int64_t v, int64_t w): v(v), w(w) {}
};
int64_t n, gas[MAX_N], nChildren[MAX_N];
int64_t l[MAX_N], up[MAX_N], down[MAX_N];
vector<edge> g[MAX_N];
bool deleted[MAX_N];
int64_t res;
ordered_set s1, s2;
void readInput() {
	cin >> n;
	for (int64_t i=1; i<=n; ++i)
		cin >> gas[i];
	for (int64_t i=1; i<n; ++i) {
		int64_t u, v, w;
		cin >> u >> v >> w;
		g[u].push_back(edge(v, w));
		g[v].push_back(edge(u, w));
	}
}
int64_t fixRoot(int64_t u, int64_t par) {
	nChildren[u] = 1;
	for (auto e : g[u]) {
		if (!deleted[e.v] && e.v!=par)
			nChildren[u] += fixRoot(e.v, u);
	}
	return nChildren[u];
}
int64_t findCentroid(int64_t u, int64_t par, int64_t curRoot) {
	for (auto e : g[u]) {
		if (!deleted[e.v] && e.v!=par && nChildren[e.v]>nChildren[curRoot]/2)
			return findCentroid(e.v, u, curRoot);
	}
	return u;
}
void dp(int64_t u, int64_t par) {
	up[u] = max(up[par], l[u]);
	down[u] = min(down[par], l[u] - gas[u]);
	if (l[u]-up[u]>=0)
		++res;
	if (down[u]>=0)
		++res;
	for (auto e : g[u]) {
		if (!deleted[e.v] && e.v!=par) {
			l[e.v] = l[u] - e.w + gas[e.v];
			dp(e.v, u);
		}
	}
}
void calc(int64_t u, int64_t par, int64_t root) {
	// if (root==4 && u==7)
	// 	debug(s1.order_of_key(down[u]-gas[root]+1));
	res += s1.order_of_key(down[u]-gas[root]+1);
	if (l[u]-up[u]>=0) {
		// if (root==4 && u==7)
		// 	debug(s2.order_of_key(l[u]+1));
		res += s2.order_of_key(l[u]+1);
	}
	for (auto e : g[u]) {
		if (!deleted[e.v] && e.v!=par)
			calc(e.v, u, root);
	}
}
void upd(int64_t u, int64_t par, int64_t root) {
	
	// if (u==1 && root==4)
	// 	debug(-(down[u]-gas[root]));
	s2.insert(-(down[u]-gas[root]));
	if (l[u]-up[u]>=0)
		s1.insert(-l[u]);
	for (auto e : g[u]) {
		if (!deleted[e.v] && e.v!=par)
			upd(e.v, u, root);
	}
}
void solve(int64_t u) {
	fixRoot(u, 0);
	u = findCentroid(u, 0, u);
	// debug(u);
	fixRoot(u, 0);
	deleted[u] = true;
	l[u] = gas[u];
	up[u] = l[u];
	down[u] = INF;
	for (auto e : g[u]) {
		if (!deleted[e.v]) {
			l[e.v] = gas[u] - e.w + gas[e.v];
			up[e.v] = max(up[u], l[e.v]);
			down[e.v] = min(down[u], l[e.v]-gas[e.v]);
			dp(e.v, u);
		}
	}
	// if (u==1)
	// 	debug(res);
	s1.clear();
	s2.clear();
	for (auto e : g[u]) {
		if (!deleted[e.v]) {
			calc(e.v, u, u);
			upd(e.v, u, u);
		}
	}
	// if (u==1)
	// 	debug(res);
	for (auto e : g[u]) {
		if (!deleted[e.v])
			solve(e.v);
	}
}
int main() {
	#ifdef GLASSES_GIRL
		freopen(FILE_NAME".inp", "r", stdin);
		freopen(FILE_NAME".out", "w", stdout);
	#endif
	ios::sync_with_stdio(0); cin.tie(0);
	readInput();
	ordered_set s;
	solve(1);
	cout << res;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |