#include <bits/stdc++.h>
using namespace std;
vector<vector<long long int>> adj;
vector<long long int> vis, color, cnt, vis2, ans;
long long int dfs(long long int a) {
  long long int sum = color[a] == 1 ? 1 : -1;
  for (auto val : adj[a]) {
    if (vis[val] == 0) {
      vis[val] = 1;
      long long int c = dfs(val);
      sum += max(0ll, c);
    }
  }
  cnt[a] = sum;
  return sum;
}
long long int get_ans(long long int a) {
  for (auto val : adj[a]) {
    if (vis2[val] == 0) {
      vis2[val] = 1;
      if (cnt[val] < 0) {
        ans[val] = max(cnt[val], ans[a] + cnt[val]);
        get_ans(val);
      } else {
        ans[val] = max(cnt[val], ans[a]);
        get_ans(val);
      }
    }
  }
  return 0;
}
int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int n, a, b;
  cin >> n;
  vis.resize(n + 2, 0);
  vis2.resize(n + 2, 0);
  ans.resize(n + 2, -1e10);
  cnt.resize(n + 2, 0);
  color.push_back(-1);
  for (long long int i = 0; i < n; i++) {
    cin >> a;
    color.push_back(a);
  }
  adj.resize(n + 2);
  for (long long int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  vis[1] = 1;
  dfs(1);
  vis2[1] = 1;
  ans[1] = cnt[1];
  get_ans(1);
  for (long long int i = 1; i < n + 1; i++) {
    cout << ans[i] << " ";
  }
  cout << endl;
  return 0;
}
