#include <bits/stdc++.h>
using namespace std;
const long long M = 998244353;
const long long nax = 2e5 + 5;
const long long inf = 9e18;
using pii = pair<long long, long long>;
long long n, a[nax], ans[nax], dp[nax];
vector<vector<long long> > v;
long long dfs(long long s, long long p) {
  dp[s] = 0;
  if (a[s])
    dp[s]++;
  else
    dp[s]--;
  for (auto j : v[s]) {
    if (j == p) continue;
    long long temp = dfs(j, s);
    if (temp >= 0) dp[s] += temp;
  }
  return dp[s];
}
void dfs1(long long s, long long p, long long last) {
  ans[s] = dp[s] + last;
  for (auto i : v[s]) {
    if (i == p) continue;
    dfs1(i, s, max(0LL, ans[s] - max(0LL, dp[i])));
  }
}
void solve() {
  cin >> n;
  long long x, y;
  v.resize(n + 1);
  for (int i = 1; i < n + 1; ++i) {
    cin >> a[i];
  }
  for (int i = 1; i < n; ++i) {
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  x = dfs(1, 1);
  dfs1(1, 1, 0);
  for (int i = 1; i <= n; ++i) {
    cout << ans[i] << " ";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1, t1 = 1;
  while (t--) {
    solve();
  }
  return 0;
}
