#include <bits/stdc++.h>
using namespace std;
vector<int> a;
vector<int> ans;
vector<int> dp;
vector<vector<int>> g;
void dfs(int vertex, int p = -1) {
  dp[vertex] = a[vertex];
  for (auto to : g[vertex]) {
    if (to == p) continue;
    dfs(to, vertex);
    dp[vertex] += max(dp[to], 0);
  }
}
void dfs2(int vertex, int p = -1) {
  ans[vertex] = dp[vertex];
  for (auto to : g[vertex]) {
    if (to == p) continue;
    dp[vertex] -= max(0, dp[to]);
    dp[to] += max(0, dp[vertex]);
    dfs2(to, vertex);
    dp[to] -= max(0, dp[vertex]);
    dp[vertex] += max(0, dp[to]);
  }
}
int main() {
  int n;
  cin >> n;
  ans = dp = a = vector<int>(n);
  g = vector<vector<int>>(n);
  for (int i = 0; i < (n); i++) {
    cin >> a[i];
    if (a[i] == 0) a[i] = -1;
  }
  for (int i = 0; i < (n - 1); i++) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  dfs(0);
  dfs2(0);
  for (int i = 0; i < (n); i++) cout << ans[i] << " ";
  cout << endl;
  return 0;
}
