#include <bits/stdc++.h>
using namespace std;
vector<int> v[200005];
int Sum[200005];
int color[200005];
void dfs(int first, int f) {
  Sum[first] += color[first];
  for (auto it : v[first]) {
    if (it != f) {
      dfs(it, first);
      Sum[first] += max(Sum[it], 0);
    }
  }
}
int ans[200005];
void dfs2(int first, int f, int sum) {
  ans[first] = max(ans[first], Sum[first] + sum);
  sum += Sum[first];
  for (auto it : v[first]) {
    if (it != f) {
      dfs2(it, first, max(sum - max(Sum[it], 0), 0));
    }
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &color[i]);
    if (color[i] == 0) color[i] = -1;
  }
  for (int i = 1; i < n; i++) {
    int first, second;
    scanf("%d %d", &first, &second);
    v[first].push_back(second);
    v[second].push_back(first);
  }
  fill(ans, ans + n + 1, -10);
  dfs(1, 0);
  dfs2(1, 0, 0);
  for (int i = 1; i <= n; i++) printf("%d ", ans[i]);
  printf("\n");
}
