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;
long long Read() {
  long long i = 0, f = 1;
  char ch = 0;
  while (!isdigit(ch) && ch != '-') ch = getchar();
  if (ch == '-') ch = getchar(), f = -1;
  while (isdigit(ch)) i = (i << 3) + (i << 1) + ch - 48, ch = getchar();
  return i * f;
}
const long long N = 1e5 + 5;
long long n, col[N], sz;
vector<long long> G[N];
long long res[N], lim;
long long root[N];
struct Tree {
  long long lc, rc, max, ans, cnt;
} tre[N * 20];
void push_up(long long p) {
  if (!tre[p].lc && !tre[p].rc) return;
  if (tre[tre[p].lc].cnt > tre[tre[p].rc].cnt) {
    tre[p].cnt = tre[tre[p].lc].cnt;
    tre[p].max = tre[tre[p].lc].max;
    tre[p].ans = tre[tre[p].lc].ans;
  } else if (tre[tre[p].lc].cnt < tre[tre[p].rc].cnt) {
    tre[p].cnt = tre[tre[p].rc].cnt;
    tre[p].max = tre[tre[p].rc].max;
    tre[p].ans = tre[tre[p].rc].ans;
  } else {
    tre[p].cnt = tre[tre[p].lc].cnt;
    tre[p].max = tre[tre[p].lc].max;
    tre[p].ans = tre[tre[p].lc].ans + tre[tre[p].rc].ans;
  }
}
void update(long long &p, long long l, long long r, long long pos,
            long long v) {
  if (!p) p = ++sz;
  if (l == r) {
    tre[p].cnt += v;
    tre[p].max = l;
    tre[p].ans = l;
    return;
  }
  long long mid = l + r >> 1;
  if (pos <= mid)
    update(tre[p].lc, l, mid, pos, v);
  else
    update(tre[p].rc, mid + 1, r, pos, v);
  push_up(p);
  return;
}
long long merge(long long p, long long rt, long long l, long long r) {
  if (!p || !rt) return p | rt;
  if (l == r) {
    tre[p].cnt += tre[rt].cnt;
    tre[p].max = l;
    tre[p].ans = l;
    return p;
  }
  long long mid = l + r >> 1;
  tre[p].lc = merge(tre[p].lc, tre[rt].lc, l, mid);
  tre[p].rc = merge(tre[p].rc, tre[rt].rc, mid + 1, r);
  push_up(p);
  return p;
}
void DFS(long long u, long long fa) {
  for (long long v : G[u])
    if (v ^ fa) {
      DFS(v, u);
      root[u] = merge(root[u], root[v], 1, lim);
    }
  update(root[u], 1, lim, col[u], 1);
  res[u] = tre[root[u]].ans;
  return;
}
signed main() {
  n = Read();
  for (long long i = 1; i <= n; ++i) lim = max(lim, col[i] = Read());
  for (long long i = 1; i < n; ++i) {
    long long u = Read(), v = Read();
    G[u].push_back(v);
    G[v].push_back(u);
  }
  DFS(1, 0);
  for (long long i = 1; i <= n; ++i) printf("%lld ", res[i]);
  return 0;
}
