#include <bits/stdc++.h>
using namespace std;
int a[200005], dp[200005], visit[200005];
vector<int> tree[200005];
void dfs(int x, int c) {
  dp[x] += c;
  visit[x] = 1;
  for (auto y : tree[x])
    if (!visit[y]) {
      dfs(y, a[y]);
      if (dp[y] > 0) dp[x] += dp[y];
    }
}
void dfs2(int x) {
  visit[x] = 1;
  for (auto y : tree[x])
    if (!visit[y]) {
      if (dp[x] > dp[y]) {
        if (dp[y] <= 0)
          dp[y] += dp[x];
        else
          dp[y] += (dp[x] - dp[y]);
      }
      dfs2(y);
    }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, i, u, v;
  cin >> n;
  for (i = 1; i < n + 1; i++) {
    cin >> a[i];
    if (a[i] == 0) a[i] = -1;
  }
  for (i = 0; i < n - 1; i++) {
    cin >> u >> v;
    tree[u].push_back(v);
    tree[v].push_back(u);
  }
  dfs(1, a[1]);
  memset(visit, 0, sizeof(visit));
  dfs2(1);
  for (i = 1; i < n + 1; i++) cout << dp[i] << ' ';
}
