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
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef basic_string<int> BI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(1); 
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=101000,M=20;
int q[N*M],f[N],del[N];
int l[N],r[N],lev[N],id[N];
ll dep[N][M],rad[N];
int rt[N][M],rk[N][M],gork[N][M];
int vto[N][M],tov[N][M];
VI vecd[N],gid[N],ch[N];
//VI E[N*M],rE[N*M];
int pE[N*M],rE[N*M],to[6*N*M],nxt[6*N*M];
int mt[N*M],T,pid[N],vis[N*M];
vector<pair<int,ll>> e[N];
int n,tot;
namespace treecenter {
int sz[N],ms[N];
int find(int u) {
	int t=1;q[0]=u;f[u]=-1;
	rep(i,0,t) {
		u=q[i];
		rep(j,0,e[u].size()) {
			int v=e[u][j].fi;
			if (!del[v]&&v!=f[u]) f[q[t++]=v]=u;
		}
		ms[q[i]]=0;
		sz[q[i]]=1;
	}
	for (int i=t-1;i>=0;i--) {
		ms[q[i]]=max(ms[q[i]],t-sz[q[i]]);
		if (ms[q[i]]*2<=t) return q[i];
		sz[f[q[i]]]+=sz[q[i]];
		ms[f[q[i]]]=max(ms[f[q[i]]],sz[q[i]]);
	}
	return 0;
}
}

int dfs(int u,int L) {
	u=treecenter::find(u);
	id[l[u]=++tot]=u;
	del[u]=1;
	lev[u]=L;
	int t=1; q[0]=u; f[u]=-1;
	dep[u][L]=0;
	rt[u][L]=u;
	rep(i,0,t) {
		int w=q[i];
		rep(j,0,e[w].size()) {
			int v=e[w][j].fi;
			if (!del[v]&&v!=f[w]) {
				f[q[t++]=v]=w;
				rt[v][L]=u;
				dep[v][L]=dep[w][L]+e[w][j].se;
			}
		}
	}
	vecd[u]=VI(q,q+t);
	sort(all(vecd[u]),[&](int p,int q) {
		return dep[p][L]<dep[q][L];
	});
	rep(i,0,t) {
		int v=vecd[u][i];
		rk[v][L]=i;
		if (rad[v]<dep[v][L]) gork[v][L]=0;
		else {
			int tl=0,tr=t;
			while (tl+1<tr) {
				int md=(tl+tr)>>1;
				if (dep[vecd[u][md]][L]<=rad[v]-dep[v][L])
					tl=md;
				else tr=md;
			}
			gork[v][L]=tl+1;
		}
	}
	gid[u]=VI(t+1,0);
	for (auto [v,w]:e[u]) if (!del[v]) {
		ch[u].pb(dfs(v,L+1));
	}
	r[u]=tot;
	assert(r[u]-l[u]+1==t);
	return u;
}

