#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
const long long mod = 1e9 + 7;
const int phash = 3;
const long long modhash = 1000000000000000003;
const long long INF = 1e12;
int a[200005];
vector<int> g[200005];
int ans[200005];
int dp[200005];
void dfs1(int v, int pr = -1) {
  dp[v] = a[v];
  for (int i : g[v]) {
    if (i == pr) continue;
    dfs1(i, v);
    dp[v] += max(0, dp[i]);
  }
}
void dfs2(int v, int pr = -1) {
  ans[v] = dp[v];
  for (int i : g[v]) {
    if (i == pr) continue;
    dp[v] -= max(0, dp[i]);
    dp[i] += max(0, dp[v]);
    dfs2(i, v);
    dp[i] -= max(0, dp[v]);
    dp[v] += max(0, dp[i]);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 0) a[i] = -1;
  }
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  dfs1(0);
  dfs2(0);
  for (int i = 0; i < n; i++) cout << ans[i] << " ";
}
