#include <bits/stdc++.h>
using namespace std;
vector<int> adj[200005];
int col[200005];
int dp1[200005];
int dp2[200005];
void dfs1(int node, int pn) {
  dp1[node] = -1;
  if (col[node]) dp1[node] = 1;
  for (int i = 0; i < adj[node].size(); i++)
    if (adj[node][i] != pn) {
      dfs1(adj[node][i], node);
      if (dp1[adj[node][i]] > 0) dp1[node] += dp1[adj[node][i]];
    }
  return;
}
void dfs2(int node, int pn, int up) {
  dp2[node] = dp1[node] + up;
  for (int i = 0; i < adj[node].size(); i++)
    if (adj[node][i] != pn) {
      int top = dp1[node] - max(0, dp1[adj[node][i]]) + up;
      dfs2(adj[node][i], node, max(top, 0));
    }
  return;
}
int main() {
  int n, x, y;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> col[i];
  for (int i = 0; i < n - 1; i++) {
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  dfs1(1, -1);
  dfs2(1, -1, 0);
  for (int i = 1; i <= n; i++) cout << dp2[i] << " ";
  cout << endl;
  return 0;
}
