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

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 300010

vector<int> G[SIZE];
int A1[SIZE], A2[SIZE];

pair<bool,int> res1[SIZE], res2[SIZE];

void dfs1(int now, int back = -1) {

  bool dead = A1[now] <= 0;
  int step = A1[now] > 0;

  A1[now] -= 1;

  for(int to : G[now]) {
    if (to == back) continue;

    dfs1(to, now);

    if(!dead) step += res1[to].second;
    dead |= res1[to].first;

    dead |= A1[now] <= 0;
    A1[now] -= 1;
    if(!dead) step++;
  }

  res1[now] = {dead, step};
}

int ans[SIZE];

void dfs2(int now, int back = -1) {
  set<int> deadList;
  map<int,int> idx;

  vector<pair<int,pair<bool,int> > > vec;

  for (int to : G[now]) {
    if (to == back)
      vec.push_back({to, res2[to]});
    else
      vec.push_back({to, res1[to]});
  }

  sort(vec.begin(), vec.end());

  vector<int> sum(G[now].size());

  for(int i=0; i<vec.size(); i++){
    idx[vec[i].first] = i;

    if (vec[i].second.first)
      deadList.insert(i);

    sum[i] = vec[i].second.second;
    if (i > 0) sum[i] += sum[i-1];
  }

  int tmpA = A2[now];

  ans[now] = tmpA > 0;
  tmpA--;

  debug(now);
  debug(res2[0]);
  debug(vec);

  for(int i=0; i<vec.size(); i++){
    if (tmpA < 0) break;
    ans[now] += vec[i].second.second;
    if (vec[i].second.first) break;
    if(tmpA <= 0) break;
    tmpA--;
    ans[now]++;
  }

  for(int to : G[now]) {
    if (to == back) continue;

    auto it = deadList.begin();

    if (it != deadList.end() && *it == idx[to]) it++;

    int deadLine = INF;

    if(it != deadList.end()){
      deadLine = *it;
    }

    int x = A2[now] - 1;

    if(x >= idx[to]) x++;

    deadLine = min(deadLine, x);

    int step = 0;
    if (deadLine >= (int)G[now].size())
      step = sum[G[now].size()-1] - vec[idx[to]].second.second;
    else if (deadLine < 0)
      step = 0;
    else
      step = sum[deadLine] - vec[idx[to]].second.second * (deadLine >= idx[to]);

    res2[now] = {deadLine < (int)G[now].size(), step + 1 + min(deadLine, (int)G[now].size()) - (deadLine >= idx[to])};

    dfs2(to, now);
  }
}


int N, A[SIZE];

int main(){

  scanf("%d", &N);

  for(int i=0; i<N; i++) {
    scanf("%d", A+i);
    A1[i] = A[i];
    A2[i] = A[i];
  }

  for(int i=0; i<N-1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--; v--;

    G[u].push_back(v);
    G[v].push_back(u);
  }

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

  dfs1(0);
  dfs2(0);

  for(int i=0; i<N; i++) {
    printf("%d\n", ans[i]);
  }

  return 0;
}



Measured memory_bytes: 51796