#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 2;
const int M = 1e5 + 2;
const long long mod = 1e9 + 7;
const int inf = 1e9;
const long long INF = 1e18;
void data() {}
int n, x, col[N], dp[N], ans[N];
vector<int> g[N];
void prec(int v = 1, int p = -1) {
  dp[v] = col[v];
  for (auto to : g[v])
    if (to != p) {
      prec(to, v);
      dp[v] += max(0, dp[to]);
    }
}
void dfs(int v = 1, int p = -1) {
  ans[v] = dp[v];
  for (auto to : g[v]) {
    if (to == p) continue;
    dp[v] -= max(0, dp[to]);
    dp[to] += max(0, dp[v]);
    dfs(to, v);
    dp[to] -= max(0, dp[v]);
    dp[v] += max(0, dp[to]);
  }
}
int main() {
  data();
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &x);
    col[i] = (x == 0 ? -1 : 1);
  }
  for (int i = 1; i < n; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    g[a].push_back(b);
    g[b].push_back(a);
  }
  prec();
  dfs();
  for (int i = 1; i <= n; ++i) printf("%d ", ans[i]);
}
