#include <bits/stdc++.h>
using namespace std;
using lli = long long;
using lld = long double;
using ulli = unsigned long long int;
using pll = pair<lli, lli>;
using ttt = pair<lli, pll>;
using vttt = vector<ttt>;
using vll = vector<pll>;
using vl = vector<lli>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using cd = complex<lld>;
int answer(int idx, int prv, vector<vector<int>>& gp, vector<int>& ans,
           vector<int>& A) {
  int tmp = 0;
  for (int i = 0; i < gp[idx].size(); i++) {
    if (gp[idx][i] != prv) {
      tmp += max(0, answer(gp[idx][i], idx, gp, ans, A));
    }
  }
  if (A[idx] == 0)
    ans[idx] = -1 + tmp;
  else
    ans[idx] = 1 + tmp;
  return ans[idx];
}
void reroot(int idx, int prv, vector<int>& ans, vector<vector<int>>& gp) {
  int prvans = ans[prv];
  prvans -= max(0, ans[idx]);
  ans[idx] = ans[idx] + max(0, prvans);
  for (int i = 0; i < gp[idx].size(); i++) {
    int k = gp[idx][i];
    if (gp[idx][i] != prv) {
      reroot(k, idx, ans, gp);
    }
  }
}
void sanjeev(int kkkk) {
  int n;
  cin >> n;
  vector<int> A;
  A.push_back(0);
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    A.push_back(tmp);
  }
  vector<vector<int>> gp(n + 1, vector<int>());
  for (int i = 1; i < n; i++) {
    int s, e;
    cin >> s >> e;
    gp[s].push_back(e);
    gp[e].push_back(s);
  }
  vector<int> ans(n + 1, 0);
  int tmp = answer(1, 0, gp, ans, A);
  reroot(1, 0, ans, gp);
  for (int i = 1; i < ans.size(); i++) cout << ans[i] << " ";
}
int main() {
  if (!0) ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed << setprecision(10);
  int t = 1;
  for (int i = 0; i < t; i++) {
    sanjeev(i + 1);
  }
  if (0) system("pause");
}
