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

#define loop(i, n) for(int i = 0; i < n; ++i)
#define pb push_back
#define paii pair<int, pair<int, int> >
#define l first
#define r second.first
#define IND second.second
#define int long long

const int MAXN = 1e5 + 1, SZ_BL = 500, CNT_BL = 201; //!!!

int n;
vector<int> graf[MAXN];
int color[MAXN];

bool cmp(paii & a, paii & b) {
    if ((a.l / SZ_BL) < (b.l / SZ_BL)) return true;
    if ((a.l / SZ_BL) > (b.l / SZ_BL)) return false;
    if (a.r > b.r) return true;
    return false;
}

void read() {
    cin >> n;
    loop(i, n) cin >> color[i];
    loop(i, n - 1) {
        int p1, p2;
        cin >> p1 >> p2;
        --p1, --p2;
        graf[p1].pb(p2);
        graf[p2].pb(p1);
    }
}

vector<int> Euler;
int TIN[MAXN], TOUT[MAXN];
int timer = 0;

void Euler_path(int p1, int pr = -1) {
    Euler.pb(color[p1]);
    TIN[p1] = timer;
    timer++;
    loop (i, graf[p1].size()) {
        int p2 = graf[p1][i];
        if (p2 == pr) continue;
        Euler_path(p2, p1);
    }
    Euler.pb(-color[p1]);
    TOUT[p1] = timer;
    timer++;
}

vector<paii> quer;

int CNT[MAXN], CNT_CL[MAXN];
int mx = 0;

void push_right(int &R, int &r0) {
    for (int i = R + 1; i <= r0; ++i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]++;
        int temp = CNT_CL[Euler[i]];
        CNT[temp] += Euler[i];
        if (temp > mx) mx = temp;
    }
    R = r0;
}

void push_left(int &L, int &l0) {
    for (int i = L - 1; i >= l0; --i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]++;
        int temp = CNT_CL[Euler[i]];
        CNT[temp] += Euler[i];
        if (temp > mx) mx = temp;
    }
    L = l0;
}

void delete_left(int &L, int &l0) {
    for (int i = L; i < l0; ++i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]--;
        int temp = CNT_CL[Euler[i]];
        CNT[temp + 1] -= Euler[i];
        if (CNT[temp + 1] == 0) mx--;
    }
    L = l0;
}

void delete_right(int &R, int &r0) {
    for (int i = R; i > r0; --i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]--;
        int temp = CNT_CL[Euler[i]];
        CNT[temp + 1] -= Euler[i];
        if (CNT[temp + 1] == 0) mx--;
    }
    R = r0;
}

void solve() {
    Euler_path(0);
    /*loop (i, Euler.size()) {
        if (Euler[i] >= 0) cout << Euler[i] + 0 << " ";
        else cout << Euler[i] - 0 << " ";
    }*/
    quer.resize(n);
    loop(i, n) {
        quer[i].l = TIN[i];
        quer[i].r = TOUT[i];
        quer[i].IND = i;
    }
    sort(quer.begin(), quer.end(), cmp);
    //for (int i = 0; i < n; ++i) cout <<quer[i].l << " " << quer[i].r << endl;

    vector<int> ans(n);
    int pr = -1, L = 0, R = -1;
    for (int i = 0; i < n; ++i) {
        if(R < quer[i].r) push_right(R, quer[i].r);
        if(L > quer[i].l) push_left(L, quer[i].l);
        if(R > quer[i].r) delete_right(R, quer[i].r);
        if(L < quer[i].l) delete_left(L, quer[i].l);
        //for (int h = 0; h < 5; ++h) cout << CNT[h] << " ";
        //cout << endl;
        ans[quer[i].IND] = CNT[mx];
    }
    loop (i, n) cout << ans[i] << " ";
}

signed main() {
    read();
    solve();
}
/*
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 loop(i, n) for(int i = 0; i < n; ++i)
#define pb push_back
#define paii pair<int, pair<int, int> >
#define l first
#define r second.first
#define IND second.second
#define int long long

const int MAXN = 1e5 + 1, SZ_BL = 320, CNT_BL = 320; //!!!

int n;
vector<int> graf[MAXN];
int color[MAXN];

bool cmp(paii & a, paii & b) {
    if ((a.l / SZ_BL) < (b.l / SZ_BL)) return true;
    if ((a.l / SZ_BL) > (b.l / SZ_BL)) return false;
    if (a.r > b.r) return true;
    return false;
}

void read() {
    cin >> n;
    loop(i, n) cin >> color[i];
    loop(i, n - 1) {
        int p1, p2;
        cin >> p1 >> p2;
        --p1, --p2;
        graf[p1].pb(p2);
        graf[p2].pb(p1);
    }
}

vector<int> Euler;
int TIN[MAXN], TOUT[MAXN];
int timer = 0;

void Euler_path(int p1, int pr = -1) {
    Euler.pb(color[p1]);
    TIN[p1] = timer;
    timer++;
    loop (i, graf[p1].size()) {
        int p2 = graf[p1][i];
        if (p2 == pr) continue;
        Euler_path(p2, p1);
    }
    Euler.pb(-color[p1]);
    TOUT[p1] = timer;
    timer++;
}

vector<paii> quer;

int CNT[MAXN], CNT_CL[MAXN];
int mx = 0;

void push_right(int &R, int &r0) {
    for (int i = R + 1; i <= r0; ++i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]++;
        int temp = CNT_CL[Euler[i]];
        CNT[temp] += Euler[i];
        if (temp > mx) mx = temp;
    }
    R = r0;
}

void push_left(int &L, int &l0) {
    for (int i = L - 1; i >= l0; --i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]++;
        int temp = CNT_CL[Euler[i]];
        CNT[temp] += Euler[i];
        if (temp > mx) mx = temp;
    }
    L = l0;
}

void delete_left(int &L, int &l0) {
    for (int i = L; i < l0; ++i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]--;
        int temp = CNT_CL[Euler[i]];
        CNT[temp + 1] -= Euler[i];
        if (CNT[temp + 1] == 0) mx--;
    }
    L = l0;
}

void delete_right(int &R, int &r0) {
    for (int i = R; i > r0; --i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]--;
        int temp = CNT_CL[Euler[i]];
        CNT[temp + 1] -= Euler[i];
        if (CNT[temp + 1] == 0) mx--;
    }
    R = r0;
}

void solve() {
    Euler_path(0);
    /*loop (i, Euler.size()) {
        if (Euler[i] >= 0) cout << Euler[i] + 0 << " ";
        else cout << Euler[i] - 0 << " ";
    }*/
    quer.resize(n);
    loop(i, n) {
        quer[i].l = TIN[i];
        quer[i].r = TOUT[i];
        quer[i].IND = i;
    }
    sort(quer.begin(), quer.end(), cmp);
    //for (int i = 0; i < n; ++i) cout <<quer[i].l << " " << quer[i].r << endl;

    vector<int> ans(n);
    int pr = -1, L = 0, R = -1;
    for (int i = 0; i < n; ++i) {
        if(R < quer[i].r) push_right(R, quer[i].r);
        if(L > quer[i].l) push_left(L, quer[i].l);
        if(R > quer[i].r) delete_right(R, quer[i].r);
        if(L < quer[i].l) delete_left(L, quer[i].l);
        //for (int h = 0; h < 5; ++h) cout << CNT[h] << " ";
        //cout << endl;
        ans[quer[i].IND] = CNT[mx];
    }
    loop (i, n) cout << ans[i] << " ";
}

