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.*;
import java.math.*;
 
public class Ideone {
    public static void main(String[] args) throws IOException {
        PrintWriter out = new PrintWriter(System.out);
        //Scanner sc = new Scanner();
        Reader in = new Reader();
        Ideone solver = new Ideone();
        solver.solve(out, in);
        out.flush();
        out.close();
 
    }
 
 
 
    static int maxn = (int)1e6+5;
    static long mod=(int)1e9+7 ;
    static int n,m;
    static long k,C;
 
    static int INF = (int)1e9;
    static int[] clr,sz,cnt;
    static long[] res;
    static Node[] ans;
    static ArrayList<Integer> adj[],arr[];
 
    void solve(PrintWriter out, Reader in) throws IOException{   
        n = in.nextInt();
        
        res = new long[n+1];
        ans = new Node[n+1];
        cnt = new int[n+1];
        sz = new int[n+1];
        clr = new int[n+1];
        for(int i=1;i<=n;i++) clr[i] = in.nextInt();
        
        arr = new ArrayList[n+1];
        adj = new ArrayList[n+1];
        for(int i=1;i<=n;i++) {
            adj[i] = new ArrayList<Integer>();
        }
        
        
        int u,v;
        for(int i=1;i<n;i++) {
            u = in.nextInt();
            v = in.nextInt();
            adj[u].add(v);
            adj[v].add(u);
        }
        
        size(1,0);
        dfs(1,0,0);
        
        out.print(res[1]);
        for(int i=2;i<=n;i++) out.print(" "+res[i]);
        out.println();
    }
 
    //<>
    
    static void size(int s,int p){
        sz[s] = 1;
        
        for(int e:adj[s]){
            if(e!=p) {
                size(e,s);
                sz[s]+=sz[e];
            }
        }
    }
    
    static void dfs(int s,int p,int keep){
        int big=-1,max=0;
        
        for(int e:adj[s]){
            if(e!=p){
                if(max<sz[e]){
                    max = sz[e];
                    big = e;
                }
            }
        }
        
        for(int e:adj[s]){
            if(e!=p && e!=big){
                dfs(e,s,0);
            }
        }
        
        if(big!=-1){
            dfs(big,s,1);
            arr[s] = arr[big];
            ans[s] = ans[big];
        }else{
            arr[s] = new ArrayList<Integer>();
            ans[s] = new Node(0,0);
        }
        
        arr[s].add(s);
        cnt[clr[s]]++;
        if(s==15){
            //System.out.println(ans[s].max+" "+clr[s]+" "+cnt[clr[s]]+" "+ans[s].res);
        }
        if(ans[s].max==cnt[clr[s]]){
            ans[s].res+=clr[s];
        }else if(ans[s].max<cnt[clr[s]]){
            ans[s].res = clr[s];
            ans[s].max = cnt[clr[s]];
        }
        
        for(int e:adj[s]){
            if(e!=p && e!=big){
                for(int v:arr[e]){
                    arr[s].add(v);
                    cnt[clr[v]]++;
                    if(cnt[clr[v]]==ans[s].max){
                        ans[s].res+=clr[v];
                    }else if(cnt[clr[v]]>ans[s].max){
                        ans[s].max = cnt[clr[v]];
                        ans[s].res = clr[v];
                    }
                }
            }
        }
        
        //System.out.println(s+" "+cnt[1]+" "+cnt[2]+" "+cnt[3]);
        // do something
        res[s] = ans[s].res;
        
        if(keep==0){
            for(int e:arr[s]){
                cnt[clr[e]]--;
            }
        }
    }
    
    static class Node{
        int max;
        long res;
        
        Node(int max,long res){
            this.max = max;
            this.res = res;
        }
    }
    
    static class Reader {

        private InputStream mIs;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
    
        public Reader() {
            this(System.in);
        }
    
        public Reader(InputStream is) {
            mIs = is;
        }
    
        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
    
        }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = mIs.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }
    
        public String nextLine() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }
    
        public String next() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }
    
        double nextDouble()
        {
            return Double.parseDouble(next());
        }
    
        public long nextLong() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }
    
        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }
    
        public boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
    
        public boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

    }
}
