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
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
const int N=100100;
ll r[N];
vector<pair<int,ll> >g[N];
ll d[N];
int sz[N];
void dfs_dist(int v,int pr){
    for (auto cur:g[v]){
        int to=cur.first;
        ll w=cur.second;
        if (to==pr) continue;
        d[to]=d[v]+w;
        dfs_dist(to,v);
    }
}
vector<int>order;
vector<int>G[N];
vector<int>R[N];
int cmp[N];
bool used[N];
void dfs1(int v,int ind){
    used[v]=true;
    cmp[v]=ind;
    sz[ind]++;
    for (int to:R[v]){
        if (!used[to]){
            dfs1(to,ind);
        }
    }
    //order.push_back(v);
}
vector<pair<int,int> >E;
void dfs(int v){
    used[v]=true;
    for (int to:G[v]){
        if (!used[to]){
            E.push_back({v,to});
            dfs(to);
        }
    }
    order.push_back(v);
}
const int K=5100;
bitset<K>bt[K];

void dfs2(int v){
    bt[v][v]=1;

    used[v]=true;
    for (int to:G[v]){
        if (!used[to]){
            dfs2(to);
        }
        bt[v]|=bt[to];

    }
}
bool good[N];
int sum[N];
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;cin>>n;
    for (int i=1;i<=n;i++) cin>>r[i];
    for (int i=1;i<n;i++){
        int a,b;cin>>a>>b;
        ll c;cin>>c;
        g[a].push_back({b,c});
        g[b].push_back({a,c});
    }
    for (int i=1;i<=n;i++){
        d[i]=0;
        dfs_dist(i,0);
        for (int j=1;j<=n;j++){
            if (i!=j && d[j]<=r[i]){
                G[i].push_back(j);
                R[j].push_back(i);
            }
        }
    }
    for (int i=1;i<=n;i++){
        if (used[i]) continue;
        dfs(i);
    }
    reverse(order.begin(),order.end());
    for (int i=1;i<=n;i++) used[i]=false;
    int cnt=0;
    for (int i:order){

        if (used[i]) continue;
        cnt++;
        dfs1(i,cnt);
    }
    for (int i=1;i<=n;i++){
//        cout<<cmp[i]<<" ";
        G[i].clear();
        used[i]=false;
    }
//    cout<<endl;
    for (int i=1;i<=n;i++){
        for (int j:R[i]){
            if (cmp[j]==cmp[i]) continue;
            G[cmp[j]].push_back(cmp[i]);
//            cout<<cmp[j]<<" "<<cmp[i]<<endl;
        }
    }
    order.clear();
    for (int i=1;i<=cnt;i++) used[i]=false;
    for (int i=1;i<=cnt;i++){
        if (!used[i]){
            dfs2(i);
        }
    }
    if (cnt>=K) exit(1);
    for (int i=1;i<=cnt;i++){
        sum[i]=0;
        for (int j=1;j<=cnt;j++) sum[i]+=bt[i][j]*sz[j];
    }
    for (int i=1;i<=n;i++) cout<<sum[cmp[i]]<<" ";
    cout<<'\n';
    return 0;
}
/*
5
8 1 0 4 6
2 4 2
3 1 9
2 5 5
2 1 2
*/