signed main() {
    read();
    solve();
}
/*
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 loop(i, n) for(int i = 0; i < n; ++i)
#define pb push_back
#define paii pair<int, pair<int, int> >
#define l first
#define r second.first
#define IND second.second
#define int long long

const int MAXN = 1e5 + 1, SZ_BL = 1750, CNT_BL = 58; //!!!

int n;
vector<int> graf[MAXN];
int color[MAXN];

bool cmp(paii & a, paii & b) {
    if ((a.l / SZ_BL) < (b.l / SZ_BL)) return true;
    if ((a.l / SZ_BL) > (b.l / SZ_BL)) return false;
    if (a.r > b.r) return true;
    return false;
}

void read() {
    cin >> n;
    loop(i, n) cin >> color[i];
    loop(i, n - 1) {
        int p1, p2;
        cin >> p1 >> p2;
        --p1, --p2;
        graf[p1].pb(p2);
        graf[p2].pb(p1);
    }
}

vector<int> Euler;
int TIN[MAXN], TOUT[MAXN];
int timer = 0;

void Euler_path(int p1, int pr = -1) {
    Euler.pb(color[p1]);
    TIN[p1] = timer;
    timer++;
    loop (i, graf[p1].size()) {
        int p2 = graf[p1][i];
        if (p2 == pr) continue;
        Euler_path(p2, p1);
    }
    Euler.pb(-color[p1]);
    TOUT[p1] = timer;
    timer++;
}

vector<paii> quer;

int CNT[MAXN], CNT_CL[MAXN];
int mx = 0;

void push_right(int &R, int &r0) {
    for (int i = R + 1; i <= r0; ++i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]++;
        int temp = CNT_CL[Euler[i]];
        CNT[temp] += Euler[i];
        if (temp > mx) mx = temp;
    }
    R = r0;
}

void push_left(int &L, int &l0) {
    for (int i = L - 1; i >= l0; --i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]++;
        int temp = CNT_CL[Euler[i]];
        CNT[temp] += Euler[i];
        if (temp > mx) mx = temp;
    }
    L = l0;
}

void delete_left(int &L, int &l0) {
    for (int i = L; i < l0; ++i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]--;
        int temp = CNT_CL[Euler[i]];
        CNT[temp + 1] -= Euler[i];
        if (CNT[temp + 1] == 0) mx--;
    }
    L = l0;
}

void delete_right(int &R, int &r0) {
    for (int i = R; i > r0; --i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]--;
        int temp = CNT_CL[Euler[i]];
        CNT[temp + 1] -= Euler[i];
        if (CNT[temp + 1] == 0) mx--;
    }
    R = r0;
}

void solve() {
    Euler_path(0);
    /*loop (i, Euler.size()) {
        if (Euler[i] >= 0) cout << Euler[i] + 0 << " ";
        else cout << Euler[i] - 0 << " ";
    }*/
    quer.resize(n);
    loop(i, n) {
        quer[i].l = TIN[i];
        quer[i].r = TOUT[i];
        quer[i].IND = i;
    }
    sort(quer.begin(), quer.end(), cmp);
    //for (int i = 0; i < n; ++i) cout <<quer[i].l << " " << quer[i].r << endl;

    vector<int> ans(n);
    int pr = -1, L = 0, R = -1;
    for (int i = 0; i < n; ++i) {
        if(R < quer[i].r) push_right(R, quer[i].r);
        if(L > quer[i].l) push_left(L, quer[i].l);
        if(R > quer[i].r) delete_right(R, quer[i].r);
        if(L < quer[i].l) delete_left(L, quer[i].l);
        //for (int h = 0; h < 5; ++h) cout << CNT[h] << " ";
        //cout << endl;
        ans[quer[i].IND] = CNT[mx];
    }
    loop (i, n) cout << ans[i] << " ";
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    read();
    solve();
}
/*
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>

#pragma GCC optimize ("O3")
#pragma GCC target ("avx2")
//#pragma GCC target (sse, sse2, sse3, ssse3, sse4,popcnt,tune=native)
//#pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)

#define FOR(i, s, e) for(int i=s; i<e; i++)
#define loop(i, n) for(int i=0; i<n; i++)
#define CIN   ios_base::sync_with_stdio(0); cin.tie(0)
#define getint(n) scanf("%d", &n)
#define pb(a) push_back(a)
#define ll long long int
#define ll unsigned long long int
#define ld long double

#define SZ(a) (int)(a.size())
#define Read() freopen("input.txt", "r", stdin)
#define Write() freopen("output.txt", "w", stdout)
#define mem(a, v) memset(a, v, sizeof(a))
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define Unique(x)  x.erase(unique(all(x)), x.end())
//#define pi acos(-1.0)
#define pf printf
#define sf scanf
#define vec vector
#define mp make_pair
#define paii pair<int, int>
#define padd pair<dd, dd>
#define pall pair<ll, ll>
#define fr first
#define sc second
//#define endl "\n"
#define CASE(n) printf("Case %d: ",++n)
#define CASE_COUT cout<<"Case "<<++cas<<": "
#define vec vector

using namespace std;

#define int long long

const int  MAXN = 1e5+ 47;

int n;
int a[MAXN];
vec<int> graf[MAXN];

void read() {
    cin >> n;
    loop(i, n) cin >> a[i];
    loop(i, n-1) {
        int p1, p2;
        cin >> p1 >> p2;
        p1--, p2--;
        graf[p1].pb(p2);
        graf[p2].pb(p1);
    }
}

vec<int> sz(MAXN, 0), tin(MAXN, 0), tout(MAXN, 0), rem(2*MAXN, MAXN-1);

int timer = 0;
void get_sz(int p1, int pr) {
    tin[p1] = timer, rem[timer] = p1;
    sz[p1]++, timer++;
    for(int p2 : graf[p1]) {
        if(p2 == pr) continue;
        get_sz(p2, p1);
        sz[p1] +=sz[p2];
    }
    tout[p1] = timer;
    timer++;
}

vec<int> cnt(MAXN, 0), ans(MAXN, 0), kek(MAXN, 0);
int GR = 0;

void dfs(int p1, int pr, bool x) {
    int mx = -1, big = -1;
    for(int p2: graf[p1]) {
        if(p2 == pr) continue;
        if(sz[p2] > mx) mx = sz[p2], big = p2;
    }

    for(int p2 : graf[p1]) {
        if(p2 == pr || p2 == big) continue;
        dfs(p2, p1, 0);
    }
   // int GR = 0;
  //  if(x == 1) GR = gr;
    if(big != -1) dfs(big, p1, 1);
    for(int p2 : graf[p1]) {
        if(p2 == pr || p2 == big) continue;
        for(int p = tin[p2]; p <= tout[p2]; ++p) {
            if(rem[p] == MAXN - 1) continue;
            ans[cnt[a[rem[p]]]] -= (a[rem[p]]);
            cnt[a[rem[p]]]++;
            ans[cnt[a[rem[p]]]] += (a[rem[p]]);
            if(cnt[a[rem[p]]] > GR) GR = cnt[a[rem[p]]];
        }
    }
    ans[cnt[a[p1]]] -=(a[p1]);
    cnt[a[p1]]++;
    ans[cnt[a[p1]]] +=(a[p1]);
     if(cnt[a[p1]] > GR) GR = cnt[a[p1]];
    //cout << p1 << ": " << big << " " << GR << endl << "ans: ";
    //loop(i, n+1) cout << ans[i] << " ";
   // loop(i, n) if(ans[i] > 0) kek[p1] = ans[i];
    //cout << ": " << kek[p1];
    //cout << endl;
    kek[p1] = ans[GR];

    if(x == 0) {
        for(int p2 : graf[p1]) {
            if(p2 == pr) continue;
            for(int p = tin[p2]; p <= tout[p2]; ++p) {
                if(rem[p] == MAXN - 1) continue;
                ans[cnt[a[rem[p]]]] -= (a[rem[p]]);
                cnt[a[rem[p]]]--;
                ans[cnt[a[rem[p]]]] += (a[rem[p]]);
               //  if(ans[cnt[a[rem[p]]]] == 0) GR--;
            }
        }
        ans[cnt[a[p1]]] -=(a[p1]);
        cnt[a[p1]]--;
        ans[cnt[a[p1]]] +=(a[p1]);
        //if(ans[cnt[a[p1]]] == 0) GR--;
       GR = 0;
    }// else gr = GR;
    //loop(i, n+1) cout << ans[i] << " ";
    //cout << endl;
}

void solve(){
    get_sz(0, -1);
    //loop(i, 2*n) cout << rem[i] << " ";
    //cout << endl;

    loop(i, n) ans[0] += i + 1;
    dfs(0, -1, 1);
    loop(i, n) cout << kek[i] << " ";
    cout << endl;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(20);

    read();
    solve();

    /*int t;
    cin >> t;
    while(t--) {
        read();
        solve();
    }*/
}
/*
4
1 2 3 4
1 2
2 3
2 4

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 loop(i, n) for(int i = 0; i < n; ++i)
#define pb push_back
#define paii pair<int, pair<int, int> >
#define l first
#define r second.first
#define IND second.second
#define int long long

const int MAXN = 1e5 + 1, SZ_BL = 1200, CNT_BL = 85; //!!!

int n;
vector<int> graf[MAXN];
int color[MAXN];

bool cmp(paii & a, paii & b) {
    if ((a.l / SZ_BL) < (b.l / SZ_BL)) return true;
    if ((a.l / SZ_BL) > (b.l / SZ_BL)) return false;
    if (a.r > b.r) return true;
    return false;
}

void read() {
    cin >> n;
    loop(i, n) cin >> color[i];
    loop(i, n - 1) {
        int p1, p2;
        cin >> p1 >> p2;
        --p1, --p2;
        graf[p1].pb(p2);
        graf[p2].pb(p1);
    }
}

vector<int> Euler;
int TIN[MAXN], TOUT[MAXN];
int timer = 0;

void Euler_path(int p1, int pr = -1) {
    Euler.pb(color[p1]);
    TIN[p1] = timer;
    timer++;
    loop (i, graf[p1].size()) {
        int p2 = graf[p1][i];
        if (p2 == pr) continue;
        Euler_path(p2, p1);
    }
    Euler.pb(-color[p1]);
    TOUT[p1] = timer;
    timer++;
}

vector<paii> quer;

int CNT[MAXN], CNT_CL[MAXN];
int mx = 0;

void push_right(int &R, int &r0) {
    for (int i = R + 1; i <= r0; ++i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]++;
        int temp = CNT_CL[Euler[i]];
        CNT[temp] += Euler[i];
        if (temp > mx) mx = temp;
    }
    R = r0;
}

void push_left(int &L, int &l0) {
    for (int i = L - 1; i >= l0; --i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]++;
        int temp = CNT_CL[Euler[i]];
        CNT[temp] += Euler[i];
        if (temp > mx) mx = temp;
    }
    L = l0;
}

void delete_left(int &L, int &l0) {
    for (int i = L; i < l0; ++i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]--;
        int temp = CNT_CL[Euler[i]];
        CNT[temp + 1] -= Euler[i];
        if (CNT[temp + 1] == 0) mx--;
    }
    L = l0;
}

void delete_right(int &R, int &r0) {
    for (int i = R; i > r0; --i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]--;
        int temp = CNT_CL[Euler[i]];
        CNT[temp + 1] -= Euler[i];
        if (CNT[temp + 1] == 0) mx--;
    }
    R = r0;
}

void solve() {
    Euler_path(0);
    /*loop (i, Euler.size()) {
        if (Euler[i] >= 0) cout << Euler[i] + 0 << " ";
        else cout << Euler[i] - 0 << " ";
    }*/
    quer.resize(n);
    loop(i, n) {
        quer[i].l = TIN[i];
        quer[i].r = TOUT[i];
        quer[i].IND = i;
    }
    sort(quer.begin(), quer.end(), cmp);
    //for (int i = 0; i < n; ++i) cout <<quer[i].l << " " << quer[i].r << endl;

    vector<int> ans(n);
    int pr = -1, L = 0, R = -1;
    for (int i = 0; i < n; ++i) {
        if(R < quer[i].r) push_right(R, quer[i].r);
        if(L > quer[i].l) push_left(L, quer[i].l);
        if(R > quer[i].r) delete_right(R, quer[i].r);
        if(L < quer[i].l) delete_left(L, quer[i].l);
        //for (int h = 0; h < 5; ++h) cout << CNT[h] << " ";
        //cout << endl;
        ans[quer[i].IND] = CNT[mx];
    }
    loop (i, n) cout << ans[i] << " ";
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    read();
    solve();
}
/*
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>

#pragma GCC optimize ("O3")
#pragma GCC target ("avx2")
//#pragma GCC target (sse, sse2, sse3, ssse3, sse4,popcnt,tune=native)
//#pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)

#define FOR(i, s, e) for(int i=s; i<e; i++)
#define loop(i, n) for(int i=0; i<n; i++)
#define CIN   ios_base::sync_with_stdio(0); cin.tie(0)
#define getint(n) scanf("%d", &n)
#define pb(a) push_back(a)
#define ll long long int
#define ll unsigned long long int
#define ld long double

#define SZ(a) (int)(a.size())
#define Read() freopen("input.txt", "r", stdin)
#define Write() freopen("output.txt", "w", stdout)
#define mem(a, v) memset(a, v, sizeof(a))
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define Unique(x)  x.erase(unique(all(x)), x.end())
//#define pi acos(-1.0)
#define pf printf
#define sf scanf
#define vec vector
#define mp make_pair
#define paii pair<int, int>
#define padd pair<dd, dd>
#define pall pair<ll, ll>
#define fr first
#define sc second
//#define endl "\n"
#define CASE(n) printf("Case %d: ",++n)
#define CASE_COUT cout<<"Case "<<++cas<<": "
#define vec vector

using namespace std;

//#define int long long

const int  MAXN = 1e5+ 47;

int n;
int a[MAXN];
vec<int> graf[MAXN];

void read() {
    cin >> n;
    loop(i, n) cin >> a[i];
    loop(i, n-1) {
        int p1, p2;
        cin >> p1 >> p2;
        p1--, p2--;
        graf[p1].pb(p2);
        graf[p2].pb(p1);
    }
}

vec<int> sz(MAXN, 0), tin(MAXN, 0), tout(MAXN, 0), rem(2*MAXN, MAXN-1);

int timer = 0;
void get_sz(int p1, int pr) {
    tin[p1] = timer, rem[timer] = p1;
    sz[p1]++, timer++;
    for(int p2 : graf[p1]) {
        if(p2 == pr) continue;
        get_sz(p2, p1);
        sz[p1] +=sz[p2];
    }
    tout[p1] = timer;
    timer++;
}

vec<int> cnt(MAXN, 0), ans(MAXN, 0), kek(MAXN, 0);
int GR = 0;

void dfs(int p1, int pr, bool x) {
    int mx = -1, big = -1;
    for(int p2: graf[p1]) {
        if(p2 == pr) continue;
        if(sz[p2] > mx) mx = sz[p2], big = p2;
    }

    for(int p2 : graf[p1]) {
        if(p2 == pr || p2 == big) continue;
        dfs(p2, p1, 0);
    }
   // int GR = 0;
  //  if(x == 1) GR = gr;
    if(big != -1) dfs(big, p1, 1);
    for(int p2 : graf[p1]) {
        if(p2 == pr || p2 == big) continue;
        for(int p = tin[p2]; p <= tout[p2]; ++p) {
            if(rem[p] == MAXN - 1) continue;
            ans[cnt[a[rem[p]]]] -= (a[rem[p]]);
            cnt[a[rem[p]]]++;
            ans[cnt[a[rem[p]]]] += (a[rem[p]]);
            if(cnt[a[rem[p]]] > GR) GR = cnt[a[rem[p]]];
        }
    }
    ans[cnt[a[p1]]] -=(a[p1]);
    cnt[a[p1]]++;
    ans[cnt[a[p1]]] +=(a[p1]);
     if(cnt[a[p1]] > GR) GR = cnt[a[p1]];
    //cout << p1 << ": " << big << " " << GR << endl << "ans: ";
    //loop(i, n+1) cout << ans[i] << " ";
   // loop(i, n) if(ans[i] > 0) kek[p1] = ans[i];
    //cout << ": " << kek[p1];
    //cout << endl;
    kek[p1] = ans[GR];

    if(x == 0) {
        for(int p2 : graf[p1]) {
            if(p2 == pr) continue;
            for(int p = tin[p2]; p <= tout[p2]; ++p) {
                if(rem[p] == MAXN - 1) continue;
                ans[cnt[a[rem[p]]]] -= (a[rem[p]]);
                cnt[a[rem[p]]]--;
                ans[cnt[a[rem[p]]]] += (a[rem[p]]);
               //  if(ans[cnt[a[rem[p]]]] == 0) GR--;
            }
        }
        ans[cnt[a[p1]]] -=(a[p1]);
        cnt[a[p1]]--;
        ans[cnt[a[p1]]] +=(a[p1]);
        //if(ans[cnt[a[p1]]] == 0) GR--;
       GR = 0;
    }// else gr = GR;
    //loop(i, n+1) cout << ans[i] << " ";
    //cout << endl;
}

void solve(){
    get_sz(0, -1);
    //loop(i, 2*n) cout << rem[i] << " ";
    //cout << endl;

    loop(i, n) ans[0] += i + 1;
    dfs(0, -1, 1);
    loop(i, n) cout << kek[i] << " ";
    cout << endl;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(20);

    read();
    solve();

    /*int t;
    cin >> t;
    while(t--) {
        read();
        solve();
    }*/
}
/*
4
1 2 3 4
1 2
2 3
2 4

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 loop(i, n) for(int i = 0; i < n; ++i)
#define pb push_back
#define paii pair<int, pair<int, int> >
#define l first
#define r second.first
#define IND second.second
#define int long long

const int MAXN = 1e5 + 1, SZ_BL = 1500, CNT_BL = 68; //!!!

int n;
vector<int> graf[MAXN];
int color[MAXN];

bool cmp(paii & a, paii & b) {
    if ((a.l / SZ_BL) < (b.l / SZ_BL)) return true;
    if ((a.l / SZ_BL) > (b.l / SZ_BL)) return false;
    if (a.r > b.r) return true;
    return false;
}

void read() {
    cin >> n;
    loop(i, n) cin >> color[i];
    loop(i, n - 1) {
        int p1, p2;
        cin >> p1 >> p2;
        --p1, --p2;
        graf[p1].pb(p2);
        graf[p2].pb(p1);
    }
}

vector<int> Euler;
int TIN[MAXN], TOUT[MAXN];
int timer = 0;

void Euler_path(int p1, int pr = -1) {
    Euler.pb(color[p1]);
    TIN[p1] = timer;
    timer++;
    loop (i, graf[p1].size()) {
        int p2 = graf[p1][i];
        if (p2 == pr) continue;
        Euler_path(p2, p1);
    }
    Euler.pb(-color[p1]);
    TOUT[p1] = timer;
    timer++;
}

vector<paii> quer;

int CNT[MAXN], CNT_CL[MAXN];
int mx = 0;

void push_right(int &R, int &r0) {
    for (int i = R + 1; i <= r0; ++i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]++;
        int temp = CNT_CL[Euler[i]];
        CNT[temp] += Euler[i];
        if (temp > mx) mx = temp;
    }
    R = r0;
}

void push_left(int &L, int &l0) {
    for (int i = L - 1; i >= l0; --i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]++;
        int temp = CNT_CL[Euler[i]];
        CNT[temp] += Euler[i];
        if (temp > mx) mx = temp;
    }
    L = l0;
}

void delete_left(int &L, int &l0) {
    for (int i = L; i < l0; ++i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]--;
        int temp = CNT_CL[Euler[i]];
        CNT[temp + 1] -= Euler[i];
        if (CNT[temp + 1] == 0) mx--;
    }
    L = l0;
}

void delete_right(int &R, int &r0) {
    for (int i = R; i > r0; --i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]--;
        int temp = CNT_CL[Euler[i]];
        CNT[temp + 1] -= Euler[i];
        if (CNT[temp + 1] == 0) mx--;
    }
    R = r0;
}

void solve() {
    Euler_path(0);
    /*loop (i, Euler.size()) {
        if (Euler[i] >= 0) cout << Euler[i] + 0 << " ";
        else cout << Euler[i] - 0 << " ";
    }*/
    quer.resize(n);
    loop(i, n) {
        quer[i].l = TIN[i];
        quer[i].r = TOUT[i];
        quer[i].IND = i;
    }
    sort(quer.begin(), quer.end(), cmp);
    //for (int i = 0; i < n; ++i) cout <<quer[i].l << " " << quer[i].r << endl;

    vector<int> ans(n);
    int pr = -1, L = 0, R = -1;
    for (int i = 0; i < n; ++i) {
        if(R < quer[i].r) push_right(R, quer[i].r);
        if(L > quer[i].l) push_left(L, quer[i].l);
        if(R > quer[i].r) delete_right(R, quer[i].r);
        if(L < quer[i].l) delete_left(L, quer[i].l);
        //for (int h = 0; h < 5; ++h) cout << CNT[h] << " ";
        //cout << endl;
        ans[quer[i].IND] = CNT[mx];
    }
    loop (i, n) cout << ans[i] << " ";
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    read();
    solve();
}
/*
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 loop(i, n) for(int i = 0; i < n; ++i)
#define pb push_back
#define paii pair<int, pair<int, int> >
#define l first
#define r second.first
#define IND second.second
#define int long long

const int MAXN = 1e5 + 1, SZ_BL = 1000, CNT_BL = 101; //!!!

int n;
vector<int> graf[MAXN];
int color[MAXN];

bool cmp(paii & a, paii & b) {
    if ((a.l / SZ_BL) < (b.l / SZ_BL)) return true;
    if ((a.l / SZ_BL) > (b.l / SZ_BL)) return false;
    if (a.r > b.r) return true;
    return false;
}

void read() {
    cin >> n;
    loop(i, n) cin >> color[i];
    loop(i, n - 1) {
        int p1, p2;
        cin >> p1 >> p2;
        --p1, --p2;
        graf[p1].pb(p2);
        graf[p2].pb(p1);
    }
}

vector<int> Euler;
int TIN[MAXN], TOUT[MAXN];
int timer = 0;

void Euler_path(int p1, int pr = -1) {
    Euler.pb(color[p1]);
    TIN[p1] = timer;
    timer++;
    loop (i, graf[p1].size()) {
        int p2 = graf[p1][i];
        if (p2 == pr) continue;
        Euler_path(p2, p1);
    }
    Euler.pb(-color[p1]);
    TOUT[p1] = timer;
    timer++;
}

vector<paii> quer;

int CNT[MAXN], CNT_CL[MAXN];
int mx = 0;

void push_right(int &R, int &r0) {
    for (int i = R + 1; i <= r0; ++i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]++;
        int temp = CNT_CL[Euler[i]];
        CNT[temp] += Euler[i];
        if (temp > mx) mx = temp;
    }
    R = r0;
}

void push_left(int &L, int &l0) {
    for (int i = L - 1; i >= l0; --i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]++;
        int temp = CNT_CL[Euler[i]];
        CNT[temp] += Euler[i];
        if (temp > mx) mx = temp;
    }
    L = l0;
}

void delete_left(int &L, int &l0) {
    for (int i = L; i < l0; ++i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]--;
        int temp = CNT_CL[Euler[i]];
        CNT[temp + 1] -= Euler[i];
        if (CNT[temp + 1] == 0) mx--;
    }
    L = l0;
}

void delete_right(int &R, int &r0) {
    for (int i = R; i > r0; --i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]--;
        int temp = CNT_CL[Euler[i]];
        CNT[temp + 1] -= Euler[i];
        if (CNT[temp + 1] == 0) mx--;
    }
    R = r0;
}

void solve() {
    Euler_path(0);
    /*loop (i, Euler.size()) {
        if (Euler[i] >= 0) cout << Euler[i] + 0 << " ";
        else cout << Euler[i] - 0 << " ";
    }*/
    quer.resize(n);
    loop(i, n) {
        quer[i].l = TIN[i];
        quer[i].r = TOUT[i];
        quer[i].IND = i;
    }
    sort(quer.begin(), quer.end(), cmp);
    //for (int i = 0; i < n; ++i) cout <<quer[i].l << " " << quer[i].r << endl;

    vector<int> ans(n);
    int pr = -1, L = 0, R = -1;
    for (int i = 0; i < n; ++i) {
        if(R < quer[i].r) push_right(R, quer[i].r);
        if(L > quer[i].l) push_left(L, quer[i].l);
        if(R > quer[i].r) delete_right(R, quer[i].r);
        if(L < quer[i].l) delete_left(L, quer[i].l);
        //for (int h = 0; h < 5; ++h) cout << CNT[h] << " ";
        //cout << endl;
        ans[quer[i].IND] = CNT[mx];
    }
    loop (i, n) cout << ans[i] << " ";
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    read();
    solve();
}
/*
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 loop(i, n) for(int i = 0; i < n; ++i)
#define pb push_back
#define paii pair<int, pair<int, int> >
#define l first
#define r second.first
#define IND second.second
#define int long long

const int MAXN = 1e5 + 1, SZ_BL = 201, CNT_BL = 500; //!!!

int n;
vector<int> graf[MAXN];
int color[MAXN];

bool cmp(paii & a, paii & b) {
    if ((a.l / SZ_BL) < (b.l / SZ_BL)) return true;
    if ((a.l / SZ_BL) > (b.l / SZ_BL)) return false;
    if (a.r > b.r) return true;
    return false;
}

void read() {
    cin >> n;
    loop(i, n) cin >> color[i];
    loop(i, n - 1) {
        int p1, p2;
        cin >> p1 >> p2;
        --p1, --p2;
        graf[p1].pb(p2);
        graf[p2].pb(p1);
    }
}

vector<int> Euler;
int TIN[MAXN], TOUT[MAXN];
int timer = 0;

void Euler_path(int p1, int pr = -1) {
    Euler.pb(color[p1]);
    TIN[p1] = timer;
    timer++;
    loop (i, graf[p1].size()) {
        int p2 = graf[p1][i];
        if (p2 == pr) continue;
        Euler_path(p2, p1);
    }
    Euler.pb(-color[p1]);
    TOUT[p1] = timer;
    timer++;
}

vector<paii> quer;

int CNT[MAXN], CNT_CL[MAXN];
int mx = 0;

void push_right(int &R, int &r0) {
    for (int i = R + 1; i <= r0; ++i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]++;
        int temp = CNT_CL[Euler[i]];
        CNT[temp] += Euler[i];
        if (temp > mx) mx = temp;
    }
    R = r0;
}

void push_left(int &L, int &l0) {
    for (int i = L - 1; i >= l0; --i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]++;
        int temp = CNT_CL[Euler[i]];
        CNT[temp] += Euler[i];
        if (temp > mx) mx = temp;
    }
    L = l0;
}

void delete_left(int &L, int &l0) {
    for (int i = L; i < l0; ++i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]--;
        int temp = CNT_CL[Euler[i]];
        CNT[temp + 1] -= Euler[i];
        if (CNT[temp + 1] == 0) mx--;
    }
    L = l0;
}

void delete_right(int &R, int &r0) {
    for (int i = R; i > r0; --i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]--;
        int temp = CNT_CL[Euler[i]];
        CNT[temp + 1] -= Euler[i];
        if (CNT[temp + 1] == 0) mx--;
    }
    R = r0;
}

void solve() {
    Euler_path(0);
    /*loop (i, Euler.size()) {
        if (Euler[i] >= 0) cout << Euler[i] + 0 << " ";
        else cout << Euler[i] - 0 << " ";
    }*/
    quer.resize(n);
    loop(i, n) {
        quer[i].l = TIN[i];
        quer[i].r = TOUT[i];
        quer[i].IND = i;
    }
    sort(quer.begin(), quer.end(), cmp);
    //for (int i = 0; i < n; ++i) cout <<quer[i].l << " " << quer[i].r << endl;

    vector<int> ans(n);
    int pr = -1, L = 0, R = -1;
    for (int i = 0; i < n; ++i) {
        if(R < quer[i].r) push_right(R, quer[i].r);
        if(L > quer[i].l) push_left(L, quer[i].l);
        if(R > quer[i].r) delete_right(R, quer[i].r);
        if(L < quer[i].l) delete_left(L, quer[i].l);
        //for (int h = 0; h < 5; ++h) cout << CNT[h] << " ";
        //cout << endl;
        ans[quer[i].IND] = CNT[mx];
    }
    loop (i, n) cout << ans[i] << " ";
}

