#include <bits/stdc++.h>   using namespace std;   bool way[5003][5003];   int dis[5003], counter;   vector<pair<int, int> > edge[5003], child[5003];   priority_queue<pair<int, int> > pq[5003];   void cari(int pos) {     int j, sz;     sz = edge[pos].size();     for (j = 0; j < sz; ++j) {       if (way[pos][edge[pos][j].first]) {         way[pos][edge[pos][j].first] = false;         way[edge[pos][j].first][pos] = false;         child[pos].push_back(edge[pos][j]);         cari(edge[pos][j].first);       }     }   }   void cek(int posisi, int time) {     int i, j, x, size, sz;     sz = child[posisi].size();     for (i = 0; i < sz; ++i) {       x = child[posisi][i].first;       size = child[posisi][i].second;       for (j = 0; j < size; ++j) {         if (pq[x].empty()) break;         pq[posisi].push(pq[x].top());         if (posisi == 1) {           dis[pq[x].top().second] = time;           ++counter;         }         pq[x].pop();       }       cek(x, time);     }   }   int main() {     int i, a, b, c, n, sz, arrive, dummy, division[5003];     scanf( %d , &n);     for (i = 1; i <= n; ++i) scanf( %d , &division[i]);     memset(way, true, sizeof(way));     for (i = 1; i < n; ++i) {       scanf( %d %d %d , &a, &b, &c);       edge[a].push_back(make_pair(b, c));       edge[b].push_back(make_pair(a, c));     }     cari(1);     for (i = 2; i <= n; ++i) pq[i].push(make_pair(-division[i], i));     sz = child[1].size();     dis[1] = 0;     int temp = 1;     counter = 1;     while (counter < n) {       cek(1, temp);       ++temp;     }     for (i = 1; i <= n; ++i) {       if (i != n)         printf( %d  , dis[i]);       else         printf( %d n , dis[i]);     }     return 0;   }   