#include <bits/stdc++.h>
using namespace std;
const int N = 2 * 1e5 + 5;
int a[N], dp[N];
vector<int> g[N];
int dfs1(int u, int p) {
  int res = 0;
  if (a[u] == 1) {
    res += 1;
  } else {
    res -= 1;
  }
  for (int i : g[u]) {
    if (i != p) {
      res += max(0, dfs1(i, u));
    }
  }
  return dp[u] = res;
}
void dfs2(int u, int p) {
  if (dp[u] >= 0) {
    dp[u] = max(dp[u], dp[p]);
  } else {
    dp[u] = max(dp[p] - 1, dp[u]);
  }
  for (int i : g[u]) {
    if (i != p) {
      dfs2(i, u);
    }
  }
}
int main() {
  int v, b, c;
  cin >> v;
  for (int i = 1; i <= v; i++) {
    cin >> a[i];
  }
  for (int i = 1; i < v; i++) {
    cin >> b >> c;
    g[b].push_back(c);
    g[c].push_back(b);
  }
  dfs1(1, 0);
  dfs2(1, 0);
  for (int i = 1; i <= v; i++) {
    cout << dp[i] << " ";
  }
  cout << endl;
}