signed main() {
    read();
    solve();
}
/*
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 loop(i, n) for(int i = 0; i < n; ++i)
#define pb push_back
#define paii pair<int, pair<int, int> >
#define l first
#define r second.first
#define IND second.second
#define int long long

const int MAXN = 1e5 + 1, SZ_BL = 2000, CNT_BL = 51; //!!!

int n;
vector<int> graf[MAXN];
int color[MAXN];

bool cmp(paii & a, paii & b) {
    if ((a.l / SZ_BL) < (b.l / SZ_BL)) return true;
    if ((a.l / SZ_BL) > (b.l / SZ_BL)) return false;
    if (a.r > b.r) return true;
    return false;
}

void read() {
    cin >> n;
    loop(i, n) cin >> color[i];
    loop(i, n - 1) {
        int p1, p2;
        cin >> p1 >> p2;
        --p1, --p2;
        graf[p1].pb(p2);
        graf[p2].pb(p1);
    }
}

vector<int> Euler;
int TIN[MAXN], TOUT[MAXN];
int timer = 0;

void Euler_path(int p1, int pr = -1) {
    Euler.pb(color[p1]);
    TIN[p1] = timer;
    timer++;
    loop (i, graf[p1].size()) {
        int p2 = graf[p1][i];
        if (p2 == pr) continue;
        Euler_path(p2, p1);
    }
    Euler.pb(-color[p1]);
    TOUT[p1] = timer;
    timer++;
}

vector<paii> quer;

int CNT[MAXN], CNT_CL[MAXN];
int mx = 0;

void push_right(int &R, int &r0) {
    for (int i = R + 1; i <= r0; ++i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]++;
        int temp = CNT_CL[Euler[i]];
        CNT[temp] += Euler[i];
        if (temp > mx) mx = temp;
    }
    R = r0;
}

void push_left(int &L, int &l0) {
    for (int i = L - 1; i >= l0; --i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]++;
        int temp = CNT_CL[Euler[i]];
        CNT[temp] += Euler[i];
        if (temp > mx) mx = temp;
    }
    L = l0;
}

void delete_left(int &L, int &l0) {
    for (int i = L; i < l0; ++i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]--;
        int temp = CNT_CL[Euler[i]];
        CNT[temp + 1] -= Euler[i];
        if (CNT[temp + 1] == 0) mx--;
    }
    L = l0;
}

void delete_right(int &R, int &r0) {
    for (int i = R; i > r0; --i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]--;
        int temp = CNT_CL[Euler[i]];
        CNT[temp + 1] -= Euler[i];
        if (CNT[temp + 1] == 0) mx--;
    }
    R = r0;
}

void solve() {
    Euler_path(0);
    /*loop (i, Euler.size()) {
        if (Euler[i] >= 0) cout << Euler[i] + 0 << " ";
        else cout << Euler[i] - 0 << " ";
    }*/
    quer.resize(n);
    loop(i, n) {
        quer[i].l = TIN[i];
        quer[i].r = TOUT[i];
        quer[i].IND = i;
    }
    sort(quer.begin(), quer.end(), cmp);
    //for (int i = 0; i < n; ++i) cout <<quer[i].l << " " << quer[i].r << endl;

    vector<int> ans(n);
    int pr = -1, L = 0, R = -1;
    for (int i = 0; i < n; ++i) {
        if(R < quer[i].r) push_right(R, quer[i].r);
        if(L > quer[i].l) push_left(L, quer[i].l);
        if(R > quer[i].r) delete_right(R, quer[i].r);
        if(L < quer[i].l) delete_left(L, quer[i].l);
        //for (int h = 0; h < 5; ++h) cout << CNT[h] << " ";
        //cout << endl;
        ans[quer[i].IND] = CNT[mx];
    }
    loop (i, n) cout << ans[i] << " ";
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    read();
    solve();
}
/*
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>

//#pragma GCC optimize ("O3")
//#pragma GCC target ("avx2")
//#pragma GCC target (sse, sse2, sse3, ssse3, sse4,popcnt,tune=native)
//#pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)

#define FOR(i, s, e) for(int i=s; i<e; i++)
#define loop(i, n) for(int i=0; i<n; i++)
#define CIN   ios_base::sync_with_stdio(0); cin.tie(0)
#define getint(n) scanf("%d", &n)
#define pb(a) push_back(a)
#define ll long long int
#define ll unsigned long long int
#define ld long double

#define SZ(a) (int)(a.size())
#define Read() freopen("input.txt", "r", stdin)
#define Write() freopen("output.txt", "w", stdout)
#define mem(a, v) memset(a, v, sizeof(a))
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define Unique(x)  x.erase(unique(all(x)), x.end())
//#define pi acos(-1.0)
#define pf printf
#define sf scanf
#define vec vector
#define mp make_pair
#define paii pair<int, int>
#define padd pair<dd, dd>
#define pall pair<ll, ll>
#define fr first
#define sc second
//#define endl "\n"
#define CASE(n) printf("Case %d: ",++n)
#define CASE_COUT cout<<"Case "<<++cas<<": "
#define vec vector

using namespace std;

//#define int long long

const int  MAXN = 1e5+ 47;

int n;
int a[MAXN];
vec<int> graf[MAXN];

void read() {
    cin >> n;
    loop(i, n) cin >> a[i];
    loop(i, n-1) {
        int p1, p2;
        cin >> p1 >> p2;
        p1--, p2--;
        graf[p1].pb(p2);
        graf[p2].pb(p1);
    }
}

vec<int> sz(MAXN, 0), tin(MAXN, 0), tout(MAXN, 0), rem(2*MAXN, MAXN-1);

int timer = 0;
void get_sz(int p1, int pr) {
    tin[p1] = timer, rem[timer] = p1;
    sz[p1]++, timer++;
    for(int p2 : graf[p1]) {
        if(p2 == pr) continue;
        get_sz(p2, p1);
        sz[p1] +=sz[p2];
    }
    tout[p1] = timer;
    timer++;
}

vec<int> cnt(MAXN, 0), ans(MAXN, 0), kek(MAXN, 0);
int gr = 0;

void dfs(int p1, int pr, bool x) {
    int mx = -1, big = -1;
    for(int p2: graf[p1]) {
        if(p2 == pr) continue;
        if(sz[p2] > mx) mx = sz[p2], big = p2;
    }

    for(int p2 : graf[p1]) {
        if(p2 == pr || p2 == big) continue;
        dfs(p2, p1, 0);
    }
   // int GR = 0;
  //  if(x == 1) GR = gr;
    if(big != -1) dfs(big, p1, 1);
    for(int p2 : graf[p1]) {
        if(p2 == pr || p2 == big) continue;
        for(int p = tin[p2]; p <= tout[p2]; ++p) {
            if(rem[p] == MAXN - 1) continue;
            ans[cnt[a[rem[p]]]] -= (a[rem[p]]);
            cnt[a[rem[p]]]++;
            ans[cnt[a[rem[p]]]] += (a[rem[p]]);
           // if(cnt[a[rem[p]]] > GR) GR = cnt[a[rem[p]]];
        }
    }
    ans[cnt[a[p1]]] -=(a[p1]);
    cnt[a[p1]]++;
    ans[cnt[a[p1]]] +=(a[p1]);
    // if(cnt[a[p1]] > GR) GR = cnt[a[p1]];
    //cout << p1 << ": " << big << " " << GR << endl << "ans: ";
    //loop(i, n+1) cout << ans[i] << " ";
    loop(i, n) if(ans[i] > 0) kek[p1] = ans[i];
    //cout << ": " << kek[p1];
    //cout << endl;
   // kek[p1] = ans[GR];

    if(x == 0) {
        for(int p2 : graf[p1]) {
            if(p2 == pr) continue;
            for(int p = tin[p2]; p <= tout[p2]; ++p) {
                if(rem[p] == MAXN - 1) continue;
                ans[cnt[a[rem[p]]]] -= (a[rem[p]]);
                cnt[a[rem[p]]]--;
                ans[cnt[a[rem[p]]]] += (a[rem[p]]);
                //if(ans[cnt[a[rem[p]]]] == 0) GR--;
            }
        }
        ans[cnt[a[p1]]] -=(a[p1]);
        cnt[a[p1]]--;
        ans[cnt[a[p1]]] +=(a[p1]);
        //if(ans[cnt[a[p1]]] == 0) GR--;
    }// else gr = GR;
    //loop(i, n+1) cout << ans[i] << " ";
    //cout << endl;
}

void solve(){
    get_sz(0, -1);
    //loop(i, 2*n) cout << rem[i] << " ";
    //cout << endl;

    loop(i, n) ans[0] += i + 1;
    dfs(0, -1, 1);
    loop(i, n) cout << kek[i] << " ";
    cout << endl;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(20);

    read();
    solve();

    /*int t;
    cin >> t;
    while(t--) {
        read();
        solve();
    }*/
}
/*
4
1 2 3 4
1 2
2 3
2 4

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 loop(i, n) for(int i = 0; i < n; ++i)
#define pb push_back
#define paii pair<int, pair<int, int> >
#define l first
#define r second.first
#define IND second.second
#define int long long

const int MAXN = 1e5 + 1, SZ_BL = 1000, CNT_BL = 101; //!!!

int n;
vector<int> graf[MAXN];
int color[MAXN];

bool cmp(paii & a, paii & b) {
    if ((a.l / SZ_BL) < (b.l / SZ_BL)) return true;
    if ((a.l / SZ_BL) > (b.l / SZ_BL)) return false;
    if (a.r > b.r) return true;
    return false;
}

void read() {
    cin >> n;
    loop(i, n) cin >> color[i];
    loop(i, n - 1) {
        int p1, p2;
        cin >> p1 >> p2;
        --p1, --p2;
        graf[p1].pb(p2);
        graf[p2].pb(p1);
    }
}

vector<int> Euler;
int TIN[MAXN], TOUT[MAXN];
int timer = 0;

void Euler_path(int p1, int pr = -1) {
    Euler.pb(color[p1]);
    TIN[p1] = timer;
    timer++;
    loop (i, graf[p1].size()) {
        int p2 = graf[p1][i];
        if (p2 == pr) continue;
        Euler_path(p2, p1);
    }
    Euler.pb(-color[p1]);
    TOUT[p1] = timer;
    timer++;
}

vector<paii> quer;

int CNT[MAXN], CNT_CL[MAXN];
int mx = 0;

void push_right(int &R, int &r0) {
    for (int i = R + 1; i <= r0; ++i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]++;
        int temp = CNT_CL[Euler[i]];
        CNT[temp] += Euler[i];
        if (temp > mx) mx = temp;
    }
    R = r0;
}

void push_left(int &L, int &l0) {
    for (int i = L - 1; i >= l0; --i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]++;
        int temp = CNT_CL[Euler[i]];
        CNT[temp] += Euler[i];
        if (temp > mx) mx = temp;
    }
    L = l0;
}

void delete_left(int &L, int &l0) {
    for (int i = L; i < l0; ++i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]--;
        int temp = CNT_CL[Euler[i]];
        CNT[temp + 1] -= Euler[i];
        if (CNT[temp + 1] == 0) mx--;
    }
    L = l0;
}

void delete_right(int &R, int &r0) {
    for (int i = R; i > r0; --i) {
        if (Euler[i] < 0) continue;
        CNT_CL[Euler[i]]--;
        int temp = CNT_CL[Euler[i]];
        CNT[temp + 1] -= Euler[i];
        if (CNT[temp + 1] == 0) mx--;
    }
    R = r0;
}

void solve() {
    Euler_path(0);
    /*loop (i, Euler.size()) {
        if (Euler[i] >= 0) cout << Euler[i] + 0 << " ";
        else cout << Euler[i] - 0 << " ";
    }*/
    quer.resize(n);
    loop(i, n) {
        quer[i].l = TIN[i];
        quer[i].r = TOUT[i];
        quer[i].IND = i;
    }
    sort(quer.begin(), quer.end(), cmp);
    //for (int i = 0; i < n; ++i) cout <<quer[i].l << " " << quer[i].r << endl;

    vector<int> ans(n);
    int pr = -1, L = 0, R = -1;
    for (int i = 0; i < n; ++i) {
        if(R < quer[i].r) push_right(R, quer[i].r);
        if(L > quer[i].l) push_left(L, quer[i].l);
        if(R > quer[i].r) delete_right(R, quer[i].r);
        if(L < quer[i].l) delete_left(L, quer[i].l);
        //for (int h = 0; h < 5; ++h) cout << CNT[h] << " ";
        //cout << endl;
        ans[quer[i].IND] = CNT[mx];
    }
    loop (i, n) cout << ans[i] << " ";
}

