#include <bits/stdc++.h>
template <class T>
inline T qmin(T a, T b) {
  return a < b ? a : b;
}
template <class T>
inline T qmax(T a, T b) {
  return a > b ? a : b;
}
const double eps = 1e-9;
const double PI = acos(-1);
const int inf = 0x3f3f3f3f;
const int mod = (int)1e9 + 7;
const int maxn = (int)2e5 + 5;
const int maxm = (int)1e5 + 5;
const long long INF = 0x3f3f3f3f3f3f3f3f;
using namespace std;
int n;
int a[maxn];
vector<int> G[maxn];
int dp[maxn];
int ans[maxn];
void dfs1(int u, int fa) {
  dp[u] = a[u];
  for (int v : G[u]) {
    if (v == fa) continue;
    dfs1(v, u);
    dp[u] += max(0, dp[v]);
  }
}
void dfs2(int u, int fa) {
  if (dp[u] >= 0) {
    int t = ans[fa] - dp[u];
    ans[u] = max(dp[u], dp[u] + t);
  } else {
    ans[u] = dp[u] + max(0, ans[fa]);
  }
  for (int v : G[u]) {
    if (v == fa) continue;
    dfs2(v, u);
  }
}
int main() {
  scanf("%d", &n);
  for (__typeof(n) i = 1; i <= (n); i++) {
    scanf("%d", a + i);
    if (a[i] == 0) a[i] = -1;
  }
  for (__typeof(n - 1) i = 1; i <= (n - 1); i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs1(1, 0);
  dfs2(1, 0);
  for (__typeof(n) i = 1; i <= (n); i++) printf("%d ", ans[i]);
  puts("");
  return 0;
}
