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 gch() {
  static char buf[100010], *h = buf, *t = buf;
  return h == t && (t = (h = buf) + fread(buf, 1, 100000, stdin), h == t)
             ? EOF
             : *h++;
}
inline void re(long long &x) {
  x = 0;
  char a;
  bool b = 0;
  while (!isdigit(a = getchar())) b = a == '-';
  while (isdigit(a)) x = x * 10 + a - '0', a = getchar();
  if (b == 1) x = -x;
}
const long long ms = 1e5 + 10;
long long n, head[ms], ta, lx, ly, tot = -1, c[ms], sz[ms], ans[ms], s[ms],
                                   num[ms], son[ms], p[ms];
struct in {
  long long to, ne;
} ter[ms << 1];
inline void build(long long f, long long l) {
  ter[++tot] = (in){l, head[f]}, head[f] = tot;
  ter[++tot] = (in){f, head[l]}, head[l] = tot;
}
void dfs1(long long no, long long f) {
  sz[no] = 1;
  for (register int i = head[no]; i >= 0; i = ter[i].ne) {
    long long to = ter[i].to;
    if (to == f) continue;
    dfs1(to, no), sz[no] += sz[to];
    if (son[no] == 0 || sz[son[no]] < sz[to]) son[no] = to;
  }
}
bool flag[ms];
void change(long long no, long long f, long long v) {
  s[num[c[no]]] -= c[no], num[c[no]] += v, s[num[c[no]]] += c[no];
  if (s[ta + 1]) ta++;
  if (!s[ta]) ta--;
  for (register int i = head[no]; i >= 0; i = ter[i].ne) {
    long long to = ter[i].to;
    if (to != f && !flag[to]) change(to, no, v);
  }
}
void dfs2(long long no, long long f, bool v) {
  for (register int i = head[no]; i >= 0; i = ter[i].ne) {
    long long to = ter[i].to;
    if (to == f || to == son[no]) continue;
    dfs2(to, no, 0);
  }
  if (son[no]) dfs2(son[no], no, 1), flag[son[no]] = 1;
  change(no, f, 1), flag[son[no]] = 0, ans[no] = s[ta];
  if (!v) change(no, f, -1);
}
int main() {
  re(n), memset(head, -1, sizeof(head));
  for (register int i = 1; i <= n; i++) re(c[i]);
  for (register int i = 1; i < n; i++) re(lx), re(ly), build(lx, ly);
  dfs1(1, 0), dfs2(1, 0, 1);
  for (register int i = 1; i <= n; i++) printf("%lld ", ans[i]);
}