signed main() {
    read();
    solve();
}
/*
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 all(c) c.begin(),c.end()
#define rep(i,a,b) for(int i=a;i<=b;++i)
typedef long long ll ;
typedef vector<int> vi;
#define mod 1000000007
vi c;vector<vi>g;int n;
vi ans;
vector<multiset<int>>t;
set<int>st;
void dfs(int s,int p){

for(auto to:g[s]){
  if(to==p) continue;
  dfs(to,s);
   if(t[s].size()<t[to].size())
   swap(t[s],t[to]);
   for(auto p:t[to])
   t[s].insert(p);
   t[to].clear();
}
t[s].insert(c[s]);
int v=0;
for(auto p:st)
v=max(v,int(t[s].count(p)));
for(auto p:st){
    if(t[s].count(p)!=0&&t[s].count(p)>=v) ans[s]+=p;

}

}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//ios::sync_with_stdio(0);
//cin.tie(0);

cin>>n;
c.resize(n);
g.resize(n);
t.resize(n);
rep(i,0,n-1) cin>>c[i],st.insert(c[i]);
rep(i,1,n-1){
  int a,b;
  cin>>a>>b;
  --a;--b;
  g[a].push_back(b);
  g[b].push_back(a);
}
ans.resize(n);
dfs(0,-1);
for(auto m:ans) cout<<m<<" ";

    return 0;
}


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

#define all(c) c.begin(),c.end()
#define rep(i,a,b) for(int i=a;i<=b;++i)
typedef long long ll ;
typedef vector<int> vi;
#define mod 1000000007
vi c;vector<vi>g;int n;
vi ans;
vector<multiset<int>>t;
set<int>st;
void dfs(int s,int p){

for(auto to:g[s]){
  if(to==p) continue;
  dfs(to,s);

   for(auto p:t[to])
   t[s].insert(p);
}
t[s].insert(c[s]);
int v=0;
for(auto p:st)
v=max(v,int(t[s].count(p)));
for(auto p:st){
    if(t[s].count(p)!=0&&t[s].count(p)>=v) ans[s]+=p;

}

}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//ios::sync_with_stdio(0);
//cin.tie(0);

cin>>n;
c.resize(n);
g.resize(n);
t.resize(n);
rep(i,0,n-1) cin>>c[i],st.insert(c[i]);
rep(i,1,n-1){
  int a,b;
  cin>>a>>b;
  --a;--b;
  g[a].push_back(b);
  g[b].push_back(a);
}
ans.resize(n);
dfs(0,-1);
for(auto m:ans) cout<<m<<" ";

    return 0;
}


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

#define all(c) c.begin(),c.end()
#define rep(i,a,b) for(int i=a;i<=b;++i)
typedef long long ll ;
typedef vector<int> vi;
#define mod 1000000007
vi c;vector<vi>g;int n;
vi ans;
vector<multiset<int>>t;
set<int>st;
void dfs(int s,int p){

for(auto to:g[s]){
  if(to==p) continue;
  dfs(to,s);

   for(auto p:t[to])
   t[s].insert(p);
}
t[s].insert(c[s]);
int v=0;
for(auto p:st)
v=max(v,int(t[s].count(p)));
for(auto p:st){
    if(t[s].count(p)!=0&&t[s].count(p)<=v) ans[s]+=p;

}

}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//ios::sync_with_stdio(0);
//cin.tie(0);

cin>>n;
c.resize(n);
g.resize(n);
t.resize(n);
rep(i,0,n-1) cin>>c[i],st.insert(c[i]);
rep(i,1,n-1){
  int a,b;
  cin>>a>>b;
  --a;--b;
  g[a].push_back(b);
  g[b].push_back(a);
}
ans.resize(n);
dfs(0,-1);
for(auto m:ans) cout<<m<<" ";

    return 0;
}


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

#define all(c) c.begin(),c.end()
#define rep(i,a,b) for(int i=a;i<=b;++i)
typedef long long ll ;
typedef vector<int> vi;
#define mod 1000000007
vi c;vector<vi>g;int n;
vector<ll> ans;
vector<map<int,int>>t;
pair<ll,ll> dfs(int s,int p){
  ll sum=c[s]; int maxn=1;
  t[s][c[s]]=1;
for(auto to:g[s]){
  if(to==p) continue;
    auto no=dfs(to,s);
   // maxn=no.first;
   if(t[s].size()<t[to].size())
   {swap(t[s],t[to]);
   maxn=no.first;
   sum=no.second;

   }
   for(auto p:t[to])
  {t[s][p.first]+=p.second;
    if(t[s][p.first]==maxn)
    sum+=p.first;
    if(t[s][p.first]>maxn){
      sum=p.first;
      maxn=t[s][p.first];
    }
  }

   t[to].clear();
}
ans[s]=sum;

return {maxn,sum};

}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);

cin>>n;
c.resize(n);
g.resize(n);
t.resize(n);
rep(i,0,n-1) cin>>c[i];
rep(i,1,n-1){
  int a,b;
  cin>>a>>b;
  --a;--b;
  g[a].push_back(b);
  g[b].push_back(a);
}
ans.resize(n);
dfs(0,-1);
for(auto m:ans) cout<<m<<" ";

    return 0;
}


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

#define all(c) c.begin(),c.end()
#define rep(i,a,b) for(int i=a;i<=b;++i)
typedef long long ll ;
typedef vector<int> vi;
#define mod 1000000007
vi c;vector<vi>g;int n;
vi ans;
vector<multiset<int>>t;
set<int>st;
void dfs(int s,int p){

for(auto to:g[s]){
  if(to==p) continue;
  dfs(to,s);
   if(t[s].size()<t[to].size())
   swap(t[s],t[to]);
   for(auto p:t[to])
   t[s].insert(p);
   t[to].clear();
}
t[s].insert(c[s]);
int v=0;
for(auto p:st)
v=max(v,int(t[s].count(p)));
for(auto p:st){
    if(t[s].count(p)!=0&&t[s].count(p)>=v) ans[s]+=p;

}

}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);

cin>>n;
c.resize(n);
g.resize(n);
t.resize(n);
rep(i,0,n-1) cin>>c[i],st.insert(c[i]);
rep(i,1,n-1){
  int a,b;
  cin>>a>>b;
  --a;--b;
  g[a].push_back(b);
  g[b].push_back(a);
}
ans.resize(n);
dfs(0,-1);
for(auto m:ans) cout<<m<<" ";

    return 0;
}


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

#define all(c) c.begin(),c.end()
#define rep(i,a,b) for(int i=a;i<=b;++i)
typedef long long ll ;
typedef vector<int> vi;
#define mod 1000000007
vi c;vector<vi>g;int n;
vi ans;
vector<unordered_map<int,int>>t;
unordered_set<int>st;
void dfs(int s,int p){

for(auto to:g[s]){
  if(to==p) continue;
  dfs(to,s);
   if(t[s].size()<t[to].size())
   swap(t[s],t[to]);
   for(auto p:t[to])
   t[s][p.first]+=t[to][p.first];
   t[to].clear();
}
t[s][c[s]]++;
int v=0;
for(auto p:t[s])
v=max(v,p.second);
for(auto p:t[s])
if(p.second==v) ans[s]+=p.first;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);

cin>>n;
c.resize(n);
g.resize(n);
t.resize(n);
rep(i,0,n-1) cin>>c[i],st.insert(c[i]);
rep(i,1,n-1){
  int a,b;
  cin>>a>>b;
  --a;--b;
  g[a].push_back(b);
  g[b].push_back(a);
}
ans.resize(n);
dfs(0,-1);
for(auto m:ans) cout<<m<<" ";

    return 0;
}


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

#define all(c) c.begin(),c.end()
#define rep(i,a,b) for(int i=a;i<=b;++i)
typedef long long ll ;
typedef vector<int> vi;
#define mod 1000000007
vi c;vector<vi>g;int n;
vector<ll> ans;
vector<map<int,int>>t;
void dfs(int s,int p){

for(auto to:g[s]){
  if(to==p) continue;
  dfs(to,s);
   if(t[s].size()<t[to].size())
   swap(t[s],t[to]);
   for(auto p:t[to])
   if(t[to].count(p.first))
   t[s][p.first]+=t[to][p.first];
   //t[to].clear();
}
t[s][c[s]]++;
int v=0;
for(auto p:t[s])
v=max(v,p.second);
for(auto p:t[s])
if(p.second==v) ans[s]+=p.first;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);

cin>>n;
c.resize(n);
g.resize(n);
t.resize(n);
rep(i,0,n-1) cin>>c[i];
rep(i,1,n-1){
  int a,b;
  cin>>a>>b;
  --a;--b;
  g[a].push_back(b);
  g[b].push_back(a);
}
ans.resize(n);
dfs(0,-1);
for(auto m:ans) cout<<m<<" ";

    return 0;
}


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

#define all(c) c.begin(),c.end()
#define rep(i,a,b) for(int i=a;i<=b;++i)
typedef long long ll ;
typedef vector<int> vi;
#define mod 1000000007
vi c;vector<vi>g;int n;
vi ans;
vector<unordered_multiset<int>>t;
unordered_set<int>st;
void dfs(int s,int p){

for(auto to:g[s]){
  if(to==p) continue;
  dfs(to,s);
   if(t[s].size()<t[to].size())
   swap(t[s],t[to]);
   for(auto p:t[to])
   t[s].insert(p);
   t[to].clear();
}
t[s].insert(c[s]);
int v=0;
for(auto p:st)
v=max(v,int(t[s].count(p)));
for(auto p:st){
    if(t[s].count(p)!=0&&t[s].count(p)>=v) ans[s]+=p;

}

}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);

cin>>n;
c.resize(n);
g.resize(n);
t.resize(n);
rep(i,0,n-1) cin>>c[i],st.insert(c[i]);
rep(i,1,n-1){
  int a,b;
  cin>>a>>b;
  --a;--b;
  g[a].push_back(b);
  g[b].push_back(a);
}
ans.resize(n);
dfs(0,-1);
for(auto m:ans) cout<<m<<" ";

    return 0;
}


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

#define all(c) c.begin(),c.end()
#define rep(i,a,b) for(int i=a;i<=b;++i)
typedef long long ll ;
typedef vector<int> vi;
#define mod 1000000007
vi c;vector<vi>g;int n;
vector<ll> ans;
vector<unordered_map<int,int>>t;
void dfs(int s,int p){

for(auto to:g[s]){
  if(to==p) continue;
  dfs(to,s);
   if(t[s].size()<t[to].size())
   swap(t[s],t[to]);
   for(auto p:t[to])
   t[s][p.first]+=t[to][p.first];
   t[to].clear();
}
t[s][c[s]]++;
int v=0;
for(auto p:t[s])
v=max(v,p.second);
for(auto p:t[s])
if(p.second==v) ans[s]+=p.first;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);

cin>>n;
c.resize(n);
g.resize(n);
t.resize(n);
rep(i,0,n-1) cin>>c[i];
rep(i,1,n-1){
  int a,b;
  cin>>a>>b;
  --a;--b;
  g[a].push_back(b);
  g[b].push_back(a);
}
ans.resize(n);
dfs(0,-1);
for(auto m:ans) cout<<m<<" ";

    return 0;
}


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

#define all(c) c.begin(),c.end()
#define rep(i,a,b) for(int i=a;i<=b;++i)
typedef long long ll ;
typedef vector<int> vi;
#define mod 1000000007
vi c;vector<vi>g;int n;
vector<ll> ans;
vector<map<int,int>>t;
void dfs(int s,int p){

for(auto to:g[s]){
  if(to==p) continue;
  dfs(to,s);
   if(t[s].size()<t[to].size())
   swap(t[s],t[to]);
   for(auto p:t[to])
   t[s][p.first]+=t[to][p.first];
   //t[to].clear();
}
t[s][c[s]]++;
int v=0;
for(auto p:t[s])
v=max(v,p.second);
for(auto p:t[s])
if(p.second==v) ans[s]+=p.first;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);

cin>>n;
c.resize(n);
g.resize(n);
t.resize(n);
rep(i,0,n-1) cin>>c[i];
rep(i,1,n-1){
  int a,b;
  cin>>a>>b;
  --a;--b;
  g[a].push_back(b);
  g[b].push_back(a);
}
ans.resize(n);
dfs(0,-1);
for(auto m:ans) cout<<m<<" ";

    return 0;
}


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

#define all(c) c.begin(),c.end()
#define rep(i,a,b) for(int i=a;i<=b;++i)
typedef long long ll ;
typedef vector<int> vi;
#define mod 1000000007
vi c;vector<vi>g;int n;
vi ans;
vector<multiset<int>>t;
set<int>st;
void dfs(int s,int p){

for(auto to:g[s]){
  if(to==p) continue;
  dfs(to,s);

   for(auto p:t[to])
   t[s].insert(p);
   t[to].clear();
}
t[s].insert(c[s]);
int v=0;
for(auto p:st)
v=max(v,int(t[s].count(p)));
for(auto p:st){
    if(t[s].count(p)!=0&&t[s].count(p)>=v) ans[s]+=p;

}

}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//ios::sync_with_stdio(0);
//cin.tie(0);

cin>>n;
c.resize(n);
g.resize(n);
t.resize(n);
rep(i,0,n-1) cin>>c[i],st.insert(c[i]);
rep(i,1,n-1){
  int a,b;
  cin>>a>>b;
  --a;--b;
  g[a].push_back(b);
  g[b].push_back(a);
}
ans.resize(n);
dfs(0,-1);
for(auto m:ans) cout<<m<<" ";

    return 0;
}


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

#define all(c) c.begin(),c.end()
#define rep(i,a,b) for(int i=a;i<=b;++i)
typedef long long ll ;
typedef vector<int> vi;
#define mod 1000000007
vi c;vector<vi>g;int n;
vector<ll> ans;
vector<unordered_map<int,int>>t;
void dfs(int s,int p){

for(auto to:g[s]){
  if(to==p) continue;
  t[s].reserve(1024);
  dfs(to,s);
   if(t[s].size()<t[to].size())
   swap(t[s],t[to]);
   for(auto p:t[to])
   t[s][p.first]+=t[to][p.first];
   t[to].clear();
}
t[s][c[s]]++;
int v=0;
for(auto p:t[s])
v=max(v,p.second);
for(auto p:t[s])
if(p.second==v) ans[s]+=p.first;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);

cin>>n;
c.resize(n);
g.resize(n);
t.resize(n);
rep(i,0,n-1) cin>>c[i];
rep(i,1,n-1){
  int a,b;
  cin>>a>>b;
  --a;--b;
  g[a].push_back(b);
  g[b].push_back(a);
}
ans.resize(n);
dfs(0,-1);
for(auto m:ans) cout<<m<<" ";

    return 0;
}


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

#define all(c) c.begin(),c.end()
#define rep(i,a,b) for(int i=a;i<=b;++i)
typedef long long ll ;
typedef vector<int> vi;
#define mod 1000000007
vi c;vector<vi>g;int n;
vector<ll> ans;
vector<unordered_map<int,int>>t;
void dfs(int s,int p){

for(auto to:g[s]){
  if(to==p) continue;
  dfs(to,s);
   if(t[s].size()<t[to].size())
   swap(t[s],t[to]);
   for(auto p:t[to])
   t[s][p.first]+=t[to][p.first];
   t[to].clear();
}
t[s][c[s]]++;
int v=0;
for(auto p:t[s])
v=max(v,p.second);
for(auto p:t[s])
if(p.second==v) ans[s]+=p.first;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);

cin>>n;
c.resize(n);
g.resize(n);
t.resize(n);
rep(i,0,n-1) cin>>c[i],st.insert(c[i]);
rep(i,1,n-1){
  int a,b;
  cin>>a>>b;
  --a;--b;
  g[a].push_back(b);
  g[b].push_back(a);
}
ans.resize(n);
dfs(0,-1);
for(auto m:ans) cout<<m<<" ";

    return 0;
}


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

#define all(c) c.begin(),c.end()
#define rep(i,a,b) for(int i=a;i<=b;++i)
typedef long long ll ;
typedef vector<int> vi;
#define mod 1000000007
vi c;vector<vi>g;int n;
vector<ll> ans;
vector<map<int,int>>t;
pair<ll,ll> dfs(int s,int p){
  ll sum=c[s]; int maxn=1;
  t[s][c[s]]++;
for(auto to:g[s]){
  if(to==p) continue;
    auto no=dfs(to,s);
    //maxn=no.first;
   if(t[s].size()<t[to].size())
   {swap(t[s],t[to]);
   maxn=no.first;
   sum=no.second;}
   for(auto p:t[to])
  {t[s][p.first]+=p.second;
    if(t[s][p.first]==maxn)
    sum+=p.first;
    if(t[s][p.first]>maxn){
      sum=p.first;
      maxn=t[s][p.first];
    }
  }

   //t[to].clear();
}
ans[s]=sum;
//cout<<s<<"    "<<maxn<<"       ";
return {maxn,sum};

}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);

cin>>n;
c.resize(n);
g.resize(n);
t.resize(n);
rep(i,0,n-1) cin>>c[i];
rep(i,1,n-1){
  int a,b;
  cin>>a>>b;
  --a;--b;
  g[a].push_back(b);
  g[b].push_back(a);
}
ans.resize(n);
dfs(0,-1);
for(auto m:ans) cout<<m<<" ";

    return 0;
}


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

#define all(c) c.begin(),c.end()
#define rep(i,a,b) for(int i=a;i<=b;++i)
typedef long long ll ;
typedef vector<int> vi;
#define mod 1000000007
vi c;vector<vi>g;int n;
vector<ll> ans;
vector<unordered_map<int,int>>t;
void dfs(int s,int p){

for(auto to:g[s]){
  if(to==p) continue;
  t[s].reserve(int(1e5+1));
  dfs(to,s);
   if(t[s].size()<t[to].size())
   swap(t[s],t[to]);
   for(auto p:t[to])
   t[s][p.first]+=t[to][p.first];
   t[to].clear();
}
t[s][c[s]]++;
int v=0;
for(auto p:t[s])
v=max(v,p.second);
for(auto p:t[s])
if(p.second==v) ans[s]+=p.first;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);

cin>>n;
c.resize(n);
g.resize(n);
t.resize(n);
rep(i,0,n-1) cin>>c[i];
rep(i,1,n-1){
  int a,b;
  cin>>a>>b;
  --a;--b;
  g[a].push_back(b);
  g[b].push_back(a);
}
ans.resize(n);
dfs(0,-1);
for(auto m:ans) cout<<m<<" ";

    return 0;
}


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

#define all(c) c.begin(),c.end()
#define rep(i,a,b) for(int i=a;i<=b;++i)
typedef long long ll ;
typedef vector<int> vi;
#define mod 1000000007
vi c;vector<vi>g;int n;
vector<ll> ans;
vector<unordered_map<int,int>>t;
void dfs(int s,int p){

for(auto to:g[s]){
  if(to==p) continue;
  dfs(to,s);
   if(t[s].size()<t[to].size())
   swap(t[s],t[to]);
   for(auto p:t[to])
   t[s][p.first]+=t[to][p.first];
   //t[to].clear();
}
t[s][c[s]]++;
int v=0;
for(auto p:t[s])
v=max(v,p.second);
for(auto p:t[s])
if(p.second==v) ans[s]+=p.first;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);

cin>>n;
c.resize(n);
g.resize(n);
t.resize(n);
rep(i,0,n-1) cin>>c[i];
rep(i,1,n-1){
  int a,b;
  cin>>a>>b;
  --a;--b;
  g[a].push_back(b);
  g[b].push_back(a);
}
ans.resize(n);
dfs(0,-1);
for(auto m:ans) cout<<m<<" ";

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
// #define int long long int
#define vec vector
#define pb push_back
#define s(x) scanf("%d",&x)
#define all(x)	x.begin(),x.end()
#define pii pair<int,int>
#define fi first
#define se second
#define mod 1000000007
#define SZ 100005
#define endl "\n"
#define trc(x) cerr << #x << ": " << x << endl
#define trcc(x,y) cerr << #x << ": " << x << endl << #y << ": " << y << endl << endl
#define boost ios::sync_with_stdio(0);cin.tie(0)
#define mii map<int,long long>

int n, u, v;
vec<int> adj[SZ];
int col[SZ];
long long ans[SZ];
mii m[SZ], f[SZ];

//size-mv should < size_mu
void merge(mii &mv, mii &fv, mii &mu, mii &fu){
	for(auto& [col, freq]: mv) {
		fu[mu[col]] -= col;
		mu[col] += freq;
		fu[mu[col]] += col;
	}
}

void sml_to_lrg_dfs(int u, int p) {
	m[u][col[u]] = 1;
	f[u][1] = col[u];
	for(int v: adj[u]) {
		if(v-p) {
			sml_to_lrg_dfs(v, u);
			if(m[v].size() > m[u].size()) swap(u, v);
			merge(m[v], f[v], m[u], f[u]);
		}
	}
	ans[u] = f[u].rbegin()->se;
}

void purple18()
{
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> col[i];
	}

	for(int i = 0; i < n-1; i++) {
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	sml_to_lrg_dfs(1, 0);
	for(int i = 0; i < n; i++) {
		cout << ans[i+1] << " ";
	}
}

signed main() {
	boost;
	int t=1;
	// cin>>t;
	while(t--) purple18();
}


#include<bits/stdc++.h>
using namespace std;
// #define int long long int
#define vec vector
#define pb push_back
#define s(x) scanf("%d",&x)
#define all(x)	x.begin(),x.end()
#define pii pair<int,int>
#define fi first
#define se second
#define mod 1000000007
#define SZ 100005
#define endl "\n"
#define trc(x) cerr << #x << ": " << x << endl
#define trcc(x,y) cerr << #x << ": " << x << endl << #y << ": " << y << endl << endl
#define boost ios::sync_with_stdio(0);cin.tie(0)
#define mii map<int,long long>

int n, u, v;
vec<int> adj[SZ];
int col[SZ];
long long ans[SZ];

//size-mv should < size_mu
void merge(mii &mv, mii &fv, mii &mu, mii &fu){
	if(mv.size() > mu.size()) {
		swap(mv, mu);
		swap(fv, fu);
	}
	for(auto& [col, freq]: mv) {
		fu[mu[col]] -= col;
		mu[col] += freq;
		fu[mu[col]] += col;
	}
}

mii mv,fv;

void sml_to_lrg_dfs(int u, int p) {
	mii mu, fu;
	mu[col[u]] = 1;
	fu[1] = col[u];
	for(int v: adj[u]) {
		if(v-p) {
			sml_to_lrg_dfs(v, u);
			merge(mv, fv, mu, fu);
		}
	}
	ans[u] = fu.rbegin()->se;
	mv.clear();
	fv.clear();
	swap(mv,mu);
	swap(fv,fu);
	// return { mu, fu };
}

void purple18()
{
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> col[i];
	}

	for(int i = 0; i < n-1; i++) {
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	sml_to_lrg_dfs(1, 0);
	for(int i = 0; i < n; i++) {
		cout << ans[i+1] << " ";
	}
}

signed main() {
	boost;
	int t=1;
	// cin>>t;
	while(t--) purple18();
}


#include<bits/stdc++.h>
using namespace std;
// #define int long long int
#define vec vector
#define pb push_back
#define s(x) scanf("%d",&x)
#define all(x)	x.begin(),x.end()
#define pii pair<int,int>
#define fi first
#define se second
#define mod 1000000007
#define SZ 100005
#define endl "\n"
#define trc(x) cerr << #x << ": " << x << endl
#define trcc(x,y) cerr << #x << ": " << x << endl << #y << ": " << y << endl << endl
#define boost ios::sync_with_stdio(0);cin.tie(0)
#define mii map<int,long long>

int n, u, v;
vec<int> adj[SZ];
int col[SZ];
long long ans[SZ];

//size-mv should < size_mu
void merge(mii &mv, mii &fv, mii &mu, mii &fu){
	if(mv.size() > mu.size()) {
		swap(mv, mu);
		swap(fv, fu);
	}
	for(auto& [col, freq]: mv) {
		fu[mu[col]] -= col;
		mu[col] += freq;
		fu[mu[col]] += col;
	}
}

mii mv,fv;

void sml_to_lrg_dfs(int u, int p) {
	mii mu, fu;
	mu[col[u]] = 1;
	fu[1] = col[u];
	for(int v: adj[u]) {
		if(v-p) {
			sml_to_lrg_dfs(v, u);
			merge(mv, fv, mu, fu);
		}
	}
	ans[u] = fu.rbegin()->se;
	mv = mu;
	fv = fu;
	// return { mu, fu };
}

void purple18()
{
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> col[i];
	}

	for(int i = 0; i < n-1; i++) {
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	sml_to_lrg_dfs(1, 0);
	for(int i = 0; i < n; i++) {
		cout << ans[i+1] << " ";
	}
}

signed main() {
	boost;
	int t=1;
	// cin>>t;
	while(t--) purple18();
}


#include<bits/stdc++.h>
using namespace std;
// #define int long long int
#define vec vector
#define pb push_back
#define s(x) scanf("%d",&x)
#define all(x)	x.begin(),x.end()
#define pii pair<int,int>
#define fi first
#define se second
#define mod 1000000007
#define SZ 200005
#define endl "\n"
#define trc(x) cerr << #x << ": " << x << endl
#define trcc(x,y) cerr << #x << ": " << x << endl << #y << ": " << y << endl << endl
#define boost ios::sync_with_stdio(0);cin.tie(0)
#define mii map<int,int>

int n, u, v;
vec<int> adj[SZ];
int col[SZ];
int ans[SZ];

//size-mv should < size_mu
void merge(mii &mv, mii &fv, mii &mu, mii &fu){
	if(mv.size() > mu.size()) {
		swap(mv, mu);
		swap(fv, fu);
	}
	for(auto& [col, freq]: mv) {
		fu[mu[col]] -= col;
		mu[col] += freq;
		fu[mu[col]] += col;
	}
}
pair< mii,mii> sml_to_lrg_dfs(int u, int p) {
	mii mu, fu;
	mu[col[u]] = 1;
	fu[1] = col[u];
	for(int v: adj[u]) {
		if(v-p) {
			pair< mii, mii> maps = sml_to_lrg_dfs(v, u);
			mii mv = maps.fi;
			mii fv = maps.se;
			merge(mv, fv, mu, fu);
		}
	}
	ans[u] = fu.rbegin()->se;
	return { mu, fu };
}

void purple18()
{
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> col[i];
	}

	for(int i = 0; i < n-1; i++) {
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	sml_to_lrg_dfs(1, 0);
	for(int i = 0; i < n; i++) {
		cout << ans[i+1] << " ";
	}
}

signed main() {
	boost;
	int t=1;
	// cin>>t;
	while(t--) purple18();
}


#include<bits/stdc++.h>
using namespace std;
// #define int long long int
#define vec vector
#define pb push_back
#define s(x) scanf("%d",&x)
#define all(x)	x.begin(),x.end()
#define pii pair<int,int>
#define fi first
#define se second
#define mod 1000000007
#define SZ 100005
#define endl "\n"
#define trc(x) cerr << #x << ": " << x << endl
#define trcc(x,y) cerr << #x << ": " << x << endl << #y << ": " << y << endl << endl
#define boost ios::sync_with_stdio(0);cin.tie(0)
#define mii map<int,long long>

int n, u, v;
vec<int> adj[SZ];
int col[SZ];
long long ans[SZ];

//size-mv should < size_mu
void merge(mii &mv, mii &fv, mii &mu, mii &fu){
	if(mv.size() > mu.size()) {
		swap(mv, mu);
		swap(fv, fu);
	}
	for(auto& [col, freq]: mv) {
		fu[mu[col]] -= col;
		mu[col] += freq;
		fu[mu[col]] += col;
	}
}
pair< mii,mii> sml_to_lrg_dfs(int u, int p) {
	mii mu, fu;
	mu[col[u]] = 1;
	fu[1] = col[u];
	for(int v: adj[u]) {
		if(v-p) {
			pair< mii, mii> maps = sml_to_lrg_dfs(v, u);
			mii mv = maps.fi;
			mii fv = maps.se;
			merge(mv, fv, mu, fu);
		}
	}
	ans[u] = fu.rbegin()->se;
	return { mu, fu };
}

void purple18()
{
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> col[i];
	}

	for(int i = 0; i < n-1; i++) {
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	sml_to_lrg_dfs(1, 0);
	for(int i = 0; i < n; i++) {
		cout << ans[i+1] << " ";
	}
}

signed main() {
	boost;
	int t=1;
	// cin>>t;
	while(t--) purple18();
}


#include<bits/stdc++.h>
using namespace std;
// #define int long long int
#define vec vector
#define pb push_back
#define s(x) scanf("%d",&x)
#define all(x)	x.begin(),x.end()
#define pii pair<int,int>
#define fi first
#define se second
#define mod 1000000007
#define SZ 100005
#define endl "\n"
#define trc(x) cerr << #x << ": " << x << endl
#define trcc(x,y) cerr << #x << ": " << x << endl << #y << ": " << y << endl << endl
#define boost ios::sync_with_stdio(0);cin.tie(0)
#define mii map<int,long long>

int n, u, v;
vec<int> adj[SZ];
int col[SZ];
long long ans[SZ];
mii m[SZ], f[SZ];

//size-mv should < size_mu
void merge(mii &mv, mii &fv, mii &mu, mii &fu){
	if(mv.size() > mu.size()) { swap(mv, mu); swap(fv, fu);}
	for(auto& [col, freq]: mv) {
		fu[mu[col]] -= col;
		mu[col] += freq;
		fu[mu[col]] += col;
	}
}

void sml_to_lrg_dfs(int u, int p) {
	m[u][col[u]] = 1;
	f[u][1] = col[u];
	for(int v: adj[u]) {
		if(v-p) {
			sml_to_lrg_dfs(v, u);
			merge(m[v], f[v], m[u], f[u]);
		}
	}
	ans[u] = f[u].rbegin()->se;
}

void purple18()
{
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> col[i];
	}

	for(int i = 0; i < n-1; i++) {
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	sml_to_lrg_dfs(1, 0);
	for(int i = 0; i < n; i++) {
		cout << ans[i+1] << " ";
	}
}

signed main() {
	boost;
	int t=1;
	// cin>>t;
	while(t--) purple18();
}


#include<bits/stdc++.h>
using namespace std;
// #define int long long int
#define vec vector
#define pb push_back
#define s(x) scanf("%d",&x)
#define all(x)	x.begin(),x.end()
#define pii pair<int,int>
#define fi first
#define se second
#define mod 1000000007
#define SZ 100005
#define endl "\n"
#define trc(x) cerr << #x << ": " << x << endl
#define trcc(x,y) cerr << #x << ": " << x << endl << #y << ": " << y << endl << endl
#define boost ios::sync_with_stdio(0);cin.tie(0)
#define mii map<int,long>

int n, u, v;
vec<int> adj[SZ];
int col[SZ];
long ans[SZ];

//size-mv should < size_mu
void merge(mii &mv, mii &fv, mii &mu, mii &fu){
	if(mv.size() > mu.size()) {
		swap(mv, mu);
		swap(fv, fu);
	}
	for(auto& [col, freq]: mv) {
		fu[mu[col]] -= col;
		mu[col] += freq;
		fu[mu[col]] += col;
	}
}
pair< mii,mii> sml_to_lrg_dfs(int u, int p) {
	mii mu, fu;
	mu[col[u]] = 1;
	fu[1] = col[u];
	for(int v: adj[u]) {
		if(v-p) {
			pair< mii, mii> maps = sml_to_lrg_dfs(v, u);
			mii mv = maps.fi;
			mii fv = maps.se;
			merge(mv, fv, mu, fu);
		}
	}
	ans[u] = fu.rbegin()->se;
	return { mu, fu };
}

void purple18()
{
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> col[i];
	}

	for(int i = 0; i < n-1; i++) {
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	sml_to_lrg_dfs(1, 0);
	for(int i = 0; i < n; i++) {
		cout << ans[i+1] << " ";
	}
}

signed main() {
	boost;
	int t=1;
	// cin>>t;
	while(t--) purple18();
}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define vec vector
#define pb push_back
#define s(x) scanf("%d",&x)
#define all(x)	x.begin(),x.end()
#define pii pair<int,int>
#define fi first
#define se second
#define mod 1000000007
#define SZ 200005
#define endl "\n"
#define trc(x) cerr << #x << ": " << x << endl
#define trcc(x,y) cerr << #x << ": " << x << endl << #y << ": " << y << endl << endl
#define boost ios::sync_with_stdio(0);cin.tie(0)
#define mii map<int,int>

int n, u, v;
vec<int> adj[SZ];
int col[SZ];
int ans[SZ];

//size-mv should < size_mu
void merge(mii &mv, mii &fv, mii &mu, mii &fu){
	if(mv.size() > mu.size()) {
		swap(mv, mu);
		swap(fv, fu);
	}
	for(auto& [col, freq]: mv) {
		fu[mu[col]] -= col;
		mu[col] += freq;
		fu[mu[col]] += col;
	}
}
pair< mii,mii> sml_to_lrg_dfs(int u, int p) {
	mii mu, fu;
	mu[col[u]] = 1;
	fu[1] = col[u];
	for(int v: adj[u]) {
		if(v-p) {
			pair< mii, mii> maps = sml_to_lrg_dfs(v, u);
			mii mv = maps.fi;
			mii fv = maps.se;
			merge(mv, fv, mu, fu);
		}
	}
	ans[u] = fu.rbegin()->se;
	return { mu, fu };
}

void purple18()
{
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> col[i];
	}

	for(int i = 0; i < n-1; i++) {
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	sml_to_lrg_dfs(1, 0);
	for(int i = 0; i < n; i++) {
		cout << ans[i+1] << " ";
	}
}

signed main() {
	boost;
	int t=1;
	// cin>>t;
	while(t--) purple18();
}


#include <bits/stdc++.h>

using namespace std;

const int N = 100005;

int n;
int c[N];
int x, y;
vector<int> g[N];
vector<int> col[N];
map<int, int> cnt;
int res[N];
int sz[N];
set<int> k;
int masz[N];
int sta[N], en[N];
int t = 0;

void predfs(int u = 1, int pa = -1) {
	t++;
	sta[u] = t;
	sz[u] = 1;
	for (auto v : g[u]) {
		if (v != pa) {
			predfs(v, u);
			sz[u] += sz[v];
		}
	}
	t++;
	en[u] = t;
}

void dfs(int u = 1, int pa = -1, int keep = 1) {
	col[u].resize(en[u] - sta[u] + 1);
	int mx = -1, bigC = -1;
	for (auto v : g[u]) {
		if (v != pa && sz[v] > mx) {
			mx = sz[v];
			bigC = v;
		}
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			dfs(v, u, 0);
		}
	}
	if (bigC != -1) {
		dfs(bigC, u, 1);
		col[u] = col[bigC];
		masz[u] = masz[bigC];
		res[u] = res[bigC];
		col[bigC].clear();
		col[bigC].shrink_to_fit();
	}
	else {
		masz[u] = -1;
		res[u] = 0;
	}
	col[u].push_back(u);
	cnt[c[u]] ++;
	if (cnt[c[u]] > masz[u]) {
		res[u] = c[u];
		masz[u] = cnt[c[u]];
	}
	else if (cnt[c[u]] == masz[u]) {
		res[u] += c[u];
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			for (auto x : col[v]) {
				cnt[c[x]] ++;
				col[u].push_back(x);
				if (cnt[c[x]] > masz[u]) {
					res[u] = c[x];
					masz[u] = cnt[c[x]];
				}
				else if (cnt[c[x]] == masz[u]) {
					res[u] += c[x];
				}
			}
			col[v].clear();
			col[v].shrink_to_fit();
		}
	}
	if (keep == 0) {
		for (auto v : col[u]) {
			cnt[c[v]]--;
		}
	}
	col[u].shrink_to_fit();
}

signed 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", &x, &y);
		g[x].push_back(y);
		g[y].push_back(x);
	}
	memset(res, 0, sizeof(res));
	predfs(); dfs();
	for (int i = 1; i <= n; i++) {
		cout << res[i] << " ";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 100005;

int n;
int c[N];
int x, y;
vector<int> g[N];
vector<int> col[N];
map<int, int> cnt;
int res[N];
int sz[N];
set<int> k;
int masz[N];
int sta[N], en[N];
int t = 0;

void predfs(int u = 1, int pa = -1) {
	t++;
	sta[u] = t;
	sz[u] = 1;
	for (auto v : g[u]) {
		if (v != pa) {
			predfs(v, u);
			sz[u] += sz[v];
		}
	}
	t++;
	en[u] = t;
}

void dfs(int u = 1, int pa = -1, int keep = 1) {
	col[u].reserve(N);
	int mx = -1, bigC = -1;
	for (auto v : g[u]) {
		if (v != pa && sz[v] > mx) {
			mx = sz[v];
			bigC = v;
		}
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			dfs(v, u, 0);
		}
	}
	if (bigC != -1) {
		dfs(bigC, u, 1);
		col[u] = col[bigC];
		masz[u] = masz[bigC];
		res[u] = res[bigC];
		col[bigC].clear();
		col[bigC].shrink_to_fit();
	}
	else {
		masz[u] = -1;
		res[u] = 0;
	}
	col[u].push_back(u);
	cnt[c[u]] ++;
	if (cnt[c[u]] > masz[u]) {
		res[u] = c[u];
		masz[u] = cnt[c[u]];
	}
	else if (cnt[c[u]] == masz[u]) {
		res[u] += c[u];
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			for (auto x : col[v]) {
				cnt[c[x]] ++;
				col[u].push_back(x);
				if (cnt[c[x]] > masz[u]) {
					res[u] = c[x];
					masz[u] = cnt[c[x]];
				}
				else if (cnt[c[x]] == masz[u]) {
					res[u] += c[x];
				}
			}
			col[v].clear();
			col[v].shrink_to_fit();
		}
	}
	if (keep == 0) {
		for (auto v : col[u]) {
			cnt[c[v]]--;
		}
	}
}

signed 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", &x, &y);
		g[x].push_back(y);
		g[y].push_back(x);
	}
	memset(res, 0, sizeof(res));
	predfs(); dfs();
	for (int i = 1; i <= n; i++) {
		cout << res[i] << " ";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
int c[100005];
int x, y;
vector<int> g[100005];
vector<int> col[100005];
map<int, int> cnt;
long long res[100005];
int sz[100005];
set<int> k;
int masz[100005];
vector<int> ne;

void predfs(int u = 1, int pa = -1) {
	sz[u] = 1;
	for (auto v : g[u]) {
		if (v != pa) {
			predfs(v, u);
			sz[u] += sz[v];
		}
	}
}

void dfs(int u = 1, int pa = -1, int keep = 1) {
	int mx = -1, bigC = -1;
	for (auto v : g[u]) {
		if (v != pa && sz[v] > mx) {
			mx = sz[v];
			bigC = v;
		}
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			dfs(v, u, 0);
		}
	}
	if (bigC != -1) {
		dfs(bigC, u, 1);
		col[u] = col[bigC];
		masz[u] = masz[bigC];
		res[u] = res[bigC];
		col[bigC] = ne;
	}
	else {
		masz[u] = -1;
		res[u] = 0;
	}
	col[u].push_back(u);
	cnt[c[u]] ++;
	if (cnt[c[u]] > masz[u]) {
		res[u] = c[u];
		masz[u] = cnt[c[u]];
	}
	else if (cnt[c[u]] == masz[u]) {
		res[u] += c[u];
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			for (auto x : col[v]) {
				cnt[c[x]] ++;
				col[u].push_back(x);
				if (cnt[c[x]] > masz[u]) {
					res[u] = c[x];
					masz[u] = cnt[c[x]];
				}
				else if (cnt[c[x]] == masz[u]) {
					res[u] += c[x];
				}
			}
			col[v] = ne;
		}
	}
	if (keep == 0) {
		for (auto v : col[u]) {
			cnt[c[v]]--;
		}
	}
}

signed main() {
	ne.shrink_to_fit();
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &c[i]);
	}
	for (int i = 1; i < n; i++) {
		scanf("%d%d", &x, &y);
		g[x].push_back(y);
		g[y].push_back(x);
	}
	memset(res, 0, sizeof(res));
	predfs(); dfs();
	for (int i = 1; i <= n; i++) {
		cout << res[i] << " ";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
int c[100005];
int x, y;
vector<int> g[100005];
vector<int> col[100005];
map<int, int> cnt;
long long res[100005];
int sz[100005];
set<int> k;
int masz[100005];
vector<int> ne;

void predfs(int u = 1, int pa = -1) {
	sz[u] = 1;
	for (auto v : g[u]) {
		if (v != pa) {
			predfs(v, u);
			sz[u] += sz[v];
		}
	}
}

void dfs(int u = 1, int pa = -1, int keep = 1) {
	int mx = -1, bigC = -1;
	for (auto v : g[u]) {
		if (v != pa && sz[v] > mx) {
			mx = sz[v];
			bigC = v;
		}
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			dfs(v, u, 0);
		}
	}
	if (bigC != -1) {
		dfs(bigC, u, 1);
		col[u] = col[bigC];
		masz[u] = masz[bigC];
		res[u] = res[bigC];
		col[bigC] = ne;
		col[bigC].shrink_to_fit();
	}
	else {
		masz[u] = -1;
		res[u] = 0;
	}
	col[u].push_back(u);
	cnt[c[u]] ++;
	if (cnt[c[u]] > masz[u]) {
		res[u] = c[u];
		masz[u] = cnt[c[u]];
	}
	else if (cnt[c[u]] == masz[u]) {
		res[u] += c[u];
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			for (auto x : col[v]) {
				cnt[c[x]] ++;
				col[u].push_back(x);
				if (cnt[c[x]] > masz[u]) {
					res[u] = c[x];
					masz[u] = cnt[c[x]];
				}
				else if (cnt[c[x]] == masz[u]) {
					res[u] += c[x];
				}
			}
			col[v] = ne;
			col[v].shrink_to_fit();
		}
	}
	if (keep == 0) {
		for (auto v : col[u]) {
			cnt[c[v]]--;
		}
	}
}

signed main() {
	ne.shrink_to_fit();
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &c[i]);
	}
	for (int i = 1; i < n; i++) {
		scanf("%d%d", &x, &y);
		g[x].push_back(y);
		g[y].push_back(x);
	}
	memset(res, 0, sizeof(res));
	predfs(); dfs();
	for (int i = 1; i <= n; i++) {
		cout << res[i] << " ";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
int c[100005];
int x, y;
vector<int> g[100005];
vector<int> col[100005];
map<int, int> cnt;
long long res[100005];
int sz[100005];
set<int> k;
int masz[100005];

void predfs(int u = 1, int pa = -1) {
	sz[u] = 1;
	for (auto v : g[u]) {
		if (v != pa) {
			predfs(v, u);
			sz[u] += sz[v];
		}
	}
}

void dfs(int u = 1, int pa = -1, int keep = 1) {
	int mx = -1, bigC = -1;
	for (auto v : g[u]) {
		if (v != pa && sz[v] > mx) {
			mx = sz[v];
			bigC = v;
		}
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			dfs(v, u, 0);
		}
	}
	if (bigC != -1) {
		dfs(bigC, u, 1);
		col[u] = col[bigC];
		masz[u] = masz[bigC];
		res[u] = res[bigC];
		col[bigC].clear();
		col[bigC].shrink_to_fit();
	}
	else {
		masz[u] = -1;
		res[u] = 0;
	}
	col[u].push_back(u);
	cnt[c[u]] ++;
	if (cnt[c[u]] > masz[u]) {
		res[u] = c[u];
		masz[u] = cnt[c[u]];
	}
	else if (cnt[c[u]] == masz[u]) {
		res[u] += c[u];
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			for (auto x : col[v]) {
				cnt[c[x]] ++;
				col[u].push_back(x);
				if (cnt[c[x]] > masz[u]) {
					res[u] = c[x];
					masz[u] = cnt[c[x]];
				}
				else if (cnt[c[x]] == masz[u]) {
					res[u] += c[x];
				}
			}
			col[v].clear();
			col[v].shrink_to_fit();
		}
	}
	if (keep == 0) {
		for (auto v : col[u]) {
			cnt[c[v]]--;
		}
	}
}

signed 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", &x, &y);
		g[x].push_back(y);
		g[y].push_back(x);
	}
	memset(res, 0, sizeof(res));
	predfs(); dfs();
	for (int i = 1; i <= n; i++) {
		cout << res[i] << " ";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
int c[100005];
int x, y;
vector<int> g[100005];
vector<int> col[100005];
map<int, int> cnt;
long long res[100005];
int sz[100005];
set<int> k;
int masz[100005];
vector<int> ne;

void predfs(int u = 1, int pa = -1) {
	sz[u] = 1;
	for (auto v : g[u]) {
		if (v != pa) {
			predfs(v, u);
			sz[u] += sz[v];
		}
	}
}

void dfs(int u = 1, int pa = -1, int keep = 1) {
	int mx = -1, bigC = -1;
	for (auto v : g[u]) {
		if (v != pa && sz[v] > mx) {
			mx = sz[v];
			bigC = v;
		}
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			dfs(v, u, 0);
		}
	}
	if (bigC != -1) {
		dfs(bigC, u, 1);
		col[u] = col[bigC];
		masz[u] = masz[bigC];
		res[u] = res[bigC];
	}
	else {
		masz[u] = -1;
		res[u] = 0;
	}
	col[u].push_back(u);
	cnt[c[u]] ++;
	if (cnt[c[u]] > masz[u]) {
		res[u] = c[u];
		masz[u] = cnt[c[u]];
	}
	else if (cnt[c[u]] == masz[u]) {
		res[u] += c[u];
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			for (auto x : col[v]) {
				cnt[c[x]] ++;
				col[u].push_back(x);
				if (cnt[c[x]] > masz[u]) {
					res[u] = c[x];
					masz[u] = cnt[c[x]];
				}
				else if (cnt[c[x]] == masz[u]) {
					res[u] += c[x];
				}
			}
			col[v].clear();
			vector<int>().swap(col[v]);
		}
	}
	if (keep == 0) {
		for (auto v : col[u]) {
			cnt[c[v]]--;
		}
	}
}

signed main() {
	ne.shrink_to_fit();
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &c[i]);
	}
	for (int i = 1; i < n; i++) {
		scanf("%d%d", &x, &y);
		g[x].push_back(y);
		g[y].push_back(x);
	}
	memset(res, 0, sizeof(res));
	predfs(); dfs();
	for (int i = 1; i <= n; i++) {
		cout << res[i] << " ";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
int c[100005];
int x, y;
vector<int> g[100005];
vector<int> col[100005];
map<int, int> cnt;
long long res[100005];
int sz[100005];
set<int> k;
int masz[100005];
vector<int> ne;

void predfs(int u = 1, int pa = -1) {
	sz[u] = 1;
	for (auto v : g[u]) {
		if (v != pa) {
			predfs(v, u);
			sz[u] += sz[v];
		}
	}
}

void dfs(int u = 1, int pa = -1, int keep = 1) {
	int mx = -1, bigC = -1;
	for (auto v : g[u]) {
		if (v != pa && sz[v] > mx) {
			mx = sz[v];
			bigC = v;
		}
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			dfs(v, u, 0);
		}
	}
	if (bigC != -1) {
		dfs(bigC, u, 1);
		col[u] = col[bigC];
		masz[u] = masz[bigC];
		res[u] = res[bigC];
		col[bigC] = ne;
	}
	else {
		masz[u] = -1;
		res[u] = 0;
	}
	col[u].push_back(u);
	cnt[c[u]] ++;
	if (cnt[c[u]] > masz[u]) {
		res[u] = c[u];
		masz[u] = cnt[c[u]];
	}
	else if (cnt[c[u]] == masz[u]) {
		res[u] += c[u];
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			for (auto x : col[v]) {
				cnt[c[x]] ++;
				col[u].push_back(x);
				if (cnt[c[x]] > masz[u]) {
					res[u] = c[x];
					masz[u] = cnt[c[x]];
				}
				else if (cnt[c[x]] == masz[u]) {
					res[u] += c[x];
				}
			}
			col[v] = ne;
		}
	}
	if (keep == 0) {
		for (auto v : col[u]) {
			cnt[c[v]]--;
		}
	}
}

signed 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", &x, &y);
		g[x].push_back(y);
		g[y].push_back(x);
	}
	memset(res, 0, sizeof(res));
	predfs(); dfs();
	for (int i = 1; i <= n; i++) {
		cout << res[i] << " ";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
int c[100005];
int x, y;
vector<int> g[100005];
vector<int> col[100005];
map<int, int> cnt;
long long res[100005];
int sz[100005];
set<int> k;
int masz[100005];
vector<int> ne;

void predfs(int u = 1, int pa = -1) {
	sz[u] = 1;
	for (auto v : g[u]) {
		if (v != pa) {
			predfs(v, u);
			sz[u] += sz[v];
		}
	}
}

void dfs(int u = 1, int pa = -1, int keep = 1) {
	int mx = -1, bigC = -1;
	for (auto v : g[u]) {
		if (v != pa && sz[v] > mx) {
			mx = sz[v];
			bigC = v;
		}
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			dfs(v, u, 0);
		}
	}
	if (bigC != -1) {
		dfs(bigC, u, 1);
		col[u] = col[bigC];
		masz[u] = masz[bigC];
		res[u] = res[bigC];
		col[bigC].clear();
		vector<int>().swap(col[bigC]);
	}
	else {
		masz[u] = -1;
		res[u] = 0;
	}
	col[u].push_back(u);
	cnt[c[u]] ++;
	if (cnt[c[u]] > masz[u]) {
		res[u] = c[u];
		masz[u] = cnt[c[u]];
	}
	else if (cnt[c[u]] == masz[u]) {
		res[u] += c[u];
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			for (auto x : col[v]) {
				cnt[c[x]] ++;
				col[u].push_back(x);
				if (cnt[c[x]] > masz[u]) {
					res[u] = c[x];
					masz[u] = cnt[c[x]];
				}
				else if (cnt[c[x]] == masz[u]) {
					res[u] += c[x];
				}
			}
		}
	}
	if (keep == 0) {
		for (auto v : col[u]) {
			cnt[c[v]]--;
		}
	}
}

signed main() {
	ne.shrink_to_fit();
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &c[i]);
	}
	for (int i = 1; i < n; i++) {
		scanf("%d%d", &x, &y);
		g[x].push_back(y);
		g[y].push_back(x);
	}
	memset(res, 0, sizeof(res));
	predfs(); dfs();
	for (int i = 1; i <= n; i++) {
		cout << res[i] << " ";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
int c[100005];
int x, y;
vector<int> g[100005];
vector<int> col[100005];
map<int, int> cnt;
long long res[100005];
int sz[100005];
set<int> k;
int masz[100005];
vector<int> ne;

void predfs(int u = 1, int pa = -1) {
	sz[u] = 1;
	for (auto v : g[u]) {
		if (v != pa) {
			predfs(v, u);
			sz[u] += sz[v];
		}
	}
}

void dfs(int u = 1, int pa = -1, int keep = 1) {
	int mx = -1, bigC = -1;
	for (auto v : g[u]) {
		if (v != pa && sz[v] > mx) {
			mx = sz[v];
			bigC = v;
		}
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			dfs(v, u, 0);
		}
	}
	if (bigC != -1) {
		dfs(bigC, u, 1);
		swap(col[u], col[bigC]);
		masz[u] = masz[bigC];
		res[u] = res[bigC];
	}
	else {
		masz[u] = -1;
		res[u] = 0;
	}
	col[u].push_back(u);
	cnt[c[u]] ++;
	if (cnt[c[u]] > masz[u]) {
		res[u] = c[u];
		masz[u] = cnt[c[u]];
	}
	else if (cnt[c[u]] == masz[u]) {
		res[u] += c[u];
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			for (auto x : col[v]) {
				cnt[c[x]] ++;
				col[u].push_back(x);
				if (cnt[c[x]] > masz[u]) {
					res[u] = c[x];
					masz[u] = cnt[c[x]];
				}
				else if (cnt[c[x]] == masz[u]) {
					res[u] += c[x];
				}
			}
		}
	}
	if (keep == 0) {
		for (auto v : col[u]) {
			cnt[c[v]]--;
		}
	}
}

signed main() {
	ne.shrink_to_fit();
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &c[i]);
	}
	for (int i = 1; i < n; i++) {
		scanf("%d%d", &x, &y);
		g[x].push_back(y);
		g[y].push_back(x);
	}
	memset(res, 0, sizeof(res));
	predfs(); dfs();
	for (int i = 1; i <= n; i++) {
		cout << res[i] << " ";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
int c[100005];
int x, y;
vector<int> g[100005];
vector<int> col[100005];
map<int, int> cnt;
long long res[100005];
int sz[100005];
set<int> k;
int masz[100005];

void predfs(int u = 1, int pa = -1) {
	sz[u] = 1;
	for (auto v : g[u]) {
		if (v != pa) {
			predfs(v, u);
			sz[u] += sz[v];
		}
	}
}

void dfs(int u = 1, int pa = -1, int keep = 1) {
	int mx = -1, bigC = -1;
	for (auto v : g[u]) {
		if (v != pa && sz[v] > mx) {
			mx = sz[v];
			bigC = v;
		}
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			dfs(v, u, 0);
		}
	}
	if (bigC != -1) {
		dfs(bigC, u, 1);
		col[u] = col[bigC];
		masz[u] = masz[bigC];
		res[u] = res[bigC];
	}
	else {
		masz[u] = -1;
		res[u] = 0;
	}
	col[u].push_back(u);
	cnt[c[u]] ++;
	if (cnt[c[u]] > masz[u]) {
		res[u] = c[u];
		masz[u] = cnt[c[u]];
	}
	else if (cnt[c[u]] == masz[u]) {
		res[u] += c[u];
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			for (auto x : col[v]) {
				cnt[c[x]] ++;
				col[u].push_back(x);
				if (cnt[c[x]] > masz[u]) {
					res[u] = c[x];
					masz[u] = cnt[c[x]];
				}
				else if (cnt[c[x]] == masz[u]) {
					res[u] += c[x];
				}
			}
		}
	}
	if (keep == 0) {
		for (auto v : col[u]) {
			cnt[c[v]]--;
		}
	}
}

signed 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", &x, &y);
		g[x].push_back(y);
		g[y].push_back(x);
	}
	memset(res, 0, sizeof(res));
	predfs(); dfs();
	for (int i = 1; i <= n; i++) {
		cout << res[i] << " ";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
int c[100005];
int x, y;
vector<int> g[100005];
vector<int> col[100005];
map<int, int> cnt;
long long res[100005];
int sz[100005];
set<int> k;
int masz[100005];

void predfs(int u = 1, int pa = -1) {
	sz[u] = 1;
	for (auto v : g[u]) {
		if (v != pa) {
			predfs(v, u);
			sz[u] += sz[v];
		}
	}
}

void dfs(int u = 1, int pa = -1, int keep = 1) {
	int mx = -1, bigC = -1;
	for (auto v : g[u]) {
		if (v != pa && sz[v] > mx) {
			mx = sz[v];
			bigC = v;
		}
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			dfs(v, u, 0);
		}
	}
	if (bigC != -1) {
		dfs(bigC, u, 1);
		col[u] = col[bigC];
		masz[u] = masz[bigC];
		res[u] = res[bigC];
		vector<int>().swap(col[bigC]);
	}
	else {
		masz[u] = -1;
		res[u] = 0;
	}
	col[u].push_back(u);
	cnt[c[u]] ++;
	if (cnt[c[u]] > masz[u]) {
		res[u] = c[u];
		masz[u] = cnt[c[u]];
	}
	else if (cnt[c[u]] == masz[u]) {
		res[u] += c[u];
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			for (auto x : col[v]) {
				cnt[c[x]] ++;
				col[u].push_back(x);
				if (cnt[c[x]] > masz[u]) {
					res[u] = c[x];
					masz[u] = cnt[c[x]];
				}
				else if (cnt[c[x]] == masz[u]) {
					res[u] += c[x];
				}
			}
			vector<int>().swap(col[v]);
		}
	}
	if (keep == 0) {
		for (auto v : col[u]) {
			cnt[c[v]]--;
		}
	}
}

signed 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", &x, &y);
		g[x].push_back(y);
		g[y].push_back(x);
	}
	memset(res, 0, sizeof(res));
	predfs(); dfs();
	for (int i = 1; i <= n; i++) {
		cout << res[i] << " ";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
int c[100005];
int x, y;
vector<int> g[100005];
vector<int> col[100005];
map<int, int> cnt;
long long res[100005];
int sz[100005];
set<int> k;
int masz[100005];
vector<int> ne;

void predfs(int u = 1, int pa = -1) {
	sz[u] = 1;
	for (auto v : g[u]) {
		if (v != pa) {
			predfs(v, u);
			sz[u] += sz[v];
		}
	}
}

void dfs(int u = 1, int pa = -1, int keep = 1) {
	int mx = -1, bigC = -1;
	for (auto v : g[u]) {
		if (v != pa && sz[v] > mx) {
			mx = sz[v];
			bigC = v;
		}
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			dfs(v, u, 0);
		}
	}
	if (bigC != -1) {
		dfs(bigC, u, 1);
		col[u] = col[bigC];
		masz[u] = masz[bigC];
		res[u] = res[bigC];
		col[bigC].clear();
		vector<int>().swap(col[bigC]);
	}
	else {
		masz[u] = -1;
		res[u] = 0;
	}
	col[u].push_back(u);
	cnt[c[u]] ++;
	if (cnt[c[u]] > masz[u]) {
		res[u] = c[u];
		masz[u] = cnt[c[u]];
	}
	else if (cnt[c[u]] == masz[u]) {
		res[u] += c[u];
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			for (auto x : col[v]) {
				cnt[c[x]] ++;
				col[u].push_back(x);
				if (cnt[c[x]] > masz[u]) {
					res[u] = c[x];
					masz[u] = cnt[c[x]];
				}
				else if (cnt[c[x]] == masz[u]) {
					res[u] += c[x];
				}
			}
			col[v].clear();
			vector<int>().swap(col[v]);
		}
	}
	if (keep == 0) {
		for (auto v : col[u]) {
			cnt[c[v]]--;
		}
	}
}

signed main() {
	ne.shrink_to_fit();
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &c[i]);
	}
	for (int i = 1; i < n; i++) {
		scanf("%d%d", &x, &y);
		g[x].push_back(y);
		g[y].push_back(x);
	}
	memset(res, 0, sizeof(res));
	predfs(); dfs();
	for (int i = 1; i <= n; i++) {
		cout << res[i] << " ";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n;
int c[100005];
int x, y;
vector<int> g[100005];
vector<int> col[100005];
map<int, int> cnt;
long long res[100005];
int sz[100005];
set<int> k;
int masz[100005];

void predfs(int u = 1, int pa = -1) {
	sz[u] = 1;
	for (auto v : g[u]) {
		if (v != pa) {
			predfs(v, u);
			sz[u] += sz[v];
		}
	}
}

void dfs(int u = 1, int pa = -1, int keep = 1) {
	int mx = -1, bigC = -1;
	for (auto v : g[u]) {
		if (v != pa && sz[v] > mx) {
			mx = sz[v];
			bigC = v;
		}
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			dfs(v, u, 0);
		}
	}
	if (bigC != -1) {
		dfs(bigC, u, 1);
		col[u] = col[bigC];
		masz[u] = masz[bigC];
		res[u] = res[bigC];
		col[bigC].clear();
		col[bigC].resize(0);
	}
	else {
		masz[u] = -1;
		res[u] = 0;
	}
	col[u].push_back(u);
	cnt[c[u]] ++;
	if (cnt[c[u]] > masz[u]) {
		res[u] = c[u];
		masz[u] = cnt[c[u]];
	}
	else if (cnt[c[u]] == masz[u]) {
		res[u] += c[u];
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			for (auto x : col[v]) {
				cnt[c[x]] ++;
				col[u].push_back(x);
				if (cnt[c[x]] > masz[u]) {
					res[u] = c[x];
					masz[u] = cnt[c[x]];
				}
				else if (cnt[c[x]] == masz[u]) {
					res[u] += c[x];
				}
			}
			col[v].clear();
			col[v].resize(0);
		}
	}
	if (keep == 0) {
		for (auto v : col[u]) {
			cnt[c[v]]--;
		}
	}
}

signed 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", &x, &y);
		g[x].push_back(y);
		g[y].push_back(x);
	}
	memset(res, 0, sizeof(res));
	predfs(); dfs();
	for (int i = 1; i <= n; i++) {
		cout << res[i] << " ";
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 100005;

int n;
int c[N];
int x, y;
vector<int> g[N];
vector<int> col[N];
map<int, int> cnt;
int res[N];
int sz[N];
set<int> k;
int masz[N];
int sta[N], en[N];
int t = 0;

void predfs(int u = 1, int pa = -1) {
	t++;
	sta[u] = t;
	sz[u] = 1;
	for (auto v : g[u]) {
		if (v != pa) {
			predfs(v, u);
			sz[u] += sz[v];
		}
	}
	t++;
	en[u] = t;
}

void dfs(int u = 1, int pa = -1, int keep = 1) {
	col[u].reserve(en[u]-sta[u]);
	int mx = -1, bigC = -1;
	for (auto v : g[u]) {
		if (v != pa && sz[v] > mx) {
			mx = sz[v];
			bigC = v;
		}
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			dfs(v, u, 0);
		}
	}
	if (bigC != -1) {
		dfs(bigC, u, 1);
		col[u] = col[bigC];
		masz[u] = masz[bigC];
		res[u] = res[bigC];
		col[bigC].clear();
		col[bigC].shrink_to_fit();
	}
	else {
		masz[u] = -1;
		res[u] = 0;
	}
	col[u].push_back(u);
	cnt[c[u]] ++;
	if (cnt[c[u]] > masz[u]) {
		res[u] = c[u];
		masz[u] = cnt[c[u]];
	}
	else if (cnt[c[u]] == masz[u]) {
		res[u] += c[u];
	}
	for (auto v : g[u]) {
		if (v != pa && v != bigC) {
			for (auto x : col[v]) {
				cnt[c[x]] ++;
				col[u].push_back(x);
				if (cnt[c[x]] > masz[u]) {
					res[u] = c[x];
					masz[u] = cnt[c[x]];
				}
				else if (cnt[c[x]] == masz[u]) {
					res[u] += c[x];
				}
			}
			col[v].clear();
			col[v].shrink_to_fit();
		}
	}
	if (keep == 0) {
		for (auto v : col[u]) {
			cnt[c[v]]--;
		}
	}
}

signed 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", &x, &y);
		g[x].push_back(y);
		g[y].push_back(x);
	}
	memset(res, 0, sizeof(res));
	predfs(); dfs();
	for (int i = 1; i <= n; i++) {
		cout << res[i] << " ";
	}
	return 0;
}


#include <algorithm>
#include <cmath>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <vector>

using namespace std;

#define ll long long
#define el '\n'
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repi(i, a, b) for (int i = a; i >= b; i--)
#define vi vector<int>
#define vvi vector<vector<int>>
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define mp make_pair
#ifdef LOCAL
#define debug(fmt, ...) printf(fmt, ##__VA_ARGS__)
#define check(...) assert(__VA_ARGS__)
#else
#define debug(fmt, ...)
#define check(...)
#endif

int n;
vi c;
vvi edge;
vi ans;

struct stat_info {
    map<int, int> color_count;
    int sum;
    int dominate;
};

stat_info my_empty{map<int, int>(), 0, 0};

stat_info &merge(stat_info &a, stat_info &b) {
    if (a.color_count.size() > b.color_count.size()) {
        return merge(b, a);
    }
    // debug("a: sum=%d,dominate=%d-----------\n", a.sum, a.dominate);
    // for (auto x : a.color_count) {
    //     debug("%d->%d\n", x.first, x.second);
    // }
    // debug("\n");
    // debug("b: sum=%d,dominate=%d-----------\n", b.sum, b.dominate);
    // for (auto x : b.color_count) {
    //     debug("%d->%d\n", x.first, x.second);
    // }
    // debug("\n");
    // debug("res: sum=%d,dominate=%d-----------\n", res.sum, res.dominate);
    // for (auto x : res.color_count) {
    //     debug("%d->%d\n", x.first, x.second);
    // }
    // debug("\n");
    // first remove num from res
    vii to_change;
    for (auto x : a.color_count) {
        int color = x.first;
        int count = x.second;
        int old_count = b.color_count[color];
        b.color_count.erase(color);
        if (old_count > 0 && old_count == b.dominate) {
            b.sum -= color;
        }
        to_change.push_back({color, old_count + count});
    }
    debug("to_change.size()=%d,b.size()=%d\n",to_change.size(),b.color_count.size());
    for (auto x : to_change) {
        check(b.color_count.find(x.first) == b.color_count.end());
        b.color_count[x.first] = x.second;
        if (x.second > b.dominate) {
            b.sum = x.first;
            b.dominate = x.second;
        } else if (x.second == b.dominate) {
            b.sum += x.first;
        }
    }
    check(b.dominate >= 0 && b.sum >= 0);
    return b;
}

void inc(stat_info &stat, int color) {
    debug("inc, color=%d\n", color);
    int count = stat.color_count[color];
    debug("before count=%d\n", count);
    if (stat.dominate > 0 && count == stat.dominate) {
        debug("stat.sum-=%d\n", color);
        stat.sum -= color;
        check(stat.sum >= 0);
    }
    count++;
    debug("after count=%d\n", count);
    stat.color_count[color] = count;
    if (count > stat.dominate) {
        stat.sum = color;
        stat.dominate = count;
        debug("new dominate and sum: %d, %d\n", stat.dominate, stat.sum);
    } else if (count == stat.dominate) {
        stat.sum += color;
        debug("inc sum by %d to %d\n", color, stat.sum);
    }
    check(stat.dominate >= 0 && stat.sum >= 0);
}

stat_info &dfs(int parent, int root) {
    debug("dfs(%d,%d)\n", parent, root);
    stat_info *new_empty = new stat_info();
    stat_info &res = *new_empty;
    for (auto child : edge[root]) {
        if (parent == child) {
            continue;
        }
        auto res_child = dfs(root, child);
        // debug("before merge\n");
        res = merge(res, res_child);
        // debug("after merge\n");
    }
    inc(res, c[root]);
    ans[root] = res.sum;
    return res;
}

void test() {
    {
        stat_info a = my_empty;
        inc(a, 3);
        debug("sum=%d,dominate=%d,map.size=%d\n", a.sum, a.dominate, a.color_count.size());
    }

    {
        debug("----------------\n");
        stat_info a = my_empty;
        a.color_count[3]++;
        a.dominate = 1;
        a.sum = 3;
        inc(a, 3);
        debug("sum=%d,dominate=%d,map.size=%d\n", a.sum, a.dominate, a.color_count.size());
        inc(a, 2);
        debug("sum=%d,dominate=%d,map.size=%d\n", a.sum, a.dominate, a.color_count.size());
        inc(a, 2);
        debug("sum=%d,dominate=%d,map.size=%d\n", a.sum, a.dominate, a.color_count.size());
    }

    {
        debug("-------merge---------\n");
        stat_info a = my_empty;
        inc(a, 3);
        inc(a, 4);
        debug("sum=%d,dominate=%d,map.size=%d\n", a.sum, a.dominate, a.color_count.size());
        stat_info b = my_empty;
        inc(b, 3);
        inc(b, 2);
        debug("sum=%d,dominate=%d,map.size=%d\n", b.sum, b.dominate, b.color_count.size());
        stat_info c = merge(a, b);
        debug("sum=%d,dominate=%d,map.size=%d\n", c.sum, c.dominate, c.color_count.size());
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    test();

    cin >> n;
    c = vi(n);
    rep(i, 0, n) {
        cin >> c[i];
    }
    edge = vvi(n);
    rep(i, 0, n - 1) {
        int x, y;
        cin >> x >> y;
        x--, y--;
        edge[x].push_back(y);
        edge[y].push_back(x);
    }
    // 对每个子树，计算颜色总和，在跟节点处要合并这些子树的颜色。

    ans = vi(n);
    dfs(0, 0);
    rep(i, 0, n) {
        cout << ans[i] << ' ';
    }
    debug("my_empty: %d, %d\n", my_empty.sum, my_empty.dominate);
}