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.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.TreeMap;
import java.util.StringTokenizer;
import java.util.Map;
import java.io.IOException;
import java.io.BufferedReader;
import java.util.LinkedList;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author xwchen
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskE solver = new TaskE();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskE {
        Graph graph;
        TreeMap<Integer, Integer>[] occurs;
        int[] c;
        long[] mostOccurs;
        long[] res;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            c = in.nextIntArray1(n);
            occurs = new TreeMap[n + 1];
            mostOccurs = new long[n + 1];
            res = new long[n + 1];
            graph = new Graph(n);
            for (int i = 1; i < n; ++i) {
                int u = in.nextInt();
                int v = in.nextInt();
                graph.addEdge(u, v);
            }
            search(1, 1);
            for (int i = 1; i <= n; ++i) {
                out.print(res[i] + " ");
            }
        }

        void search(int cur, int pre) {
            occurs[cur] = new TreeMap<>();
            occurs[cur].put(c[cur], 1);
            mostOccurs[cur] = 1;
            res[cur] = c[cur];
            for (Graph.Edge e : graph.adj[cur]) {
                int v = e.v;
                if (v != pre) {
                    search(v, cur);
                    if (occurs[cur].size() < occurs[v].size()) {
                        TreeMap<Integer, Integer> temp = occurs[cur];
                        occurs[cur] = occurs[v];
                        occurs[v] = temp;
                        res[cur] = res[v];
                        mostOccurs[cur] = mostOccurs[v];
                    }
                    for (int color : occurs[v].keySet()) {
                        int count = occurs[v].get(color);
                        occurs[cur].put(color, occurs[cur].getOrDefault(color, 0) + count);
                        if (occurs[cur].get(color) > mostOccurs[cur]) {
                            mostOccurs[cur] = occurs[cur].get(color);
                            res[cur] = 0;
                        }
                        if (occurs[cur].get(color) == mostOccurs[cur]) {
                            res[cur] += color;
                        }
                    }
                }
            }
        }

    }

    static class Graph {
        public LinkedList<Edge>[] adj;
        int n;

        public Graph() {

        }

        public Graph(int n) {
            this.n = n;
            adj = new LinkedList[n + 1];
            for (int i = 0; i <= n; ++i) {
                adj[i] = new LinkedList<>();
            }
        }

        public void addEdge(int u, int v) {

            adj[u].add(new Edge(v, 1));
            adj[v].add(new Edge(u, 1));
            //System.out.println("add edge "+u+" "+v);
        }

        public class Edge {
            public int u;
            public int v;
            public int value;

            public Edge(int u, int v, int value) {
                this.u = u;
                this.v = v;
                this.value = value;
            }

            public Edge(int v, int value) {
                this.v = v;
                this.value = value;
            }

        }

    }

    static class InputReader {
        private BufferedReader reader;
        private StringTokenizer tokenizer = new StringTokenizer("");

        public InputReader(InputStream inputStream) {
            this.reader = new BufferedReader(
                    new InputStreamReader(inputStream));
        }

        public String next() {
            while (!tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return tokenizer.nextToken();
        }

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

        public int[] nextIntArray1(int n) {
            int[] a = new int[n + 1];
            for (int i = 1; i <= n; i++)
                a[i] = nextInt();
            return a;
        }

    }
}

