#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const int MX = 2e5 + 7;
const int mod = 1e9 + 7;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long quimod(long long a, long long k) {
  long long res = 1;
  while (k) {
    if (k & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    k >>= 1;
  }
  return res;
}
int n;
int cnt, head[MX];
int val[MX];
int dp[MX];
int ans[MX];
struct Edge {
  int v, next;
} e[MX << 1];
void add(int u, int v) {
  e[++cnt].v = v;
  e[cnt].next = head[u];
  head[u] = cnt;
  e[++cnt].v = u;
  e[cnt].next = head[v];
  head[v] = cnt;
}
void predfs(int u, int fa) {
  dp[u] = val[u];
  for (int i = head[u]; i; i = e[i].next) {
    int v = e[i].v;
    if (v == fa) continue;
    predfs(v, u);
    dp[u] += max(0, dp[v]);
  }
}
void dfs(int u, int fa) {
  ans[u] = dp[u];
  for (int i = head[u]; i; i = e[i].next) {
    int v = e[i].v;
    if (v == fa) continue;
    int tmpu = dp[u], tmpv = dp[v];
    dp[u] -= max(0, dp[v]);
    dp[v] += max(0, dp[u]);
    dfs(v, u);
    dp[u] = tmpu;
    dp[v] = tmpv;
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> val[i];
    if (val[i] == 0) val[i] = -1;
  }
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    add(u, v);
  }
  predfs(1, -1);
  dfs(1, -1);
  for (int i = 1; i <= n; ++i) {
    cout << ans[i] << " ";
  }
  cout << '\n';
  return 0;
}
