#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int a[maxn], dp[maxn], root = 1, ans[maxn], n;
vector<int> e[maxn];
inline void addEdge(int u, int v) {
  e[u].push_back(v);
  e[v].push_back(u);
}
void dfs(int u, int fa) {
  dp[u] = a[u] == 0 ? -1 : 1;
  for (int i = 0; i < e[u].size(); ++i) {
    if (e[u][i] != fa) {
      dfs(e[u][i], u);
      dp[u] += max(0, dp[e[u][i]]);
    }
  }
}
void dfs2(int u, int fa) {
  ans[u] = dp[u];
  for (int i = 0; i < e[u].size(); ++i) {
    dp[u] -= max(0, dp[e[u][i]]);
    dp[e[u][i]] += max(0, dp[u]);
    if (e[u][i] != fa) dfs2(e[u][i], u);
    dp[e[u][i]] -= max(0, dp[u]);
    dp[u] += max(0, dp[e[u][i]]);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  for (int i = 1; i < n; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    addEdge(u, v);
  }
  dfs(root, 0);
  dfs2(root, 0);
  printf("%d", ans[1]);
  for (int i = 2; i <= n; ++i) printf(" %d", ans[i]);
  printf("\n");
  return 0;
}
