#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> adj[200002];
int a[200002];
int ans[200002];
int val[200002];
void dfs1(int v = 1, int p = -1) {
  ans[v] = a[v];
  for (auto vv : adj[v]) {
    if (vv == p) continue;
    dfs1(vv, v);
    ans[v] += max(0, ans[vv]);
  }
}
void dfs2(int v = 1, int p = 0) {
  val[v] = ans[v];
  for (auto vv : adj[v]) {
    if (vv == p) continue;
    ans[v] -= max(0, ans[vv]);
    ans[vv] += max(0, ans[v]);
    dfs2(vv, v);
    ans[vv] -= max(0, ans[v]);
    ans[v] += max(0, ans[vv]);
  }
}
void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (!a[i]) a[i] = -1;
  }
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  a[0] = ans[0] = val[0] = 0;
  dfs1();
  dfs2();
  for (int i = 1; i <= n; i++) cout << val[i] << " ";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) {
    solve();
  }
}
