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;
template <typename T>
void readin(T &x) {
  x = 0;
  char ch = getchar();
  T fh = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') fh = -1;
  for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
  x *= fh;
}
template <typename T>
void wt(T x) {
  if (x > 9) wt(x / 10);
  putchar(x % 10 + 48);
}
template <typename T>
void writeln(T x, char ch) {
  wt(x);
  putchar(ch);
}
const int N = 1e5 + 5;
int hd[N], num[N], siz[N], son[N], cnt[N], c[N], st[N], en[N], tot, rec[N];
struct info {
  int to, nex;
} l[N << 1];
void build(int p, int fa) {
  tot++;
  rec[tot] = p;
  st[p] = tot;
  int u;
  for (int e = hd[p]; e; e = l[e].nex) {
    u = l[e].to;
    if (u != fa) {
      build(u, p);
      if (siz[u] > siz[son[p]]) son[p] = u;
      siz[p] += siz[u];
    }
  }
  siz[p]++;
  en[p] = tot;
}
long long ans[N];
void dfs(int p, int fa, int opt) {
  int u;
  for (int e = hd[p]; e; e = l[e].nex) {
    u = l[e].to;
    if (u != fa && u != son[p]) dfs(u, p, 0);
  }
  if (son[p]) {
    dfs(son[p], p, 1);
    num[p] = num[son[p]];
    ans[p] = ans[son[p]];
  }
  cnt[c[p]]++;
  if (cnt[c[p]] > num[p]) {
    num[p] = cnt[c[p]];
    ans[p] = c[p];
  } else if (cnt[c[p]] == num[p]) {
    ans[p] += c[p];
  }
  int v;
  for (int e = hd[p]; e; e = l[e].nex) {
    u = l[e].to;
    if (u != fa && u != son[p]) {
      for (int i = st[u]; i <= en[u]; i++) {
        v = rec[i];
        cnt[c[v]]++;
        if (cnt[c[v]] > num[p]) {
          num[p] = cnt[c[v]];
          ans[p] = c[v];
        } else if (cnt[c[v]] == num[p]) {
          ans[p] += c[v];
        }
      }
    }
  }
  if (!opt) {
    for (int i = st[p]; i <= en[p]; i++) {
      cnt[c[rec[i]]]--;
    }
  }
}
int main() {
  int n;
  readin(n);
  for (int i = 1; i <= n; i++) readin(c[i]);
  int u, v;
  for (int i = 1; i < n; i++) {
    readin(u);
    readin(v);
    l[i] = (info){v, hd[u]};
    hd[u] = i;
    l[i + n] = (info){u, hd[v]};
    hd[v] = i + n;
  }
  build(1, 0);
  dfs(1, 0, 1);
  for (int i = 1; i < n; i++) writeln(ans[i], ' ');
  writeln(ans[n], '\n');
  return 0;
}
