#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 5;
int dp[MX], ans[MX], a[MX];
vector<int> adj[MX];
void dfs(int node, int par) {
  dp[node] = a[node];
  for (auto& i : adj[node]) {
    if (i != par) {
      dfs(i, node);
      dp[node] += max(0, dp[i]);
    }
  }
}
void dfs1(int node, int par) {
  ans[node] = dp[node];
  for (auto& i : adj[node]) {
    if (i != par) {
      dp[node] -= max(dp[i], 0);
      dp[i] += max(dp[node], 0);
      dfs1(i, node);
      dp[i] -= max(dp[node], 0);
      dp[node] += max(dp[i], 0);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> 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 u, v;
    cin >> u >> v;
    u--;
    v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(0, -1);
  dfs1(0, -1);
  for (int i = 0; i < n; i++) cout << ans[i] << " ";
}
