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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Marcin Knapik

#pragma GCC optimize ("O3")
#include<bits/stdc++.h>
using namespace std;

#define FOR(i, n) for (int i = 0; i < n; i++)
#define f first
#define s second
#define pb push_back
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;

template <class T>ostream &operator<<(ostream &os, vector<T> &vec){for (T &el : vec){os << el << ' ';}return os;}
template <class T>istream &operator>>(istream &is, vector<T> &vec) {for (T &el : vec){is >> el;}return is;}

template <class T, class G> ostream &operator<<(ostream &os, pair<T, G> para) { os << para.f << ' ' << para.s; return os;}

using ll = long long;
using vi = vector<int>;
using ii = pair<int, int>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vvpll = vector<vpll>;

const int N = 100 * 1000 + 5;

vvpll G;
int n;

using sett = multiset<pair<ll, pll>>;
ll range[N];
sett sety[N];
ll lazy[N];
int rep[N];

vector<vi> G2;

int all_edges;
const int BOUND = 2000;
const int EDGES = 500;

void dodaj(pair<ll, pll> el, int set_id){
    el.f -= lazy[set_id];
    el.s.s += lazy[set_id];
    sety[set_id].insert(el);
}

pair<ll, pll> wyciagnij_gore(int set_id){
    assert(sz(sety[set_id]) > 0);
    auto ret = *sety[set_id].begin();
    ret.f += lazy[set_id];
    ret.s.s -= lazy[set_id];
    return ret;
}

void wypisz(int set_id){
    for(auto u : sety[set_id]){
        u.f += lazy[set_id];
        u.s.s -= lazy[set_id];
        cout << u.f << ' ' << u.s.f << ' ' << u.s.s << endl;
    }
}

// #define DEBUG

void polacz(int start, int id_set_drugi){
    int my_id = rep[start];

    if(sz(sety[my_id]) < sz(sety[id_set_drugi])){
        swap(my_id, id_set_drugi);
        rep[start] = my_id;
    }

#ifdef DEBUG
    cout << "LACZE  " << start + 1 << ' ' << rep[start] + 1 << ' ' << id_set_drugi + 1 << endl;
    cout << "MOJ SET " << endl;
    wypisz(my_id);
    cout << "DRUGI SET " << endl;
    wypisz(id_set_drugi);
#endif


    for(auto u : sety[id_set_drugi]){
        u.f += lazy[id_set_drugi];
        u.s.s -= lazy[id_set_drugi];
        // cout << "RZECZYWISTY STAN DODANEGO " << u.f << ' ' << u.s.f << ' ' << u.s.s << endl;
        u.f -= lazy[my_id];
        u.s.s += lazy[my_id];

        // cout << "DODAWANY PO ZMIANIE Z LAZY " << u.f << ' ' << u.s.f << ' ' << u.s.s << endl;

        auto check = [](int set_id, sett::iterator it){
            if(next(it) == sety[set_id].end()){
                return sety[set_id].end();
            }

            auto nast = next(it);

            // cout << "IT CZECK" << endl;
            // cout << "IT BEZ LAZY" << it -> f << ' ' << it -> s.f << ' ' << it -> s.s << '\n';

            // cout << it -> f + lazy[set_id] << ' ' << it -> s.f << ' ' << it -> s.s - lazy[set_id] << '\n';
            // cout << nast -> f + lazy[set_id] << ' ' << nast -> s.f << ' ' << nast -> s.s - lazy[set_id] << '\n';
            
            if(it -> s.s - lazy[set_id] >= nast -> f + lazy[set_id]){
                // cout << "WSZED " << endl;
                auto dod = *it;
                dod.s.f += nast -> s.f;
                dod.s.s = max(dod.s.s, nast -> s.s);
                
                sety[set_id].erase(it);
                sety[set_id].erase(nast);

                return sety[set_id].insert(dod);
            } else {
                // cout << "NIE WSZED " << endl;
                return sety[set_id].end();
            }
        };

        auto it = sety[my_id].insert(u);
        auto it2 = it;

        if(it != sety[my_id].begin())
            it = check(my_id, prev(it));
        
        if(it == sety[my_id].end()){
            it = it2;
        }

        while(it != sety[my_id].end()) {
            it = check(my_id, it);
        }
    }

#ifdef DEBUG
    cout << "WYNIK " << endl;
    wypisz(my_id);
    // exit(0);
#endif
}

int vis[N];
void dfs1(int very_start, int start, int przodek, ll dist=0){
    if(range[very_start] >= dist){
        G2[very_start].pb(start);
        all_edges++;
        // assert(all_edges < n * 50);
    } else {
        return;
    }

    for(auto & [b, c] : G[start]){
        if(b == przodek)
            continue;
        dfs1(very_start, b, start, dist + c);
    }
}

void dfs2(int start){
    vis[start] = true;
    for(auto & u : G2[start]){
        if(vis[u] == false) {
            dfs2(u);
        }
    }
}

void dfs(int start, int przodek){
    rep[start] = start;
    sety[start] = sett{{0, {1, range[start]}}}; 
    lazy[start] = 0;

    for(auto & [neigh, cost] : G[start]){
        if(neigh == przodek){
            continue;
        }
        dfs(neigh, start);
        lazy[rep[neigh]] += cost;
        polacz(start, rep[neigh]);
    }
#ifdef DEBUG
    cout << "SET DLA: " << start + 1 << ' ' << "REPREZENTANT " << rep[start] + 1 << endl;
    for(auto & [a, b] : sety[rep[start]]){
        cout << a + lazy[rep[start]] << ' ' << b.f << ' ' << b.s - lazy[rep[start]] << endl;
    }
#endif

}

void solve () {
    cin >> n;
    G = vvpll(n);
    G2 = vector<vector<int>> (n);

    FOR(i, n) {
        cin >> range[i];
    }

    for(int i = 0; i < n - 1; i++) {
        ll a, b, c;
        cin >> a >> b >> c;
        a--, b--;

        G[a].pb({b, c});
        G[b].pb({a, c});
    }

    FOR(i, n){
        if(n <= BOUND or all_edges < n * EDGES)
            dfs1(i, i, i, 0);
    }
    // BRUT N^3
    // cerr << "n   " << n << "   all edges: " << all_edges << endl;
    
    if(n <= BOUND or all_edges < n * EDGES){
        FOR(i, n){
            dfs2(i);
            int ans = 0;
            FOR(j, n){
                ans += vis[j];
                vis[j] = false;
            }
            cout << ans << ' ';

            // if(i % 1000 == 0){
                // cerr << i << endl;
            // }
        }
        cout << '\n';
        return;
    }
    // KONIEC BRUTA

    // TROCHE LEPSZY
    FOR(i, n){
        dfs(i, i);
        // if(i % 100 == 0){
        //     cerr << i << endl;
        // }
        cout << sety[rep[i]].begin() -> s.f << ' ';
    }
}

int main () {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int tests = 1;
    // cin >> tests;

    for (int test = 1; test <= tests; test++) {
        solve();
    }

    return 0;
}