#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long llinf = (1LL << 62);
const int inf = (1 << 30);
const int nmax = 2e5 + 50;
const int mod = 1e9 + 7;
using namespace std;
int n, i, x, y, g[nmax], f[nmax];
vector<int> a[nmax];
void dfs(int x, int p) {
  f[x] = g[x];
  for (int i = 0; i < (int)a[x].size(); i++) {
    int y = a[x][i];
    if (y == p) continue;
    dfs(y, x);
    if (f[y] > 0) f[x] += f[y];
  }
}
void up(int x, int p) {
  if (p) {
    int vl = f[p];
    if (f[x] > 0) vl -= f[x];
    if (vl > 0) f[x] += vl;
  }
  for (int i = 0; i < (int)a[x].size(); i++) {
    int y = a[x][i];
    if (y == p) continue;
    up(y, x);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cerr.tie(0);
  cout.tie(0);
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> g[i];
    if (!g[i]) g[i] = -1;
  }
  for (i = 1; i < n; i++) {
    cin >> x >> y;
    a[x].push_back(y);
    a[y].push_back(x);
  }
  dfs(1, 0);
  up(1, 0);
  for (i = 1; i <= n; i++) cout << f[i] << " ";
  cout << endl;
  return 0;
}
