1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// XD, obstawiam 7 punktów (chyba że nie działa xd)

#define NDEBUG
#include <bits/stdc++.h>

#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define st first
#define nd second

using namespace std;
 
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
	enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
	ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
	*this << "[";
	for (auto it = d.b; it != d.e; ++it)
	*this << ", " + 2 * (it == d.b) << *it;
	ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
 
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vll = vector<ll>;

template <typename T> void maxi(T &a, T b) { a = max(a, b); }

const ll kInf = 3e18;

struct Edge {
	int to;
	ll dist;
};

struct CentroidInfo {
	int centroid;
	int tree_idx;
	ll dist;
	ll in, out;
	
	CentroidInfo(int centroid_, int tree_idx_, ll dist_) :
		centroid{centroid_}, tree_idx{tree_idx_}, dist{dist_}, in{-1}, out{kInf} {}
	
	friend ostream &operator<<(ostream &os, const CentroidInfo &cinfo) {
		os <<
			"(centroid=" << cinfo.centroid <<
			", tree_idx=" << cinfo.tree_idx <<
			", dist=" << cinfo.dist <<
			", in=" << cinfo.in <<
			", out=" << (cinfo.out == kInf ? string("INF") : to_string(cinfo.out)) << ")";
		return os;
	}
};

int n;
vector<vector<Edge>> input_graph;
vector<ll> radii;
vi centroid_discovery_order;

vector<vector<CentroidInfo>> centroid_infos;

void Input() {
	cin >> n;
	input_graph.resize(n + 1);
	radii.resize(n + 1);
	for (int i = 1; i <= n; ++i) {
		cin >> radii[i];
	}
	for (int i = 0; i < n - 1; ++i) {
		int u, v;
		ll dist;
		cin >> u >> v >> dist;
		input_graph[u].push_back(Edge{v, dist});
		input_graph[v].push_back(Edge{u, dist});
	}
}

vector<int> tree_preorder;
int tree_tm;

void DfsPreprocPreorder(int v, int p) {
	tree_preorder[v] = ++tree_tm;
	for (const auto &edge : input_graph[v]) {
		if (edge.to != p) {
			DfsPreprocPreorder(edge.to, v);
		}
	}
}

void PreprocPreorder() {
	tree_preorder.resize(n + 1);
	DfsPreprocPreorder(1, -1);
	debug() << imie(tree_preorder);
	
	vector<vector<Edge>> old_graph(n + 1);
	vll old_radii(n + 1);
	swap(input_graph, old_graph);
	swap(radii, old_radii);
	
	for (int v = 1; v <= n; ++v) {
		input_graph[tree_preorder[v]] = old_graph[v];
		for (auto &edge : input_graph[tree_preorder[v]]) {
			edge.to = tree_preorder[edge.to];
		}
		radii[tree_preorder[v]] = old_radii[v];
	}
}

vector<int> tree_size;
vector<bool> is_centroid_taken;
vector<int> vis_list;
vector<int> centroid_level;

void DfsSizes(int v, int p) {
	vis_list.push_back(v);
	tree_size[v] = 1;
	for (const auto &edge : input_graph[v]) {
		if (edge.to == p || is_centroid_taken[edge.to]) { continue; }
		DfsSizes(edge.to, v);
		tree_size[v] += tree_size[edge.to];
	}
}

