#include <bits/stdc++.h>
using namespace std;
const long long mod = (long long)1e9 + 7;
int n;
int best[500500];
int a[500500], gp[500500], dp[500500];
vector<int> g[500500];
void dfs(int v, int p) {
  dp[v] = 1;
  if (a[v] == 0) dp[v] = -1;
  for (int to : g[v]) {
    if (to == p) continue;
    dfs(to, v);
    if (dp[to] > 0) dp[v] += dp[to];
  }
}
void solve(int v, int p) {
  if (p != 0) {
    int val = dp[p];
    if (dp[v] > 0) val -= dp[v];
    gp[v] = max(gp[v], val + gp[p]);
  }
  best[v] = dp[v] + gp[v];
  for (int to : g[v])
    if (to != p) solve(to, v);
}
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    cin >> a >> b;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }
  dfs(1, 0);
  solve(1, 0);
  for (int i = 1; i <= n; ++i) cout << best[i] << " ";
  return 0;
}
