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;
set<string> s1[500005];
long long ans[500005];
long long in[500005];
long long out[500005];
long long euler[500005];
long long col[500005];
long long a[500005] = {0};
long long m[500005];
pair<long long, long long> store[500005];
vector<pair<long long, long long>> query[500005];
vector<pair<string, long long>> s2;
vector<long long> v1[500005];
void dfs(int node, int par, bool keep) {
  int mx = -1, big = -1, mx1 = 0;
  for (auto it : v1[node])
    if (it != par) {
      int sizee = out[it] - in[it] + 1;
      if (sizee > mx) mx = sizee, big = it;
    }
  for (auto it : v1[node])
    if (it != par && it != big) dfs(it, node, 0);
  if (big != -1) {
    dfs(big, node, 1);
    ans[node] = store[big].first;
    mx1 = store[big].second;
  }
  m[col[node]]++;
  if (m[col[node]] > mx1) {
    ans[node] = col[node];
    mx1 = m[col[node]];
  } else if (m[col[node]] == mx1)
    ans[node] += col[node];
  for (auto it : v1[node]) {
    if (it != par && it != big) {
      for (long long i = in[it]; i < out[it] + 1; i++) {
        int u = euler[i];
        m[col[u]]++;
        if (m[col[u]] > mx1) {
          ans[node] = col[u];
          mx1 = m[col[u]];
        } else if (m[col[u]] == mx1)
          ans[node] += col[u];
      }
    }
  }
  if (!keep) {
    for (long long i = in[node]; i < out[node] + 1; i++) {
      int u = euler[i];
      m[col[u]]--;
    }
  } else {
    store[node].first = ans[node];
    store[node].second = mx1;
  }
}
long long tim = 1;
void dfs2(long long node, long long par) {
  in[node] = tim;
  euler[tim] = node;
  for (auto it : v1[node])
    if (it != par) {
      tim++;
      dfs2(it, node);
    }
  out[node] = tim;
}
int main() {
  long long n;
  scanf("%lld", &n);
  long long par1[n + 1];
  for (long long i = 1; i < n + 1; i++) cin >> col[i];
  for (long long i = 1; i < n; i++) {
    long long u, v;
    cin >> u >> v;
    v1[u].push_back(v);
    v1[v].push_back(u);
  }
  dfs2(1, 1);
  dfs(1, 1, 0);
  for (long long i = 1; i < n + 1; i++)
    if (ans[i] != 0) cout << ans[i] << endl;
}
