#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100;
int a[N], dp[N], ans[N];
vector<int> g[N];
void dfs1(int u, int fa) {
  dp[u] = a[u];
  for (auto v : g[u]) {
    if (v == fa) continue;
    dfs1(v, u);
    dp[u] += max(dp[v], 0);
  }
}
void dfs2(int u, int fa, int sum) {
  ans[u] = dp[u] + sum;
  for (auto v : g[u]) {
    if (v == fa) continue;
    dfs2(v, u, max(0, ans[u] - max(0, dp[v])));
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == 0) a[i] = -1;
  }
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  dfs1(1, -1);
  dfs2(1, -1, 0);
  for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
  return 0;
}
