#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 2e5;
vector<int> v[N + 5];
int n;
int color[N + 5];
int dp[N + 5];
int ans[N + 5];
void dfs(int x, int pre) {
  for (auto y : v[x]) {
    if (y == pre) continue;
    dfs(y, x);
    if (dp[y] > 0) dp[x] += dp[y];
  }
  if (color[x])
    dp[x]++;
  else
    dp[x]--;
}
void calc(int x, int pre) {
  if (dp[x] < 0) {
    dp[x] = max(dp[x], dp[pre] - 1);
  } else {
    dp[x] = max(dp[x], dp[pre]);
  }
  for (auto y : v[x]) {
    if (y == pre) continue;
    calc(y, x);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", color + i);
  for (int i = 1; i < n; i++) {
    int uu, vv;
    scanf("%d%d", &uu, &vv);
    v[uu].push_back(vv);
    v[vv].push_back(uu);
  }
  v[1].push_back(0);
  v[0].push_back(1);
  dfs(1, 0);
  calc(1, 0);
  for (int i = 1; i <= n; i++) printf("%d ", dp[i]);
  putchar('\n');
  return 0;
}
