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
#include <iostream>
#include <vector>
#include <bitset>
#include <algorithm>
#include <cassert>

using namespace std;

// FIXME: consider Ofast and tune=native
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt,tune=native")

// #define DEBUG
#ifdef DEBUG
#define dassert(x) assert(x);
#define df(...) printf(__VA_ARGS__)
#else
#define dassert(x)
#define df(...)
#endif

#define x first
#define y second
#define mp make_pair
#define pb push_back
#define ir(x, a, b) ((a) <= (x) && (x) <= (b))
#define vec vector
#define sz(x) (ll)x.size()
#define foru(i, n) for (int i = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()

typedef int64_t ll;

int read() {
    int n = 0; bool b = 0; char c = getchar();
    for (; !ir(c, '0', '9'); c = getchar()) b = (c == '-');
    for (; ir(c, '0', '9'); c = getchar()) n = 10*n + (c-'0');
    if (b) return -n;
    return n;
}

const int N = 10001; // adjust according to test runs
int n;
int lcast[18][2*N];
ll r[N];
ll d[N];
vec<vec<pair<int, ll>>> g;
vec<vec<int>> g_;
// bool used[N];
// vec<int> order;
vec<int> euler;
int height[N];
int first[N];
// int rep[N];
int sz[N];

void dfs0(int v, int p) {
    first[v] = euler.size();
    euler.pb(v);
    for (auto [c, cost] : g[v]) {
        if (c == p) continue;
        d[c] = d[v] + cost;
        height[c] = height[v]+1;
        dfs0(c, v);
        euler.pb(v);
    }
}

void buildlcast() {
    ll sz = euler.size();
    // cout << "sz: " << sz << endl;
    for (int i = 0; i < sz; ++i) {
        lcast[0][i] = euler[i];
    }
    for (ll j = 1; (1 << j) <= sz; ++j) {
        for (int i = 0; i + (1 << j) < sz; ++i) {
            ll left = lcast[j-1][i], right = lcast[j-1][i + (1 << (j-1))];
            if (height[left] < height[right]) {
                lcast[j][i] = left;
            } else {
                lcast[j][i] = right;
            }
        }
    }
}

ll log(ll x) {
    return 31 - __builtin_clz(x);
}

ll lca(ll v1, ll v2) {
    ll i1 = first[v1], i2 = first[v2];
    if (i2 < i1) {
        swap(i1, i2);
    }
    ll len = i2-i1+1, j = log(len);
    ll left = lcast[j][i1], right = lcast[j][i2 + 1 - (1 << j)];
    if (height[left] < height[right]) {
        return left;
    } else {
        return right;
    }
}

// void dfs1(int v) {
//     used[v] = 1;
//
//     foru (c, n) {
//         if (!used[c] && e[v][c])
//             dfs1(c);
//     }
//
//     order.pb(v);
// }
//
// vec<int> component;
// void dfs2(int v) {
//     df("v: %d\n", v);
//     used[v] = true;
//     component.push_back(v);
//
//     foru (c, n) {
//         if (!used[c] && e[c][v])
//             dfs2(c);
//     }
// }
//
// void dfs3(int v) {
//     dassert(v == rep[v]);
//     used[v] = 1;
//     foru (c, n) {
//         if (e[v][c]) {
//                 
//             dfs3(rep[c]);
//             sz[v] += sz[rep[c]];
//         }
//     }
// }



int main() {
    df("debug mode\n");
#ifndef DEBUG
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
    // #define TEST
#ifdef TEST
    n = 5000;
    g.resize(n);
    euler.reserve(2*n);
    foru (i, n) r[i] = rand();
    for (int b = 1; b < n; ++b) {
        int a = rand()%b;
        int c = rand();
        g[a].pb({b, c});
        g[b].pb({a, c});
    }
#else
    cin >> n;
    g.resize(n);
    euler.reserve(2*n);
    foru (i, n) {
        cin >> r[i];
    }
    foru (i, n-1) {
        ll a, b, c;
        cin >> a >> b >> c;
        --a, --b;
        g[a].pb({b, c});
        g[b].pb({a, c});
    }
#endif
    // order.reserve(n);
    dfs0(0, 0);
    buildlcast();
    // if (n <= 6000)  {
        #define mN 6010
        bitset<mN> e[mN], mask;
        foru (i, n) {
            e[i][i] = 1;
            foru (j, i) {
                // FIXME: possibly add to component if it's already obvious here
                ll dist = (d[i] + d[j] - 2*d[lca(j, i)]);
                e[i][j] = dist <= r[i];
                if (e[i][j]) { df("%d->%d\n", i, j); }
                e[j][i] = dist <= r[j];
                if (e[j][i]) { df("%d->%d\n", j, i); }
            }
        }

        foru (k, n) {
            mask[k]=1;
            bitset <mN> temp = e[k];
            foru (i, n)
                if((e[i] & mask).any())
                    e[i] = e[i]|temp;
            mask[k] = 0;
        }
        foru (v, n) cout << e[v].count() << " ";
    // } 
    // else {
    //     for (int i = 0; i < n; i++)
    //         if (!used[i])
    //             dfs1(i);
    //
    //     foru (i, n) used[i] = 0;
    //     reverse(all(order));
    //
    //     foru (i, n) rep[i] = -1;
    //     for (auto v : order) {
    //         if (!used[v]) {
    //             // FIXME: make a new graph - a condensation graph
    //             dfs2 (v);
    //
    //             df("component: ");
    //             for (auto c : component) {
    //                 for 
    //                 df("%d ", c);
    //                 rep[c] = v;
    //                 ++sz[v];
    //             }
    //             df("\n");
    //             component.clear();
    //         }
    //     }
    //
    //     foru (i, n) used[i] = 0;
    //     foru (v, n) {
    //         if (!used[rep[v]]) dfs3(rep[v]);
    //     }
    // }

    return 0;
}