#include <bits/stdc++.h>
using namespace std;
int ar[200005];
int dp[200005];
int ans[200005];
vector<int> v[200005];
int n;
void dfs(int node, int pr) {
  dp[node] = ar[node];
  int i;
  for (i = 0; i < v[node].size(); i++) {
    int to = v[node][i];
    if (to != pr) {
      dfs(to, node);
      dp[node] += max(0, dp[to]);
    }
  }
}
void dfs2(int node, int pr) {
  ans[node] = dp[node];
  int i;
  for (i = 0; i < v[node].size(); i++) {
    int to = v[node][i];
    if (to == pr) {
      continue;
    }
    dp[node] -= max(0, dp[to]);
    dp[to] += max(0, dp[node]);
    dfs2(to, node);
    dp[to] -= max(0, dp[node]);
    dp[node] += max(0, dp[to]);
  }
}
int main() {
  cin >> n;
  int i;
  for (i = 1; i <= n; i++) {
    cin >> ar[i];
    if (ar[i] == 0) {
      ar[i] = -1;
    }
  }
  int a, b;
  for (i = 1; i < n; i++) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  dfs(1, 0);
  dfs2(1, 0);
  for (i = 1; i <= n; i++) {
    cout << ans[i] << ' ';
  }
  return 0;
}
