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 CFB {

    BufferedReader br;
    PrintWriter out;
    StringTokenizer st;
    boolean eof;
    private static final long MOD = 1000L * 1000L * 1000L + 7;
    private static final int[] dx = {0, -1, 0, 1};
    private static final int[] dy = {1, 0, -1, 0};
    private static final String yes = "Yes";
    private static final String no = "No";

    int n;
    List<List<Integer>> graph = new ArrayList<>();
    int INDEX = 0;
    int[] color; // color for each vertex
    int[] sz; // number of vertex of subtree i
    int[] cnt; // number of color i in subtree

    int[] startTime;
    int[] endTime;
    int[] reverse; // reverse index of startTime
    TreeMap<Integer, Long> treeMap = new TreeMap<>();
    long[] res;
    void solve() throws IOException {
        n = nextInt();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }

        color = nextIntArr(n);
        reverse = new int[n];
        sz = new int[n];
        cnt = new int[n + 10];
        for (int i = 0; i < n - 1; i++) {
            int u = nextInt() - 1;
            int v = nextInt() - 1;
            graph.get(u).add(v);
            graph.get(v).add(u);
        }

        startTime = new int[n];
        endTime = new int[n];
        res = new long[n];
        dfs(-1, 0, startTime, endTime);
        solve(0, -1, true);

        for (int i = 0; i < n; i++) {
            out(res[i] + " ");
        }
    }

    void solve(int cur, int prev, boolean isBigChild){
        int mx = -1, bigChild = -1;
        for(int nxt : graph.get(cur)) {
            if (nxt != prev && sz[nxt] > mx) {
                mx = sz[nxt];
                bigChild = nxt;
            }
        }
        for(int nxt : graph.get(cur)) {
            if (nxt != prev && nxt != bigChild) {
                solve(nxt, cur, false);  // run a dfs on small childs and clear them from cnt
            }
        }

        if(bigChild != -1) {
            solve(bigChild, cur, true);  // bigChild marked as big and not cleared from cnt
        }

        for(int nxt : graph.get(cur)) {
            if (nxt != prev && nxt != bigChild) {
                for (int i = startTime[nxt]; i <= endTime[nxt]; i++) {
                    incrementColor(color[reverse[i]]);
                }
            }
        }

        incrementColor(color[cur]);
        // now cnt[c] is the number of vertices in subtree of vertex v that has color c.
        // You can answer the queries easily.

        res[cur] = treeMap.lastEntry().getValue();
        if(!isBigChild) {
            for (int i = startTime[cur]; i <= endTime[cur]; i++) {
                decrementColor(color[reverse[i]]);
            }
        }
    }

    void incrementColor(int c) {
        long sum = treeMap.getOrDefault(cnt[c], 0L);
        if (sum == c) {
            treeMap.remove(cnt[c]);
        }
        else {
            treeMap.put(cnt[c], sum - c);
        }

        sum = treeMap.getOrDefault(cnt[c] + 1, 0L);
        treeMap.put(cnt[c] + 1, sum + c);
        cnt[c]++;
    }

    void decrementColor(int c) {
        long sum = treeMap.getOrDefault(cnt[c], 0L);
        if (sum == c) {
            treeMap.remove(cnt[c]);
        }
        else {
            treeMap.put(cnt[c], sum - c);
        }

        sum = treeMap.getOrDefault(cnt[c] - 1, 0L);
        treeMap.put(cnt[c] - 1, sum + c);
        cnt[c]--;
    }

    // Euler's tour:
    void dfs(int parent, int nodeIndex, int[] startIndex, int[] endIndex) {
        startIndex[nodeIndex] = INDEX;
        reverse[INDEX] = nodeIndex;
        INDEX++;
        sz[nodeIndex]++;
        for (int nxt : graph.get(nodeIndex)) {
            if (nxt != parent) {
                dfs(nodeIndex, nxt, startIndex, endIndex);
                sz[nodeIndex] += sz[nxt];
            }
        }

        endIndex[nodeIndex] = INDEX - 1;
    }

    void shuffle(int[] a) {
        int n = a.length;
        for(int i = 0; i < n; i++) {
            int r = i + (int) (Math.random() * (n - i));
            int tmp = a[i];
            a[i] = a[r];
            a[r] = tmp;
        }
    }
    long gcd(long a, long b) {
        while(a != 0 && b != 0) {
            long c = b;
            b = a % b;
            a = c;
        }
        return a + b;
    }
    private void outln(Object o) {
        out.println(o);
    }
    private void out(Object o) {
        out.print(o);
    }
    private void formatPrint(double val) {
        outln(String.format("%.9f%n", val));
    }
    public CFB() throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        solve();
        out.close();
    }
    public static void main(String[] args) throws IOException {
        new CFB();
    }

    public long[] nextLongArr(int n) throws IOException{
        long[] res = new long[n];
        for(int i = 0; i < n; i++)
            res[i] = nextLong();
        return res;
    }
    public int[] nextIntArr(int n) throws IOException {
        int[] res = new int[n];
        for(int i = 0; i < n; i++)
            res[i] = nextInt();
        return res;
    }
    public String nextToken() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                eof = true;
                return null;
            }
        }
        return st.nextToken();
    }
    public String nextString() {
        try {
            return br.readLine();
        } catch (IOException e) {
            eof = true;
            return null;
        }
    }
    public int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }
    public long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }
    public double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }
}
