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
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
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

int n;

template<size_t limit>
void solve(int __xddx__) {
    (void)__xddx__;
    using BS = bitset<limit>;

    vector<LL> ranges(n);
    REP(i, n)
        cin >> ranges[i];

    vector<tuple<int, int, LL>> edges(n - 1);
    for (auto& [a, b, c] : edges) {
        cin >> a >> b >> c;
        --a, --b;
    }

    pair<LL, int> best = {-1, -1};
    REP(i, n)
        best = max(best, pair(ranges[i], i));
    const int root = best.second;

    vector<vector<pair<int, LL>>> graph(n);
    vector<int> permutation(n), inverse_permutation(n);

    {
        vector<vector<int>> hld_graph(n);
        for (const auto [a, b, c] : edges) {
            hld_graph[a].emplace_back(b);
            hld_graph[b].emplace_back(a);
        }

        vector<int> subtree_size(n);
        function<void(int,int)> hld_dfs = [&](int v, int p) {
            ++subtree_size[v];
            for (const auto b : hld_graph[v]) {
                if (b != p) {
                    hld_dfs(b, v);
                    subtree_size[v] += subtree_size[b];
                }
            }
            sort(hld_graph[v].begin(), hld_graph[v].end(), [&](int a, int b) {
                    if (a == p)
                        return false;
                    if (b == p)
                        return true;
                    return subtree_size[a] > subtree_size[b];
                    });
        };
        hld_dfs(root, root);

        int next = 0;
        function<void(int, int)> reorder_dfs = [&](int v, int p) {
            permutation[v] = next;
            inverse_permutation[next] = v;
            ++next;
            for (const auto b : hld_graph[v]) {
                if (b != p) {
                    reorder_dfs(b, v);
                }
            }
        };
        reorder_dfs(root, root);

        vector<LL> new_ranges(n);
        REP(i, n) {
            new_ranges[i] = ranges[inverse_permutation[i]];
        }
        ranges = new_ranges;

        for (auto& [a, b, c] : edges) {
            a = permutation[a];
            b = permutation[b];
            graph[a].emplace_back(b, c);
            graph[b].emplace_back(a, c);
        }

        function<void(int, int)> sorting_dfs = [&](int v, int p) {
            for (const auto [b, c] : graph[v]) {
                if (b != p) {
                    sorting_dfs(b, v);
                }
            }
            sort(graph[v].begin(), graph[v].end(), [p](pair<int, int> e, pair<int, int> f) {
                    if (e.first == p)
                        return false;
                    if (f.first == p)
                        return true;
                    return e < f;
                    });
        };
        sorting_dfs(0, 0);
    }
    debug(n, ranges);
    debug(graph);

    function<void(int, int, LL, BS&)> calc_basic_dfs = [&](int v, int p, LL l, BS& bs) {
        bs[v] = true;
        for (const auto [b, c] : graph[v]) {
            if (b != p and l >= c) {
                calc_basic_dfs(b, v, l - c, bs);
            }
        }
    };

    vector<BS> basic(n);

    REP(i, n) {
        calc_basic_dfs(i, i, ranges[i], basic[i]);
    }
    debug(basic);

    BS known;

    REP(i, n) {
        BS to_be_considered = basic[i];
        to_be_considered[i] = false;

        while (true) {
            BS temp = to_be_considered & known;
            auto pos = temp._Find_first();
            if (pos != limit) {
                basic[i] |= basic[pos];
                to_be_considered &= ~basic[pos];
            }
            else {
                auto x = to_be_considered._Find_first();
                if (x == limit) {
                    known[i] = true;
                    break;
                }
                to_be_considered |= (basic[x] & ~basic[i]);
                basic[i] |= basic[x];
                to_be_considered[x] = false;
            }
        }
    }

    REP(i, n)
        cout << basic[permutation[i]].count() << ' ';
    cout << '\n';
}

bool run_solve_done = false;

template<size_t limit>
void run_solve() {
    if (run_solve_done)
        return;
    if (n <= int(limit)) {
        run_solve_done = true;
        solve<limit>(69);
    }
}

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

    cin >> n;

    constexpr int n_limit = 1e5;

    run_solve<1000>();
    run_solve<2000>();
    run_solve<3000>();
    run_solve<4000>();
    run_solve<5000>();
    run_solve<6000>();
    run_solve<7000>();
    run_solve<8000>();
    run_solve<9000>();
    run_solve<10000>();
    run_solve<12500>();
    run_solve<15000>();
    run_solve<17500>();
    run_solve<20000>();
    run_solve<25000>();
    run_solve<30000>();
    run_solve<40000>();
    run_solve<50000>();
    run_solve<75000>();

    run_solve<n_limit>();

    assert(run_solve_done);
}