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;
inline char nc() {
  static char buf[100000], *l = buf, *r = buf;
  return l == r && (r = (l = buf) + fread(buf, 1, 100000, stdin), l == r)
             ? EOF
             : *l++;
}
inline int red() {
  int res = 0, f = 1;
  char ch = nc();
  while (ch < '0' || '9' < ch) {
    if (ch == '-') f = -f;
    ch = nc();
  }
  while ('0' <= ch && ch <= '9') res = res * 10 + ch - 48, ch = nc();
  return res * f;
}
const int maxn = 100005, maxe = 200005;
int n, c[maxn], cnt[maxn], num[maxn];
long long sum[maxn], ans[maxn];
int tot, son[maxe], nxt[maxe], lnk[maxn];
inline void add(int x, int y) {
  son[++tot] = y;
  nxt[tot] = lnk[x];
  lnk[x] = tot;
}
int H_son[maxn], s[maxn], MAX;
bool xH[maxn];
void getH(int x, int fa) {
  s[x] = 1;
  H_son[x] = 0;
  for (int j = lnk[x]; j; j = nxt[j])
    if (son[j] != fa) {
      getH(son[j], x);
      s[x] += s[son[j]];
      if (!H_son[x] || s[H_son[x]] < s[son[j]]) H_son[x] = son[j];
    }
}
void change(int x, int fa, int w) {
  num[cnt[c[x]]]--;
  sum[cnt[c[x]]] -= c[x];
  cnt[c[x]] += w;
  num[cnt[c[x]]]++;
  sum[cnt[c[x]]] += c[x];
  if (num[MAX + 1])
    MAX++;
  else if (!num[MAX])
    MAX--;
  for (int j = lnk[x]; j; j = nxt[j])
    if (son[j] != fa && !xH[son[j]]) change(son[j], x, w);
}
void dsu(int x, int fa, bool keep) {
  for (int j = lnk[x]; j; j = nxt[j])
    if (son[j] != fa && son[j] != H_son[x]) dsu(son[j], x, 0);
  if (H_son[x]) dsu(H_son[x], x, 1), xH[H_son[x]] = 1;
  change(x, fa, 1);
  xH[H_son[x]] = 0;
  ans[x] = sum[MAX];
  if (!keep) change(x, fa, -1);
}
int main() {
  n = red();
  for (int i = 1; i <= n; i++) c[i] = red();
  for (int i = 1, x, y; i < n; i++) x = red(), y = red(), add(x, y), add(y, x);
  getH(1, 0);
  dsu(1, 0, 0);
  for (int i = 1; i <= n; i++) printf("%I64d ", ans[i]);
  return 0;
}
