#include <bits/stdc++.h>
using namespace std;
int n, result[200009], color[200009], a, b, visited[200009];
vector<int> tree[200009];
void oneRootDFS(int x) {
  result[x] = color[x] == 1 ? 1 : -1;
  visited[x] = 1;
  for (int i = 0; i < tree[x].size(); i++) {
    if (visited[tree[x][i]] == 0) {
      oneRootDFS(tree[x][i]);
      result[x] += max(result[tree[x][i]], 0);
    }
  }
}
void reRootDFS(int x, int from) {
  visited[x] = 2;
  result[x] += max(0, result[from] - max(0, result[x]));
  for (int i = 0; i < tree[x].size(); i++) {
    if (visited[tree[x][i]] == 1) {
      reRootDFS(tree[x][i], x);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> color[i];
    result[i] = -1;
    visited[i] = 0;
  }
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    tree[a].push_back(b);
    tree[b].push_back(a);
  }
  oneRootDFS(1);
  reRootDFS(1, 1);
  for (int i = 1; i <= n; i++) {
    cout << result[i] << " ";
  }
}
