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
#include "bits/stdc++.h" // Tomasz Nowak
using namespace std;     // University of Warsaw
using LL = long long;
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
#define ssize(x) int(x.size())
template<class A, class B> auto& operator<<(ostream &o, pair<A, B> p) {
	return o << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) {
	o << '{'; int i = 0; for(auto e : x) o << (", ")+2*!i++ << e; return o << '}';
}
#ifdef DEBUG
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'
#else
#define debug(...) {}
#endif

vector<LL> r;
vector<vector<pair<int, LL>>> tree;

struct Timespan {
	LL r_in = 0, r_out = 0;
	int c = 0;
};

ostream& operator<<(ostream &o, Timespan t) {
	return o << vector<LL>{t.r_in, t.r_out, t.c};
}

vector<Timespan> merge(vector<Timespan> a, vector<Timespan> b) {
	auto pop = [&](vector<Timespan> &x) {
		auto ret = x.back();
		x.pop_back();
		return ret;
	};
	vector<Timespan> ret;
	reverse(a.begin(), a.end());
	reverse(b.begin(), b.end());
	auto retrieve_smallest = [&] {
		if(a.empty())
			return pop(b);
		if(b.empty())
			return pop(a);
		if(a.back().r_in < b.back().r_in)
			return pop(a);
		else
			return pop(b);
	};
	assert(not a.empty() or not b.empty());
	ret = {retrieve_smallest()};
	while(not a.empty() or not b.empty()) {
		auto sm = retrieve_smallest();
		if(sm.r_in > ret.back().r_out)
			ret.emplace_back(sm);
		else {
			ret.back().r_out = max(ret.back().r_out, sm.r_out);
			ret.back().c += sm.c;
		}
	}
	return ret;
}

vector<Timespan> shrink(const vector<Timespan> &v, LL c) {
	auto ret = v;
	for(Timespan &t : ret) {
		t.r_in += c;
		t.r_out = max(t.r_in, t.r_out - c);
	}
	return ret;
}

using State = vector<Timespan>;
vector<State> dp_down;

void dp_down_dfs(int v = 0, int p = -1) {
	dp_down[v] = {Timespan{0, r[v], 1}};
	for(auto [u, c] : tree[v])
		if(u != p) {
			dp_down_dfs(u, v);
			dp_down[v] = merge(dp_down[v], shrink(dp_down[u], c));
		}
}

vector<State> dp_up;

void dp_up_dfs(int v = 0, int p = -1) {
	debug(v, p);
	vector<pair<int, LL>> sons;
	for(auto [u, c] : tree[v])
		if(u != p)
			sons.emplace_back(u, c);

	int ss = ssize(sons);
	vector<State> prefix(ss), suffix(ss);
	REP(i, ss) {
		auto [u, c] = sons[i];
		prefix[i] = shrink(dp_down[u], c);
		if(i != 0)
			prefix[i] = merge(prefix[i], prefix[i - 1]);
	}
	for(int i = ss - 1; i >= 0; --i) {
		auto [u, c] = sons[i];
		suffix[i] = shrink(dp_down[u], c);
		if(i != ss - 1)
			suffix[i] = merge(suffix[i], suffix[i + 1]);
	}
	debug(v, ss, sons, prefix, suffix);

	REP(i, ss) {
		auto [u, uc] = sons[i];
		State merged = {Timespan{0, r[v], 1}};
		merged = merge(merged, dp_up[v]);
		debug(v, i, u, uc, merged);
		if(i != 0)
			merged = merge(merged, prefix[i - 1]);
		if(i != ss - 1)
			merged = merge(merged, suffix[i + 1]);
		dp_up[u] = shrink(merged, uc);
		debug(v, u, dp_up[u]);
		dp_up_dfs(u, v);
	}
}

int main() {
	cin.tie(0)->sync_with_stdio(0);

	int n;
	cin >> n;
	r.resize(n);
	for(LL &ri : r)
		cin >> ri;
	tree.resize(n);
	REP(edge, n - 1) {
		int v, u;
		LL c;
		cin >> v >> u >> c;
		--v, --u;
		tree[v].emplace_back(u, c);
		tree[u].emplace_back(v, c);
	}
	debug(tree);

	dp_down.resize(n);
	dp_down_dfs();
	dp_up.resize(n);
	dp_up_dfs();

	vector<int> ans(n);
	REP(root, n) {
		auto merged = merge(dp_down[root], dp_up[root]);
		debug(root, merged);
		assert(not merged.empty() and merged.front().r_in == 0);
		ans[root] = merged.front().c;
	}
	REP(v, n)
		cout << ans[v] << ' ';
	cout << '\n';
}