#include <bits/stdc++.h>
using namespace std;
long long arr[200005];
vector<long long> tree[200005];
long long in_dp[200005], out_dp[200005];
void dfs1(long long u, long long par) {
  in_dp[u] = 2 * arr[u] - 1;
  for (int i = 0; i < tree[u].size(); i++) {
    if (tree[u][i] == par) continue;
    dfs1(tree[u][i], u);
    if (in_dp[tree[u][i]] > 0) in_dp[u] += in_dp[tree[u][i]];
  }
}
void dfs2(long long u, long long par) {
  for (int i = 0; i < tree[u].size(); i++) {
    if (tree[u][i] == par) continue;
    out_dp[tree[u][i]] = max(
        in_dp[u] - (in_dp[tree[u][i]] > 0 ? in_dp[tree[u][i]] : 0),
        in_dp[u] - (in_dp[tree[u][i]] > 0 ? in_dp[tree[u][i]] : 0) + out_dp[u]);
    dfs2(tree[u][i], u);
  }
}
void solve() {
  long long n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> arr[i];
  for (int i = 0; i < n - 1; i++) {
    long long u, v;
    cin >> u >> v;
    tree[u].push_back(v);
    tree[v].push_back(u);
  }
  dfs1(1, 0);
  dfs2(1, 0);
  for (int i = 1; i <= n; i++) {
    if (out_dp[i] > 0)
      cout << in_dp[i] + out_dp[i] << " ";
    else
      cout << in_dp[i] << " ";
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long t;
  t = 1;
  while (t--) {
    solve();
  }
}
