#include <bits/stdc++.h>
using namespace std;
const int NN = (int)2e5 + 5;
int n;
int a[NN];
int dp[2][NN];
vector<int> adj[NN];
int ans[NN];
void dfs(int node, int father) {
  dp[0][node] = (a[node] == 0);
  dp[1][node] = (a[node] == 1);
  for (int child : adj[node]) {
    if (child == father) continue;
    dfs(child, node);
    if (dp[1][child] - dp[0][child] > 0) {
      dp[1][node] += dp[1][child];
      dp[0][node] += dp[0][child];
    }
  }
  return;
}
void reroot(int oldRoot, int seen) {
  for (int newRoot : adj[oldRoot]) {
    if (newRoot == seen) continue;
    int diff = dp[1][newRoot] - dp[0][newRoot];
    int old0 = dp[0][oldRoot];
    int old1 = dp[1][oldRoot];
    int new0 = dp[0][newRoot];
    int new1 = dp[1][newRoot];
    if (diff > 0) {
      dp[1][oldRoot] -= dp[1][newRoot];
      dp[0][oldRoot] -= dp[0][newRoot];
    }
    if (dp[1][oldRoot] - dp[0][oldRoot] > 0) {
      dp[1][newRoot] += dp[1][oldRoot];
      dp[0][newRoot] += dp[0][oldRoot];
    }
    reroot(newRoot, oldRoot);
    dp[0][oldRoot] = old0;
    dp[1][oldRoot] = old1;
    dp[0][newRoot] = new0;
    dp[1][newRoot] = new1;
  }
  ans[oldRoot] = dp[1][oldRoot] - dp[0][oldRoot];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    x--, y--;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  dfs(0, -1);
  reroot(0, -1);
  for (int i = 0; i < n; i++) printf("%d ", ans[i]);
  return 0;
}
