#include <bits/stdc++.h>
using namespace std;
long long int power(long long int a, long long int b, long long int mod) {
  long long int ans = 1;
  while (b) {
    if (b & 1LL) ans = (ans * a) % mod;
    a = (a * a) % mod;
    b = b >> 1LL;
  }
  return ans;
}
long long int u, v;
long long int n;
long long int a[2 * 100005];
long long int ans[2 * 100005];
std::vector<long long int> adj[2 * 100005];
long long int dp[2 * 100005];
long long int dfs(long long int src, long long int prev) {
  dp[src] = ans[src];
  for (auto it : adj[src]) {
    if (it != prev) {
      dp[src] += max(0LL, dfs(it, src));
    }
  }
  return dp[src];
}
void dfs2(long long int src, long long int prev) {
  ans[src] = dp[src];
  for (auto it : adj[src]) {
    if (it != prev) {
      dp[src] -= max(0LL, dp[it]);
      dp[it] += max(0LL, dp[src]);
      dfs2(it, src);
      dp[it] -= max(0LL, dp[src]);
      dp[src] += max(0LL, dp[it]);
    }
  }
  return;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (long long int i = 1; i < n + 1; i++) {
    cin >> a[i];
    if (a[i] == 0) {
      ans[i] = -1;
    } else
      ans[i] = 1;
  }
  for (long long int i = 0; i < n - 1; i++) {
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1, -1);
  dfs2(1, -1);
  for (long long int i = 1; i < n + 1; i++) {
    cout << ans[i] << " ";
  }
  return 0;
}
