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
#pragma GCC optimize("Ofast")
#pragma GCC optimize("-funsafe-math-optimizations")
#pragma GCC optimize("-freciprocal-math")
#pragma GCC optimize("-fno-trapping-math")
#pragma GCC optimize("-ffinite-math-only")
#pragma GCC optimize("-fno-stack-protector")
#include<immintrin.h>
#include<emmintrin.h>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=100005,M=1600005,CE=4600005,K=7;
//22N+6.25M+5CE
//134MB
//824MB
int n,m,i,x,y;ll z,rad[N];
int ed,g[N],nxt[N<<1],v[N<<1],ok[N<<1];ll w[N<<1];
int son[N],f[N],all,now;
int cp,pool[N],pool2[N];ll dis[N],lim[N];
int cnt;
int dep[N],who,root,fa[N],pos[N],len;
inline bool cmp(int x,int y){return dis[x]<dis[y];}
inline bool cmp2(int x,int y){return lim[x]<lim[y];}
inline void add(int x,int y,ll z){
  v[++ed]=y;
  w[ed]=z;
  nxt[ed]=g[x];
  g[x]=ed;
  ok[ed]=1;
}
void findroot(int x,int y){
  son[x]=1;f[x]=0;
  for(int i=g[x];i;i=nxt[i])if(ok[i]&&v[i]!=y){
    findroot(v[i],x);
    son[x]+=son[v[i]];
    if(son[v[i]]>f[x])f[x]=son[v[i]];
  }
  if(all-son[x]>f[x])f[x]=all-son[x];
  if(f[x]<f[now])now=x;
}
void dfs(int x,int y,ll z){
  pool[++cp]=x;
  dis[x]=z;
  lim[x]=rad[x]-z;
  for(int i=g[x];i;i=nxt[i])if(ok[i]&&v[i]!=y)dfs(v[i],x,z+w[i]);
}
namespace Graph{
int g[M],h[M],v[CE*2],nxt[CE*2],ed;
bool vis[M];
int seen[M],q[M],t,f[M],scc,en[M],edge[CE],ce;
int ans[N];
static __m512i zero,dp[M][K];
bool have[M],need[M],ok[M];
int count[M];
int id[N],cid;
inline void addedge(int x,int y){
  v[++ed]=y;nxt[ed]=g[x];g[x]=ed;
  v[++ed]=x;nxt[ed]=h[y];h[y]=ed;
}
void dfs1(int x){
  vis[x]=1;
  for(int i=h[x];i;i=nxt[i])if(!vis[v[i]])dfs1(v[i]);
  q[++t]=x;
}
void dfs2(int x){
  //printf(">>%d %d\n",x,scc);
  vis[x]=0;f[x]=scc;
  for(int i=g[x];i;i=nxt[i])if(vis[v[i]])dfs2(v[i]);
  else{
    if(f[v[i]]!=scc&&seen[f[v[i]]]!=scc){
      seen[f[v[i]]]=scc;
      edge[++ce]=f[v[i]];
    }
  }
}
inline void umin(int&a,int b){a>b?(a=b):0;}
inline void umax(int&a,int b){a<b?(a=b):0;}
void gao(){
  int i,j,x,l,r;
  for(i=1;i<=cnt;i++)if(!vis[i])dfs1(i);
  for(i=cnt;i;i--)if(vis[q[i]]){
    scc++;
    dfs2(q[i]);
    en[scc]=ce;
  }
  //for(i=1;i<=scc;i++)for(j=en[i-1]+1;j<=en[i];j++)printf("%d->%d\n",i,edge[j]);
  for(i=1;i<=scc;i++)seen[i]=len+1,q[i]=0;
  for(i=1;i<=n;i++){
    x=pos[i];
    if(!x)continue;
    umin(seen[f[i]],x);
    umax(q[f[i]],x);
  }
  for(i=1;i<=scc;i++){
    int L=en[i-1]+1,R=en[i];
    for(j=L;j<=R;j++){
      x=edge[j];
      umin(seen[i],seen[x]);
      umax(q[i],q[x]);
    }
    count[i]=seen[i]<=q[i]?q[i]-seen[i]+1:0;
  }
  for(i=1;i<=n;i++)ans[i]=count[f[i]];
  
  for(i=1;i<=n;i++)if(!pos[i])id[++cid]=i;
  if(!cid)return;
  for(i=1;i<=n;i++)need[f[i]]=ok[f[i]]=1;
  for(i=scc;i;i--){
    if(!ok[i])continue;
    int L=en[i-1]+1,R=en[i];
    for(j=L;j<=R;j++)ok[edge[j]]=1;
  }
  for(l=1;l<=cid;l=r+1){
    r=min(cid,l+512*K-1);
    for(i=1;i<=scc;i++){
      dp[i][0]=zero;
      dp[i][1]=zero;
      dp[i][2]=zero;
      dp[i][3]=zero;
      dp[i][4]=zero;
      dp[i][5]=zero;
      dp[i][6]=zero;
      have[i]=0;
    }
    for(i=l;i<=r;i++){
      x=f[id[i]];
      have[x]=1;
      ull*ptr=(ull*)&(dp[x][(i-l)>>9]);
      ptr[((i-l)&511)>>6]^=1ULL<<((i-l)&63);
    }
    for(i=1;i<=scc;i++)if(ok[i]){
      int L=en[i-1]+1,R=en[i];
      bool now=0;
      for(j=L;j<=R;j++){
        x=edge[j];
        if(!have[x])continue;
        now=1;
        dp[i][0]|=dp[x][0];
        dp[i][1]|=dp[x][1];
        dp[i][2]|=dp[x][2];
        dp[i][3]|=dp[x][3];
        dp[i][4]|=dp[x][4];
        dp[i][5]|=dp[x][5];
        dp[i][6]|=dp[x][6];
      }
      if(now)have[i]=1;
      if(need[i]){
        int cur=0;
        if(have[i]){
          for(j=0;j<K;j++){
            ull*ptr=(ull*)&(dp[i][j]);
            cur+=__builtin_popcountll(ptr[0]);
            cur+=__builtin_popcountll(ptr[1]);
            cur+=__builtin_popcountll(ptr[2]);
            cur+=__builtin_popcountll(ptr[3]);
            cur+=__builtin_popcountll(ptr[4]);
            cur+=__builtin_popcountll(ptr[5]);
            cur+=__builtin_popcountll(ptr[6]);
            cur+=__builtin_popcountll(ptr[7]);
          }
        }
        count[i]=cur;
      }
    }
    for(i=1;i<=n;i++)ans[i]+=count[f[i]];
  }
}
}
void solve(int x){
  int i,j;
  cp=0;
  dfs(x,0,0);
  if(cp<=4){
    for(i=1;i<=cp;i++)for(j=1;j<=cp;j++)if(i!=j&&lim[pool[i]]>=dis[pool[j]])Graph::addedge(pool[i],pool[j]);
  }else{
    sort(pool+1,pool+cp+1,cmp);
    for(i=1;i<=cp;i++)pool2[i]=pool[i];
    sort(pool2+1,pool2+cp+1,cmp2);
    for(i=1,j=0;i<=cp;i++){
      int A=pool2[i];
      ll B=lim[A];
      while(j<cp&&dis[pool[j+1]]<=B)j++;
      if(j)Graph::addedge(A,cnt+j);
    }
    for(i=1;i<=cp;i++)Graph::addedge(cnt+i,pool[i]);
    for(i=2;i<=cp;i++)Graph::addedge(cnt+i,cnt+i-1);
    cnt+=cp;
  }
  for(i=g[x];i;i=nxt[i])if(ok[i]){
    ok[i^1]=0;
    f[0]=all=son[v[i]];
    findroot(v[i],now=0);
    solve(now);
  }
}
void go(int x,int y,int z){
  dep[x]=z;
  if(z>dep[who])who=x;
  fa[x]=y;
  for(int i=g[x];i;i=nxt[i])if(v[i]!=y)go(v[i],x,z+1);
}
int main(){
  scanf("%d",&n);
  for(i=1;i<=n;i++)scanf("%lld",&rad[i]);
  for(ed=i=1;i<n;i++)scanf("%d%d%lld",&x,&y,&z),add(x,y,z),add(y,x,z);
  cnt=n;
  f[0]=all=n;
  findroot(1,0);
  solve(now);
  go(who=1,0,1);
  go(root=who,0,1);
  for(i=who;i;i=fa[i])pos[i]=++len;
  //printf("cnt=%d ce=%d\n",cnt,Graph::ed);
  Graph::gao();
  for(i=1;i<=n;i++)printf("%d ",Graph::ans[i]);
}