#include <bits/stdc++.h>
using namespace std;
int i, i0, n, m, ans[200005], a[200005], dp[200005];
vector<int> mp[200005];
void dfs(int now, int root) {
  dp[now] = a[now];
  for (int i : mp[now]) {
    if (i == root) continue;
    dfs(i, now);
    dp[now] += max(0, dp[i]);
  }
}
void solve(int now, int root);
void change_root(int nowa, int nowb) {
  int tmpa = dp[nowa], tmpb = dp[nowb];
  dp[nowa] -= max(0, dp[nowb]);
  dp[nowb] += max(0, dp[nowa]);
  solve(nowb, nowa);
  dp[nowb] = tmpb, dp[nowa] = tmpa;
}
void solve(int now, int root) {
  ans[now] = dp[now];
  for (auto i : mp[now]) {
    if (i == root) continue;
    change_root(now, i);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    if (!a[i]) a[i] = -1;
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    mp[x].push_back(y);
    mp[y].push_back(x);
  }
  dfs(1, -1);
  solve(1, -1);
  for (int i = 1; i <= n; i++) printf("%d%c", ans[i], i == n ? '\n' : ' ');
  return 0;
}
