import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.util.HashMap;
import java.util.Map;

public class CF199B {

    private static class Pair {
        int l;
        int r;

        public Pair(final int le, final int ri) {
            l = le;
            r = ri;
        }
    }

    public static void main(final String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] parts = br.readLine().split("\\s+");

        int n = Integer.parseInt(parts[0]);
        int m = Integer.parseInt(parts[1]);
        int s = Integer.parseInt(parts[2]);
        int f = Integer.parseInt(parts[3]);

        int mark = s;
        int dir = f - s > 0 ? 1 : 0;

        Map<Integer, Pair> map = new HashMap<Integer, Pair>();

        StringBuilder sb = new StringBuilder();
        int step = 0;
        while (mark != f) {
            step++;

            if (m-- > 0) {
                parts = br.readLine().split("\\s+");

                int t = Integer.parseInt(parts[0]);
                int l = Integer.parseInt(parts[1]);
                int r = Integer.parseInt(parts[2]);

                map.put(t, new Pair(l, r));
            }

            Pair p = map.get(step);
            if (p != null) {
                if (inRange(mark, p.l, p.r) || inRange(mark + dir, p.l, p.r)) {
                    sb.append("X");
                } else if (dir < 0) {
                    sb.append("L");
                    mark += dir;
                } else {
                    sb.append("R");
                    mark += dir;
                }
            } else {
                if (dir < 0) {
                    sb.append("L");
                    mark += dir;
                } else {
                    sb.append("R");
                    mark += dir;
                }
            }

        }

