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

public class Main {

    static int cnt[];
    static boolean big[];
    static int max = -1;
    static Map<Integer, Long> maxa = new HashMap<>();

    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        int n = in.nextInt();
        cnt = new int[n + 1];
        big = new boolean[n + 1];
        Vertex[] vertices = new Vertex[n + 1];
        for (int i = 1; i <= n; i++) {
            vertices[i] = new Vertex(i);
        }
        int[] c = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            c[i] = in.nextInt();
        }
        int m = 1;
        while (m < n) {
            int x = in.nextInt();
            int y = in.nextInt();
            vertices[x].adj.add(vertices[y]);
            vertices[y].adj.add(vertices[x]);
            m++;
        }
        int[] sz = new int[n + 1];
        getSizeDepth(1, -1, vertices, sz);
        long[] answer = new long[n + 1];
        dfs(1, vertices, -1, true, sz, c, answer);
        StringBuilder sb = new StringBuilder();
        sb.append(answer[1]);
        for (int k = 2; k <= n; k++) {
            sb.append(" ");
            sb.append(answer[k]);
        }
        out.println(sb.toString());
        out.close();
    }

    static void getSizeDepth(int v, int p, Vertex[] vertices, int[] sz) {
        sz[v] = 1;
        for (Vertex u : vertices[v].adj) {
            if (u.i != p) {
                getSizeDepth(u.i, v, vertices, sz);
                sz[v] += sz[u.i];
            }
        }
    }

    static void add(int v, Vertex[] vertices, int p, int x, int[] c, int val) {
        cnt[c[v]] += val;
        if (cnt[c[v]] > max) {
            max = cnt[c[v]];
            maxa.put(cnt[c[v]], (long) c[v]);
        } else if (cnt[c[v]] == max) {
            maxa.put(cnt[c[v]], maxa.get(max) + c[v]);
        }
        for (Vertex u : vertices[v].adj)
            if (u.i != p && !big[u.i])
                add(u.i, vertices, v, x, c, val);
    }

    static void dfs(int v,
                    Vertex[] vertices,
                    int p,
                    boolean keep,
                    int[] sz,
                    int[] c,
                    long[] answer) {
        int mx = -1;
        int bigChild = -1;
        for (Vertex u : vertices[v].adj) {
            if (u.i != p && sz[u.i] > mx) {
                mx = sz[u.i];
                bigChild = u.i;
            }
        }

        for (Vertex u : vertices[v].adj) {
            if (u.i != p && u.i != bigChild)
                dfs(u.i, vertices, v, false, sz, c, answer);
        }

        if (bigChild != -1) {
            dfs(bigChild, vertices, v, true, sz, c, answer);
            big[bigChild] = true;
        }
        add(v, vertices, p, 1, c, 1);
        // answer
        answer[v] = maxa.get(max);

        if (bigChild != -1)
            big[bigChild] = false;
        if (!keep) {
            add(v, vertices, p, -1, c, -1);
            maxa.clear();
            max = -1;
        }
    }

    static class Pair {
        int x;
        int y;

        public Pair(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Pair pair = (Pair) o;

            if (x != pair.x) return false;
            return y == pair.y;
        }

        @Override
        public int hashCode() {
            int result = x;
            result = 31 * result + y;
            return result;
        }
    }


    static class Vertex {
        int i;
        List<Vertex> adj = new ArrayList<>();

        public Vertex(int i) {
            this.i = i;
        }
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

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

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

        public double nextDouble() {
            return Double.parseDouble(next());
        }

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

    }
}