#include <bits/stdc++.h>
using namespace std;
int n, arr[200020], depth[200020], dp[200020], ans[200020];
vector<int> v[200020];
int dfs(int node, int p) {
  depth[node] = depth[p] + 1;
  long long sum = 0;
  bool leaf = true;
  for (int i = 0; i < v[node].size(); i++) {
    int cur = v[node][i], x;
    if (cur != p) x = dfs(cur, node), sum += (x == -1 ? 0 : x), leaf = false;
  }
  if (leaf) return dp[node] = (arr[node] ? 1 : -1);
  return dp[node] = sum + (arr[node] ? 1 : -1);
}
void dfs2(int node, int p) {
  for (int i = 0; i < v[node].size(); i++) {
    int cur = v[node][i];
    if (cur != p) {
      dp[node] -= max(0, dp[cur]);
      dp[cur] += max(0, dp[node]);
      ans[cur] = dp[cur];
      dfs2(cur, node);
      dp[cur] -= max(0, dp[node]);
      dp[node] += max(0, dp[cur]);
    }
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> arr[i];
  for (int i = 0, a, b; i < n - 1; i++) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  ans[1] = dfs(1, 0);
  dfs2(1, -1);
  for (int i = 1; i <= n; i++) cout << ans[i] << " ";
  cout << endl;
  return 0;
}
