#include <bits/stdc++.h>
using namespace std;
inline int read() {
  char c = getchar();
  int x = 0, f = 1;
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * f;
}
const int MAXN = 4e5 + 5;
int to[MAXN], nxt[MAXN], head[MAXN];
int cnte;
int a[MAXN], f[MAXN], g[MAXN];
inline void addedge(int x, int y) {
  to[++cnte] = y;
  nxt[cnte] = head[x];
  head[x] = cnte;
}
inline void dfs1(int x, int fth) {
  if (a[x] == 1)
    f[x] = 1;
  else
    f[x] = -1;
  for (int i = head[x]; i; i = nxt[i]) {
    int y = to[i];
    if (y != fth) {
      dfs1(y, x);
      f[x] += max(0, f[y]);
    }
  }
}
inline void dfs2(int x, int fth) {
  for (int i = head[x]; i; i = nxt[i]) {
    int y = to[i];
    if (y != fth) {
      g[y] = f[y] + max(0, g[x] - max(0, f[y]));
      dfs2(y, x);
    }
  }
}
int main() {
  int N = read();
  for (int i = 1; i <= N; i++) a[i] = read();
  for (int i = 1; i < N; i++) {
    int x = read(), y = read();
    addedge(x, y);
    addedge(y, x);
  }
  dfs1(1, 0);
  g[1] = f[1];
  dfs2(1, 0);
  for (int i = 1; i <= N; i++) printf("%d%c", g[i], " \n"[i == N]);
  return 0;
}
