3189. Lomsat gelral
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
const double eps=1e-7;
#define x first
#define y second
#define int long long
#define lb long double
#define pb push_back
#define endl '\n'
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define rep(i,x,n) for(int i=x;i<=n;i++)
#define dwn(i,n,x) for(int i=n;i>=x;i--)
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
int Mod(int a,int mod){return (a%mod+mod)%mod;}
int lowbit(int x){return x&-x;}//最低位1及其后面的0构成的数值
int qmi(int a, int k, int p){int res = 1 % p;while (k){if (k & 1) res = Mod(res * a , p);a = Mod(a * a , p);k >>= 1;}return res;}
int inv(int a,int mod){return qmi(a,mod-2,mod);}
const int N=100010,M=N*2;
int n;
int h[N],e[M],ne[M],idx;
int color[N],cnt[N],sz[N],son[N];
int ans[N],sum;
int maxv;
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
int dfs_son(int u,int father)//轻重链剖分
{
sz[u]=1;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(j==father)continue;
sz[u]+=dfs_son(j,u);
if(sz[j]>sz[son[u]])son[u]=j;
}
return sz[u];
}
void update(int u,int father,int sign,int pson)
{
int c=color[u];
cnt[c]+=sign;
if(cnt[c]>maxv)maxv=cnt[c],sum=c;
else if(cnt[c]==maxv)sum+=c;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(j==father||j==pson)continue;
update(j,u,sign,pson);
}
}
void dfs(int u,int father,int op)//暴力统计轻边的贡献,op==0表示递归完成后消除对该点的影响
{
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(j==father||j==son[u])continue;
dfs(j,u,0);
}
if(son[u])dfs(son[u],u,1);
update(u,father,1,son[u]);
ans[u]=sum;
if(!op)update(u,father,-1,0),sum=maxv=0;
}
void solve()
{
cin>>n;
rep(i,1,n)cin>>color[i];
memset(h,-1,sizeof h);
rep(i,0,n-2)
{
int a,b;
cin>>a>>b;
add(a,b),add(b,a);
}
dfs_son(1,-1);
dfs(1,-1,1);
rep(i,1,n)cout<<ans[i]<<' ';
}
signed main()
{
io;
int _;_=1;
// cin>>_;
while(_--)solve();
return 0;
}