#include <bits/stdc++.h>
using namespace std;
vector<long long> AdjList[200005];
long long dfs_num[200005], dp[200005], ans[200005], a[200005];
void dfs(int u) {
  dfs_num[u] = 1;
  dp[u] = a[u];
  for (int j = 0; j < (int)AdjList[u].size(); j++) {
    long long v = AdjList[u][j];
    if (dfs_num[v] == 0) {
      dfs(v);
      dp[u] += max(dp[v], (long long)0);
    }
  }
}
void dfs2(int u) {
  dfs_num[u] = 1;
  ans[u] = dp[u];
  for (int j = 0; j < (int)AdjList[u].size(); j++) {
    long long v = AdjList[u][j];
    if (dfs_num[v] == 0) {
      dp[u] -= max((long long)0, dp[v]);
      dp[v] += max((long long)0, dp[u]);
      dfs2(v);
      dp[v] -= max((long long)0, dp[u]);
      dp[u] += max((long long)0, dp[v]);
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, x, y;
  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++) {
    cin >> x >> y;
    x--;
    y--;
    AdjList[x].push_back(y);
    AdjList[y].push_back(x);
  }
  for (int i = 0; i < n; i++) {
    dfs_num[i] = 0;
  }
  dfs(0);
  for (int i = 0; i < n; i++) {
    dfs_num[i] = 0;
  }
  dfs2(0);
  for (int i = 0; i < n; i++) {
    cout << ans[i] << " ";
  }
}
