#include <bits/stdc++.h>
using namespace std;
vector<int> col, dp1, dp2;
vector<vector<int> > adj;
void dfs(int curr, int par) {
  if (col[curr - 1] == 0) {
    dp1[curr] = -1;
  } else {
    dp1[curr] = 1;
  }
  for (int node : adj[curr]) {
    if (node == par) continue;
    dfs(node, curr);
    dp1[curr] += max(0, dp1[node]);
  }
}
void dfs1(int curr, int par) {
  dp2[curr] = dp1[curr];
  if (par) {
    int val = dp2[par] - max(0, dp1[curr]);
    dp2[curr] += max(0, dp2[par] - max(0, dp1[curr]));
  }
  for (int node : adj[curr]) {
    if (node == par) continue;
    dfs1(node, curr);
  }
}
int main() {
  int n;
  cin >> n;
  dp1.resize(n + 1);
  dp2.resize(n + 1);
  adj.resize(n + 1);
  col.resize(n + 1);
  for (int i = 0; i < n; i++) {
    int ele;
    cin >> ele;
    col[i] = ele;
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1, 0);
  dfs1(1, 0);
  for (int i = 1; i <= n; i++) {
    printf("%d ", dp2[i]);
  }
  printf("\n");
  return 0;
}
