#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;


vector<int> v[303030];
vector<int> sum[303030],die[303030];
int a[303030];

pair<int,int> dfs(int x,int p){
    int m = v[x].size();
    die[x].resize(m+1), sum[x].resize(m+1);
    rep(i,m){
        if(v[x][i]==p)continue;
        auto ret = dfs(v[x][i],x);
        die[x][i+1] += ret.first;
        sum[x][i+1] += ret.second;
    }
    int ans = 0;
    int c = 0;
    rep(i,m){
        if(v[x][i]==p)continue;
        if(a[x]<=c)return {1,ans};
        ++ans;
        if(die[x][i+1])return {1,ans+sum[x][i+1]};
        ans += sum[x][i+1];
        ++c;
    }
    if(a[x]<=c)return {1,ans};
    ++ans;
    return {0,ans};
}

pair<int,int> ret[303030];
int ANS[303030];

void dfs2(int x,int p){
    int m = v[x].size();
    rep(i,m){
        if(v[x][i]==p)die[x][i+1]=ret[x].first,sum[x][i+1]=ret[x].second;
        die[x][i+1]+=die[x][i];
        sum[x][i+1]+=sum[x][i];
    }

    int ans = 0;
    bool ok=true;
    rep(j,m){
        if(a[x]<=j){
            ok=false;
            break;
        }
        ++ans;
        if(die[x][j+1]!=die[x][j]){
            ans+=sum[x][j+1]-sum[x][j];
            ok=false;
            break;
        }
        else ans += sum[x][j+1]-sum[x][j];
    }
    if(ok&&a[x]>=m+1)++ans;
    int ans2 = 0;
    ok=true;
    rep(j,m){
        if(a[x]<=j-1){
            ok=false;
            break;
        }
        ++ans2;
        if(die[x][j+1]!=die[x][j]){
            ans2+=sum[x][j+1]-sum[x][j];
            ok=false;
            break;
        }
        else ans2+=sum[x][j+1]-sum[x][j];
    }
    if(ok&&a[x]>=m)++ans2;
    else ok=false;
    rep(i,m){
        int to = v[x][i];
        if(v[x][i]==p)continue;
        if(i+1>a[x]){
            ret[to]={1,ans};
        }
        else {
            if(die[x][i]>=1){
                ret[to]={1,ans};
            }
            else if(die[x][i+1]==0){
                ret[to]={!ok,ans2-sum[x][i+1]+sum[x][i]-1};
            }
            else {
                int ans3 = 0;
                bool ok3=true;
                int c = 0;
                rep(j,m){
                    if(j==i)continue;
                    if(a[x]<=c){
                        ok3=false;
                        break;
                    }
                    ++ans3;
                    ++c;
                    if(die[x][j+1]!=die[x][j]){
                        ans3+=sum[x][j+1]-sum[x][j];
                        ok3=false;
                        break;
                    }
                    else ans3+=sum[x][j+1]-sum[x][j];
                }
                if(ok3&&a[x]>=m)++ans3;
                else ok3=false;
                ret[to]={!ok3,ans3};
            }
        }
    }
    ANS[x]=ans;
    for(auto to : v[x]){
        if(to==p)continue;
        dfs2(to,x);
    }
}
int main(){
    int n;
    cin>>n;
    rep(i,n)cin>>a[i];
    rep(i,n-1){
        int x,y;
        cin>>x>>y;
        --x;--y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    rep(i,n)sort(v[i].begin(),v[i].end());
    dfs(0,-1);
    dfs2(0,-1);
    rep(i,n)cout<<ANS[i]<<"\n";
    return 0;
}


Measured memory_bytes: 61888