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 t, c, n, k, m, u, v, l, rs, r, tim, mx, col[100010], a[100010],
    res[100010], sum[100010], freq[100010], mxf[100010], disc[100010],
    fin[100010];
vector<long long> adj[100010];
bool vis[100010];
void dfs(long long s) {
  vis[s] = 1;
  disc[s] = ++tim;
  long long sz = adj[s].size();
  for (int i = 0; i < sz; i++) {
    u = adj[s][i];
    if (vis[u] == 0) {
      dfs(u);
    }
  }
  fin[s] = tim;
}
struct d {
  long long bn, lt, rt, id, k;
};
d q[100010];
bool ac(d lhs, d rhs) {
  if (lhs.bn == rhs.bn) return lhs.rt < rhs.rt;
  return lhs.bn < rhs.bn;
}
void in(long long x) {
  freq[x]++;
  mxf[freq[x]]++;
  if (freq[x] > mx) {
    mx = freq[x];
  }
  sum[freq[x]] += x;
}
void ex(long long x) {
  mxf[freq[x]]--;
  if (mxf[freq[x]] == 0) mx--;
  sum[freq[x]] -= x;
  freq[x]--;
}
int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) scanf("%lld", &col[i]);
  for (int i = 0; i < n - 1; i++) {
    scanf("%lld%lld", &u, &v);
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1);
  for (int i = 1; i <= n; i++) {
    a[disc[i]] = col[i];
  }
  for (int i = 1; i <= n; i++) {
    q[i].lt = disc[i];
    q[i].rt = fin[i];
    q[i].bn = sqrt(q[i].lt);
    q[i].id = i;
  }
  long long i = 0, j = 0;
  sort(q + 1, q + (n + 1), ac);
  for (int ii = 1; ii <= n; ii++) {
    l = q[ii].lt;
    r = q[ii].rt;
    while (j < r) {
      j++;
      in(a[j]);
    }
    while (j > r) {
      ex(a[j]);
      j--;
    }
    while (i < l) {
      ex(a[i]);
      i++;
    }
    while (i > l) {
      i--;
      in(a[i]);
    }
    res[q[ii].id] = sum[mx];
  }
  for (int i = 1; i <= n; i++) {
    if (i > 1) printf(" ");
    printf("%lld", res[i]);
  }
  printf("\n");
  return 0;
}
