#include <bits/stdc++.h>
using namespace std;
inline bool isvowel(char ch) {
  ch = tolower(ch);
  return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
          ch == 'y');
}
inline bool isprime(int n) {
  if (n < 2 || (n % 2 == 0 && n != 2)) return false;
  for (int i = 3; i * i <= n; i += 2)
    if (n % i == 0) return false;
  return true;
}
const int N = 2e5 + 7;
vector<vector<int>> graph(N);
int cntdown[N], cntup[N], color[N];
void dfs(int v, int p) {
  cntdown[v] = color[v];
  for (auto to : graph[v]) {
    if (to != p) {
      dfs(to, v);
      cntdown[v] += max(0, cntdown[to]);
    }
  }
}
void dfs2(int v, int p) {
  cntup[v] = cntdown[p] - max(0, cntdown[v]) - color[p] + cntup[p];
  cntup[v] = max(cntup[v], 0);
  cntup[v] += color[v];
  for (auto to : graph[v]) {
    if (to != p) {
      dfs2(to, v);
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  int n, i;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> color[i];
    if (color[i] == 0) color[i]--;
  }
  for (i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  dfs(1, 0);
  dfs2(1, 0);
  for (i = 1; i <= n; i++) {
    cout << max({cntdown[i] + cntup[i] - color[i], cntdown[i], cntup[i]})
         << ' ';
  }
  return 0;
}
