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;
int n, a[100005];
struct edge {
  int nxt, to;
} e[200005];
int cnt, head[100005];
void add(int u, int v) {
  e[cnt].to = v;
  e[cnt].nxt = head[u];
  head[u] = cnt++;
}
struct point {
  int num, l, r, maxn;
  long long sum;
} s[2000005];
int top, root[100005];
void change_father(int u) {
  if (s[s[u].l].maxn < s[s[u].r].maxn)
    s[u].maxn = s[s[u].r].maxn, s[u].sum = s[s[u].r].sum;
  if (s[s[u].l].maxn == s[s[u].r].maxn)
    s[u].maxn = s[s[u].l].maxn, s[u].sum = s[s[u].l].sum + s[s[u].r].sum;
  if (s[s[u].l].maxn > s[s[u].r].maxn)
    s[u].maxn = s[s[u].l].maxn, s[u].sum = s[s[u].l].sum;
}
void build(int u, int l, int r, int t) {
  if (l == r && l == t) {
    s[u].maxn = 1;
    s[u].sum = l;
  } else {
    int mid = (l + r) / 2;
    if (mid >= t)
      s[u].l = ++top, build(s[u].l, l, mid, t);
    else
      s[u].r = ++top, build(s[u].r, mid + 1, r, t);
    change_father(u);
  }
}
int merge(int u, int v, int l, int r) {
  if (!u) return v;
  if (!v) return u;
  if (l == r) {
    s[u].maxn += s[v].maxn;
  } else {
    int mid = (l + r) / 2;
    s[u].l = merge(s[u].l, s[v].l, l, mid);
    s[u].r = merge(s[u].r, s[v].r, mid + 1, r);
    change_father(u);
  }
  return u;
}
void dfs(int u, int fa) {
  for (int i = head[u]; i != -1; i = e[i].nxt) {
    int v = e[i].to;
    if (v == fa) continue;
    dfs(v, u);
    merge(root[u], root[v], 1, n);
  }
}
int main() {
  memset(head, -1, sizeof(head));
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    add(u, v);
    add(v, u);
  }
  for (int i = 1; i <= n; i++) {
    root[i] = ++top;
    build(root[i], 1, n, a[i]);
  }
  dfs(1, 1);
  for (int i = 1; i <= n; i++) printf("%lld ", s[root[i]].sum);
}
