#include <bits/stdc++.h>
using namespace std;
const long long N = 200010;
inline long long read() {
  register char cc = getchar();
  register long long cn(0), flus(1);
  while (cc > '9' || cc < '0') {
    if (cc == '-') flus = -flus;
    cc = getchar();
  }
  while (cc >= '0' && cc <= '9') {
    cn = cn * 10 + cc - 48;
    cc = getchar();
  }
  return cn * flus;
}
struct edge {
  long long to, next;
} e[N << 1];
long long col[N];
long long head[N], tot;
inline void add(long long u, long long v) {
  e[++tot].next = head[u];
  head[u] = tot;
  e[tot].to = v;
}
long long dp[N];
long long ans[N];
void dfs1(long long u, long long last) {
  if (col[u] == 1) {
    dp[u] = 1;
  } else
    dp[u] = -1;
  for (long long i = head[u]; i; i = e[i].next) {
    long long v = e[i].to;
    if (v == last) continue;
    dfs1(v, u);
    dp[u] += max(0ll, dp[v]);
  }
}
long long sum = 0;
void dfs2(long long u, long long last) {
  if (last == 0)
    ans[u] = dp[u];
  else {
    if (dp[u] >= 0)
      ans[u] = max(dp[u], ans[last]);
    else
      ans[u] = max(ans[last], 0ll) + (col[u] == 1 ? 1 : -1);
  }
  for (long long i = head[u]; i; i = e[i].next) {
    long long v = e[i].to;
    if (v == last) continue;
    dfs2(v, u);
  }
}
signed main() {
  long long n = read();
  for (register long long i = 1; i <= n; ++i) {
    col[i] = read();
  }
  for (register long long i = 2; i <= n; ++i) {
    long long u = read(), v = read();
    ;
    add(u, v);
    add(v, u);
  }
  dfs1(1, 0);
  dfs2(1, 0);
  for (register long long i = 1; i <= n; ++i) {
    printf("%d ", ans[i]);
  }
  return 0;
}
