#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int black[maxn];
vector<int> nex[maxn];
int dp[maxn];
void dfs1(int now, int fa) {
  dp[now] = black[now] ? -1 : 1;
  for (auto x : nex[now]) {
    if (x == fa) continue;
    dfs1(x, now);
    if (dp[x] >= 0) dp[now] += dp[x];
  }
}
int ans[maxn];
void dfs2(int now, int fa) {
  for (auto x : nex[now]) {
    if (x == fa) continue;
    if (dp[x] >= 0)
      ans[x] = max(dp[x], ans[now]);
    else
      ans[x] = max(dp[x], ans[now] + dp[x]);
    dfs2(x, now);
  }
}
int main() {
  int n, u, v;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> u;
    black[i] = u ^ 1;
  }
  for (int i = 1; i < n; i++) {
    cin >> u >> v;
    nex[u].push_back(v);
    nex[v].push_back(u);
  }
  dfs1(1, 0);
  ans[1] = dp[1];
  dfs2(1, 0);
  for (int i = 1; i <= n; i++) cout << ans[i] << ' ';
  return 0;
}
