#include <bits/stdc++.h>
using namespace std;
const int modd = 1e9 + 7;
inline int add(int a, int b) { return a + b >= modd ? a + b - modd : a + b; }
inline void inc(int& a, int b) { a = add(a, b); }
inline int sub(int a, int b) { return a - b < 0 ? a - b + modd : a - b; }
inline void dec(int& a, int b) { a = sub(a, b); }
const int inf = 1e8;
const int mxN = 200004;
const int di[] = {0, -1, 0, 1};
const int dj[] = {1, 0, -1, 0};
bool a[mxN];
vector<vector<int>> adj(mxN);
int in[mxN], out[mxN];
void dfs2(int u, int par) {
  int ans = -1;
  if (a[u]) ans = 1;
  ans = max(ans, ans + out[u]);
  for (int v : adj[u]) {
    if (v == par) continue;
    if (in[v] != -1) ans += in[v];
  }
  for (int v : adj[u]) {
    if (v == par) continue;
    int cans = ans;
    if (in[v] != -1) cans -= in[v];
    out[v] = cans;
    dfs2(v, u);
  }
}
void dfs1(int u, int par) {
  int ans = -1;
  if (a[u]) ans = 1;
  for (int v : adj[u]) {
    if (v == par) continue;
    dfs1(v, u);
    if (in[v] != -1) ans += in[v];
  }
  in[u] = ans;
  return;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u - 1].push_back(v - 1);
    adj[v - 1].push_back(u - 1);
  }
  dfs1(0, -1);
  out[0] = 0;
  dfs2(0, -1);
  for (int i = 0; i < n; i++) {
    int cs = -1;
    if (in[i] != -1) cs = in[i];
    if (out[i] != -1) cs += out[i];
    cout << cs << " ";
  }
}
