#include <bits/stdc++.h>
using namespace std;
int v[200005];
int dp[200005];
int sol[200005];
vector<int> lista[200005];
void dfs(int nod, int daddy) {
  dp[nod] = v[nod];
  for (int i = 0; i < lista[nod].size(); i++) {
    int x = lista[nod][i];
    if (x == daddy) continue;
    dfs(x, nod);
    dp[nod] = dp[nod] + max(0, dp[x]);
  }
}
void dfs2(int nod, int daddy) {
  sol[nod] = dp[nod];
  for (int i = 0; i < lista[nod].size(); i++) {
    int x = lista[nod][i];
    if (x == daddy) continue;
    dp[nod] -= max(0, dp[x]);
    dp[x] += max(dp[nod], 0);
    dfs2(x, nod);
    dp[x] -= max(dp[nod], 0);
    dp[nod] += max(0, dp[x]);
  }
}
int main() {
  int n, i;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> v[i];
    if (!v[i]) v[i] = -1;
  }
  for (i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    lista[x].push_back(y);
    lista[y].push_back(x);
  }
  dfs(1, 0);
  dfs2(1, 0);
  for (i = 1; i <= n; i++) cout << sol[i] << " ";
  return 0;
}