void RecCentroids(int root, int level) {
	//~ visited.resize(n + 1);
	
	vis_list.clear();
	DfsSizes(root, -1);
	const int whole_size = tree_size[root];
	int centroid = root;
	for (int vtx : vis_list) {
		if (tree_size[vtx] * 2 >= whole_size && tree_size[vtx] < tree_size[centroid]) {
			centroid = vtx;
		}
	}

	debug() << imie(centroid);
	centroid_level[centroid] = level;
	centroid_discovery_order.push_back(centroid);
	int tree_idx = 0;
	
	function<void(int, int, ll)> DfsSubtree = [&](int v, int p, ll dist) {
		debug() << "subtree" << imie(v) << imie(tree_idx) << imie(dist);
		centroid_infos[v].push_back(CentroidInfo(centroid, tree_idx, dist));
		for (const auto &edge : input_graph[v]) {
			if (!is_centroid_taken[edge.to] && edge.to != p) {
				DfsSubtree(edge.to, v, dist + edge.dist);
			}
		}
	};
	
	centroid_infos[centroid].push_back(CentroidInfo(centroid, -1, 0));
	for (const auto &edge : input_graph[centroid]) {
		if (!is_centroid_taken[edge.to]) {
			DfsSubtree(edge.to, centroid, edge.dist);
			++tree_idx;
		}
	}
	
	is_centroid_taken[centroid] = true;
	for (const auto &edge : input_graph[centroid]) {
		if (!is_centroid_taken[edge.to]) {
			RecCentroids(edge.to, level + 1);
		}
	}
}

void CreateCentroids() {
	tree_size.resize(n + 1);
	is_centroid_taken.resize(n + 1);
	centroid_infos.resize(n + 1);
	centroid_level.resize(n + 1);
	RecCentroids(1, 0);
}

vector<vi> reachability_graph, reachability_graph_rev;
int reachability_nverts;
vector<vll> vertex_dists;
vector<int> reachability_intv_starts;
vector<vector<pii>> schedule_edge_at;

void CreateReachabilityGraph() {
	reachability_nverts = n + 1;
	
	schedule_edge_at.resize(n + 1);
	vertex_dists.resize(n + 1);
	auto &intv_starts = reachability_intv_starts;
	intv_starts.resize(n + 1);
	for (int v = 1; v <= n; ++v) {
		for (const auto &centroid_info : centroid_infos[v]) {
			vertex_dists[centroid_info.centroid].push_back(centroid_info.dist);
		}
	}
	
	auto AddEdge = [&](int u, int v, int schedule) {
		debug() << imie(u) << imie(v);
		const int mx = max(u, v) + 1;
		if (SZ(reachability_graph) < mx) {
			reachability_graph.resize(mx);
			reachability_graph_rev.resize(mx);
		}
		if (schedule == -1) {
			reachability_graph[u].push_back(v);
			reachability_graph_rev[v].push_back(u);
		} else {
			schedule_edge_at[schedule].emplace_back(u, v);
		}
	};
	
	for (int v = 1; v <= n; ++v) {
		auto &dists = vertex_dists[v];
		sort(ALL(dists));
		dists.resize(unique(ALL(dists)) - dists.begin());
		intv_starts[v] = reachability_nverts;
		for (int i = 0; i < SZ(dists); ++i) {
			centroid_level.push_back(centroid_level[v]);
		}
		debug() << "path" << imie(v) << imie(dists) << imie(intv_starts[v]);
		reachability_nverts += SZ(dists);
		for (int i = 1; i < SZ(dists); ++i) {
			AddEdge(intv_starts[v] + i, intv_starts[v] + i - 1, -1);
		}
	}
	
	for (int v = 1; v <= n; ++v) {
		debug() << "vtx" << imie(v);
		for (const auto &centroid_info : centroid_infos[v]) {
			const int centroid = centroid_info.centroid;
			const ll dist = centroid_info.dist;
			const auto &dists = vertex_dists[centroid];
			if (dist <= radii[v]) {
				const ll rem_dist = radii[v] - dist;
				const int loc = upper_bound(ALL(dists), rem_dist) - dists.begin() - 1;
				AddEdge(v, intv_starts[centroid] + loc, centroid);
			}
			{
				const int loc = lower_bound(ALL(dists), dist) - dists.begin();
				AddEdge(intv_starts[centroid] + loc, v, centroid);
			}
		}
	}
}

