#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int head[N], edgenum = 0, dp[N], n, g[N], a[N], ans[N];
struct Edge {
  int nxt, to;
} edge[2 * N];
inline int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return f * x;
}
inline void addedge(int from, int to) {
  edge[++edgenum].nxt = head[from];
  edge[edgenum].to = to;
  head[from] = edgenum;
}
inline void dfs1(int u, int fa) {
  dp[u] = a[u];
  for (int i = head[u]; i; i = edge[i].nxt)
    if (edge[i].to != fa) {
      dfs1(edge[i].to, u);
      dp[u] += max(dp[edge[i].to], 0);
    }
}
inline void dfs2(int u, int fa) {
  ans[u] = dp[u] + max(g[u] - a[u], 0);
  for (int i = head[u]; i; i = edge[i].nxt)
    if (edge[i].to != fa) {
      g[edge[i].to] = max(a[edge[i].to], g[u] + dp[u] - max(0, dp[edge[i].to]) +
                                             a[edge[i].to] - a[u]);
      dfs2(edge[i].to, u);
    }
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    int x = read();
    a[i] = (x == 1) ? 1 : -1;
  }
  for (int i = 1; i < n; i++) {
    int x = read(), y = read();
    addedge(x, y);
    addedge(y, x);
  }
  dfs1(1, 0);
  g[1] = a[1];
  dfs2(1, 0);
  for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
  return 0;
}
