#include <bits/stdc++.h>
using namespace std;
int a[300000];
vector<int> G[300000];
int dp[300000];
int ans[300000];
void dfs1(int u, int fa) {
  dp[u] = a[u];
  for (auto i : G[u]) {
    if (i == fa) {
      continue;
    }
    dfs1(i, u);
    dp[u] += max(dp[i], 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++) {
    scanf("%d", a + i);
    if (a[i] == 0) a[i] = -1;
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs1(1, -1);
  dfs2(1, -1, 0);
  for (int i = 1; i <= n; i++) {
    printf("%d ", ans[i]);
  }
}
