You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour.

Let's call colour c dominating in the subtree of vertex v if there are no other colours that appear in the subtree of vertex v more times than colour c. So it's possible that two or more colours will be dominating in the subtree of some vertex.

The subtree of vertex v is the vertex v and all other vertices that contains vertex v in each path to the root.

For each vertex v find the sum of all dominating colours in the subtree of vertex v.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of vertices in the tree.

The second line contains n integers ci (1 ≤ ci ≤ n), ci — the colour of the i-th vertex.

Each of the next n - 1 lines contains two integers xj, yj (1 ≤ xj, yj ≤ n) — the edge of the tree. The first vertex is the root of the tree.

Output

Print n integers — the sums of dominating colours for each vertex.

Examples

Input

4
1 2 3 4
1 2
2 3
2 4


Output

10 9 3 4


Input

15
1 2 3 1 2 3 3 1 1 3 2 2 1 2 3
1 2
1 3
1 4
1 14
1 15
2 5
2 6
2 7
3 8
3 9
3 10
4 11
4 12
4 13


Output

6 5 4 3 2 3 3 1 1 3 2 2 1 2 3
#include <bits/stdc++.h>
using namespace std;
const int MX = 100005;
int n, seg_sz, mxf, clr[MX], dpth[MX][2], ordr[MX], frq[MX];
long long ff[MX], ans[MX];
bool vis[MX];
static int clk;
vector<int> g[MX];
class _node {
 public:
  int lft, rgt, id, seg_no;
  void inpt(int nd) {
    lft = dpth[nd][0], rgt = dpth[nd][1];
    id = nd, seg_no = lft / seg_sz + ((lft % seg_sz) ? 1 : 0);
  }
  bool operator<(_node p) const {
    if (seg_no != p.seg_no) return (seg_no < p.seg_no);
    if (rgt != p.rgt) return (rgt < p.rgt);
    return (lft < p.lft);
  }
} qq[MX];
void dfs(int u) {
  dpth[u][0] = ++clk;
  ordr[clk] = u, vis[u] = true;
  for (int i = 0, sz = g[u].size(); i < sz; i++) {
    int v = g[u][i];
    if (!vis[v]) dfs(v);
  }
  dpth[u][1] = clk;
}
void addition(int st, int ed) {
  for (int i = st; i <= ed; i++) {
    ff[frq[clr[ordr[i]]]] -= clr[ordr[i]];
    mxf = max(mxf, ++frq[clr[ordr[i]]]);
    ff[frq[clr[ordr[i]]]] += clr[ordr[i]];
  }
}
void subtract(int st, int ed) {
  for (int i = st; i <= ed; i++) {
    ff[frq[clr[ordr[i]]]] -= clr[ordr[i]];
    if (mxf == frq[clr[ordr[i]]] && ff[frq[clr[ordr[i]]]] == 0) mxf--;
    --frq[clr[ordr[i]]];
    ff[frq[clr[ordr[i]]]] += clr[ordr[i]];
  }
}
void solution() {
  mxf = -1;
  int pre_lft, pre_rgt, cur_lft, cur_rgt;
  for (int i = 1; i <= n; i++) {
    cur_lft = qq[i].lft, cur_rgt = qq[i].rgt;
    if (i == 1) {
      addition(cur_lft, cur_rgt);
    } else {
      if (pre_lft > cur_lft) {
        addition(cur_lft, pre_lft - 1);
      } else {
        subtract(pre_lft, cur_lft - 1);
      }
      if (pre_rgt > cur_rgt) {
        subtract(cur_rgt + 1, pre_rgt);
      } else {
        addition(pre_rgt + 1, cur_rgt);
      }
    }
    ans[qq[i].id] = ff[mxf];
    pre_lft = cur_lft, pre_rgt = cur_rgt;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", clr + i);
  }
  for (int i = 1, a, b; i < n; i++) {
    scanf("%d %d", &a, &b);
    g[a].push_back(b), g[b].push_back(a);
  }
  clk = 0;
  dfs(1);
  seg_sz = max(1, (int)sqrt(n));
  for (int i = 1; i <= n; i++) {
    qq[i].inpt(i);
  }
  sort(qq + 1, qq + n + 1);
  solution();
  for (int i = 1; i <= n; i++) {
    printf("%lld", ans[i]);
    (i == n) ? printf("\n") : printf(" ");
  }
  return 0;
}
