func_code_string: #include <bits/stdc++.h>   const int kMaxN = 5000;   std::vector<int> way[kMaxN], cost[kMaxN];   int f[kMaxN], c[kMaxN];   short hash[kMaxN][kMaxN], father[kMaxN][2 * kMaxN];   int a[kMaxN], id[kMaxN], t[kMaxN];   inline bool cmp(int x, int y) { return a[x] < a[y]; }   void BuildTree(int u, int ff) {     f[u] = ff;     for (size_t i = 0; i < way[u].size(); ++i) {       int v = way[u][i];       if (v == ff) continue;       c[v] = cost[u][i];       BuildTree(v, u);     }   }   int Find(short *f, int x) {     int y = x, z;     while (f[y] != -1) y = f[y];     while (x != y) {       z = x;       x = f[x];       f[z] = y;     }     return y;   }   int Put(int x) {     int t = 0;     while (x != 0) {       ++t;       if (f[x] == 0) return t;       int ff = f[x];       t = Find(father[ff], t);       if (++hash[ff][t] == c[ff]) {         father[ff][t] = t + 1;       }       x = ff;     }     return 0;   }   int main() {     int n, x, y, cc;     scanf( %d , &n);     for (int i = 0; i < n; ++i) {       scanf( %d , a + i);       id[i] = i;     }     std::sort(id, id + n, cmp);     for (int j = 1; j < n; ++j) {       scanf( %d%d%d , &x, &y, &cc);       --x;       --y;       way[x].push_back(y);       way[y].push_back(x);       cost[x].push_back(cc);       cost[y].push_back(cc);     }     BuildTree(0, -1);     memset(father, -1, sizeof(father));     for (int i = 0; i < n; ++i) {       t[id[i]] = Put(id[i]);     }     for (int i = 0; i < n; ++i) {       if (i != 0) printf(   );       printf( %d , t[i]);     }     puts(  );     return 0;   }   