#include <bits/stdc++.h>
using namespace std;
void fileio() {}
vector<vector<long long int>> g;
long long int n;
vector<long long int> a, dp;
long long int dfs1(long long int u, long long int p) {
  dp[u] = 1;
  if (!a[u]) dp[u] = -1;
  for (auto child : g[u]) {
    if (child == p) continue;
    dp[u] += max(dfs1(child, u), 0ll);
  }
  return dp[u];
}
void dfs2(long long int u, long long int p, long long int val) {
  dp[u] += val;
  for (auto child : g[u]) {
    if (child == p) continue;
    dfs2(child, u, max(0ll, dp[u] - max(dp[child], 0ll)));
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  fileio();
  cin >> n;
  dp = vector<long long int>(n, 0);
  a = vector<long long int>(n, 0);
  for (long long int i = 0; i < n; i++) cin >> a[i];
  g = vector<vector<long long int>>(n, vector<long long int>(0));
  for (long long int i = 0; i < n - 1; i++) {
    long long int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs1(0, 0);
  dfs2(0, 0, 0);
  for (auto it : dp) cout << it << " ";
  return 0;
}