void ComputeInOut() {
	vector<int> last_vis(reachability_nverts);
	vector<vector<pll>> verts_subtree;
	
	vi Q;
	int curq;
	
	// in
	for (int cid = SZ(centroid_discovery_order) - 1; cid >= 0; --cid) {
		const int centroid = centroid_discovery_order[cid];
		for (const auto &[a, b] : schedule_edge_at[centroid]) {
			reachability_graph_rev[b].push_back(a);
		}
	//~ for (int centroid = 1; centroid <= n; ++centroid) {
		const int intv_start = reachability_intv_starts[centroid];
		const int intv_len = SZ(vertex_dists[centroid]);
		const int level = centroid_level[centroid];
		
		Q.clear(); curq = 0;
		for (int gvtx = intv_start + intv_len - 1; gvtx >= intv_start; --gvtx) {
			if (last_vis[gvtx] == centroid) { continue; }
			const ll cost_in = vertex_dists[centroid][gvtx - intv_start];
			Q.push_back(gvtx);
			last_vis[gvtx] = centroid;
			while (curq < SZ(Q)) {
				int v = Q[curq++];
				if (v <= n) {
					assert(centroid_infos[v][level].in == -1);
					centroid_infos[v][level].in = cost_in;
				}
				for (int s : reachability_graph_rev[v]) {
					//~ if (centroid_level[s] < level) { continue; }
					if (last_vis[s] == centroid) { continue; }
					last_vis[s] = centroid;
					Q.push_back(s);
				}
			}
		}
	}
	
	// out
	verts_subtree.resize(n + 1);
	for (int v = 1; v <= n; ++v) {
		for (const auto &centroid_info : centroid_infos[v]) {
			verts_subtree[centroid_info.centroid].emplace_back(centroid_info.dist, v);
		}
	}
	fill(ALL(last_vis), 0);
	
	for (int cid = SZ(centroid_discovery_order) - 1; cid >= 0; --cid) {
		const int centroid = centroid_discovery_order[cid];
		for (const auto &[a, b] : schedule_edge_at[centroid]) {
			reachability_graph[a].push_back(b);
		}
	//~ for (int centroid = 1; centroid <= n; ++centroid) {
		const int level = centroid_level[centroid];
		sort(ALL(verts_subtree[centroid]));

		Q.clear(); curq = 0;
		for (auto [cost_out, source] : verts_subtree[centroid]) {
			if (last_vis[source] == centroid) { continue; }
			Q.push_back(source);
			last_vis[source] = centroid;
			while (curq < SZ(Q)) {
				int v = Q[curq++];
				if (v <= n) {
					assert(centroid_infos[v][level].out == kInf);
					centroid_infos[v][level].out = cost_out;
				}
				for (int s : reachability_graph[v]) {
					//~ if (centroid_level[s] < level) { continue; }
					if (last_vis[s] == centroid) { continue; }
					last_vis[s] = centroid;
					Q.push_back(s);
				}
			}
		}
	}
	
	for (int v = 1; v <= n; ++v) {
		debug() << imie(v) << imie(centroid_infos[v]);
	}
}

struct BitsetView {
	int start = -1, size;
	vector<uint64_t> data;
	
	BitsetView(int start_, int size_) {
		SetView(start_, size_);
	}
	
	void Clear() {
		fill(ALL(data), 0);
	}

	void Set(int loc) {
		loc -= start;
		if (loc < 0 || loc >= size) { return; }
		data[loc >> 6] |= (1ULL << (loc & 63));
	}
	
	void SetView(int start_, int size_) {
		int end = start_ + size_;
		start = start_ & ~63;
		if (end & 63) { end += (~end & 63) + 1; }
		size = (end - start);
		data.resize(size >> 6);
	}
	
