#include <bits/stdc++.h>
using namespace std;
vector<int> dp;
vector<vector<int> > tree;
vector<int> color, visited;
void dfs(int source, int parent) {
  if (visited[source])
    return;
  else
    visited[source] = 1;
  int value = color[source] == 0 ? -1 : 1;
  int ans = 0;
  int i;
  for (i = 0; i < tree[source].size(); ++i) {
    int node = tree[source][i];
    if (node == parent) continue;
    dfs(node, source);
    ans += max((int)0, dp[node]);
  }
  dp[source] = ans + value;
  return;
}
void dfs2(int source, int parent, int t) {
  if (visited[source])
    return;
  else
    visited[source] = 1;
  int value = color[source] == 0 ? -1 : 1;
  dp[source] += max((int)0, t);
  int i;
  for (i = 0; i < tree[source].size(); ++i) {
    int node = tree[source][i];
    if (node == parent) continue;
    int t = dp[source] - max((int)0, dp[node]);
    dfs2(node, source, t);
  }
  return;
}
int main() {
  int n;
  cin >> n;
  int i, j;
  dp.assign(n + 1, 0);
  color.assign(n + 1, 0);
  tree.assign(n + 1, vector<int>(0, 0));
  for (i = 1; i <= n; ++i) cin >> color[i];
  for (i = 1; i <= n - 1; ++i) {
    int x, y;
    cin >> x >> y;
    tree[x].push_back(y);
    tree[y].push_back(x);
  }
  int root = 1;
  visited.assign(n + 1, 0);
  dfs(root, -1);
  visited.assign(n + 1, 0);
  dfs2(root, -1, 0);
  for (i = 1; i <= n; ++i) cout << dp[i] << " ";
  return 0;
}
