#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll inf = 1000000001, INF = (ll)1e18 + 1;
vector<int> dp;
vector<int> ans;
vector<int> color;
vector<vector<int> > adj;
int dfs(int node, int father) {
  dp[node] = (color[node] ? 1 : -1);
  for (int v : adj[node]) {
    if (v == father) continue;
    dp[node] += max(0, dfs(v, node));
  }
  return dp[node];
}
void reroot(int node, int father) {
  for (int v : adj[node]) {
    if (v == father) continue;
    int restNode = dp[node], restV = dp[v];
    dp[node] -= max(0, dp[v]);
    dp[v] += max(0, dp[node]);
    ans[v] = dp[v];
    reroot(v, node);
    dp[node] = restNode;
    dp[v] = restV;
  }
}
void solve() {
  int n;
  cin >> n;
  adj.resize(n);
  color.resize(n);
  dp.resize(n);
  ans.resize(n);
  for (int i = 0; i < n; i++) cin >> color[i];
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(0, -1);
  ans[0] = dp[0];
  reroot(0, -1);
  for (int i = 0; i < n; i++) cout << ans[i] << " ";
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  solve();
  return 0;
}
