#include <bits/stdc++.h>
using namespace std;
void dfs(int flag, int index, vector<vector<int>> &ad, int parent, int arr[],
         int dp[]) {
  int h = 0;
  if (flag == 1) {
    for (int i = 0; i < ad[index].size(); i++) {
      int b = ad[index][i];
      if (b != parent) {
        dfs(flag, b, ad, index, arr, dp);
        int c = dp[b];
        h += max(c, 0);
      }
    }
    if (arr[index] == 1) {
      dp[index] = h + 1;
    } else {
      dp[index] = h - 1;
    }
    return;
  } else {
    for (int i = 0; i < ad[index].size(); i++) {
      int b = ad[index][i];
      if (b != parent) {
        int c = dp[index];
        int d = dp[b];
        int e = dp[index] - max(dp[b], 0);
        dp[b] += max(0, e);
        dfs(flag, b, ad, index, arr, dp);
      }
    }
  }
}
int main() {
  int t;
  t = 1;
  while (t--) {
    int n;
    cin >> n;
    int a, b, c, d, f;
    vector<vector<int>> ad(n + 1);
    int arr[n + 1];
    for (int i = 0; i < n; i++) {
      cin >> arr[i + 1];
    }
    for (int i = 0; i < n - 1; i++) {
      cin >> a >> b;
      ad[a].push_back(b);
      ad[b].push_back(a);
    }
    int dp[n + 1];
    dfs(1, 1, ad, 0, arr, dp);
    dfs(0, 1, ad, 0, arr, dp);
    for (int i = 0; i < n; i++) {
      cout << dp[i + 1] << " ";
    }
    cout << endl;
  }
}