        System.out.println(sb.toString());
    }

    private static boolean inRange(final int mark, final int l, final int r) {
        return mark >= l && mark <= r;
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.util.HashMap;
import java.util.Map;

public class CF199B {

    private static class Pair {
        int l;
        int r;

        public Pair(final int le, final int ri) {
            l = le;
            r = ri;
        }
    }

    public static void main(final String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] parts = br.readLine().split("\\s+");

        int n = Integer.parseInt(parts[0]);
        int m = Integer.parseInt(parts[1]);
        int s = Integer.parseInt(parts[2]);
        int f = Integer.parseInt(parts[3]);

        int mark = s;
        int dir = f - s > 0 ? 1 : 0;

        Map<Integer, Pair> map = new HashMap<Integer, Pair>();

        StringBuilder sb = new StringBuilder();
        int step = 0;
        while (mark != f) {
            step++;

            parts = br.readLine().split("\\s+");

            if (m-- > 0) {
                int t = Integer.parseInt(parts[0]);
                int l = Integer.parseInt(parts[1]);
                int r = Integer.parseInt(parts[2]);

                map.put(t, new Pair(l, r));
            }

            Pair p = map.get(step);
            if (p != null) {
                if (inRange(mark, p.l, p.l) || inRange(mark + dir, p.l, p.l)) {
                    sb.append("X");
                } else if (dir < 0) {
                    sb.append("L");
                    mark += dir;
                } else {
                    sb.append("R");
                    mark += dir;
                }
            } else {
                if (dir < 0) {
                    sb.append("L");
                    mark += dir;
                } else {
                    sb.append("R");
                    mark += dir;
                }
            }

        }

        System.out.println(sb.toString());
    }

    private static boolean inRange(final int mark, final int l, final int r) {
        return mark >= l && mark <= r;
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.util.HashMap;
import java.util.Map;

public class CF199B {

    private static class Pair {
        int l;
        int r;

        public Pair(final int le, final int ri) {
            l = le;
            r = ri;
        }
    }

    public static void main(final String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] parts = br.readLine().split("\\s+");

        int n = Integer.parseInt(parts[0]);
        int m = Integer.parseInt(parts[1]);
        int s = Integer.parseInt(parts[2]);
        int f = Integer.parseInt(parts[3]);

        int dir = f - s > 0 ? 1 : -1;

        Map<Integer, Pair> map = new HashMap<Integer, Pair>();

        while (m-- > 0) {
            parts = br.readLine().split("\\s+");

            int t = Integer.parseInt(parts[0]);
            int l = Integer.parseInt(parts[1]);
            int r = Integer.parseInt(parts[2]);

            map.put(t, new Pair(l, r));
        }

        StringBuilder sb = new StringBuilder();
        StringBuilder sb1 = new StringBuilder();
        int step = 0;
        while (s != f) {
            step++;

            Pair p = map.get(step);
            if (p != null) {
                if (inRange(s, p.l, p.r) || inRange(s + dir, p.l, p.r)) {
                    sb.append("X");

                } else if (dir < 0) {
                    sb.append("L");
                    s += dir;
                } else {
                    sb.append("R");
                    s += dir;
                }
            } else {
                if (dir < 0) {
                    sb.append("L");
                    s += dir;
                } else {
                    sb.append("R");
                    s += dir;
                }
            }

        }

        System.out.println(sb.toString());
    }

    private static boolean inRange(final int mark, final int l, final int r) {
        return mark >= l && mark <= r;
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.util.HashMap;
import java.util.Map;

public class CF199B {

    private static class Pair {
        int l;
        int r;

        public Pair(final int le, final int ri) {
            l = le;
            r = ri;
        }
    }

    public static void main(final String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] parts = br.readLine().split("\\s+");

        int n = Integer.parseInt(parts[0]);
        int m = Integer.parseInt(parts[1]);
        int s = Integer.parseInt(parts[2]);
        int f = Integer.parseInt(parts[3]);

        int mark = s;
        int dir = f - s > 0 ? 1 : 0;

        Map<Integer, Pair> map = new HashMap<Integer, Pair>();

        while (m-- > 0) {
            parts = br.readLine().split("\\s+");

            int t = Integer.parseInt(parts[0]);
            int l = Integer.parseInt(parts[1]);
            int r = Integer.parseInt(parts[2]);

            map.put(t, new Pair(l, r));
        }

        StringBuilder sb = new StringBuilder();
        int step = 0;
        while (mark != f) {
            step++;

            Pair p = map.get(step);
            if (p != null) {
                if (inRange(mark, p.l, p.r) || inRange(mark + dir, p.l, p.r)) {
                    sb.append("X");
                } else if (dir < 0) {
                    sb.append("L");
                    mark += dir;
                } else {
                    sb.append("R");
                    mark += dir;
                }
            } else {
                if (dir < 0) {
                    sb.append("L");
                    mark += dir;
                } else {
                    sb.append("R");
                    mark += dir;
                }
            }

        }

        System.out.println(sb.toString());
    }

    private static boolean inRange(final int mark, final int l, final int r) {
        return mark >= l && mark <= r;
    }

}


import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

/*
br = new BufferedReader(new FileReader("input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
 */

public class Main {
  private static BufferedReader br;
  private static StringTokenizer st;
  private static PrintWriter pw;

  public static void main(String[] args) throws IOException {
    br = new BufferedReader(new InputStreamReader(System.in));
    pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    //int qq = 1;
    int qq = Integer.MAX_VALUE;
    //int qq = readInt();
    for(int casenum = 1; casenum <= qq; casenum++)  {
      int n = readInt();
      int m = readInt();
      int s = readInt();
      int f = readInt();
      int[] time = new int[m];
      int[] l = new int[m];
      int[] r = new int[m];
      for(int i = 0; i < m; i++) {
        time[i] = readInt();
        l[i] = readInt();
        r[i] = readInt();
      }
      int timeIndex = 0;
      for(int qqq = 1; s != f; qqq++) {
        int next = s<f ? s+1 : s-1;
        if(timeIndex < time.length && time[timeIndex] != qqq) {
          if(s<f) {
            pw.print('R');
          }
          else {
            pw.print('L');
          }
          s = next;
        }
        else {
          boolean can = true;
          if(s >= l[timeIndex] && s <= r[timeIndex]){
            can = false;
          }
          if(next >= l[timeIndex] && next <= r[timeIndex]){
            can = false;
          }
          if(can) {
            if(s<f) {
              pw.print('R');
            }
            else {
              pw.print('L');
            }
            s = next;
          }
          else {
            pw.print('X');
          }
          timeIndex++;
        }
      }
    }
    pw.close();
  }

  public static int get(int s) {
    String ret = Integer.toString(s);
    while(ret.length() < 3) {
      ret = 0 + ret;
    }
    int mask = 0;
    for(int i = 0; i < 3; i++) {
      if(ret.charAt(i) != '0') {
        mask |= 1 << i;
      }
    }
    return mask;
  }

  private static void exitImmediately() {
    pw.close();
    System.exit(0);
  }

  private static long readLong() throws IOException {
    return Long.parseLong(nextToken());
  }

  private static double readDouble() throws IOException {
    return Double.parseDouble(nextToken());
  }

  private static int readInt() throws IOException {
    return Integer.parseInt(nextToken());
  }

  private static String nextToken() throws IOException  {
    while(st == null || !st.hasMoreTokens())  {
      if(!br.ready()) {
        exitImmediately();
      }
      st = new StringTokenizer(br.readLine().trim());
    }
    return st.nextToken();
  }
}


import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

/*
br = new BufferedReader(new FileReader("input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
 */

public class Main {
  private static BufferedReader br;
  private static StringTokenizer st;
  private static PrintWriter pw;

  public static void main(String[] args) throws IOException {
    br = new BufferedReader(new InputStreamReader(System.in));
    pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    //int qq = 1;
    int qq = Integer.MAX_VALUE;
    //int qq = readInt();
    for(int casenum = 1; casenum <= qq; casenum++)  {
      int n = readInt();
      int m = readInt();
      int s = readInt();
      int f = readInt();
      int[] time = new int[m];
      int[] l = new int[m];
      int[] r = new int[m];
      for(int i = 0; i < m; i++) {
        time[i] = readInt();
        l[i] = readInt();
        r[i] = readInt();
      }
      int timeIndex = 0;
      for(int qqq = 1; s != f; qqq++) {
        int next = s<f ? s+1 : s-1;
        if(timeIndex == time.length || time[timeIndex] != qqq) {
          if(s<f) {
            pw.print('R');
          }
          else {
            pw.print('L');
          }
          s = next;
        }
        else {
          boolean can = true;
          if(s >= l[timeIndex] && s <= r[timeIndex]){
            can = false;
          }
          if(next >= l[timeIndex] && next <= r[timeIndex]){
            can = false;
          }
          if(can) {
            if(s<f) {
              pw.print('R');
            }
            else {
              pw.print('L');
            }
            s = next;
          }
          else {
            pw.print('X');
          }
          timeIndex++;
        }
      }
    }
    pw.close();
  }

  public static int get(int s) {
    String ret = Integer.toString(s);
    while(ret.length() < 3) {
      ret = 0 + ret;
    }
    int mask = 0;
    for(int i = 0; i < 3; i++) {
      if(ret.charAt(i) != '0') {
        mask |= 1 << i;
      }
    }
    return mask;
  }

  private static void exitImmediately() {
    pw.close();
    System.exit(0);
  }

  private static long readLong() throws IOException {
    return Long.parseLong(nextToken());
  }

  private static double readDouble() throws IOException {
    return Double.parseDouble(nextToken());
  }

  private static int readInt() throws IOException {
    return Integer.parseInt(nextToken());
  }

  private static String nextToken() throws IOException  {
    while(st == null || !st.hasMoreTokens())  {
      if(!br.ready()) {
        exitImmediately();
      }
      st = new StringTokenizer(br.readLine().trim());
    }
    return st.nextToken();
  }
}


import java.util.*;
import java.io.*;

public class Main{

    BufferedReader in;
    StringTokenizer str = null;
    PrintWriter out;

    private String next() throws Exception{
    	if (str == null || !str.hasMoreElements())
    	    str = new StringTokenizer(in.readLine());
    	return str.nextToken();
    }

    private int nextInt() throws Exception{
	   return Integer.parseInt(next());
    }

    public void run() throws Exception{
    	in = new BufferedReader(new InputStreamReader(System.in));
    	out = new PrintWriter(System.out);

        int n = nextInt(), m = nextInt();
        int s = nextInt(), f = nextInt();
        StringBuilder ret = new StringBuilder();

        int t[] = new int[m];
        int left[] = new int[m];
        int right[] = new int[m];
        for(int i=0;i<m;i++){
            t[i] = nextInt();
            left[i] = nextInt();
            right[i] = nextInt();
        }

        for(int i=0;i<m;i++) {
            int l = left[i];
            int r = right[i];
            int diff = 1;
            if (i > 0) {
                diff = t[i] - t[i-1];
            }

            for(int j=0;j<diff-1;j++){
                if (s < f) {
                    ret.append("R");
                    s++;
                }else if (s > f) {
                    ret.append("L");
                    s--;
                }else{
                    out.println(ret.toString());
                    out.close();
                    return;
                }
            }

            if (s == f) {
                out.println(ret.toString());
                out.close();
                return;
            }

            if (l <= s && s <= r) {
                ret.append("X");
            }else {
                if (s < f) {
                    if (l <= s+1 && s+1 <= r) {
                        ret.append("X");
                    }else{
                        ret.append("R");
                        s++;
                    }
                }else{
                    if (l <= s-1 && s-1 <= r) {
                        ret.append("X");
                    }else{
                        ret.append("L");
                        s--;
                    }
                }
            }

            if (s == f) {
                out.println(ret.toString());
                out.close();
                return;
            }
        }

        out.close();
    }

    public static void main(String args[]) throws Exception{
	   new Main().run();
    }
}


import java.util.*;
import java.io.*;

public class Main{

    BufferedReader in;
    StringTokenizer str = null;
    PrintWriter out;

    private String next() throws Exception{
    	if (str == null || !str.hasMoreElements())
    	    str = new StringTokenizer(in.readLine());
    	return str.nextToken();
    }

    private int nextInt() throws Exception{
	   return Integer.parseInt(next());
    }

    public void run() throws Exception{
    	in = new BufferedReader(new InputStreamReader(System.in));
    	out = new PrintWriter(System.out);

        int n = nextInt(), m = nextInt();
        int s = nextInt(), f = nextInt();
        StringBuilder ret = new StringBuilder();

        int t[] = new int[m];
        int left[] = new int[m];
        int right[] = new int[m];
        for(int i=0;i<m;i++){
            t[i] = nextInt();
            left[i] = nextInt();
            right[i] = nextInt();
        }

        for(int i=0;i<m;i++) {
            int l = left[i];
            int r = right[i];
            int diff = 1;
            if (i > 0) {
                diff = t[i] - t[i-1];
            }

            for(int j=0;j<diff-1;j++){
                if (s < f) {
                    ret.append("R");
                    s++;
                }else if (s > f) {
                    ret.append("L");
                    s--;
                }else{
                    out.println(ret.toString());
                    out.close();
                    return;
                }
            }

            if (l <= s && s <= r) {
                ret.append("X");
            }else {
                if (s < f) {
                    if (l <= s+1 && s+1 <= r) {
                        ret.append("X");
                    }else{
                        ret.append("R");
                        s++;
                    }
                }else{
                    if (l <= s-1 && s-1 <= r) {
                        ret.append("X");
                    }else{
                        ret.append("L");
                        s--;
                    }
                }
            }

            if (s == f) {
                out.println(ret.toString());
                out.close();
                return;
            }
        }

        out.close();
    }

    public static void main(String args[]) throws Exception{
	   new Main().run();
    }
}


#include<bits/stdc++.h>
using namespace std;
struct node
{
	int l,r,max;
	long long ans;
}tr[1000001];
struct node1
{
	int x,y,ne;
}a[200001];
int len=0,la[100001];
long long w[100001],t[100001];
long long root[100001];
int n;
void read(int &num)
{
	char ch;
	while(ch=getchar(),ch<'!');
	num=ch-48;
	while(ch=getchar(),ch>'!')
	 num=num*10+ch-48;
}
void add(int x,int y)
{
	len++;
	int k=len;
	a[k].x=x,a[k].y=y;
	a[k].ne=la[x],la[x]=len;
}
int find(int k1,int k2,int l,int r)
{
	if((k1==0)||(k2==0)) return k1+k2;
	if(l==r)
	 {
	 	tr[k1].max=tr[k1].max+tr[k2].max;
	 	return k1;
	 }
	int mid=(l+r)>>1;
	tr[k1].l=find(tr[k1].l,tr[k2].l,l,mid);
	tr[k1].r=find(tr[k1].r,tr[k2].r,mid+1,r);
	int ls=tr[k1].l,rs=tr[k1].r;
	if(tr[ls].max>tr[rs].max)
	 {
	 	tr[k1].max=tr[ls].max;
	 	tr[k1].ans=tr[ls].ans;
	 }
	if(tr[ls].max<tr[rs].max)
	 {
	 	tr[k1].max=tr[rs].max;
	 	tr[k1].ans=tr[rs].ans;
	 }
	if(tr[ls].max==tr[rs].max)
	 {
	 	tr[k1].max=tr[ls].max;
	 	tr[k1].ans=tr[ls].ans+tr[rs].ans;
	 }
	return k1;
}
void add2(int k,int l,int r,int pos)
{
	if(l==r)
	 {
	 	tr[k].max++;
	 	tr[k].ans=l; return ;
	 }
	int mid=(l+r)>>1;
	if(pos<=mid)
	 {
	 	if(tr[k].l==0)
	 	 {
	 	 	len++;
	 	 	tr[k].l=len;
		  }
	 	add2(tr[k].l,l,mid,pos);
	 }
	else
	 {
	 	if(tr[k].r==0)
	 	 {
	 	 	len++;
	 	 	tr[k].r=len;
		  }
		add2(tr[k].r,mid+1,r,pos);
	 }
	int ls=tr[k].l,rs=tr[k].r;
	if(tr[ls].max>tr[rs].max)
	 {
	 	tr[k].max=tr[ls].max;
	 	tr[k].ans=tr[ls].ans;
	 }
	if(tr[ls].max<tr[rs].max)
	 {
	 	tr[k].max=tr[rs].max;
	 	tr[k].ans=tr[rs].ans;
	 }
	if(tr[ls].max==tr[rs].max)
	 {
	 	tr[k].max=tr[ls].max;
	 	tr[k].ans=tr[ls].ans+tr[rs].ans;
	 }
}
void go(int x,int fa)
{
	for(int k=la[x];k;k=a[k].ne)
	 {
	 	int y=a[k].y;
	 	if(y==fa) continue;
	 	go(y,x);
	 	root[x]=find(root[x],root[y],1,n);
	 }
	if(root[x]==0)
	 {
	 	len++;
	 	root[x]=len;
	 }
	add2(root[x],1,n,w[x]);
	t[x]=tr[root[x]].ans;
}
int main()
{
	int x,y;
	read(n);
	for(int i=1;i<=n;i++) scanf("%ld",&w[i]);
	for(int i=1;i<=n-1;i++)
	 {
	 	read(x),read(y);
	 	add(x,y),add(y,x);
	 }
	go(1,0);
	for(int i=1;i<=n;i++)
	 printf("%ld ",t[i]);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
const int N=1e5+1000;
long long read(void)
{
	char ch=getchar();long long sum=0;bool f=0;
	while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
	if(ch=='-'){f=1;ch=getchar();}
	while(ch>='0'&&ch<='9'){sum=sum*10+ch-'0';ch=getchar();}
	if(f)sum=-sum;return sum;
}
int n,m,len;
int cnt[N],res[N],col[N],maxn;
int son[N],size[N],sum;
struct E
{
	int num;
	int next;
}hd[N],edge[N*3];
void addedge(int x,int y){edge[++len]=(E){y,hd[x].next};hd[x].next=len;return;}
void dfs1(int u,int f)
{
	size[u]=1;
	for(int p=hd[u].next,v;p;p=edge[p].next)
	{
		v=edge[p].num;if(v==f)continue;
		dfs1(v,u);size[u]+=size[v];if(size[v]>size[son[u]])son[u]=v;
	}return;
}
void add(int u,int f,int w,int S)
{
	cnt[col[u]]+=w;
	if(cnt[col[u]]>maxn)maxn=cnt[col[u]],sum=col[u];
	else if(cnt[col[u]]==maxn)sum+=col[u];
	for(int p=hd[u].next,v;p;p=edge[p].next)
	{
		v=edge[p].num;if(v==f||v==S)continue;
		add(v,u,w,S);
	}return;
}
void dfs2(int u,int f,int opt)
{
	for(int p=hd[u].next,v;p;p=edge[p].next)
	{
		v=edge[p].num;if(v==son[u]||v==f)continue;
		dfs2(v,u,0);
	}
	if(son[u])dfs2(son[u],u,1);
	add(u,f,1,son[u]);
	res[u]=sum;
	if(!opt)add(u,f,-1,0),sum=0,maxn=0;return;
}
int main()
{
	n=read();
	for(int i=1;i<=n;i++)col[i]=read();
	for(int i=1,x,y;i<=n-1;i++)
		x=read(),y=read(),addedge(x,y),addedge(y,x);
	dfs1(1,0);dfs2(1,0,0);
	for(int i=1;i<=n;i++)printf("%d ",res[i]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=100010;
struct Way{
	int to,next;
} way[N<<1];
int n,color[N],a,b,cnt,head[N];
int size[N],fa[N],son[N];
void dfs1(int cur){
	size[cur]=1;
	for(int i=head[cur];i;i=way[i].next)
		if(way[i].to!=fa[cur]){
			fa[way[i].to]=cur;
			dfs1(way[i].to);
			size[cur]+=size[way[i].to];
			if(size[way[i].to]>size[son[cur]])
				son[cur]=way[i].to;
		}
}
int ans[N],num[N],maxx,sum;//表示当前各个颜色之和
void dfs(int cur,bool is_son,bool keep_ans){
	if(keep_ans){
		for(int i=head[cur];i;i=way[i].next)
			if(way[i].to!=fa[cur]&&way[i].to!=son[cur])
				dfs(way[i].to,0,1);
	}//如果是第一遍遍历则先单独处理轻儿子
	if(son[cur]&&keep_ans) dfs(son[cur],1,1);
	else if(son[cur]&&!keep_ans) dfs(son[cur],1,0);
	for(int i=head[cur];i;i=way[i].next)
		if(way[i].to!=fa[cur]&&way[i].to!=son[cur])
			dfs(way[i].to,0,0);
	num[color[cur]]++;
	if(num[color[cur]]==maxx) sum+=color[cur];
	else if(num[color[cur]]>maxx){
		maxx=num[color[cur]]; sum=color[cur];
	}
	if(keep_ans) ans[cur]=sum;
	if(keep_ans&&!is_son){
		sum=maxx=0;
		memset(num,0,sizeof(num));
	}
}
void add(int u,int v){
	way[++cnt].to=v;
	way[cnt].next=head[u];
	head[u]=cnt;
}
int main(){
//	freopen("data.in","r",stdin);
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&color[i]);
	for(int i=1;i<n;i++){
		scanf("%d %d",&a,&b);
		add(a,b); add(b,a);
	}
	dfs1(1); dfs(1,1,1);
	for(int i=1;i<=n;i++)
		printf("%d ",ans[i]);
	return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
inline int read()
{
	int n=0,f=1,ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		n=n*10+ch-'0';
		ch=getchar();
	}
	return n*f;
}
struct bbian
{
	int u,v,nex;
}a[800004];
int head[200001],tmp;
void addb(int x,int y)
{
	a[++tmp].u=x;
	a[tmp].v=y;
	a[tmp].nex=head[x];
	head[x]=tmp;
}
int son[200001],siz[200001];
void dfs1(int x,int f)
{
	siz[x]=1;
	int maxn=-1;
	for(int i=head[x];i;i=a[i].nex)
	{
		if(a[i].v==f)continue;
		dfs1(a[i].v,x);
		siz[x]+=siz[a[i].v];
		if(siz[a[i].v]>maxn)
		{
			maxn=siz[a[i].v];
			son[x]=a[i].v;
		}
	}
}
int sum,maxn;
int col[200001],qz[200001];
void findans(int x,int f,int tson)
{
	col[qz[x]]++;
	if(col[qz[x]]>maxn)
	{
		maxn=col[qz[x]];
		sum=qz[x];
	}
	else if(col[qz[x]]==maxn)sum+=qz[x];
	for(int i=head[x];i;i=a[i].nex)
	{
		if(a[i].v==f||a[i].v==tson)continue;
		findans(a[i].v,x,tson);
	}
}
void qk(int x,int f)
{
	col[qz[x]]--;
	for(int i=head[x];i;i=a[i].nex)
	{
		if(a[i].v==f)continue;
		qk(a[i].v,x);
	}
}
int ans[200001];
void dfs(int x,int f)
{
	maxn=0;
	sum=0;
	for(int i=head[x];i;i=a[i].nex)
	{
		if(a[i].v==f||a[i].v==son[x])continue;
		dfs(a[i].v,x);
		qk(a[i].v,x);
	}
	if(son[x])dfs(son[x],x);
	findans(x,f,son[x]);
	ans[x]=sum;
}
int main()
{
	int n,x,y;
	n=read();
	for(int i=1;i<=n;i++)qz[i]=read();
	for(int i=1;i<=n-1;i++)
	{
		x=read();
		y=read();
		addb(x,y);
		addb(y,x);
	}
	dfs1(1,0);
	dfs(1,0);
	for(int i=1;i<=n;i++)printf("%d ",ans[i]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int N=100005;

int n,cnt=1,head[N],son[N],siz[N],color[N],c[N],maxx,nowSon;
long long sum=0,ans[N];

struct edge{
	int nxt,to;
} h[N<<1];

inline void add(int f,int t){
	h[++cnt].nxt=head[f];
	h[cnt].to=t;
	head[f]=cnt;
}

void dfs1(int x,int fa){
	siz[x]=1;
	int mx=0;
	for (int i=head[x];i;i=h[i].nxt){
		int t=h[i].to;
		if (t==fa) continue;
		dfs1(t,x);
		siz[x]+=siz[t];
		if (siz[t]>=mx) mx=siz[t],son[x]=t;
	}
}

void solve(int x,int fa,int val){
	color[c[x]]+=val;
	if (color[c[x]]>maxx) maxx=color[c[x]],sum=c[x];
	else if (color[c[x]]==maxx) sum+=(long long)c[x];
	for (int i=head[x];i;i=h[i].nxt){
		int t=h[i].to;
		if (t==fa || t==nowSon) continue;
		solve(t,x,val);
	}

}

void dfs2(int x,int fa,int opt){
	for (int i=head[x];i;i=h[i].nxt){
		int t=h[i].to;
		if (t==fa || t==son[x]) continue;
		dfs2(t,x,0);
	}
	if (son[x]){
		dfs2(son[x],x,1);
		nowSon=son[x];
	}
	solve(x,fa,1);
	nowSon=0;
	ans[x]=sum;
	if (!opt) solve(x,fa,-1),sum=0,maxx=0;
}

int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++){
		scanf("%d",&c[i]);
	}
	int x,y;
	for (int i=1;i<n;i++){
		scanf("%d%d",&x,&y);
		add(x,y);
		add(y,x);
	}
	dfs1(1,0);
	dfs2(1,0,0);
	for (int i=1;i<=n;i++){
		printf("%lld ",ans[i]);
	}
}


#include<bits/stdc++.h>
using namespace std;
struct node
{
	int l,r,max,ans;
}tr[800001];
struct node1
{
	int x,y,ne;
}a[200001];
int len=0,la[100001];
int w[100001],t[100001];
int root[100001],n;
void read(int &num)
{
	char ch;
	while(ch=getchar(),ch<'!');
	num=ch-48;
	while(ch=getchar(),ch>'!')
	 num=num*10+ch-48;
}
void add(int x,int y)
{
	len++;
	int k=len;
	a[k].x=x,a[k].y=y;
	a[k].ne=la[x],la[x]=len;
}
int find(int k1,int k2,int l,int r)
{
	if((k1==0)||(k2==0)) return k1+k2;
	if(l==r)
	 {
	 	tr[k1].max=tr[k1].max+tr[k2].max;
	 	return k1;
	 }
	int mid=(l+r)>>1;
	tr[k1].l=find(tr[k1].l,tr[k2].l,l,mid);
	tr[k1].r=find(tr[k1].r,tr[k2].r,mid+1,r);
	int ls=tr[k1].l,rs=tr[k1].r;
	if(tr[ls].max>tr[rs].max)
	 {
	 	tr[k1].max=tr[ls].max;
	 	tr[k1].ans=tr[ls].ans;
	 }
	if(tr[ls].max<tr[rs].max)
	 {
	 	tr[k1].max=tr[rs].max;
	 	tr[k1].ans=tr[rs].ans;
	 }
	if(tr[ls].max==tr[rs].max)
	 {
	 	tr[k1].max=tr[ls].max;
	 	tr[k1].ans=tr[ls].ans+tr[rs].ans;
	 }
	return k1;
}
void add2(int k,int l,int r,int pos)
{
	if(l==r)
	 {
	 	tr[k].max++;
	 	tr[k].ans=l; return ;
	 }
	int mid=(l+r)>>1;
	if(pos<=mid)
	 {
	 	if(tr[k].l==0)
	 	 {
	 	 	len++;
	 	 	tr[k].l=len;
		  }
	 	add2(tr[k].l,l,mid,pos);
	 }
	else
	 {
	 	if(tr[k].r==0)
	 	 {
	 	 	len++;
	 	 	tr[k].r=len;
		  }
		add2(tr[k].r,mid+1,r,pos);
	 }
	int ls=tr[k].l,rs=tr[k].r;
	if(tr[ls].max>tr[rs].max)
	 {
	 	tr[k].max=tr[ls].max;
	 	tr[k].ans=tr[ls].ans;
	 }
	if(tr[ls].max<tr[rs].max)
	 {
	 	tr[k].max=tr[rs].max;
	 	tr[k].ans=tr[rs].ans;
	 }
	if(tr[ls].max==tr[rs].max)
	 {
	 	tr[k].max=tr[ls].max;
	 	tr[k].ans=tr[ls].ans+tr[rs].ans;
	 }
}
void go(int x,int fa)
{
	for(int k=la[x];k;k=a[k].ne)
	 {
	 	int y=a[k].y;
	 	if(y==fa) continue;
	 	go(y,x);
	 	root[x]=find(root[x],root[y],1,n);
	 }
	if(root[x]==0)
	 {
	 	len++;
	 	root[x]=len;
	 }
	add2(root[x],1,n,w[x]);
	t[x]=tr[root[x]].ans;
}
int main()
{
	int x,y;
	read(n);
	for(int i=1;i<=n;i++) read(w[i]);
	for(int i=1;i<=n-1;i++)
	 {
	 	read(x),read(y);
	 	add(x,y),add(y,x);
	 }
	go(1,0);
	for(int i=1;i<=n;i++)
	 printf("%d ",t[i]);
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+7;
ll sum,Ans[N];
int n,mx,tot[N],col[N],sz[N],son[N];
vector<int> g[N];
void dfs1(int x,int fa){
	sz[x]=1;
	for(auto to:g[x]) if(to!=fa){
		dfs1(to,x);
		sz[x]+=sz[to];
		if(sz[to]>sz[son[x]]) son[x]=to;
	}
}
void clr(int x,int fa){
	tot[col[x]]=0;
	for(auto to:g[x]) if(to!=fa) clr(to,x);
}
void add(int c){
	++tot[c];
	if(tot[c]>mx) mx=tot[c],sum=c;
	else if(tot[c]==mx) sum+=c;
}
void toadd(int x,int fa){
	add(col[x]);
	for(auto to:g[x]) if(to!=fa) toadd(to,x);
}
void dfs2(int x,int fa){
	for(auto to:g[x]) if(to!=fa&&to!=son[x]) dfs2(to,x),sum=0,mx=0,clr(to,x);
	if(son[x]) dfs2(son[x],x); add(col[x]);
	for(auto to:g[x]) if(to!=fa&&to!=son[x]) toadd(to,x);
	Ans[x]=sum;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;++i) scanf("%d",&col[i]);
	for(int i=1,a,b;i<n;++i) scanf("%d%d",&a,&b),g[a].push_back(b),g[b].push_back(a);
	dfs1(1,0); dfs2(1,0);
	for(int i=1;i<=n;++i) printf("%lld ",Ans[i]);
	return 0;
}


/*cinput
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
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define gc() (p1 == p2 ? (p2 = buf + fread(p1 = buf, 1, 1 << 20, stdin), p1 == p2 ? EOF : *p1++) : *p1++)
#define read() ({ register int x = 0, f = 1; register char c = gc(); while(c < '0' || c > '9') { if (c == '-') f = -1; c = gc();} while(c >= '0' && c <= '9') x = x * 10 + (c & 15), c = gc(); f * x; })
char buf[1 << 20], *p1, *p2;
const int maxn = 1e5+10;
struct Node {
	int v,next;
}e[maxn<<1];
int bala;
int head[maxn],tot;
int sum,Max,ans[maxn];
int siz[maxn],son[maxn],fa[maxn];
int col[maxn],cnt[maxn];
inline void Add(int x,int y){
	e[++tot].v = y;
	e[tot].next = head[x];
	head[x] = tot;
}
inline void dfs1(int x,int f){
	siz[x] = 1;
	for(int i = head[x];i;i = e[i].next){
		int v = e[i].v;
		if(v == f)continue;
		dfs1(v,x);
		siz[x] += siz[v];
		if(!son[x] || siz[v] > siz[son[x]])son[x] = v;
	}
}
inline void modify(int x,int f,int val){
	cnt[col[x]]+=val;
	if(cnt[col[x]] > Max)sum = col[x],Max = cnt[col[x]];
	else if(cnt[col[x]] == Max)sum += col[x];
	for(int i = head[x];i;i = e[i].next){
		int v = e[i].v;
		if(v == f || v == bala)continue;
		modify(v,x,val);
	}
}
inline void dfs2(int x,int f,int opt){
	for(int i = head[x];i;i = e[i].next){
		int v = e[i].v;
		if(v == f)continue;
		if(v != son[x])dfs2(v,x,0);
	}
	if(son[x])dfs2(son[x],x,1),bala = son[x];
	modify(x,f,1);
	bala = 0;
	ans[x] = sum;
	if(!opt)modify(x,f,-1),sum = 0,Max = 0;
}
signed main(){
	int n = read();
	for(int i = 1;i <= n;++i)col[i] = read();
	for(int i = 1;i < n;++i){
		int x = read(),y = read();
		Add(x,y);Add(y,x);
	}
	dfs1(1,0);
	dfs2(1,0,0);
	for(int i = 1;i <= n;++i){
		printf("%lld ",ans[i]);
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <map>
using namespace std;

const int maxN = 100005;
int n;
int c[maxN];
vector <int> son[maxN];
int MAX[maxN];
int res[maxN];
map <int, int> cnt[maxN];
void dfs(int u, int p){
    MAX[u] = -1;
    cnt[u][c[u]]++;
    for(int i = 0; i < son[u].size(); i++){
        int v = son[u][i];
        if(v == p) continue;
        dfs(v, u);
    }
    for(int i = 1; i <= n; i++){
        cnt[p][i] += cnt[u][i];
        if(cnt[u][i] > MAX[u]){
            MAX[u] = cnt[u][i];
            res[u] = i;
        }
        else if(cnt[u][i] == MAX[u]){
            res[u] += i;
        }
//        cout << u << " " << res[u] << endl;
    }
}

int main(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> c[i];
    }
    for(int i = 1; i < n; i++){
        int u, v;
        cin >> u >> v;
        son[u].push_back(v);
    }
    dfs(1, 0);
    for(int i = 1; i <= n; i++){
        cout << res[i] << " ";
    }
    cout << endl;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=2e5+10;
struct tree
{
    int l,r,max,ans;
}tr[maxn<<4];

int rt[maxn],cl[maxn],cnt,n,anss[maxn];
vector<int> g[maxn];

void push_up(int k)
{
     int ls=tr[k].l,rs=tr[k].r;
     if(tr[ls].max>tr[rs].max)
     {
         tr[k].max=tr[ls].max;
         tr[k].ans=tr[ls].ans;
     }
     else if(tr[ls].max<tr[rs].max)
     {
         tr[k].max=tr[rs].max;
         tr[k].ans=tr[rs].ans;
     }
     else
     {
         tr[k].max=tr[ls].max;
         tr[k].ans=tr[ls].ans+tr[rs].ans;
     }
}
int merge(int k1,int k2,int l,int r)//把k1,k2(范围是l到r)合并到一起
{
 if(k1==0||k2==0) return k1+k2;
 if(l==r)
 {
     tr[k1].max=tr[k1].max+tr[k2].max;
     return k1;
 }
 int mid=(l+r)>>1;
 tr[k1].l=merge(tr[k1].l,tr[k2].l,l,mid);
 tr[k1].r=merge(tr[k1].r,tr[k2].r,mid+1,r);
 push_up(k1);
 return k1;
}
void update(int p,int l,int r,int k)//插入p这个节点
{
    if(l==r)
    {
        tr[k].max++;
        tr[k].ans=l;
        return;
    }
    int mid=(l+r)>>1;
    if(p<=mid)
    {
        if(tr[k].l==0) tr[k].l=++cnt;
        update(p,l,mid,tr[k].l);
    }
    else
    {
        if(tr[k].r==0) tr[k].r=++cnt;
        update(p,mid+1,r,tr[k].r);
    }//递归合并
    push_up(k);//更新k节点

}
void dfs(int now,int f)
{
 for(int i=0;i<g[now].size();i++)
 {
     if(g[now][i]==f) continue;
     dfs(g[now][i],now);
     merge(rt[now],rt[g[now][i]],1,n);//把儿子合并
 }
 update(cl[now],1,n,rt[now]);
 anss[now]=tr[rt[now]].ans;
}
signed main()
{
 scanf("%lld",&n);
 for(int i=1;i<=n;i++)
 {
     scanf("%lld",&cl[i]);
     rt[i]=i;
     cnt++;
 }
 int from,to;
 for(int i=1;i<n;i++)
 {
    scanf("%lld%lld",&from,&to);
    g[from].push_back(to);
    g[to].push_back(from);
 }
 dfs(1,0);
 for(int i=1;i<=n;i++)
 {
    printf("%lld ",anss[i]);
 }
 return 0;
}


#include <cstdio>
#include <map>

typedef long long ll;
typedef std::map<int,int>::iterator mit;
const int maxn=100000+10;
std::map<int,int> mp[maxn];
int cnt,id[maxn],sn[maxn],sz[maxn],ax[maxn],clr[maxn];
ll sm[maxn];

struct Edge{
    int cnt,beg[maxn],nxt[maxn*2],poi[maxn*2];
    void push(int u,int v){
        int o=++cnt;
        nxt[o]=beg[u];
        poi[o]=v;
        beg[u]=o;
    }
}e;

void dfs1(int u,int fa){
    sz[u]=1;
    for(int i=e.beg[u];i;i=e.nxt[i]){
        int v=e.poi[i];if(v==fa)continue;
        // printf("%d\n",v);
        dfs1(v,u);
        sz[u]+=sz[v];
        if(sz[sn[u]]<sz[v])
            sn[u]=v;
    }
}

void dfs2(int u,int fa){
    if(!sn[u]){
        id[u]=++cnt;
        mp[id[u]][clr[u]]=1;
        ax[u]=1;
        sm[u]=clr[u];
        // printf("%d %d\n",u,clr[u]);
    }
    else{
        dfs2(sn[u],u);
        id[u]=id[sn[u]];
        ax[u]=ax[sn[u]];
        sm[u]=sm[sn[u]];
        ++mp[id[u]][clr[u]];
        if(mp[id[u]][clr[u]]==ax[u])sm[u]+=clr[u];
        else if(mp[id[u]][clr[u]]>ax[u]){ax[u]=mp[id[u]][clr[u]];sm[u]=clr[u];}
        // printf("HH %d %d\n",u,sm[u]);
        for(int i=e.beg[u];i;i=e.nxt[i]){
            int v=e.poi[i];if(v==fa || v==sn[u])continue;
        // printf("%d\n",v);

            dfs2(v,u);
            for(mit j=mp[id[v]].begin();j!=mp[id[v]].end();++j){
                // printf("%d %d %d %d %d\n",v,j->first,j->second,mp[id[u]][j->first],ax[u]);
                mp[id[u]][j->first]+=j->second;
                if(mp[id[u]][j->first]==ax[u])sm[u]+=j->first;
                else if(mp[id[u]][j->first]>ax[u]){ax[u]=mp[id[u]][j->first];sm[u]=j->first;}
            }
            mp[id[v]].clear();
        }
    }
}

int main(){
    // freopen("in.txt","r",stdin);
    int n,u,v;scanf("%d",&n);
    for(int i=1;i<=n;++i)scanf("%d",clr+i);
    for(int i=1;i<n;++i){
        scanf("%d%d",&u,&v);
        e.push(u,v);
        e.push(v,u);
    }
    dfs1(1,0);
    dfs2(1,0);
    // printf("%d\n",n);
    for(int i=1;i<=n;++i)printf("%lld ",sm[i]);
    printf("\n");
    return 0;
}


#include<iostream>
#include<cstdio>
#pragma GCC optimize("Ofast")
using namespace std;
const int maxn=2e5+10;
int col[maxn],y,t,tot,head[maxn];
int x,n,ver[maxn],nxt[maxn];
long long sum[maxn],cnt;
inline int read(){
    char c=getchar();int x=0;
    while(c<'0'||c>'9') c=getchar();
    while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    return x;
}
struct ziji{
    int l,r;
    long long big,sum,ans;
}mn[maxn*15];
#define ls(i) mn[i].l
#define rs(i) mn[i].r
#define big(i) mn[i].big
#define sum(i) mn[i].sum
#define ans(i) mn[i].ans
inline void pushup(int now){
    if(sum(ls(now))>sum(rs(now)))
        sum(now)=sum(ls(now)),
        big(now)=big(ls(now)),
        ans(now)=ans(ls(now));
    else if(sum(ls(now))<sum(rs(now)))
        sum(now)=sum(rs(now)),
        big(now)=big(rs(now)),
        ans(now)=ans(rs(now));
    else ans(now)=ans(ls(now))+ans(rs(now)),
    big(now)=big(ls(now)),sum(now)=sum(ls(now));
}
inline void insert(int &now,int l,int r,int pos,int d){
    if(!now) now=++cnt;
    if(l==r){
        big(now)=l,sum(now)+=d,ans(now)=l;
        return ;
    }
    int mid=l+r>>1;
    if(pos<=mid) insert(ls(now),l,mid,pos,d);
    else insert(rs(now),mid+1,r,pos,d);
    pushup(now);
}
inline int merge(int x,int y,int l,int r){
    if((!x)||(!y)) return x+y;
    if(l==r){
        big(x)=l,sum(x)+=sum(y),ans(x)=l;
        return x;
    }
    int mid=l+r>>1;
    ls(x)=merge(ls(x),ls(y),l,mid);
    rs(x)=merge(rs(x),rs(y),mid+1,r);
    pushup(x);return x;
}
inline void dfs(int x,int fa){
    for(register int i=head[x];i;i=nxt[i]){
        int y=ver[i];if(y==fa) continue;
        dfs(y,x);merge(x,y,1,n);
    }
    insert(x,1,n,col[x],1);sum[x]=ans(x);
}
inline void add(int x,int y){
    ver[++tot]=y,nxt[tot]=head[x],head[x]=tot;
}
int main(){
    n=read();cnt=n;
    for(register int i=1;i<=n;i++) col[i]=read();
    for(register int i=1;i<n;i++)
        x=read(),y=read(),add(x,y),add(y,x);
    dfs(1,0);
    for(register int i=1;i<=n;i++)
        printf("%lld ",sum[i]);
}


#include <bits/stdc++.h>
using namespace std;
const int N=1e5+100;
int head[N]={0},to[N*2]={0},nxt[N*2]={0},id[N]={0},tot=1;
int book[N]={0},son[N]={0};
long long ans[N],mx=0,sum=0;
inline void add(int u,int v){
	to[++tot]=v;
	nxt[tot]=head[u];
	head[u]=tot;
}
int fds(int u,int f){
	int size=1,msz=0;
	for(int i=head[u];i;i=nxt[i]){
		int v=to[i];
		if(v==f)continue;
		int nsz=fds(v,u);
		if(nsz>msz)son[u]=v,msz=nsz;
		size+=nsz;
	}
}
void init(int u,int f){
	book[id[u]]--;
	for(int i=head[u];i;i=nxt[i]){
		int v=to[i];
		if(v==f)continue;
		init(v,u);
	}
}
void calc(int u,int f){
	book[id[u]]++;
	if(book[id[u]]>mx){
		mx=book[id[u]];
		sum=id[u];
	}else if(book[id[u]]==mx)sum+=id[u];
	for(int i=head[u];i;i=nxt[i]){
		int v=to[i];
		if(v==f||v==son[u])continue;
		calc(v,u);
	}
}
void dfs(int u,int f){
	for(int i=head[u];i;i=nxt[i]){
		int v=to[i];
		if(v==f||v==son[u])continue;
		dfs(v,u);
		init(v,u);
		mx=0,sum=0;
	}
	book[id[u]]++;
	if(book[id[u]]>mx){
		mx=book[id[u]];
		sum=id[u];
	}else if(book[id[u]]==mx)sum+=id[u];
	if(son[u]!=0)dfs(son[u],u);
	for(int i=head[u];i;i=nxt[i]){
		int v=to[i];
		if(v==f||v==son[u])continue;
		calc(v,u);
	}
	ans[u]=sum;
}
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>id[i];
	}
	for(int i=1;i<n;i++){
		int x,y;
		cin>>x>>y;
		add(x,y);
		add(y,x);
	}
	fds(1,0);
	dfs(1,0);
	for(int i=1;i<=n;i++)cout<<ans[i]<<" ";
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define maxn 100010
using namespace std;
int n,a[maxn],son[maxn],cnt[maxn],so,maxx=-1,size[maxn],sum=0;
LL ans[maxn];
std::vector<int> v[maxn];
void add(int x,int fa,int vv)
{
    cnt[a[x]]+=vv;
    if(cnt[a[x]]>maxx) maxx=cnt[a[x]],sum=a[x];
    else if(cnt[a[x]]==maxx) sum+=a[x];

    for(int i=0;i<v[x].size();i++)
	{
        int to=v[x][i];
        if(to==fa||to==so) continue;
        add(to,x,vv);
    }
}
void pou(int x,int fa)
{
    size[x]=1;
    for(int i=0;i<v[x].size();i++)
	{
        int to=v[x][i];
        if(to==fa) continue;
        pou(to,x);
        size[x]+=size[to];
        if(size[to]>size[son[x]])
		son[x]=to;//选最大的子树剖
    }
    return ;
}
void js(int x,int fa,int jl)
{
    for(int i=0;i<v[x].size();i++)
	{
        int to=v[x][i];
        if(to==fa) continue;
        if(to!=son[x])
		js(to,x,0);//轻 0是消除影响
    }
    if(son[x])
	js(son[x],x,1),so=son[x];//重 1是不消除影响

    add(x,fa,1);//1是加入
	so=0,ans[x]=sum;
    if(!jl)
	{
		add(x,fa,-1);//-1是删除
		sum=0,maxx=0;
	}
	return ;
}
int main()
{
	//freopen("treasure.in", "r", stdin);
   	//freopen("treasure.out", "w", stdout);

    scanf("%d",&n);
    for(int i=1;i<=n;i++)
	scanf("%d",&a[i]);

    for(int i=1;i<n;i++)
	{
        int x,y;
        scanf("%d%d",&x,&y);
        v[x].push_back(y),v[y].push_back(x);
    }
    pou(1,0);
    js(1,0,0);
    for(int i=1;i<=n;i++)
	printf("%lld ",ans[i]);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

inline int read()
{
	int x = 0, f = 1; char s = getchar();
	for(; !isdigit(s); s = getchar()) if(s == '-') f = -1;
	for(; isdigit(s); s = getchar()) x = x * 10 + s - 48;
	return x * f;
}

typedef long long ll;
const int MAXN = 100010;

struct Edge
{
	int to, nxt;
}e[MAXN << 1];
int tot, h[MAXN];

inline void add(int u, int v)
{
	e[++tot] = (Edge){v, h[u]};
	h[u] = tot;
}

int n, c[MAXN], siz[MAXN], son[MAXN], vis[MAXN];
int top, cnt[MAXN]; ll sum[MAXN], ans[MAXN];

void calc(int x, int fa, int flag)
{
	if(flag > 0)
	{
		sum[cnt[c[x]]] -= c[x];
		++cnt[c[x]];
		sum[cnt[c[x]]] += c[x];
		if(cnt[c[x]] > top) top = cnt[c[x]];
	}
	else
	{
		sum[cnt[c[x]]] -= c[x];
		--cnt[c[x]];
		sum[cnt[c[x]]] += c[x];
		if(!sum[top] && top) --top;
	}
	for(int i = h[x]; i; i = e[i].nxt)
	{
		int y = e[i].to;
		if(y == fa || vis[y]) continue;
		calc(y, x, flag);
	}
}

void dfs1(int x, int fa)
{
	siz[x] = 1;
	for(int i = h[x]; i; i = e[i].nxt)
	{
		int y = e[i].to;
		if(y == fa) continue;
		dfs1(y, x);
		siz[x] += siz[y];
		if(!son[x]) son[x] = y;
		else if(siz[y] > siz[son[x]]) son[x] = y;
	}
}

void dfs2(int x, int fa, int flag)
{
	for(int i = h[x]; i; i = e[i].nxt)
	{
		int y = e[i].to;
		if(y == fa || y == son[x]) continue;
		dfs2(y, x, 0);
	}
	if(son[x]) dfs2(son[x], x, 1), vis[son[x]] = 1;
	calc(x, fa, 1); vis[son[x]] = 0; ans[x] = sum[top];
	if(!flag) calc(x, fa, -1);
}

int main()
{
	n = read();
	for(int i = 1; i <= n; ++i) c[i] = read();
	for(int i = 1; i < n; ++i)
	{
		int u = read(), v = read();
		add(u, v), add(v, u);
	}
	dfs1(1, 0); dfs2(1, 0, 0);
	for(int i = 1; i <= n; ++i) printf("%lld ", ans[i]);
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
template < class T > inline void read (T &x){
	x=0;char ch=getchar();bool f=0;
	while(!isdigit(ch)){if(ch=='-')f=1;ch=getchar();}
	while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	if(f)x=-x;
}
int n;
const int maxn=100001;
int c[maxn];
const int maxe=maxn<<1;
int head[maxn],nex[maxe],to[maxe];
int cntt=0;
inline void add (int x,int y){
	nex[++cntt]=head[x]; to[cntt]=y;
	head[x]=cntt;
}
int son[maxn];
int ans[maxn];
int siz[maxn];
int cnt[maxn];
void dfs1 (int u,int fa){
	siz[u]=1;
	for (int i=head[u];i;i=nex[i]){
		int v=to[i];
		if (v==fa) continue ;
		dfs1 (v,u);
		siz[u]+=siz[v];
		if (siz[v]>siz[son[u]]) son[u]=v;
	}
}
int maxx=0,sum=0;
void change (int u,int fa,int val,int wa){
	cnt[c[u]]+=val;
	if (cnt[c[u]]>maxx){
		maxx=cnt[c[u]]; sum=c[u];
	}
	else if (cnt[c[u]]==maxx) sum+=c[u];
	for (int i=head[u];i;i=nex[i]){
		int v=to[i];
		if ((v==fa)||(v==wa)) continue ;
		change (v,u,val,wa);
	}
}
void dfs2 (int u,int fa,bool flag){
	for (int i=head[u];i;i=nex[i]){
		int v=to[i];
		if ((v==fa)||(v==son[u])) continue ;
		dfs2 (v,u,0);
	}
	if (son[u]) dfs2 (son[u],u,1);
	change (u,fa,1,son[u]);
	ans[u]=sum;
	if (!flag){
		change (u,fa,-1,0);
		maxx=0; sum=0;
	}
}
int main(){
	read (n);
	for (register int i=1;i<=n;++i) read (c[i]);
	for (register int i=1;i<n;++i){
		int x,y; read (x); read (y);
		add (x,y); add (y,x);
	}
	dfs1 (1,0); dfs2 (1,0,0);
	for (register int i=1;i<=n;++i) printf ("%d ",ans[i]);
	return 0;
}


#include <bits/stdc++.h>
#define int long long
#define N (int) 1e6
using namespace std;
int n;
int c[N];
struct Edge {
	int v, nxt;
} edge[N<<1];
int fst[N], ecnt;
void add_edge (int u, int v) {
	edge[ecnt].v = v, edge[ecnt].nxt = fst[u], fst[u] = ecnt ++;
}
void init_e () {
	memset (fst, -1, sizeof (fst));
}

int wson[N], sz[N];
void dfs1 (int u, int f) {
	sz[u] = 1;
	int mx = 0;
	for (int e = fst[u]; e != -1; e = edge[e].nxt) {
		int v = edge[e].v;
		if (v == f) continue;
		dfs1 (v, u);
		sz[u] += sz[v];
		if (sz[v] > mx) mx = sz[v], wson[u] = v;
	}
}

int cnt[N], sum, mx;
int ans[N];
void init (int u, int f) {
	cnt[c[u]] --;
	for (int e = fst[u]; e != -1; e = edge[e].nxt) {
		int v = edge[e].v;
		if (v == f) continue;
		init (v, u);
	}
}

void DFS (int u, int f, int ws) {
	cnt[c[u]] ++;
	if (cnt[c[u]] > mx) mx = cnt[c[u]], sum = c[u];
	else if (cnt[c[u]] == mx) sum += c[u];

	for (int e = fst[u]; e != -1; e = edge[e].nxt) {
		int v = edge[e].v;
		if (v == f || v == ws) continue;
		DFS (v, u, ws);
	}
}

void dfs (int u, int f) {
	for (int e = fst[u]; e != -1; e = edge[e].nxt) {
		int v = edge[e].v;
		if (v == f || v == wson[u]) continue;
		dfs (v, u);
		init (v, u);
		sum = mx = 0;
	}

	if (wson[u]) dfs (wson[u], u);

	DFS (u, f, wson[u]);

	ans[u] = sum;
}

signed main () {
	init_e ();

	scanf ("%lld", & n);
	for (int i = 1; i <= n; i ++) scanf ("%lld", & c[i]);
	for (int i = 1; i < n; i ++) {
		int u, v;
		scanf ("%lld%lld", & u, & v);
		add_edge (u, v);
		add_edge (v, u);
	}

	dfs1 (1, 0);

	dfs (1, 0);

	for (int i = 1; i <= n; i ++) cout << ans[i] << " ";

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10, M = 2e5 + 10;
struct SegTree {
	#define lson(x) t[x][0]
	#define rson(x) t[x][1]
	#define mid (l + r >> 1)
	int t[N << 5][2], sum[N << 5], cnt[N << 5], idx;
	void pushup(int k) {
		if (sum[lson(k)] == sum[rson(k)]) {
			sum[k] = sum[lson(k)];
			cnt[k] = cnt[lson(k)] + cnt[rson(k)];
		} else {
			int p = sum[lson(k)] > sum[rson(k)] ? lson(k) : rson(k);
			sum[k] = sum[p]; cnt[k] = cnt[p];
		}
	}
	void update(int &k, int l, int r, int pos, int v) {
		if (!k) k = ++idx;
		if (l == r) {
			sum[k] += v;
			cnt[k] = l;
			return ;
		}
		if (pos <= mid) update(lson(k), l, mid, pos, v);
		else update(rson(k), mid + 1, r, pos, v);
		pushup(k);
	}
	int merge(int a, int b, int l, int r) {
		if (!b) return a;
		if (!a) return b;
		if (l == r) {
			sum[a] += sum[b];
			cnt[a] = l;
			return a;
		}
		lson(a) = merge(lson(a), lson(b), l, mid);
		rson(a) = merge(rson(a), rson(b), mid + 1, r);
		pushup(a);
		return a;
	}
}T;

int head[N], tot, to[M], nxt[M];
void add_edge(int x, int y) {
	nxt[++tot] = head[x]; head[x] = tot; to[tot] = y;
}

int c[N], n, ans[N], rt[N];
void dfs(int u, int fa) {
	for (int i = head[u]; i; i = nxt[i]) {
		int v = to[i];
		if (v == fa) continue ;
		dfs(v, u);
		T.merge(rt[u], rt[v], 1, n);
	}
	T.update(rt[u], 1, n, c[u], 1);
	ans[u] = T.cnt[rt[u]];
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &c[i]);
		rt[i] = i;
	}
	T.idx = n;
	for (int i = 1; i < n; i++) {
		int x, y; scanf("%d%d", &x, &y);
		add_edge(x, y); add_edge(y, x);
	}
	dfs(1, 0);
	for (int i = 1; i <= n; i++)
		printf("%d ", ans[i]);
	puts("");
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define lson tr[pos].l
#define rson tr[pos].r
using namespace std;
const int N=1e5+10;
const int inf=1e5;
int cnt,tot,n,a[N],rt[N],ver[N<<1],Next[N<<1],lin[N];ll anss[N];
struct tree{
	int sum,val,l,r;ll ans;
}tr[N*20];
struct edge{
	int next,to;
}e[N];
int head[N<<1];
void add(int u,int v){
    e[++tot].next=head[u];
	e[tot].to=v;
	head[u]=tot;
}
/*void add(int x,int y){ver[++tot]=y;Next[tot]=lin[x];lin[x]=tot;}*/
void maintain(int pos){
	if(tr[lson].sum>tr[rson].sum){
		tr[pos].val=tr[lson].val;
		tr[pos].sum=tr[lson].sum;
		tr[pos].ans=tr[lson].ans;
	}else if(tr[lson].sum<tr[rson].sum){
		tr[pos].val=tr[rson].val;
		tr[pos].sum=tr[rson].sum;
		tr[pos].ans=tr[rson].ans;
	}else{
		tr[pos].val=tr[lson].val;
		tr[pos].sum=tr[lson].sum;
		tr[pos].ans=tr[lson].ans+tr[rson].ans;
	}
	return;
}
int merge(int a,int b,int l,int r){
	if(!b||!a) return a+b;
	if(l==r){
		tr[a].val=l;
		tr[a].sum+=tr[b].sum;
		tr[a].ans=l;
		return a;
	}
	int mid=l+r>>1;
	tr[a].l=merge(tr[a].l,tr[b].l,l,mid);
	tr[a].r=merge(tr[a].r,tr[b].r,mid+1,r);
	maintain(a);
	return a;
}
void update(int &pos,int l,int r,int now,int v){
	if(!pos){
		pos=++cnt;
	}
	if(l==r){
		tr[pos].sum++;
		tr[pos].val=l;
		tr[pos].ans=l;
		return ;
	}
	int mid=l+r>>1;
	if(now<=mid) update(tr[pos].l,l,mid,now,v);
	else update(tr[pos].r,mid+1,r,now,v);
	maintain(pos);
}
void dfs(int x,int f){
	for(int i=head[x];i;i=e[i].next){
		int y=e[i].to;
		if(y==f) continue;
		dfs(y,x);
		merge(rt[x],rt[y],1,inf);
	}
	update(rt[x],1,inf,a[x],1);
	anss[x]=tr[rt[x]].ans;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;++i) scanf("%d",&a[i]),rt[i]=++cnt;
	for(int i=1;i<n;++i){
		int x,y;scanf("%d%d",&x,&y);
		add(x,y);add(y,x);
	}
	dfs(1,0);
	for(int i=1;i<=n;++i) printf("%I64d ",anss[i]);
	cout<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#define int long long
#define N 200005
using namespace std;
int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	return x*f;
}
int n,v[N],nxt[N],head[N],cnt,tot,rt[N],ans[N],col[N];
void add(int a,int b)
{
	v[++cnt]=b;
	nxt[cnt]=head[a];
	head[a]=cnt;
}
struct node
{
	int ls,rs,val,ans;
}t[N*4];
void pushup(int k)
{
	if(t[t[k].ls].val>t[t[k].rs].val)
	{
		t[k].val=t[t[k].ls].val;
		t[k].ans=t[t[k].ls].ans;
	}
	else if(t[t[k].ls].val<t[t[k].rs].val)
	{
		t[k].val=t[t[k].rs].val;
		t[k].ans=t[t[k].rs].ans;
	}
	else
	{
		t[k].val=t[t[k].rs].val;
		t[k].ans=t[t[k].rs].ans+t[t[k].ls].ans;
	}
}
int merge(int x, int y,int ll,int rr)
{
	if(x==0||y==0)return x+y;
	if(ll==rr)
	{
		t[x].val+=t[y].val;
		t[x].ans=ll;
		return x;
	}
	int mid=(ll+rr)>>1;
	t[x].ls=merge(t[x].ls,t[y].ls,ll,mid);
	t[x].rs=merge(t[x].rs,t[y].rs,mid+1,rr);
	pushup(x);
	return x;
}
void modify(int &now,int l,int r,int pos,int val)
{
	if(!now)now=++tot;
	//cout<<now<<" "<<l<<" "<<r<<" "<<pos<<" "<<val<<endl;
	if(l==r)
	{
		t[now].val+=val;
		t[now].ans=l;
		return;
	}
	int mid=(l+r)>>1;
	if(pos<=mid)modify(t[now].ls,l,mid,pos,val);
	else modify(t[now].rs,mid+1,r,pos,val);
	pushup(now);
}
void dfs(int x,int fa)
{
	for(int i=head[x];i;i=nxt[i])
	{
		if(v[i]==fa)continue;
		dfs(v[i],x);
		merge(rt[x],rt[v[i]],1,1e5);
	}
//	if(x!=4)modify(rt[x],1,1e5,col[x],1);
//	if(x!=4)ans[x]=t[rt[x]].ans;
//	if(x==4)
//	{
//		printf("-------------\n");
//		modify(rt[x],1,1e5,col[x],1);
//		ans[x]=t[rt[x]].ans;
//		cout<<x<<" "<<col[x]<<" "<<t[rt[x]].ans<<endl;
//		puts("--------------");
//	}
	modify(rt[x],1,1e5,col[x],1);
	ans[x]=t[rt[x]].ans;
}
signed main()
{
	n=read();
	for(int i=1;i<=n;i++)
	{
		col[i]=read();
		rt[i]=++tot;
	}
	for(int i=1;i<n;i++)
	{
		int x=read(),y=read();
		add(x,y);
		add(y,x);
	}
	dfs(1,0);
	for(int i=1;i<=n;i++)
	{
		printf("%lld ",ans[i]);
	}
	return 0;
}


#include<iostream>

using namespace std;
const int Max = 1e5 + 5;
struct E
{
	int to, ne;
}edge[Max];
int head[Max],tot;
void addnode(int u, int v)
{
	edge[tot].to = v;
	edge[tot].ne = head[u];
	head[u] = tot++;
}
int sz[Max], wson[Max];
void dfs(int u, int f)
{
	sz[Max] = 1;
	int maxsz = -1;
	for (int i = head[u]; ~i; i = edge[i].ne)
	{
		int v = edge[i].to;
		if (v == f)continue;
		dfs(v, u);
		sz[u] += sz[v];
		if (sz[v] > maxsz)
		{
			maxsz = sz[v];
			wson[u] = v;
		}
	}
}
int col[Max], cnt[Max];
long long ans[Max], sum;
int flag, maxc;
void count(int u, int f, int val)
{
	cnt[col[u]] += val;
	if (cnt[col[u]] > maxc)
	{
		maxc = cnt[col[u]];
		sum = col[u];
	}
	else if (maxc == cnt[col[u]])
	{
		sum += col[u];
	}
	for (int i = head[u]; ~i; i = edge[i].ne)
	{
		int v = edge[i].to;
		if (v == f || v == flag)continue;//不能写成v == wson[u];
		count(v, u, val);
	}
}
void dfs(int u, int f, bool keep)
{
	for (int i = head[u]; ~i; i = edge[i].ne)
	{
		int v = edge[i].to;
		if (v == f || v == wson[u])continue;
		dfs(v, u, false);
	}
	if (wson[u])
	{
		dfs(wson[u], u, true);
		flag = wson[u];
	}
	count(u, f, 1);
	flag = 0;
	ans[u] = sum;
	if (!keep)
	{
		count(u, f, -1);
		sum = maxc = 0;
	}
}

int main()
{
	int n, a, b;
	memset(head, -1, sizeof(head));
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> col[i];
	for (int i = 1; i <= n - 1; i++)
	{
		cin >> a >> b;
		addnode(a, b);
		addnode(b, a);
	}
	dfs(1, 0);
	dfs(1, 0, 0);
	for (int i = 1; i <= n; i++)
		cout << ans[i] << ' ';
	return 0;
}


#include<bits/stdc++.h>
#define maxn 100005
#define ll long long
using namespace std;
int n;
int c[maxn];
vector<int>adj[maxn];
map<int,int>cnt[maxn];
map<int,ll>sum[maxn];
map<int,int>::iterator p1;
map<int,ll>::iterator p2;
ll ans[maxn];
void dfs(int u,int fa)
{
	cnt[u][c[u]]=1;
	sum[u][1]=c[u];
	int sz=adj[u].size();
	for(int i=0;i<sz;i++)
	{
		int v=adj[u][i];
		if(v==fa)continue;
		if(cnt[v].size()>cnt[u].size())
		{
			swap(cnt[u],cnt[v]);
			swap(sum[u],sum[v]);
		}
		dfs(v,u);
		for(p1=cnt[v].begin();p1!=cnt[v].end();p1++)
		{
			sum[u][cnt[u][p1->first]]-=p1->first;
			cnt[u][p1->first]+=p1->second;
			sum[u][cnt[u][p1->first]]+=p1->first;
		}
	}
	p2=sum[u].end();
	p2--;
	ans[u]=p2->second;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&c[i]);
	int u,v;
	for(int i=1;i<=n-1;i++)
	{
		scanf("%d%d",&u,&v);
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	dfs(1,0);
	for(int i=1;i<=n;i++)
		printf("%lld ",ans[i]);
	printf("\n");
	return 0;
}


#include<cstdio>
#include<vector>
using namespace std;
int n,c[100010],cnt[100010],u,v,siz[100010],son[100010],ans[100010],flag,big;
vector<int> e[100010];
void dfs(int x,int las)
{
	siz[x]=1;
	int s=0,b=-1e9;
	for(int i=0;i<e[x].size();++i)
	{
		int y=e[x][i];
		if(y^las)
		{
			dfs(y,x);
			siz[x]+=siz[y];
			if(siz[y]>b)
			{
				b=siz[y];
				s=y;
			}
		}
	}
	son[x]=s;
}
void del(int x,int las)
{
	--cnt[c[x]];
	for(int i=0;i<e[x].size();++i)
	{
		int y=e[x][i];
		if(y^las)	del(y,x);
	}
}
void cfs(int x,int las)
{
	++cnt[c[x]];
	if(cnt[c[x]]>big)
	{
		flag=c[x];
		big=cnt[c[x]];
	}
	else if(cnt[c[x]]==big)	flag+=c[x];
	for(int i=0;i<e[x].size();++i)
	{
		int y=e[x][i];
		if(y^las)	cfs(y,x);
	}
}
void count(int x,int las)
{
	++cnt[c[x]];
	if(cnt[c[x]]>big)
	{
		flag=c[x];
		big=cnt[c[x]];
	}
	else if(cnt[c[x]]==big)	flag+=c[x];
	for(int i=0;i<e[x].size();++i)
	{
		int y=e[x][i];
		if(y^las&&y^son[x])	cfs(y,x);
	}
}
void dsu(int x,int las,int heavy)
{
	for(int i=0;i<e[x].size();++i)
	{
		int y=e[x][i];
		if(y^las&&y^son[x])	dsu(y,x,0);
	}
	if(son[x])	dsu(son[x],x,1);
	count(x,las);
	ans[x]=flag;
	if(!heavy)
	{
		del(x,las);
		big=flag=0;
	}
}
void getans(int x,int las)
{
	for(int i=0;i<e[x].size();++i)
	{
		int y=e[x][i];
		if(y^las)
		{
			getans(y,x);
			ans[x]+=ans[y];
		}
	}
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;++i)	scanf("%d",&c[i]);
	for(int i=1;i<n;++i)
	{
		scanf("%d %d",&u,&v);
		e[u].push_back(v);
		e[v].push_back(u);
	}
	dfs(1,1);
	dsu(1,1,1);
//	getans(1,1);
	for(int i=1;i<=n;++i)	printf("%d ",ans[i]);
	return 0;
}


#include<stdio.h>
#include<queue>
#include<cstring>
#include<vector>
#include<algorithm>
#define re register
#define oo 0x7f7f7f7f
#define O_O 100010
#define int long long
using namespace std;
inline int read()
{
    re int x=0,f=1;
    re char c=getchar();
    while(c<'0'||c>'9')
    {
        if(c=='-') f=-1;
        c=getchar();
    }
    while('0'<=c&&c<='9')
    {
        x=(x<<1)+(x<<3)+(c^48);
        c=getchar();
    }
    return x*f;
}

int n;
int rt[O_O],co[O_O],ans[O_O];
int end[O_O];
int head[O_O],cnt,tot;
struct node
{
	int to,next;
}ed[O_O];

inline void add(int u,int v)
{
	ed[++cnt].to=v;
	ed[cnt].next=head[u];
	head[u]=cnt;
}

namespace SEG
{
	#define lson ls[u]
	#define rson rs[u]

	int val[O_O*30],sum[O_O*30],ls[O_O*30],rs[O_O*30];

	int up(int u)
	{
		if(sum[lson]>sum[rson])
		{
			sum[u]=sum[lson];
			val[u]=val[lson];
			ans[u]=ans[lson];
		}
		if(sum[rson]>sum[lson])
		{
			sum[u]=sum[rson];
			val[u]=val[rson];
			ans[u]=ans[rson];
		}
		if(sum[lson]==sum[rson])
		{
			sum[u]=sum[lson];
			val[u]=val[lson];
			ans[u]=ans[lson]+ans[rson];
		}

	}

	int update(int &u,int l,int r,int pos,int v)
	{
		if(!u) u=++tot;
		if(l==r)
		{
			val[u]=l;
			sum[u]+=v;
			ans[u]=l;
			return 0;
		}
		re int mid=(l+r)>>1;
		if(pos<=mid) update(lson,l,mid,pos,v);
		else update(rson,mid+1,r,pos,v);
		up(u);
	}

	int merge(int a,int b,int l,int r)
	{
		if(!a||!b) return a+b;
		if(l==r)
		{
			val[a]=l;
			sum[a]+=sum[b];
			ans[a]=l;
			return a;
		}
		re int mid=(l+r)>>1;
		ls[a]=merge(ls[a],ls[b],l,mid);
		rs[a]=merge(rs[a],rs[b],mid+1,r);
		up(a);
		return a;
	}

	void dfs(int u,int fa)
	{
		for(re int i=head[u];i;i=ed[i].next)
		{
			re int v=ed[i].to;
			if(v==fa) continue;
			dfs(v,u);
			merge(rt[u],rt[v],1,100000);
		}
		update(rt[u],1,100000,co[u],1);
		end[u]=ans[rt[u]];
	}
}
using namespace SEG;

signed main()
{
	n=read();
	for(re int i=1;i<=n;++i)
	{
		co[i]=read();
		rt[i]=i;
		tot++;
	}
	int a,b;
	for(re int i=1;i<n;++i)
	{
		a=read(); b=read();
		add(a,b); add(b,a);
	}
	dfs(1,0);
	for(re int i=1;i<=n;++i) printf("%lld ",end[i]);
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int N = 1e5 + 10;
int n, c[N], son[N], sz[N], cnt[N], ms = 0;
ll res[N], sum = 0;
vector<int> g[N];

void dfs1(int u,int p){
    int maxs = 0;
    sz[u] = 1;
    for(int i = 0;i < g[u].size(); ++i){
        int v = g[u][i];
        if(v == p) continue;
        dfs1(v,u);
        sz[u] += sz[v];
        if(sz[v] > maxs){
            maxs = sz[v];
            son[u] = v;
        }
    }
}

void get(int u,int p,int op){
    cnt[c[u]] ++;
    if(cnt[c[u]] > ms){
        ms = cnt[c[u]];
        sum = 1ll * c[u];
    }
    else if(cnt[c[u]] == ms){
        sum += 1ll * c[u];
    }

    for(int i = 0;i < g[u].size(); ++i){
        int v = g[u][i];
        if(v == p || v == op) continue;
        get(v,u,op);
    }
}

void rev(int u,int p){
    cnt[c[u]]--;
    for(int i = 0;i < g[u].size(); ++i){
        int v = g[u][i];
        if(v == p) continue;
        rev(v,u);
    }
}

void dfs2(int u,int p){
    for(int i = 0;i < g[u].size(); ++i){
        int v = g[u][i];
        if(v == p) continue;
        if(v != son[u]){
            dfs2(v,u);
            rev(v,u);
            sum = ms = 0;
        }
    }
    if(son[u]) dfs2(son[u],u);
    get(u,p,son[u]);
    res[u] = sum;
}

int main(){
    scanf("%d",&n);
    for(int i = 1;i <= n; ++i) scanf("%d",&c[i]);
    for(int i = 0;i < n - 1; ++i){
        int x,y;
        scanf("%d%d",&x,&y);
        g[x].push_back(y);
        g[y].push_back(x);
    }

    dfs1(1,0);
    dfs2(1,0);
    for(int i = 1;i <= n; ++i) printf("%lld%c",res[i],i == n ? '\n' : ' ');
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <ctype.h>
const int bufSize = 1e6;
inline char nc()
{
    #ifdef DEBUG
    return getchar();
    #endif
    static char buf[bufSize], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, bufSize, stdin), p1 == p2) ? EOF : *p1++;
}
template<typename T>
inline T read(T &r)
{
    static char c;
    static int flag;
    flag = 1, r = 0;
    for (c = nc(); !isdigit(c); c = nc()) if (c == '-') flag = -1;
    for (; isdigit(c); c = nc()) r = r * 10 + c - 48;
    return r *= flag;
}
const int maxn = 1e5 + 100;
int n;
struct node
{
    int to,next;
}E[maxn << 1];
int head[maxn];
inline void add(const int &x,const int &y)
{
    static int tot = 0;
    E[++tot].next = head[x],E[tot].to = y,head[x] = tot;
}
int c[maxn],size[maxn],son[maxn],isson[maxn],vis[maxn],cnt[maxn],maxx;
long long sum[maxn];
void dfs(int u,int fa)
{
    size[u] = 1;
    for (int p = head[u]; p; p = E[p].next)
    {
        int v = E[p].to;
        if(v == fa) continue;
        dfs(v,u),size[u] += size[v];
        if(size[v] > size[son[u]]) son[u] = v;
    }
    if(son[u]) isson[son[u]] = 1;
}
void update(int u,int fa,int val)
{
    sum[cnt[c[u]]] -= c[u];
    cnt[c[u]] += val;
    sum[cnt[c[u]]] += c[u];
    if(sum[maxx + 1]) ++maxx;
    if(!sum[maxx]) --maxx;
    for (int p = head[u]; p; p = E[p].next)
    {
        int v = E[p].to;
        if(v != fa && !vis[v]) update(v,u,val);
    }
}
long long ANS[maxn];
void dfs2(int u,int fa,int keep)
{
    for (int p = head[u]; p; p = E[p].next)
    {
        int v = E[p].to;
        if (v == fa || v == son[u]) continue;
        dfs2(v, u, 0);
    }
    if (son[u]) dfs2(son[u], u, 1), vis[son[u]] = 1;
    update(u, fa, 1);
    vis[son[u]] = 0, ANS[u] = sum[maxx];
    if (!keep) update(u, fa, -1);
}
int main()
{
    read(n);
    for (int i = 1; i <= n; ++i) read(c[i]);
    for (int i = 1, a, b; i < n; ++i) read(a), read(b), add(a, b), add(b, a);
    dfs(1, 0), dfs2(1, 0, 1);
    for (int i = 1; i <= n; ++i) printf("%lld ",ANS[i]);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
//const int MOD=
const int N=7e3;
int n,m;
int len=0;
int val[N];
int ass[N][N];
int lin[N];
int Mmax[N];
int Vmax[N];
struct edge
{
	int next,y;
}e[N<<1];
inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    return x*f;
}
inline void insert(int xx,int yy)
{
	e[++len].next=lin[xx];
	lin[xx]=len;
	e[len].y=yy;
}
void dfs(int x,int fa)
{
	ass[x][val[x]]=1;
	Mmax[x]=val[x];
	Vmax[x]=1;
	for(int i=lin[x];i;i=e[i].next)
	{
		int v=e[i].y;
		if(v==fa) continue;
		dfs(v,x);
		Mmax[x]=max(Mmax[x],Mmax[v]);
		for(int i=1;i<=Mmax[x];i++)
		{
			ass[x][i]+=ass[v][i];
			Vmax[x]=max(Vmax[x],ass[x][i]);
		}
	}
}
int main()
{
//    freopen("add.in","r",stdin);
//    freopen("add.out","w",stdout);
    n=read();
    for(int i=1;i<=n;i++) val[i]=read();
    for(int i=1;i<n;i++)
    {
    	int u=read(),v=read();
    	insert(u,v),insert(v,u);
    }
    dfs(1,0);
//    for(int i=1;i<=n;i++) printf("%d\n",Vmax[i]);
    for(int i=1;i<=n;i++)
    {
    	int ans=0;
    	for(int j=1;j<=Mmax[i];j++)
    		if(ass[i][j]==Vmax[i]) ans+=j;
    	printf("%d ",ans);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
#define pii pair<int, ll>
#define MP make_pair
#define fir first
#define sec second
const int N=1e5+5;
int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

int n, a[N];
struct edge{int v, ne;}e[N<<1];
int cnt, h[N];
inline void ins(int u, int v) {
    e[++cnt]=(edge){v, h[u]}; h[u]=cnt;
    e[++cnt]=(edge){u, h[v]}; h[v]=cnt;
}
int size[N], mx[N], big[N];
void dfs(int u, int fa) {
    size[u]=1;
    for(int i=h[u];i;i=e[i].ne)
        if(e[i].v != fa) {
            dfs(e[i].v, u);
            size[u] += size[e[i].v];
            if(size[e[i].v] > size[mx[u]]) mx[u] = e[i].v;
        }
}

int cou[N], Max; ll ans[N];
pii f[N];
void update(int u, int fa, int val) {
    int &c = cou[a[u]];
    f[c].fir --; f[c].sec -= a[u];
    c += val;
    f[c].fir ++; f[c].sec += a[u];
    if(val==1) Max = max(Max, c);
    else if(!f[Max].fir) Max--;

    for(int i=h[u];i;i=e[i].ne)
        if(e[i].v != fa && !big[e[i].v]) update(e[i].v, u, val);
}

void dfs(int u, int fa, int keep) {
    for(int i=h[u];i;i=e[i].ne)
        if(e[i].v != fa && e[i].v != mx[u]) dfs(e[i].v, u, 0);
    if(mx[u]) dfs(mx[u], u, 1), big[mx[u]] = 1;
    update(u, fa, 1);
    ans[u] = f[Max].sec;
    big[mx[u]] = 0;
    if(!keep) update(u, fa, -1);
}
int main() {
    //freopen("in","r",stdin);
    n=read();
    for(int i=1; i<=n; i++) a[i]=read();
    for(int i=1; i<n; i++) ins(read(), read());
    dfs(1, 0);
    dfs(1, 0, 1);
    for(int i=1; i<=n; i++) printf("%I64d ",ans[i]);
}


//template: dsu on tree
#include<iostream>
#include<cstdio>
#include<set>
#include<map>
#include<cstring>
#include<vector>
#define maxn 100000
using namespace std;
typedef long long ll;
int n;
int a[maxn+5];
vector<int>E[maxn+5];

int sz[maxn+5];
int fa[maxn+5],son[maxn+5];
void dfs1(int x,int f){
	sz[x]=1;
	fa[x]=f;
	for(int y : E[x]){
		if(y!=f){
			dfs1(y,x);
			sz[x]+=sz[y];
			if(sz[y]>sz[son[x]]) son[x]=y;
		}
	}
}

bool vis[maxn+5];
int cnt[maxn+5];
ll sum[maxn+5];//出现次数为i的颜色的编号和
ll ans[maxn+5];
int top=0;
void calc(int x,int type){//type=1加入,type=-1擦除
	sum[cnt[a[x]]]-=a[x];
	cnt[a[x]]+=type;
	sum[cnt[a[x]]]+=a[x];
	if(sum[top+1]) top++;
	else if(top>0&&sum[top]==0) top--;
	for(int y : E[x]){
		if(vis[y]||y==fa[x]) continue;
		calc(y,type);
	}
}
void dfs2(int x,int is_heavy){
	for(int y : E[x]){
		if(y!=fa[x]&&y!=son[x]){
			dfs2(y,0);
		}
	}
	if(son[x]){
		dfs2(son[x],1);
		vis[son[x]]=1;
	}
	calc(x,1);
	vis[son[x]]=0;
	ans[x]=sum[top];
	if(!is_heavy) calc(x,-1);
}

int main(){
	int u,v;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	for(int i=1;i<n;i++){
		scanf("%d %d",&u,&v);
		E[u].push_back(v);
		E[v].push_back(u);
	}
	dfs1(1,0);
	dfs2(1,0);
	for(int i=1;i<=n;i++) printf("%lld ",ans[i]);
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson t[now].l
#define rson t[now].r
#define repi(i,a,b) for(register int i=int(a);i<=(b);++i)
#define pb push_back
constexpr int N=1e5+100;
template<typename T>
inline T read(){
    T s=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){s=(s<<3)+(s<<1)+ch-48,ch=getchar();}
    return s*f;
}
#define gl() read<ll>()
#define gn() read<int>()
template<typename T>
inline void print(T x){
    if(x<0)putchar('-'),x=-x;
    if(x>9)print(x/10);
    putchar(x%10+'0');
}
//////////////////////////////////////////////////////////
struct node{
    int l,r,sum,cnt;
}t[N*30];
int root[N],color[N],ans[N],n;
vector<int> v[N];
int tot=0;
void pushup(int now){
    if(t[lson].cnt>t[rson].cnt){
        t[now].cnt=t[lson].cnt;
        t[now].sum=t[lson].sum;
    }else if(t[rson].cnt>t[lson].cnt){
        t[now].cnt=t[rson].cnt;
        t[now].sum=t[rson].sum;
    }else if(t[lson].cnt==t[rson].cnt){
        t[now].cnt=t[lson].cnt;
        t[now].sum=t[lson].sum+t[rson].sum;
    }
}
void add(int val,int l,int r,int &now){
    if(!now)now=++tot;
    if(l==r){
        t[now].sum=l;
        t[now].cnt++;
        return ;
    }
    int mid=(l+r)>>1;
    if(val<=mid) add(val,l,mid,lson);
    else add(val,mid+1,r,rson);
    pushup(now);
}
int merge(int lc,int rc,int l,int r){
    //cout<<"merge:"<<lc<<" "<<rc<<" "<<l<<" "<<r<<endl;
    if(!lc||!rc)return rc|lc;
    if(l==r){
        //t[lc].sum+=t[rc].sum;
        t[lc].cnt+=t[rc].cnt;
        return lc;
    }
    int mid=(l+r)>>1;
    t[lc].l=merge(t[lc].l,t[rc].l,l,mid);
    t[lc].r=merge(t[lc].r,t[rc].r,mid+1,r);
    pushup(lc);
    return lc;
}
void dfs(int node,int fa){
    for(int k:v[node]){
        if(k==fa)continue;
        dfs(k,node);
        root[node]=merge(root[node],root[k],1,n);
    }
    add(color[node],1,n,root[node]);
    ans[node]=t[root[node]].sum;
    //cout<<node<<" "<<fa<<" "<<color[node]<<" "<<root[node]<<" "<<t[root[node]].sum<<endl;
}
int main(){
    n=gn();
    repi(i,1,n)color[i]=gn();
    repi(i,2,n){
        int a=gn(),b=gn();
        v[a].pb(b);
        v[b].pb(a);
    }
    dfs(1,0);
    for(int i=1;i<=n;++i){
        print(ans[i]),putchar(' ');
    }
    putchar(10);
}


#include<bits/stdc++.h>
using namespace std;
struct node//我定义的t
{
    int l,r,max;
    long long ans;
}t[10000005];
int n,u,v,top,nex[200005],head[100005],go[200005],root[100005],color[100005];
long long ans[100005];
void add(int u,int v)//链式前向星
{
    top++;
    nex[top]=head[u];
    head[u]=top;
    go[top]=v;
}
int merge(int k1,int k2,int l,int r)//即当前合并k1节点和k2节点
{
    if ((k1==0)||(k2==0)) return k1+k2;//考虑前三种情况
    if (l==r)//边界情况就是max值相加
    {
        t[k1].max=t[k1].max+t[k2].max;
        return k1;
    }
    int mid=(l+r)>>1;//第四种情况就是递归合并
    t[k1].l=merge(t[k1].l,t[k2].l,l,mid);
    t[k1].r=merge(t[k1].r,t[k2].r,mid+1,r);
    //下面这一段是push_up,也就是线段树常规维护
    int ls=t[k1].l,rs=t[k1].r;
    if (t[ls].max>t[rs].max)
    {
        t[k1].max=t[ls].max;
        t[k1].ans=t[ls].ans;
    }
    else
    if (t[ls].max<t[rs].max)
    {
        t[k1].max=t[rs].max;
        t[k1].ans=t[rs].ans;
    }
    else
    {
        t[k1].max=t[ls].max;
        t[k1].ans=t[ls].ans+t[rs].ans;
    }
    return k1;
}
void add(int p,int l,int r,int k)//在权值线段树中加入p这个点
{
    if (l==r)
    {
        t[k].max++;
        t[k].ans=l;
        return;
    }
    int mid=(l+r)>>1;
    if (p<=mid)
    {
        if (t[k].l==0)
        {
            top++;
            t[k].l=top;
        }
        add(p,l,mid,t[k].l);
    }
    else
    {
        if (t[k].r==0)
        {
            top++;
            t[k].r=top;
        }
        add(p,mid+1,r,t[k].r);
    }
    int ls=t[k].l,rs=t[k].r;
    if (t[ls].max>t[rs].max)
    {
        t[k].max=t[ls].max;
        t[k].ans=t[ls].ans;
    }
    else
    if (t[ls].max<t[rs].max)
    {
        t[k].max=t[rs].max;
        t[k].ans=t[rs].ans;
    }
    else
    {
        t[k].max=t[ls].max;
        t[k].ans=t[ls].ans+t[rs].ans;
    }
}
void dfs(int u,int fa)
{
    for (int i=head[u];i!=0;i=nex[i])//先递归儿子
    {
        int v=go[i];
        if (v!=fa)
        {
            dfs(v,u);
            root[u]=merge(root[u],root[v],1,n);//把儿子合并
        }
    }
    if (root[u]==0)
    {
        top++;
        root[u]=top;
    }
    add(color[u],1,n,root[u]);//把节点u的权值更新
    ans[u]=t[root[u]].ans;//更新答案
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    scanf("%d",&color[i]);
    for (int i=1;i<n;i++)
    {
        scanf("%d%d",&u,&v);
        add(u,v);
        add(v,u);
    }
    dfs(1,0);
    for (int i=1;i<=n;i++)//输出答案
    printf("%lld ",ans[i]);
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,x,y,t,ti;
int nxt[N<<1],head[N],adj[N<<1],mx[N<<6],lc[N<<6],rc[N<<6],rt[N];
long long s[N<<6],ans[N];
void push(int u,int v)
{
	nxt[++t]=head[u];
	head[u]=t;
	adj[t]=v;
}
void pushup(int rt)
{
	mx[rt]=max(mx[lc[rt]],mx[rc[rt]]);
	if(mx[lc[rt]]==mx[rc[rt]])s[rt]=s[lc[rt]]+s[rc[rt]];
	else if(mx[lc[rt]]>mx[rc[rt]])s[rt]=s[lc[rt]];
	else s[rt]=s[rc[rt]];
}
void update(int &rt,int l,int r,int x)
{
	if(!rt)rt=++ti;
	if(l==r){mx[rt]++,s[rt]=l;return;}
	int m=l+r>>1;
	if(x<=m)update(lc[rt],l,m,x);
	else update(rc[rt],m+1,r,x);
	pushup(rt);
}
int merge(int x,int y,int l,int r)
{
	if(!x || !y)return x+y;
	if(l==r){mx[x]+=mx[y],s[x]=l;return x;}
	int m=l+r>>1;
	lc[x]=merge(lc[x],lc[y],l,m);
	rc[x]=merge(rc[x],rc[y],m+1,r);
	pushup(x);
	return x;
}
void dfs(int u,int fa)
{
	for(int i=head[u];i;i=nxt[i])
		if(adj[i]!=fa)
		{
			dfs(adj[i],u);
			rt[u]=merge(rt[u],rt[adj[i]],1,n);
		}
	ans[u]=s[rt[u]];
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&x),update(rt[i],1,n,x);
	for(int i=1;i<n;i++)
		scanf("%d%d",&x,&y),push(x,y),push(y,x);
	dfs(1,0);
	for(int i=1;i<=n;i++)
		printf("%lld ",ans[i]);
}


#include<bits/stdc++.h>
using namespace std;
#define N 100100
typedef long long ll;
struct edge{
    int next,to;
}e[N*2];
int head[N],bian;
void add(int x,int y){
    bian++;
    e[bian].next=head[x],head[x]=bian;
    e[bian].to=y;
}
ll siz[N],cnt[N],son[N],w[N];
ll ans[N];
ll flase,sum=0,mas=0;
void dfs1(int x,int pre){
    siz[x]=1,son[x]=-1;
    ll ma=0;
    for(int i=head[x];i!=-1;i=e[i].next){
        int y=e[i].to;
        if(y==pre)continue;
        dfs1(y,x);
        siz[x]+=siz[y];
        if(siz[y]>ma)ma=siz[y],son[x]=y;
    }
}
void coun(int x,int pre,int q){
    cnt[w[x]]+=q;
    if(cnt[w[x]]>mas){
        mas=cnt[w[x]];
        sum=w[x];
    }
    else if(cnt[w[x]]==mas)sum+=w[x];
    for(int i=head[x];i!=-1;i=e[i].next){
        int y=e[i].to;
        if(y==pre||y==flase)continue;
        coun(y,x,q);
    }
}
void dfs2(int x,int pre,int f){
    for(int i=head[x];i!=-1;i=e[i].next){
        int y=e[i].to;
        if(y==pre||y==son[x])continue;
        dfs2(y,x,0);
    }
    if(son[x]!=-1)dfs2(son[x],x,1);
    flase=son[x];
    coun(x,pre,1);
    flase=0;
    ans[x]=sum;
    if(f==0)coun(x,pre,-1),sum=0,mas=0;
}
int main(){
    int n;
    memset(head,-1,sizeof(head)),bian=-1;
    memset(ans,0,sizeof(ans));
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",w+i);
    for(int i=1;i<n;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        add(a,b),add(b,a);
    }
    //printf("1111\n");
    dfs1(1,-1);
  //  printf("122222\n");
    dfs2(1,-1,0);
  //  printf("3333\n");
    printf("%lld",ans[1]);
    for(int i=2;i<=n;i++)printf(" %lld",ans[i]);
    printf("\n");
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>

using namespace std;
typedef long long LL;
const int maxn=100020;

LL ans[maxn];
struct pj{int y, nxt;} g[maxn<<1];
int len, la[maxn], n;
int col[maxn], son[maxn], tot[maxn];
LL cnt[maxn], exsum[maxn];
int mx;

int read() {
	int x=0, f=1; char c=getchar();
	for(; c<'0'||c>'9'; c=getchar()) if(c=='-') f=0;
	for(; c>='0'&&c<='9'; c=getchar()) x=x*10+c-'0';
	return f?x:-x;
}

void add_e(int x, int y) {
	g[++len].y=y; g[len].nxt=la[x]; la[x]=len;
}

void get_a(int x, int f, LL t, int xx) {
	exsum[cnt[col[x]]]-=col[x];
	cnt[col[x]]+=t;
	exsum[cnt[col[x]]]+=col[x];
	if(cnt[col[x]]>mx) mx=cnt[col[x]];
	for(int i=la[x]; i; i=g[i].nxt) {
		int y=g[i].y;
		if(y!=f&&y!=xx) get_a(y, x, t, xx);
	}
}

void dfs(int x, int f, int fl) {
	for(int i=la[x]; i; i=g[i].nxt) {
		int y=g[i].y;
		if(y!=f&&y!=son[x]) dfs(y, x, 1);
	}
	if(son[x]) dfs(son[x], x, 0);
	get_a(x, f, 1, son[x]);
	ans[x]=exsum[mx];
	if(fl) get_a(x, f, -1, 0), mx=0;
//	printf("\n%d: %d\n", x, son[x]);
//	for(int i=1; i<=n; ++i) printf("%lld ", cnt[i]); puts("");
//	for(int i=1; i<=n; ++i) printf("%lld ", exsum[i]);
}

void dfs1(int x, int f) {
	tot[x]=1;
	for(int i=la[x]; i; i=g[i].nxt) {
		int y=g[i].y;
		if(y!=f) {
			dfs1(y, x);
			tot[x]+=tot[y];
			if(tot[y]>tot[son[x]]) son[x]=y;
		}
	}

}

int main() {
	n=read();
	for(int i=1; i<=n; ++i) col[i]=read();
	for(int i=1, x, y; i<n; ++i)
		x=read(), y=read(), add_e(x, y), add_e(y, x);

	dfs1(1, 0);
	dfs(1, 0, 0);

	for(int i=1; i<=n; ++i) printf("%lld ", ans[i]);
    return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define int long long

const int N=1e5+5;

inline int read()
{
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9')
	{
		if(c=='-')
			f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9')
	{
		x=x*10+c-'0';
		c=getchar();
	}
	return x*f;
}

struct node{
	int to,last;
}tree[N*2];
int s=0,head[N];

inline void add(int from,int to)
{
	tree[++s].to=to;
	tree[s].last=head[from];
	head[from]=s;
}

int c[N];
int siz[N],son[N],re[N],mx,sum=0;
inline void dfs1(int from,int to)
{
	siz[to]=1; int maxx=0;
	for(int i=head[to];i;i=tree[i].last)
	{
		int k=tree[i].to;
		if(k==from)
			continue;
		dfs1(to,k);
		siz[to]+=siz[k];
		if(siz[k]>maxx)
		{
			maxx=siz[k];
			son[to]=k;
		}
	}
}

inline void update(int from,int to)
{
	re[c[to]]--;
	for(int i=head[to];i;i=tree[i].last)
	{
		int k=tree[i].to;
		if(k==from)
			continue;
		update(to,k);
	}
}

inline void dfs3(int from,int to,int f)
{
	re[c[to]]++;
	if(re[c[to]]>mx)
	{
		mx=re[c[to]];
		sum=c[to];
	}
	else if(re[c[to]]==mx)
		sum+=c[to];
	for(int i=head[to];i;i=tree[i].last)
	{
		int k=tree[i].to;
		if(k==from||k==f)
			continue;
		dfs3(to,k,f);
	}
}

int ans[N];
inline void dfs2(int from,int to)
{
	for(int i=head[to];i;i=tree[i].last)
	{
		int k=tree[i].to;
		if(k==from||k==son[to])
			continue;
		dfs2(to,k);
		update(to,k);
		sum=mx=0;
	}
	if(son[to])
		dfs2(to,son[to]);
	dfs3(from,to,son[to]);
	ans[to]=sum;
}

signed main()
{
	int n=read();
	for(int i=1;i<=n;i++)
		c[i]=read();
	int u,v;
	for(int i=1;i<=n-1;i++)
	{
		u=read(); v=read();
		add(u,v); add(v,u);
	}
	dfs1(0,1); dfs2(0,1);
	for(int i=1;i<=n;i++)
		printf("%lld ",ans[i]);
	return 0;
}


#include<bits/stdc++.h>
#define N 100005
#define re register
using namespace std;
typedef long long ll;
int n,a[N],cnt[N];
int size[N],son[N],S;
ll ans[N],sum,mx;

struct Edge
{
	int next,to;
}edge[N<<1];int head[N],ct=1;
void add_edge(int from,int to)
{
	edge[++ct].next=head[from];
	edge[ct].to=to;
	head[from]=ct;
}
template <class T>
void read(T &x)
{
	char c;int sign=1;
	while((c=getchar())>'9'||c<'0') if(c=='-') sign=-1; x=c-48;
	while((c=getchar())>='0'&&c<='9') x=x*10+c-48; x*=sign;
}
void DFS(int rt,int fa)
{
	size[rt]=1;
	for(re int i=head[rt];i;i=edge[i].next)
	{
		int v=edge[i].to;
		if(v==fa) continue;
		size[rt]+=size[v];
		if(size[son[rt]]<size[v]) son[rt]=v;
	}
}
void ad(int rt,int fa,int v)
{
	cnt[a[rt]]+=v;
	if(cnt[a[rt]]>mx) {mx=cnt[a[rt]];sum=a[rt];}
	else if(cnt[a[rt]]==mx) sum+=a[rt];
	for(re int i=head[rt];i;i=edge[i].next)
	{
		int to=edge[i].to;
		if(to==fa||to==S) continue;
		ad(to,rt,v);
	}
}
void dfs(int rt,int fa,bool del)
{
	for(re int i=head[rt];i;i=edge[i].next)
	{
		int v=edge[i].to;
		if(v==fa||v==son[rt]) continue;
		dfs(v,rt,1);
	}
	if(son[rt]) dfs(son[rt],rt,0),S=son[rt];
	ad(rt,fa,1);S=0;
	ans[rt]=sum;
	if(del) ad(rt,fa,-1),sum=mx=0;
}

int main()
{
	read(n);
	for(re int i=1;i<=n;++i) read(a[i]);
	for(re int i=1;i<n;++i)
	{
		int x,y;
		read(x);read(y);
		add_edge(x,y);
		add_edge(y,x);
	}
	DFS(1,0);
	dfs(1,0,0);
	for(re int i=1;i<=n;++i) printf("%lld ",ans[i]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100005;
int n, tot = 0, a[N], to[N << 1], nxt[N << 1], fir[N], Ans[N], ecnt = 0;
int Rt[N], ls[N * 60], rs[N * 60]; ll mx[N * 60], ans[N * 60];
template <class T> void rd(T &x) {
	char c = getchar(); int f = 1; x = 0;
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -1;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
struct Segment_Tree {
	#define mid (l + r) / 2
	#define lc ls[rt]
	#define rc rs[rt]
	void pushup(int rt) {
		if (mx[lc] > mx[rc]) mx[rt] = mx[lc], ans[rt] = ans[lc];
		else if (mx[lc] < mx[rc]) mx[rt] = mx[rc], ans[rt] = ans[rc];
		else mx[rt] = mx[lc], ans[rt] = ans[lc] + ans[rc];
	}
	void modify(int rt, int l, int r, int x) {
		if (l == r) {mx[rt] = 1; ans[rt] = l;  return;}
		if (x <= mid) lc = ++tot, modify(lc, l, mid, x);
		else rc = ++tot, modify(rc, mid + 1, r, x);
		pushup(rt);
	}
	int merge(int u, int v, int l, int r) {
		if (!u || !v) return u + v;
		int rt = ++tot;
		if (l == r) {mx[rt] = mx[u] + mx[v]; ans[rt] = l; return rt;}
		lc = merge(ls[u], ls[v], l, mid);
		rc = merge(rs[u], rs[v], mid + 1, r);
		pushup(rt); return rt;
	}
} T;
void ae(int u, int v) {to[++ecnt] = v; nxt[ecnt] = fir[u]; fir[u] = ecnt;}
void dfs(int u, int f) {
	for (int i = fir[u]; i; i = nxt[i]) {
		int v = to[i];
		if (v != f) dfs(v, u);
	}
	for (int i = fir[u]; i; i = nxt[i]) {
		int v = to[i];
		if (v != f) Rt[u] = T.merge(Rt[u], Rt[v], 1, n);
	}
	Ans[u] = ans[Rt[u]];
}
int main() {
	rd(n);
	for (int i = 1; i <= n; ++i) rd(a[i]);
	for (int i = 1; i < n; ++i) {
		int u, v; rd(u); rd(v);
		ae(u, v); ae(v, u);
	}
	for (int i = 1; i <= n; ++i) Rt[i] = ++tot, T.modify(Rt[i], 1, n, a[i]);
	dfs(1, 0);
	for (int i = 1; i <= n; ++i) printf(i == n ? "%lld\n" : "%lld ", Ans[i]);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define ll long long
using namespace std;
inline int read(){
    int x=0,o=1;char ch=getchar();
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')o=-1,ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*o;
}
const int N=1e5+5;
int n,maxn,tot[N],num[N];ll sum[N],ans[N];
int a[N],size[N],son[N],visit[N];
int cnt,head[N],nxt[N<<1],to[N<<1];
inline void add(int a,int b){
	nxt[++cnt]=head[a];head[a]=cnt;to[cnt]=b;
}
inline void pre_dfs(int u,int fa){
	size[u]=1;
	for(int i=head[u];i;i=nxt[i]){
		int v=to[i];if(v==fa)continue;
		pre_dfs(v,u);size[u]+=size[v];
		if(size[v]>size[son[u]])son[u]=v;
	}
}
inline void update(int u,int fa,int val){
	int &t=tot[a[u]];--num[t];sum[t]-=a[u];
	t+=val;++num[t];sum[t]+=a[u];
	if(val>0)maxn=max(maxn,t);
	else if(!num[maxn])--maxn;
	for(int i=head[u];i;i=nxt[i]){
		int v=to[i];if(v==fa)continue;
		if(visit[v])continue;
		update(v,u,val);
	}
}
inline void dfs(int u,int fa,int keep){
	for(int i=head[u];i;i=nxt[i]){
		int v=to[i];if(v==fa)continue;
		if(v==son[u])continue;dfs(v,u,0);
	}
	if(son[u])dfs(son[u],u,1),visit[son[u]]=1;
	update(u,fa,1);ans[u]=sum[maxn];visit[son[u]]=0;
	if(!keep)update(u,fa,-1);
}
int main(){
	n=read();for(int i=1;i<=n;++i)a[i]=read();
	for(int i=1;i<n;++i){
		int a=read(),b=read();
		add(a,b);add(b,a);
	}
	pre_dfs(1,0);dfs(1,0,1);
	for(int i=1;i<=n;++i)printf("%lld ",ans[i]);
	printf("\n");
    return 0;
}


#include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
#define int long long
using namespace std;
const int N=400005;
int tot,n;
int root[N<<2],ls[N<<2],rs[N<<2],sum[N<<2],ans[N<<2];
vector<int>va[N];
int val[N];
void up(int k)
{
	if(sum[ls[k]]>sum[rs[k]])
	{
		sum[k]=sum[ls[k]];
		ans[k]=ans[ls[k]];
	}
	if(sum[rs[k]]>sum[ls[k]])
	{
		sum[k]=sum[rs[k]];
		ans[k]=ans[rs[k]];
	}
	if(sum[ls[k]]==sum[rs[k]])
	{
		sum[k]=sum[ls[k]];
		ans[k]=ans[ls[k]]+ans[rs[k]];
	}
}
void change(int &k,int l,int r,int clo)
{
	if(!k) k=++tot;
	if(l==r)return(void)(sum[k]=1,ans[k]=clo);
	int mid=(l+r)>>1;
	if(clo<=mid) change(ls[k],l,mid,clo);
	else change(rs[k],mid+1,r,clo);
	up(k);
}
int merge(int k,int pre,int l,int r)
{
	if(!k||!pre)return k+pre;
	if(l==r)
	{
		sum[k]+=sum[pre];
		ans[k]=l;
		return k;
	}
	int mid=(l+r)>>1;
	ls[k]=merge(ls[k],ls[pre],l,mid);
	rs[k]=merge(rs[k],rs[pre],mid+1,r);
	up(k);
	return k;
}
int ansl[N];
void dfs(int now,int f)
{
	for(int i=0;i<va[now].size();i++)
	{
		int to=va[now][i];
		if(to==f)continue;
		dfs(to,now);
		root[now]=merge(root[now],root[to],1,n);
	}
	ansl[now]=ans[root[now]];
}
signed main()
{
	scanf("%lld",&n);
	for(int i=1,clo;i<=n;i++)
	{
		scanf("%lld",&clo);
		change(root[i],1,n,clo);
	}
	for(int i=1,u,v;i<=n-1;i++)
	{
		scanf("%lld%lld",&u,&v);
		va[u].push_back(v);
		va[v].push_back(u);
	}
	dfs(1,-1);
	for(int i=1;i<=n;i++)cout<<ansl[i]<<" ";
}
/*
3
2 3 4
1 2
1 3
*/


#include<bits/stdc++.h>
#define MAXN 100005
#define LL long long
using namespace std;
inline int read(){
	int s=0,w=0;char ch=getchar();
	while(ch<'0'||ch>'9')w|=(ch=='-'),ch=getchar();
	while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=getchar();
	return w?-s:s;
}
#define kd (read())
int n;
int c[MAXN];
struct rr{
	int nt,to;
}bl[MAXN<<1];int hd[MAXN],itot=0;
inline void adde(int x,int y){
	bl[++itot]=(rr){hd[x],y};hd[x]=itot;
	bl[++itot]=(rr){hd[y],x};hd[y]=itot;
	return ;
}
LL an[MAXN];
int root[MAXN],num=0;
int ls[MAXN*50],rs[MAXN*50],mx[MAXN*50];
LL sm[MAXN*50];
void insert(int &u,int l,int r,int p){
	if(!u)u=++num;
	sm[u]=p,mx[u]=1;
	if(l==r)return ;
	int mid=l+r>>1;
	if(p<=mid)insert(ls[u],l,mid,p);
	else insert(rs[u],mid+1,r,p);
	return ;
}
int merge(int x,int y,int l,int r){
	if(!x||!y)return x+y;
	if(l==r){
		mx[x]+=mx[y];
		return x;
	}int mid=l+r>>1;
	ls[x]=merge(ls[x],ls[y],l,mid);
	rs[x]=merge(rs[x],rs[y],mid+1,r);
	if(mx[ls[x]]==mx[rs[x]]){
		sm[x]=sm[ls[x]]+sm[rs[x]],mx[x]=mx[ls[x]];
	}else if(mx[ls[x]]>mx[rs[x]]){
		sm[x]=sm[ls[x]],mx[x]=mx[ls[x]];
	}else{
		sm[x]=sm[rs[x]],mx[x]=mx[rs[x]];
	}return x;
}
void dfs(int u,int fa){
	insert(root[u],1,n,c[u]);
	for(int i=hd[u],y;i;i=bl[i].nt)
		if((y=bl[i].to)!=fa){
			dfs(y,u);
			merge(root[u],root[y],1,n);
		}
	an[u]=sm[root[u]];
	return ;
}
int main(){
	n=kd;
	for(int i=1;i<=n;++i)c[i]=kd;
	for(int i=1,x,y;i<n;++i)
		x=kd,y=kd,adde(x,y);
	dfs(1,0);
	for(int i=1;i<=n;++i)printf("%lld ",an[i]);
	puts("");
	return 0;
}


#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lson tr[now].l
#define rson tr[now].r
#define int long long
using namespace std;

struct tree
{
    int l,r,sum,val,ans;
}tr[5000050];

int rt[100010],cl[100010],cnt,n,anss[100010];
vector<int> g[100010];

int push_up(int now)
{
    if(tr[lson].sum>tr[rson].sum)
    {
        tr[now].sum=tr[lson].sum;
        tr[now].ans=tr[lson].ans;
    }
    if(tr[rson].sum>tr[lson].sum)
    {
        tr[now].sum=tr[rson].sum;
        tr[now].ans=tr[rson].ans;
    }
    if(tr[lson].sum==tr[rson].sum)
    {
        tr[now].sum=tr[lson].sum;
        tr[now].ans=tr[lson].ans+tr[rson].ans;
    }
}
int update(int &now,int l,int r,int pos,int v)
{
    if(!now) now=++cnt;
    if(l==r)
    {
        tr[now].sum+=v;
        tr[now].ans=l;
        return 0;
    }
    int mid=(l+r)>>1;
    if(pos<=mid) update(lson,l,mid,pos,v);
    else update(rson,mid+1,r,pos,v);
    push_up(now);
}
int merge(int a,int b,int l,int r)
{
    if(!a) return b;
    if(!b) return a;
    int mid=(l+r)>>1;
    tr[a].l=merge(tr[a].l,tr[b].l,l,mid);
    tr[a].r=merge(tr[a].r,tr[b].r,mid+1,r);
    push_up(a);return a;
}

int dfs(int now,int f)
{
    for(int i=0;i<g[now].size();i++)
    {
        if(g[now][i]==f) continue;
        dfs(g[now][i],now);
        merge(rt[now],rt[g[now][i]],1,100000);
    }
    update(rt[now],1,100000,cl[now],1);
    anss[now]=tr[rt[now]].ans;
}
signed main()
{
    scanf("%lld",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&cl[i]);
        rt[i]=i;
        cnt++;
    }
    int from,to;
    for(int i=1;i<n;i++)
    {
        scanf("%lld%lld",&from,&to);
        g[from].push_back(to);
        g[to].push_back(from);
    }
    dfs(1,0);
    for(int i=1;i<=n;i++)
    {
        printf("%lld ",anss[i]);
    }
}


#include <cstdio>
#include <algorithm>
using namespace std;
#define N 200005
#define lint long long
struct edge{ lint des; lint next; }e[N<<1];
lint cnt; lint head[N];
void ins(lint u,lint v){ ++cnt; e[cnt].des=v; e[cnt].next=head[u]; head[u]=cnt; }
lint n; lint c[N]; lint num[N]; lint size[N]; lint son[N]; lint cur_son;
void dfs1(lint u,lint f){
	lint maxson=-1;  size[u]=1;
	for(lint i=head[u];i;i=e[i].next){
		lint v=e[i].des;
		if(v==f) continue;
		dfs1(v,u);
		size[u]+=size[v];
		if(size[v]>maxson) maxson=size[v],son[u]=v;
	}
}
lint sum; lint mx; lint ans[N];
void dfs3(lint u,lint f,lint val){
	num[c[u]]+=val;
	if(num[c[u]]>mx) mx=num[c[u]],sum=c[u];
	else if(num[c[u]]==mx)	 	 	 sum+=c[u];
	for(lint i=head[u];i;i=e[i].next){
		lint v=e[i].des;
		if(v==f) continue;
		if(v==cur_son) continue;
		dfs3(v,u,val);
	}
}

void dfs2(lint u,lint f,lint clear){
	for(lint i=head[u];i;i=e[i].next){
		lint v=e[i].des;
		if(v==f) continue;
		if(v==son[u]) continue;
		dfs2(v,u,1);
	}
	if(son[u]) dfs2(son[u],u,0),cur_son=son[u];
	dfs3(u,f,1); cur_son=0;
	ans[u]=sum;
	if(clear) dfs3(u,f,-1),sum=0,mx=0;
}
int main(){
	scanf("%lld",&n);
	for(lint i=1;i<=n;++i) scanf("%lld",c+i);
	for(lint i=1;i<n;++i){
		lint u; lint v; scanf("%lld %lld",&u,&v);
		ins(u,v); ins(v,u);
	}
	dfs1(1,-1);
	dfs2(1,-1,1);
	for(lint i=1;i<=n;++i) printf("%lld%c",ans[i],i==n?'\n':' ');
	return 0;
}


#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lson tr[now].l
#define rson tr[now].r
using namespace std;

struct tree
{
    int l,r,sum,val,ans;
}tr[100010<<5];

int rt[100010],cl[100010],cnt,n,anss[100010];
vector<int> g[100010];

void push_up(int now)
{
    if(tr[lson].sum>tr[rson].sum)
    {
        tr[now].sum=tr[lson].sum;
        tr[now].val=tr[lson].val;
        tr[now].ans=tr[lson].ans;
    }
    if(tr[rson].sum>tr[lson].sum)
    {
        tr[now].sum=tr[rson].sum;
        tr[now].val=tr[rson].val;
        tr[now].ans=tr[rson].ans;
    }
    if(tr[lson].sum==tr[rson].sum)
    {
        tr[now].sum=tr[lson].sum;
        tr[now].val=tr[lson].val;
        tr[now].ans=tr[lson].ans+tr[rson].ans;
    }
}

void update(int &now,int l,int r,int pos,int v)
{
    if(!now) now=++cnt;
    if(l==r)
    {
        tr[now].val=l;
        tr[now].sum+=v;
        tr[now].ans=l;
    }
    int mid=(l+r)>>1;
    if(pos<=mid) update(lson,l,mid,pos,v);
    else update(rson,mid+1,r,pos,v);
    push_up(now);
}

int merge(int a,int b,int l,int r)
{
    if(!a) return b;
    if(!b) return a;
    if(l==r)
    {
        tr[a].val=l;
        tr[a].sum+=tr[b].sum;
        tr[a].ans=l;
        return a;
    }
    int mid=(l+r)>>1;
    tr[a].l=merge(tr[a].l,tr[b].l,l,mid);
    tr[a].r=merge(tr[a].r,tr[b].r,mid+1,r);
    push_up(a);
    return a;
}

void dfs(int now,int f)
{
    for(int i=0;i<g[now].size();i++)
    {
        if(g[now][i]==f) continue;
        dfs(g[now][i],now);
        merge(rt[now],rt[g[now][i]],1,100000);
    }
    update(rt[now],1,100000,cl[now],1);
    anss[now]=tr[rt[now]].ans;
}

int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&cl[i]);
        rt[i]=i;
        cnt++;
    }
    int from,to;
    for(int i=1;i<n;i++)
    {
        scanf("%lld%lld",&from,&to);
        g[from].push_back(to);
        g[to].push_back(from);
    }
    dfs(1,0);
    for(int i=1;i<=n;i++)
    {
        printf("%lld ",anss[i]);
    }
    return 0;
}


#include<bits/stdc++.h>
#define maxn 1000005
#define maxe 20000005
using namespace std;
int rt[maxe],n,a[maxn],x,y,Ans[maxn],nxt[maxn],lnk[maxn],son[maxn],tot,sum[maxe],Mx[maxe],lc[maxe],rc[maxe];
inline int read(){
	int ret=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-f;ch=getchar();}
	while (ch<='9'&&ch>='0') ret=ret*10+ch-'0',ch=getchar();
	return ret*f;
}
inline void add(int x,int y){nxt[++tot]=lnk[x];lnk[x]=tot;son[tot]=y;}
inline void push_down(int k){
	if (sum[lc[k]]>sum[rc[k]]) sum[k]=sum[lc[k]],Mx[k]=Mx[lc[k]];
	else if (sum[lc[k]]<sum[rc[k]]) sum[k]=sum[rc[k]],Mx[k]=Mx[rc[k]];
	else sum[k]=sum[rc[k]],Mx[k]=Mx[rc[k]]+Mx[lc[k]];
}
inline void change(int &k,int L,int R){
	if (!k) k=++tot;
	if (L==R){sum[k]++,Mx[k]=x;return;}
	int mid=L+R>>1;
	if (x<=mid) change(lc[k],L,mid);
	else change(rc[k],mid+1,R);
	push_down(k);
}
inline int Merge(int x,int y,int L,int R){
	if (!x||!y) return x^y;
	if (L==R){sum[x]+=sum[y];return x;}
	int mid=L+R>>1;
	lc[x]=Merge(lc[x],lc[y],L,mid);
	rc[x]=Merge(rc[x],rc[y],mid+1,R);
	push_down(x);return x;
}
inline void Get(int x,int fa){
	for (int i=lnk[x];i;i=nxt[i])
	  if (son[i]!=fa) Get(son[i],x),rt[x]=Merge(rt[x],rt[son[i]],1,n);
	if (sum[rt[x]]>0) Ans[x]=Mx[rt[x]];
}
int main(){
	n=read();
	for (int i=1;i<=n;i++) x=read(),change(rt[i],1,n);tot=0;
	for (int i=1;i<n;i++) x=read(),y=read(),add(x,y),add(y,x);
	Get(1,0);
	for (int i=1;i<=n;i++) printf("%d ",Ans[i]);
	return 0;
}


#include<bits/stdc++.h>
#define rint register int
#define ll long long
using namespace std;
inline int read(){
	int s=0,f=1; char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=0;c=getchar();}
	while(c>='0'&&c<='9') s=(s<<1)+(s<<3)+(c^48),c=getchar();
	return f?s:-s;
}
int n,col[100010];
int tot,head[100010],ver[200010],nxt[200010];
void add(int x,int y){
	nxt[++tot]=head[x]; ver[tot]=y;
	head[x]=tot;
}
int Sz[100010],son[100010];
void dfs_first(int x,int fa){
	Sz[x]=1;
	for(rint i=head[x];i;i=nxt[i]){
		int y=ver[i];
		if(y==fa) continue;
		dfs_first(y,x);
		Sz[x]+=Sz[y];
		if(Sz[son[x]]<Sz[y]) son[x]=y;
	}
}
ll ap[100010],Max,Sum,ans[100010];
void dfs_Sum(int x,int fa,int Son){
	++ap[col[x]];
	if(ap[col[x]]>Max){
		Max=ap[col[x]];
		Sum=col[x];
	}
	else if(ap[col[x]]==Max) Sum+=col[x];
	for(rint i=head[x];i;i=nxt[i]){
		int y=ver[i];
		if(y==fa||y==Son) continue;
		dfs_Sum(y,x,Son);
	}
}
void Clear(int x,int fa){
	--ap[col[x]];
	for(rint i=head[x];i;i=nxt[i]){
		int y=ver[i];
		if(y==fa) continue;
		Clear(y,x);
	}
}
void dsu(int x,int fa){
	for(rint i=head[x];i;i=nxt[i]){
		int y=ver[i];
		if(y==fa||y==son[x]) continue;
		dsu(y,x); Clear(y,x);
		Sum=Max=0;
	}
	if(son[x]) dsu(son[x],x);
	dfs_Sum(x,fa,son[x]);
	ans[x]=Sum;
}
int main(){
	n=read();
	for(rint i=1;i<=n;++i) col[i]=read();
	for(rint i=1;i<n;++i){
		int x=read(),y=read();
		add(x,y); add(y,x);
	}
	dfs_first(1,0);
	dsu(1,0);
	for(rint i=1;i<=n;++i) printf("%lld ",ans[i]);
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#define smid (l+r>>1)
#define I inline
using namespace std;
typedef long long LL;
const int N=100010;
LL col[N];
LL maxcol;
int n;
int ls[N*40],rs[N*40],cnt,rt[N];
LL sum[N*40],ans[N*40];
vector<int>g[N];
LL out[N];

I void pushup(int now)
{
    if(sum[ls[now]]==sum[rs[now]])
    {
        sum[now]=sum[ls[now]];
        ans[now]=ans[ls[now]]+ans[rs[now]];
    }
    else if(sum[ls[now]]<sum[rs[now]])
    {
        sum[now]=sum[rs[now]];
        ans[now]=ans[rs[now]];
    }
    else
    {
        sum[now]=sum[ls[now]];
        ans[now]=ans[ls[now]];
    }
}

I void modify(int &now,int l,int r,int pos)
{
    if(!now)now=++cnt;
    if(l==r)
    {
        sum[now]++;ans[now]=l;
        return;
    }
    if(pos<=smid)modify(ls[now],l,smid,pos);
    else modify(rs[now],smid+1,r,pos);
    pushup(now);
}

I int merge(int x,int y,int l,int r)
{
    if(!x||!y)return x+y;
    if(l==r)
    {
        sum[x]+=sum[y];ans[x]=l;
        return x;
    }
    ls[x]=merge(ls[x],ls[y],l,smid);
    rs[x]=merge(rs[x],rs[y],smid+1,r);
    pushup(x);
    return x;
}

I void dfs(int u,int fa)
{
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(v==fa)continue;
        dfs(v,u);
        merge(rt[u],rt[v],1,100000);
    }
    modify(rt[u],1,100000,col[u]);
    out[u]=ans[rt[u]];
}

int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>col[i];
        rt[i]=i;cnt++;
    }
    for(int i=1;i<n;i++)
    {
        int x,y;cin>>x>>y;
        g[x].push_back(y);g[y].push_back(x);
    }
    dfs(1,0);
    for(int i=1;i<=n;i++)
    {
        cout<<out[i]<<" ";
    }
}


#include <cstdio>

#define R register
const int MAXN=1e5+10;

int n;
int c[MAXN];

struct edge
{
	int to,next;
}e[MAXN*2];
int cnt,head[MAXN];

inline void add(int x,int y)
{
	cnt++;
	e[cnt].to=y;
	e[cnt].next=head[x];
	head[x]=cnt;
}

int siz[MAXN],son[MAXN];

inline void get(int x,int f)
{
	siz[x]=1;
	son[x]=0;
	for(R int i=head[x];i;i=e[i].next)
	{
		int y=e[i].to;
		if(y==f) continue;
		get(y,x);
		siz[x]+=siz[y];
		if(siz[y]>siz[son[x]]) son[x]=y;
	}
}

inline void Init()
{
	scanf("%d",&n);
	for(R int i=1;i<=n;i++)	scanf("%d",&c[i]);
	for(R int i=1;i<n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		add(x,y);add(y,x);
	}
}

int ans[MAXN];
int sum,mx;
int tong[MAXN];

inline void change(int x,int f,int g)
{
	tong[c[x]]++;
	if(tong[c[x]]>mx)
	{
		mx=tong[c[x]];
		sum=c[x];
	}
	else
		if(tong[c[x]]==mx)
			sum+=c[x];
	for(R int i=head[x];i;i=e[i].next)
	{
		int y=e[i].to;
		if(y==f||y==g) continue;
		change(y,x,g);
	}
}

inline void del(int x,int f)
{
	tong[c[x]]--;
	for(R int i=head[x];i;i=e[i].next)
	{
		int y=e[i].to;
		if(y==f) continue;
		del(y,x);
	}
}

inline void dfs(int x,int f,int opt)
{
	for(R int i=head[x];i;i=e[i].next)
	{
		int y=e[i].to;
		if(y==f||y==son[x]) continue;
		dfs(y,x,0);
	}
	if(son[x]) dfs(son[x],x,1);
	change(x,f,son[x]);ans[x]=sum;
	if(opt==0)
	{
		del(x,f);
		sum=0;
		mx=0;
	}
}

int main()
{
	Init();
	get(1,0);
	dfs(1,0,1);
	for(R int i=1;i<=n;i++)
		printf("%d ",ans[i]);
	return 0;
}


#include<stdio.h>
#include<queue>
#include<cstring>
#include<vector>
#include<algorithm>
#define re register
#define oo 0x7f7f7f7f
#define O_O 100010
#define int long long
using namespace std;
inline int read()
{
    re int x=0,f=1;
    re char c=getchar();
    while(c<'0'||c>'9')
    {
        if(c=='-') f=-1;
        c=getchar();
    }
    while('0'<=c&&c<='9')
    {
        x=(x<<1)+(x<<3)+(c^48);
        c=getchar();
    }
    return x*f;
}

int n;
int rt[O_O],co[O_O],ans[O_O];
int end[O_O];
int head[O_O],cnt,tot;
struct node
{
	int to,next;
}ed[O_O];

inline void add(int u,int v)
{
	ed[++cnt].to=v;
	ed[cnt].next=head[u];
	head[u]=cnt;
}

namespace SEG
{
	#define lson ls[u]
	#define rson rs[u]

	int val[O_O*50],sum[O_O*50],ls[O_O*50],rs[O_O*50];

	int up(int u)
	{
		if(sum[lson]>sum[rson])
		{
			sum[u]=sum[lson];
			val[u]=val[lson];
			ans[u]=ans[lson];
		}
		if(sum[rson]>sum[lson])
		{
			sum[u]=sum[rson];
			val[u]=val[rson];
			ans[u]=ans[rson];
		}
		if(sum[lson]==sum[rson])
		{
			sum[u]=sum[lson];
			val[u]=val[lson];
			ans[u]=ans[lson]+ans[rson];
		}

	}

	int update(int &u,int l,int r,int pos,int v)
	{
		if(!u) u=++tot;
		if(l==r)
		{
			val[u]=l;
			sum[u]+=v;
			ans[u]=l;
			return 0;
		}
		re int mid=(l+r)>>1;
		if(pos<=mid) update(lson,l,mid,pos,v);
		else update(rson,mid+1,r,pos,v);
		up(u);
	}

	int merge(int a,int b,int l,int r)
	{
		if(!a||!b) return a+b;
		if(l==r)
		{
			val[a]=l;
			sum[a]+=sum[b];
			ans[a]=l;
			return a;
		}
		re int mid=(l+r)>>1;
		ls[a]=merge(ls[a],ls[b],l,mid);
		rs[a]=merge(rs[a],rs[b],mid+1,r);
		up(a);
		return a;
	}

	void dfs(int u,int fa)
	{
		for(re int i=head[u];i;i=ed[i].next)
		{
			re int v=ed[i].to;
			if(v==fa) continue;
			dfs(v,u);
			merge(rt[u],rt[v],1,100000);
		}
		update(rt[u],1,100000,co[u],1);
		end[u]=ans[rt[u]];
	}
}
using namespace SEG;

signed main()
{
	n=read();
	for(re int i=1;i<=n;++i)
	{
		co[i]=read();
		rt[i]=i;
		tot++;
	}
	int a,b;
	for(re int i=1;i<n;++i)
	{
		a=read(); b=read();
		add(a,b); add(b,a);
	}
	dfs(1,0);
	for(re int i=1;i<=n;++i) printf("%lld ",end[i]);
}


#include<iostream>
#include<string.h>
#include<algorithm>
#include<cstdio>
using namespace std;
const int Max = 1e5 + 5;
struct E
{
	int to, ne;
}edge[Max * 2];
int head[Max],tot;
inline void addnode(int u, int v)
{
	edge[tot].to = v;
	edge[tot].ne = head[u];
	head[u] = tot++;
}
int sz[Max], wson[Max];
void dfs(int u, int f)
{
	sz[Max] = 1;
	int maxsz = -1;
	for (int i = head[u]; ~i; i = edge[i].ne)
	{
		int v = edge[i].to;
		if (v == f)continue;
		dfs(v, u);
		sz[u] += sz[v];
		if (sz[v] > sz[wson[u]])
		{
			wson[u] = v;
		}
	}
}
int col[Max], cnt[Max];
long long ans[Max], sum;
int flag, maxc;
void count(int u, int f, int val)
{
	cnt[col[u]] += val;
	if (cnt[col[u]] > maxc)
	{
		maxc = cnt[col[u]];
		sum = col[u];
	}
	else if (maxc == cnt[col[u]])
	{
		sum += col[u];
	}
	for (int i = head[u]; ~i; i = edge[i].ne)
	{
		int v = edge[i].to;
		if (v == f || v == flag)continue;//不能写成v == wson[u];
		count(v, u, val);
	}
}
void dfs(int u, int f, bool keep)
{
	for (int i = head[u]; ~i; i = edge[i].ne)
	{
		int v = edge[i].to;
		if (v == f || v == wson[u])continue;
		dfs(v, u, false);
	}
	if (wson[u])
	{
		dfs(wson[u], u, true);
		flag = wson[u];
	}
	count(u, f, 1);
	flag = 0;
	ans[u] = sum;
	if (!keep)
	{
		count(u, f, -1);
		sum = maxc = 0;
	}
}

int main()
{
	ios::sync_with_stdio(false);
	int n, a, b;
	memset(head, -1, sizeof(head));
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> col[i];
	for (int i = 1; i <= n - 1; i++)
	{
		cin >> a >> b;
		addnode(a, b);
		addnode(b, a);
	}
	dfs(1, 0);
	dfs(1, 0, 0);
	for (int i = 1; i <= n; i++)
		cout << ans[i];
	return 0;
}