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.HashMap;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.Map;
import java.util.Map.Entry;
import java.io.BufferedReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
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 {
        int[] color;
        int[] maxc;
        long[] sum;
        long[] ans;
        ArrayList<Integer>[] edg;
        HashMap<Integer, Integer>[] mp;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            color = new int[n];
            for (int i = 0; i < n; ++i) {
                color[i] = in.nextInt();
            }
            edg = new ArrayList[n];
            for (int i = 0; i < n; ++i) {
                edg[i] = new ArrayList<>();
            }
            for (int i = 1; i < n; ++i) {
                int u = in.nextInt() - 1, v = in.nextInt() - 1;
                edg[u].add(v);
                edg[v].add(u);
            }
            sum = new long[n];
            maxc = new int[n];
            mp = new HashMap[n];
            ans = new long[n];
            go(0, -1);
            for (int i = 0; i < n; ++i) {
                out.print(ans[i] + (i == n - 1 ? "\n" : " "));
            }
        }

        void go(int u, int fa) {
            mp[u] = new HashMap<>();
            for (int i = 0; i < edg[u].size(); ++i) {
                int v = edg[u].get(i);
                if (v == fa) continue;
                go(v, u);
                if (mp[u].size() < mp[v].size()) {
                    merge(u, v);
                    mp[u] = mp[v];
                    sum[u] = sum[v];
                    maxc[u] = maxc[v];
                } else {
                    merge(v, u);
                }
            }
            insert(u, color[u], 1);
            ans[u] = sum[u];
        }

        void merge(int from, int to) {
            for (Map.Entry<Integer, Integer> entry : mp[from].entrySet()) {
                insert(to, entry.getKey(), entry.getValue());
            }
        }

        void insert(int id, int key, int value) {
            int newValue = value;
            if (mp[id].containsKey(key)) {
                newValue += mp[id].get(key);
            }
            mp[id].put(key, newValue);
            if (newValue > maxc[id]) {
                sum[id] = key;
                maxc[id] = newValue;
            } else if (newValue == maxc[id]) {
                sum[id] += key;
            }
        }

    }

    static class InputReader {
        BufferedReader reader;
        StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            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());
        }

    }
}
