#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;


template<typename F>
struct FixPoint : F{
  FixPoint(F&& f):F(forward<F>(f)){}
  template<typename... Args>
  decltype(auto) operator()(Args&&... args) const{
    return F::operator()(*this,forward<Args>(args)...);
  }
};
template<typename F>
inline decltype(auto) MFP(F&& f){
  return FixPoint<F>{forward<F>(f)};
}

//INSERT ABOVE HERE
signed main(){
  Int n;
  cin>>n;
  vector<Int> as(n);
  for(Int i=0;i<n;i++) cin>>as[i];

  vector< vector<Int> > G(n);
  for(Int i=1;i<n;i++){
    Int u,v;
    cin>>u>>v;
    u--;v--;
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }

  for(Int i=0;i<n;i++)
    sort(G[i].begin(),G[i].end());

  // dead?, #step
  using P = pair<Int, Int>;
  vector< vector<P> > dp(n);

  auto dfs1=
    MFP([&](auto dfs,Int v,Int p)->P{
          for(Int u:G[v]){
            if(u==p) continue;
            auto res=dfs(u,v);
            dp[v].emplace_back(res);
          }
          if(as[v]==0) return P(1,0);

          Int num=as[v]-1,idx=0;
          Int dead=0,step=1;
          for(Int u:G[v]){
            if(u==p) continue;
            step+=dp[v][idx].second;
            if(dp[v][idx].first){
              dead=1;
              break;
            }
            if(num==0){
              dead=1;
              break;
            }
            num--;
            step++;
            idx++;
          }
          return P(dead,step);
        });
  dfs1(0,-1);

  //assert(*min_element(as.begin(),as.end())>0);

  vector<Int> ans(n,-1);
  MFP([&](auto dfs,Int v,Int p,P f)->void{
        if(as[v]==0){
          ans[v]=0;
          for(Int u:G[v]){
            if(u==p) continue;
            dfs(u,v,P(1,0));
          }
          return;
        }

        // find ans[v]
        vector<Int> to;
        for(Int u:G[v]){
          if(u==p) continue;
          to.emplace_back(u);
        }

        Int dead=0,step=1;
        Int num=as[v]-1;

        Int flg_for_check=0;
        auto check=
          [&](){
            if(dead) return;
            if(p<0) return;
            if(flg_for_check) return;
            flg_for_check=1;
            dead|=f.first;
            step+=f.second;

            // up from par
            if(dead||num==0){
              dead=1;
              return;
            }
            num--;
            step++;
          };

        for(Int i=0;i<(Int)to.size();i++){
          if(p<to[i]) check();
          if(dead) break;

          step+=dp[v][i].second;
          if(dp[v][i].first||num==0){
            dead=1;
            break;
          }
          num--;
          step++;
        }
        check();

        ans[v]=step;

        // call child
        for(Int u:G[v]){
          if(u==p) continue;

          flg_for_check=0;
          dead=0;step=1;num=as[v]-1;

          for(Int i=0;i<(Int)to.size();i++){
            if(p<to[i]) check();
            if(dead) break;

            if(to[i]==u) continue;

            step+=dp[v][i].second;
            if(dp[v][i].first||num==0){
              dead=1;
              break;
            }
            num--;
            step++;
          }

          check();
          dfs(u,v,P(dead,step));
        }
      })(0,-1,P(0,0));

  for(Int i=0;i<n;i++) cout<<ans[i]<<"\n";
  cout<<flush;
  return 0;
}



Measured memory_bytes: 47128