#include <bits/stdc++.h>
using namespace std;
int n;
int a[222222];
vector<int> adj[222222], child[222222];
bool chk[222222];
int dp[222222];
int ans[222222];
void reform(int v) {
  chk[v] = true;
  for (int x : adj[v]) {
    if (!chk[x]) {
      child[v].push_back(x);
      reform(x);
    }
  }
}
void dfs(int v) {
  dp[v] = a[v];
  for (int x : child[v]) {
    dfs(x);
    dp[v] += max(0, dp[x]);
  }
}
void dfs2(int v) {
  ans[v] = dp[v];
  for (int x : child[v]) {
    dp[v] -= max(0, dp[x]);
    dp[x] += max(0, dp[v]);
    dfs2(x);
    dp[x] -= max(0, dp[v]);
    dp[v] += max(0, dp[x]);
  }
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    if (a[i] == 0) a[i] = -1;
  }
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  reform(1);
  dfs(1);
  dfs2(1);
  for (int i = 1; i <= n; ++i) {
    cout << ans[i] << " ";
  }
  cout << "\n";
}
