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
import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        FastScanner in = new FastScanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        new Main().run(in, out);
        out.close();
    }

    // dsu on tree

    int[] size;
    int[] heavy;
    List<Integer>[] adj;
    int n;
    int[] count;
    int[] col;
    List<Integer>[] removeLists;
    long[] ret;

    int maxColorCount = 0;

    void run(FastScanner in, PrintWriter out) {

        n = in.nextInt();
        adj = new List[n];
        size = new int[n];
        count = new int[n+1];
        heavy = new int[n];
        col = new int[n];
        ret = new long[n];
        removeLists = new List[n];

        for (int i = 0; i < n; i++) adj[i] = new ArrayList<>();
        for (int i = 0; i < n; i++) removeLists[i] = new ArrayList<>();
        for (int i = 0; i < n; i++) col[i] = in.nextInt();

        for (int i = 0; i < n-1; i++) {
            int u = in.nextInt()-1;
            int v = in.nextInt()-1;
            adj[u].add(v);
            adj[v].add(u);
        }

        size(0, -1);
        dfs(0, -1, true);
        for (int i = 0; i < n; i++) {
            System.out.print(ret[i] + " ");
        }
    }

    void size(int u, int parent) {
        int maxSize = -1;
        int heavyChild = -1;

        size[u] = 1;
        for (int next : adj[u]) {
            if (next == parent) continue;
            size(next, u);
            if (size[next] > maxSize) {
                maxSize = size[next];
                heavyChild = next;
            }
            size[u] += size[next];
        }
        heavy[u] = heavyChild;
    }

    void dfs(int u, int p, boolean amIHeavy) {
        for (int next : adj[u]) {
            if (next == p || heavy[u] == next) continue;
            dfs(next, u, false);
        }

        if (heavy[u] != -1) {
            dfs(heavy[u], u, true);
            removeLists[u] = removeLists[heavy[u]];
            ret[u] = ret[heavy[u]];
        }

        for (int next : adj[u]) {
            if (next == p || heavy[u] == next) continue;
            for (int x : removeLists[next]) {
                count[col[x]]++;
                if (count[col[x]] > maxColorCount) {
                    maxColorCount = count[col[x]];
                    ret[u] = col[x];
                } else if (count[col[x]] == maxColorCount) {
                    ret[u] += col[x];
                }
                removeLists[u].add(x);
            }
        }

        count[col[u]]++;
        if (count[col[u]] > maxColorCount) {
            maxColorCount = count[col[u]];
            ret[u] = col[u];
        } else if (count[col[u]] == maxColorCount) {
            ret[u] += col[u];
        }
        removeLists[u].add(u);

        if (!amIHeavy) {
            for (int x : removeLists[u]) count[col[x]]--;
            maxColorCount = 0;
        }
    }

    static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(InputStream in) {
            br = new BufferedReader(new InputStreamReader(in));
            st = null;
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }
    }
}
