#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000;
struct edge {
  long long to, cost;
  edge(int a, int b) : to(a), cost(b) {}
};
vector<int> G[210000];
int color[210000];
int dp[210000];
int dfs(int p, int from) {
  if (color[p] == 0)
    dp[p] = -1;
  else
    dp[p] = 1;
  for (int adj : G[p]) {
    if (adj != from) {
      dp[p] += dfs(adj, p);
    }
  }
  return max(0, dp[p]);
}
void make(int p, int from) {
  if (from != -1) dp[p] += max(0, dp[from] - max(0, dp[p]));
  for (int adj : G[p]) {
    if (adj != from) make(adj, p);
  }
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> color[i];
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  int root = -1;
  for (int i = 0; i < n; i++)
    if (G[i].size() == 1) {
      root = i;
      break;
    }
  root = 0;
  dfs(root, -1);
  make(root, -1);
  for (int i = 0; i < n; i++) cout << dp[i] << " ";
  cout << endl;
}