	void Apply(const BitsetView &other) {
		int L = max(start, other.start);
		int R = min(start + size, other.start + other.size);
		if (L >= R) { return; }
		
		int blk_this = (L - start) >> 6;
		int blk_other = (L - other.start) >> 6;
		int nblocks = (R - L) >> 6;
		for (int i = 0; i < nblocks; ++i) {
			data[blk_this] |= other.data[blk_other];
			++blk_this;
			++blk_other;
		}
	}
	
	int Popcount() const {
		int ans = 0;
		for (auto elem : data) {
			ans += __builtin_popcountll(elem);
		}
		return ans;
	}
};

const int kMaxNumRuns = 8;

vi FindResults() {
	vi preorder(n + 1), postorder(n + 1);
	vector<vi> verts_centr(n + 1);
	
	for (int i = 0; i < SZ(centroid_discovery_order); ++i) {
		preorder[centroid_discovery_order[i]] = i;
	}
	
	for (int v = 1; v <= n; ++v) {
		for (const auto &centroid_info : centroid_infos[v]) {
			verts_centr[centroid_info.centroid].push_back(v);
			//~ verts_by_out[centroid_info.centroid].push_back(v);
		}
	}
	
	vector<BitsetView> centroid_masks(n + 1, BitsetView(0, 0));
	
	for (int centroid = 1; centroid <= n; ++centroid) {
		const int level = centroid_level[centroid];
		vector<int> ins = verts_centr[centroid];
		vector<int> outs = verts_centr[centroid];
		sort(ALL(ins), [&](int lhs, int rhs) {
			return centroid_infos[lhs][level].in < centroid_infos[rhs][level].in;
		});
		sort(ALL(outs), [&](int lhs, int rhs) {
			return centroid_infos[lhs][level].out < centroid_infos[rhs][level].out;
		});
		centroid_masks[centroid].SetView(preorder[centroid], SZ(ins));
		postorder[centroid] = preorder[centroid] + SZ(ins);
		
		vector<int> order;
		int lptr = 0;
		for (int v : ins) {
			while (lptr < SZ(outs)) {
				const int x = outs[lptr];
				if (centroid_infos[x][level].out > centroid_infos[v][level].in) {
					break;
				}
				order.push_back(preorder[x]);
				++lptr;
			}
			order.push_back(v + n);
		}
		verts_centr[centroid] = order;
	}
	

	int num_total_blocks = (n >> 6) + 1;
	const int num_runs = min(num_total_blocks, kMaxNumRuns);
	vi block_stops;
	for (int i = 0; i <= num_runs; ++i) {
		block_stops.push_back((ll)i * num_total_blocks / num_runs);
	}
	
	vector<BitsetView> reach_masks(n + 1, BitsetView(0, 0));
	
	vi results(n + 1);
	for (int run_idx = 0; run_idx < num_runs; ++run_idx) {
		const int run_from = block_stops[run_idx] << 6;
		const int run_to = block_stops[run_idx + 1] << 6;
		for (int v = 1; v <= n; ++v) {
			reach_masks[v].SetView(run_from, run_to - run_from);
			reach_masks[v].Clear();
		}
		for (int centroid = 1; centroid <= n; ++centroid) {
			if (preorder[centroid] >= run_to) { continue; }
			if (postorder[centroid] <= run_from) { continue; }
			auto &centroid_mask = centroid_masks[centroid];
			centroid_mask.Clear();
			
			for (int v : verts_centr[centroid]) {
				if (v > n) {
					reach_masks[v - n].Apply(centroid_mask);
				} else {
					centroid_mask.Set(v);
				}
			}
		}
		
		for (int i = 1; i <= n; ++i) {
			results[i] += reach_masks[i].Popcount();
		}
	}

	return results;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(11);
	cerr << fixed << setprecision(6);
	
	Input();
	PreprocPreorder();
	CreateCentroids();
	CreateReachabilityGraph();
	ComputeInOut();
	auto sol = FindResults();
	
	for (int i = 1; i <= n; ++i) {
		cout << sol[tree_preorder[i]] << " ";
	}
	cout << "\n";
}