void solve(int u) {
	int m=0;
	int L=lev[u];
	for (int i=l[u];i<=r[u];i++) {
		int v=id[i];
		pid[v]=++m;
		gid[v][0]=0;
		rep(j,1,SZ(gid[v])) gid[v][j]=++m;
	}
	int etot=0;
	auto add=[&](int u,int v) {
		if (u==0||v==0) return;
		to[etot]=v; nxt[etot]=pE[u]; pE[u]=etot++;
		to[etot]=u; nxt[etot]=rE[v]; rE[v]=etot++;
		//E[u].pb(v); rE[v].pb(u);
	};
	rep(i,1,m+1) pE[i]=-1,rE[i]=-1;
	//printf("solving %d\n",u);
	for (int i=l[u];i<=r[u];i++) {
		int v=id[i];
		rep(j,L,lev[v]+1) {
			add(pid[v],gid[rt[v][j]][gork[v][j]]);
		}
		rep(j,0,SZ(gid[v])-1) {
			add(gid[v][j+1],gid[v][j]);
			add(gid[v][j+1],pid[vecd[v][j]]);
		}
	}
	//fprintf(stderr,"%d %d\n",m,etot);
	++T;
	rep(k,1,SZ(gid[u])) {
		int x=gid[u][k];
		int t=0; q[t++]=x; vis[x]=T;
		rep(i,0,t) {
			int y=q[i];
			for (int j=pE[y],z=to[j];j!=-1;j=nxt[j],z=to[j]) if (vis[z]!=T) {
				vis[z]=T; q[t++]=z;
				mt[z]=k;
			}
		}
	}
	rep(j,0,SZ(vecd[u])) {
		int v=vecd[u][j];
		tov[v][L]=vis[pid[v]]==T?mt[pid[v]]:SZ(gid[u]);
	}
	++T;
	per(k,1,SZ(gid[u])) {
		int x=gid[u][k];
		int t=0; q[t++]=x; vis[x]=T;
		rep(i,0,t) {
			int y=q[i];
			for (int j=rE[y],z=to[j];j!=-1;j=nxt[j],z=to[j]) if (vis[z]!=T) {
				vis[z]=T; q[t++]=z;
				mt[z]=k;
			}
		}
	}
	rep(j,0,SZ(vecd[u])) {
		int v=vecd[u][j];
		vto[v][L]=vis[pid[v]]==T?mt[pid[v]]:0;
	}
	for (auto v:ch[u]) solve(v);
}

typedef unsigned long long u64;
struct Bitset {
	int offset,sz;
	vector<u64> blk;
	void init(int l,int r) {
		int lb=l>>6,rb=(r>>6)+1;
		offset=lb;
		sz=rb-lb;
		blk=vector<u64>(rb-lb,0);
	}
	void setp(int x) {
		blk[(x>>6)-offset]|=1ull<<(x&63);
	}
};
vector<Bitset> pc[N];
bitset<64*1600> rc;

int main() {
	scanf("%d",&n);
	rep(i,1,n+1) scanf("%lld",&rad[i]);
	rep(i,1,n) {
		int u,v;
		ll w;
		scanf("%d%d%lld",&u,&v,&w);
		e[u].pb(mp(v,w));
		e[v].pb(mp(u,w));
	}
	int Rt=dfs(1,0);
	//fprintf(stderr,"%d\n",(int)clock());
	solve(Rt);
	/*
	for (int u=1;u<=n;u++) {
		printf("node %d lev %d\n",u,lev[u]);
		for (int j=0;j<=lev[u];j++) {
			printf("Lev #%d: rt %d dep %lld rk %d gork %d vto %d tov %d\n",j,
				rt[u][j],dep[u][j],rk[u][j],gork[u][j],
				vto[u][j],tov[u][j]);
		}
	}*/
	int M=500;
	rep(i,1,n+1) {
		sort(all(vecd[i]),[&](int p,int q) {
			return tov[p][lev[i]]<tov[q][lev[i]];
		});
		Bitset c; c.init(l[i],r[i]);
		rep(j,0,SZ(vecd[i])) {
			int u=vecd[i][j]; c.setp(l[u]);
			if (j%M==M-1) pc[i].pb(c);
		}
	}
	//fprintf(stderr,"%d\n",(int)clock());
	rep(i,1,n+1) {
		//T++;
		//int ans=0;
		rc.reset();
		rep(j,0,lev[i]+1) {
			int v=rt[i][j];
			int tl=-1,tr=SZ(vecd[v]);
			while (tl+1<tr) {
				int md=(tl+tr)>>1;
				if (tov[vecd[v][md]][j]<=vto[i][j]) tl=md; else tr=md;
			}
			u64 *rcp=(u64*)&rc;
			if (tr/M) {
				auto &bt=pc[v][tr/M-1];
				rep(k,0,bt.sz) rcp[bt.offset+k]|=bt.blk[k];
			}
			rep(k,tr/M*M,tr) rc.set(l[vecd[v][k]]);
		}
		printf("%d%c",(int)rc.count()," \n"[i==n]);
	}
	//fprintf(stderr,"%d\n",(int)clock());
}