/*
 * Author: lanzhiguanhuang
 * Time: 2020-03-13 13:31:19
**/

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, a, n) for(int i=a;i<n;++i)
#define repd(i, a, n) for(int i=a;i<=n;++i)

int n, u, v;

vector<int> ccnt;
vector<int> R;
vector<vector<int>> E;
vector<int> C;
vector<int> res;

void dfsU(int root, int pcnt, int pid) {
  if (pcnt < 0) pcnt = 0;

  int cnt = ccnt[root];
  cnt += pcnt;
  res[root] = cnt;

  for (auto c : E[root]) {
    if (c == pid) continue;
    dfsU(c, cnt - (ccnt[c] > 0 ? ccnt[c] : 0), root);
  }
}

int dfsD(int root, int pid) {
  int ss = C[root] == 1 ? 1 : -1;
  for (auto c : E[root]) {
    if (c == pid) continue;
    int cnt = dfsD(c, root);
    ss += cnt < 0 ? 0 : cnt;
  }
  ccnt[root] = ss;
  return ss;
}

void solve() {
  dfsD(0, -1);

  dfsU(0, 0, 0);

  for (auto cnt : res) {
    cout << cnt << ' ';
  }
  cout << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;
  ccnt.resize(n);
  res.resize(n);
  E.resize(n);

  int cc;
  rep(i, 0, n) {
    cin >> cc;
    C.push_back(cc);
  }
  rep(i, 0, n - 1) {
    cin >> u >> v;
    E[u - 1].push_back(v - 1);
    E[v - 1].push_back(u - 1);
  }
  solve();

  return 0;
}


/*
 * Author: lanzhiguanhuang
 * Time: 2020-03-13 13:31:19
**/

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, a, n) for(int i=a;i<n;++i)
#define repd(i, a, n) for(int i=a;i<=n;++i)

int n, u, v;

vector<int> ccnt;
vector<int> R;
vector<vector<int>> E;
vector<int> C;
vector<int> res;

void dfsU(int root, int pcnt, int pid) {
  if (pcnt < 0) pcnt = 0;

  int cnt = ccnt[root];
  cnt += pcnt;
  res[root] = cnt;

  for (auto c : E[root]) {
    if (c == pid) continue;
    dfsU(c, cnt - (ccnt[c] > 0 ? ccnt[c] : 0), root);
  }
}

int dfsD(int root, int pid) {
  int ss = C[root] == 1 ? 1 : -1;
  for (auto c : E[root]) {
    if (c == pid) continue;
    int cnt = dfsD(c, root);
    ss += cnt < 0 ? 0 : cnt;
  }
  ccnt[root] = ss;
  return ss;
}

void solve() {
  dfsD(0, -1);

  dfsU(0, 0, 0);

  for (auto cnt : res) {
    cout << cnt << ' ';
  }
  cout << endl;
}

int main() {
  // ios::sync_with_stdio(false);
  // cin.tie(0);

  scanf("%d", &n);
  ccnt.resize(n);
  res.resize(n);
  E.resize(n);

  int cc;
  rep(i, 0, n) {
    scanf("%d", &cc);
    C.push_back(cc);
  }
  rep(i, 0, n - 1) {
    scanf("%d %d", &u, &v);
    E[u - 1].push_back(v - 1);
    E[v - 1].push_back(u - 1);
  }
  solve();

  return 0;
}


/*
 * Author: lanzhiguanhuang
 * Time: 2020-03-13 13:31:19
**/

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, a, n) for(int i=a;i<n;++i)
#define repd(i, a, n) for(int i=a;i<=n;++i)

int n, u, v;

vector<int> ccnt;
vector<int> R;
vector<vector<int>> E;
vector<int> C;
vector<int> res;

void dfsU(int root, unordered_set<int> visited, int pcnt) {
  if (pcnt < 0) pcnt = 0;

  int cnt = ccnt[root];
  cnt += pcnt;
  res[root] = cnt;

  for (auto c : E[root]) {
    if (!visited.count(c)) {
      visited.insert(c);

      dfsU(c, visited, cnt - (ccnt[c] > 0 ? ccnt[c] : 0));
    }
  }
}

int dfsD(int root, unordered_set<int> visited) {
  int ss = C[root] == 1 ? 1 : -1;
  for (auto c : E[root]) {
    if (!visited.count(c)) {
      visited.insert(c);
      int cnt = dfsD(c, visited);
      ss += cnt < 0 ? 0 : cnt;
    }
  }
  ccnt[root] = ss;
  return ss;
}

void solve() {
  unordered_set<int> visited;
  visited.insert(0);
  dfsD(0, visited);

  visited.clear();
  visited.insert(0);
  dfsU(0, visited, 0);

  for (auto cnt : res) {
    cout << cnt << ' ';
  }
  cout << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n;
  ccnt.resize(n);
  res.resize(n);
  E.resize(n);

  int cc;
  rep(i, 0, n) {
    cin >> cc;
    C.push_back(cc);
  }
  rep(i, 0, n - 1) {
    cin >> u >> v;
    E[u - 1].push_back(v - 1);
    E[v - 1].push_back(u - 1);
  }
  solve();

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

vector<vector<int>> g;
vector<int> a, u, d1, d2;

void dfs1(int v) {
    u[v] = 1;
    int sum = 0;
    for (auto to : g[v]) {
        if (!u[to]) {
            dfs1(to);
            sum += max(d1[to], 0ll);
        }
    }
    d1[v] = sum + (a[v] ? 1 : -1);
}

void dfs2(int v, int mx) {
    u[v] = 1;
    d2[v] = max(0ll, mx - max(d1[v], 0ll)) + d1[v];
    for (auto to : g[v]) {
        if (!u[to]) {
            dfs2(to, d2[v]);
        }
    }
}

signed main()
{
    int n;
    cin >> n;
    g.resize(n);
    a.resize(n);
    u.resize(n);
    d1.resize(n);
    d2.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs1(0);
    u.assign(n, 0);
    dfs2(0, 0);
//    for (int i = 0; i < n; i++) {
//        cout << d1[i] << " ";
//    }
//    cout << endl;
    for (int i = 0; i < n; i++) {
        cout << d2[i] << " ";
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

vector<vector<int>> g;
vector<int> a, u, d1, d2;

void dfs1(int v) {
    u[v] = 1;
    int sum = 0;
    for (auto to : g[v]) {
        if (!u[to]) {
            dfs1(to);
            sum += max(d1[to], 0ll);
        }
    }
    d1[v] = sum + (a[v] ? 1 : -1);
}

void dfs2(int v, int mx) {
    u[v] = 1;
    d2[v] = max(0ll, mx - max(d1[v], 0ll)) + d1[v];
    for (auto to : g[v]) {
        if (!u[to]) {
            dfs2(to, max(0ll, d2[v]));
        }
    }
}

signed main()
{
    int n;
    cin >> n;
    g.resize(n);
    a.resize(n);
    u.resize(n);
    d1.resize(n);
    d2.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        g[u].push_back(v);
    }
    dfs1(0);
    u.assign(n, 0);
    dfs2(0, 0);
//    for (int i = 0; i < n; i++) {
//        cout << d1[i] << " ";
//    }
//    cout << endl;
    for (int i = 0; i < n; i++) {
        cout << d2[i] << " ";
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

vector<vector<int>> g;
vector<int> a, u, d1, d2;

void dfs1(int v) {
    u[v] = 1;
    int sum = 0;
    for (auto to : g[v]) {
        if (!u[to]) {
            dfs1(to);
            sum += max(d1[to], 0ll);
        }
    }
    d1[v] = sum + (a[v] ? 1 : -1);
}

void dfs2(int v, int mx) {
    u[v] = 1;
    d2[v] = mx + d1[v];
    for (auto to : g[v]) {
        if (!u[to]) {
            dfs2(to, max(0ll, d2[v] - max(d1[to], 0ll)));
        }
    }
}

signed main()
{
    int n;
    cin >> n;
    g.resize(n);
    a.resize(n);
    u.resize(n);
    d1.resize(n);
    d2.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        g[u].push_back(v);
    }
    dfs1(0);
    u.assign(n, 0);
    dfs2(0, 0);
//    for (int i = 0; i < n; i++) {
//        cout << d1[i] << " ";
//    }
//    cout << endl;
    for (int i = 0; i < n; i++) {
        cout << d2[i] << " ";
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

vector<vector<int>> g;
vector<int> a, u, d1, d2;

void dfs1(int v) {
    u[v] = 1;
    int sum = 0;
    for (auto to : g[v]) {
        if (!u[to]) {
            dfs1(to);
            sum += max(d1[to], 0ll);
        }
    }
    d1[v] = sum + (a[v] ? 1 : -1);
}

void dfs2(int v, int mx) {
    u[v] = 1;
    d2[v] = max(0ll, min(mx - d1[v], mx)) + d1[v];
    for (auto to : g[v]) {
        if (!u[to]) {
            dfs2(to, d2[v]);
        }
    }
}

signed main()
{
    int n;
    cin >> n;
    g.resize(n);
    a.resize(n);
    u.resize(n);
    d1.resize(n);
    d2.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        g[u].push_back(v);
    }
    dfs1(0);
    u.assign(n, 0);
    dfs2(0, 0);
//    for (int i = 0; i < n; i++) {
//        cout << d1[i] << " ";
//    }
//    cout << endl;
    for (int i = 0; i < n; i++) {
        cout << d2[i] << " ";
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

vector<vector<int>> g;
vector<int> a, u, d1, d2;

void dfs1(int v) {
    u[v] = 1;
    int sum = 0;
    for (auto to : g[v]) {
        if (!u[to]) {
            dfs1(to);
            sum += max(d1[to], 0ll);
        }
    }
    d1[v] = sum + (a[v] ? 1 : -1);
}

void dfs2(int v, int mx) {
    u[v] = 1;
    d2[v] = max(0ll, mx - max(d1[v], 0ll)) + d1[v];
    for (auto to : g[v]) {
        if (!u[to]) {
            dfs2(to, d2[v]);
        }
    }
}

signed main()
{
    int n;
    cin >> n;
    g.resize(n);
    a.resize(n);
    u.resize(n);
    d1.resize(n);
    d2.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        g[u].push_back(v);
    }
    dfs1(0);
    u.assign(n, 0);
    dfs2(0, 0);
    for (int i = 0; i < n; i++) {
        cout << d2[i] << " ";
    }
    return 0;
}


#include <stdio.h>
#include <bits/stdc++.h>

#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)

#define file ""

//#pragma comment(linker, "/STACK:306777216")

using namespace std;
using ll = long long;
using ull = unsigned long long;
using str = string;

const int INF = 1e9 + 7;
const int mod = 998244353;
const ll inf = 1e18;
const ull oo = 1000000000000001;

vector<int> isWhite;
vector<vector<int>> tree;
vector<int> ans;

void dfs(int v, int p = -1)
{
  isWhite[v] = (isWhite[v] == 1 ? 1 : -1);
  for (auto& to : tree[v])
  {
    if (p == to) continue;
    dfs(to, v);
    isWhite[v] += max(isWhite[to], 0);
  }
  if (v != 0)
  {
    return;
  }
  ans[v] = isWhite[v];
  for (auto& to : tree[v])
  {
    if (to == p) continue;
    isWhite[v] -= max(0, isWhite[to]);
    isWhite[to] += max(0, isWhite[v]);
    dfs(to, v);
    isWhite[to] -= max(0, isWhite[v]);
    isWhite[v] += max(0, isWhite[to]);
  }
}

signed main()
{
  //freopen("Utility/input.txt", "r", stdin);
  //freopen("Utility/output.txt", "w", stdout);
  ios::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); //freopen(file".in", "r", stdin); freopen(file".out", "w", stdout);

  int n;
  cin >> n;
  isWhite.resize(n);
  ans.resize(n);
  tree.resize(n);
  for (int i = 0; i < n; ++i)
  {
    cin >> isWhite[i];
  }
  for (int i = 0; i < n - 1; ++i)
  {
    int a, b;
    cin >> a >> b;
    tree[a - 1].push_back(b - 1);
    tree[b - 1].push_back(a - 1);
  }
  dfs(0);
  for (int i = 0; i < n; ++i)
  {
    cout << ans[i] << ' ';
  }
  cout << '\n';
}


#include <stdio.h>
#include <bits/stdc++.h>

#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)

#define file ""

//#pragma comment(linker, "/STACK:306777216")

using namespace std;
using ll = long long;
using ull = unsigned long long;
using str = string;

const int INF = 1e9 + 7;
const int mod = 998244353;
const ll inf = 1e18;
const ull oo = 1000000000000001;

vector<int> isWhite;
vector<vector<int>> tree;
vector<int> ans;

void dfs(int v, int p = -1)
{
  isWhite[v] = (isWhite[v] == 1 ? 1 : -1);
  for (auto& to : tree[v])
  {
    if (p == to) continue;
    dfs(to, v);
    isWhite[v] += max(isWhite[to], 0);
  }
}

void calcAns(int v, int p = -1)
{
  ans[v] = isWhite[v];
  for (auto& to : tree[v])
  {
    if (to == p) continue;
    isWhite[v] -= max(0, isWhite[to]);
    isWhite[to] += max(0, isWhite[v]);
    calcAns(to, v);
    isWhite[to] -= max(0, isWhite[v]);
    isWhite[v] += max(0, isWhite[to]);
  }
}

signed main()
{
  //freopen("Utility/input.txt", "r", stdin);
  //freopen("Utility/output.txt", "w", stdout);
  ios::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); //freopen(file".in", "r", stdin); freopen(file".out", "w", stdout);

  int n;
  cin >> n;
  isWhite.resize(n);
  ans.resize(n);
  tree.resize(n);
  for (int i = 0; i < n; ++i)
  {
    cin >> isWhite[i];
  }
  for (int i = 0; i < n - 1; ++i)
  {
    int a, b;
    cin >> a >> b;
    tree[a - 1].push_back(b - 1);
    tree[b - 1].push_back(a - 1);
  }
  dfs(0);
  calcAns(0);
  for (int i = 0; i < n; ++i)
  {
    cout << ans[i] << ' ';
  }
  cout << '\n';
}


#include <stdio.h>
#include <bits/stdc++.h>

#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)

#define file ""

//#pragma comment(linker, "/STACK:306777216")

#define int long long

using namespace std;
using ll = long long;
using ull = unsigned long long;
using str = string;

const int INF = 1e9 + 7;
const int mod = 998244353;
const ll inf = 1e18;
const ull oo = 1000000000000001;

vector<int> dp;
vector<char> isWhite;
vector<vector<int>> tree;
vector<int> ans;

void dfs(int v, int p = -1)
{
  dp[v] = (isWhite[v] == '1' ? 1 : -1);
  for (auto& to : tree[v])
  {
    if (p == to) continue;
    dfs(to, v);
    dp[v] += max(dp[to], 0LL);
  }
}

void calcAns(int v, int p = -1)
{
  ans[v] = dp[v];
  for (auto& to : tree[v])
  {
    if (to == p) continue;
    dp[v] -= max(0LL, dp[to]);
    dp[to] += max(0LL, dp[v]);
    calcAns(to, v);
    dp[to] -= max(0LL, dp[v]);
    dp[v] += max(0LL, dp[to]);
  }
}

signed main()
{
  //freopen("Utility/input.txt", "r", stdin);
  //freopen("Utility/output.txt", "w", stdout);
  ios::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); //freopen(file".in", "r", stdin); freopen(file".out", "w", stdout);

  int n;
  cin >> n;
  isWhite.resize(n);
  dp.resize(n);
  ans.resize(n);
  tree.resize(n);
  for (int i = 0; i < n; ++i)
  {
    cin >> isWhite[i];
  }
  for (int i = 0; i < n - 1; ++i)
  {
    int a, b;
    cin >> a >> b;
    tree[a - 1].push_back(b - 1);
    tree[b - 1].push_back(a - 1);
  }
  dfs(0);
  calcAns(0);
  for (int i = 0; i < n; ++i)
  {
    cout << ans[i] << ' ';
  }
  cout << '\n';
}


#include <stdio.h>
#include <bits/stdc++.h>

#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)

#define file ""

//#pragma comment(linker, "/STACK:306777216")

using namespace std;
using ll = long long;
using ull = unsigned long long;
using str = string;

const int INF = 1e9 + 7;
const int mod = 998244353;
const ll inf = 1e18;
const ull oo = 1000000000000001;

vector<int> isWhite;
vector<vector<int>> tree;
vector<int> ans;

void dfs(int v, int p = -1)
{
  isWhite[v] = (isWhite[v] == 1 ? 1 : -1);
  for (auto& to : tree[v])
  {
    if (p == to) continue;
    dfs(to, v);
    isWhite[v] += max(isWhite[to], 0);
  }
  ans[v] = isWhite[v];
  for (auto& to : tree[v])
  {
    if (to == p) continue;
    isWhite[v] -= max(0, isWhite[to]);
    isWhite[to] += max(0, isWhite[v]);
    dfs(to, v);
    isWhite[to] -= max(0, isWhite[v]);
    isWhite[v] += max(0, isWhite[to]);
  }
}

signed main()
{
  //freopen("Utility/input.txt", "r", stdin);
  //freopen("Utility/output.txt", "w", stdout);
  ios::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); //freopen(file".in", "r", stdin); freopen(file".out", "w", stdout);

  int n;
  cin >> n;
  isWhite.resize(n);
  ans.resize(n);
  tree.resize(n);
  for (int i = 0; i < n; ++i)
  {
    cin >> isWhite[i];
  }
  for (int i = 0; i < n - 1; ++i)
  {
    int a, b;
    cin >> a >> b;
    tree[a - 1].push_back(b - 1);
    tree[b - 1].push_back(a - 1);
  }
  dfs(0);
  for (int i = 0; i < n; ++i)
  {
    cout << ans[i] << ' ';
  }
  cout << '\n';
}


#include <bits/stdc++.h>

using namespace std;

int32_t main() {

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    long long n;

    cin >> n;

    vector<long long> a(n);

    for(long long i = 0 ; i < n ; i++) {

        cin >> a[i];

    }

    map<long long,set<long long>> adj;

    map<long long,long long> frnd;

    for(long long i = 0 ; i < n-1 ; i++) {

        long long u;

        cin >> u;

        long long v;

        cin >> v;

        adj[u].insert(v);

        adj[v].insert(u);

    }

    map<long long,set<long long>> adj2=adj;

    set<long long> ghar,noghar;

    queue<long long> Q;

    map<long long,long long> pass;

    for(long long i = 0 ; i < n ; i++) {

        ghar.insert(i+1);

        if(a[i]==0 and adj[i+1].size()==1) {

            Q.push(i+1);

            pass[i+1]=1;

        }

    }

    while(Q.size()>0) {

        long long node = Q.front();

        Q.pop();

        long long par = *adj[node].begin();

        frnd[node]=par;

        ghar.erase(node);

        noghar.insert(node);

        adj[par].erase(node);

        if(adj[par].size()==1 and a[par-1]==0) {

            Q.push(par);

            pass[par]=pass[node]+1;

        }

    }

    map<long long,set<long long>,greater<long long>> pass2;

    for(auto x:pass) {

        pass2[x.second].insert(x.first);

    }

    long long cw=0;

    long long cb=0;

    for(long long x:ghar) {

        if(a[x-1]==0)cb++;

        else cw++;

    }

    vector<long long> res(n);

    for(long long x:ghar) {

        res[x-1]=cw-cb;

    }

    long long tw=count(a.begin(),a.end(),1);

    long long tb=count(a.begin(),a.end(),0);

    if(tw==0) {

        for(long long i = 0 ; i < n ; i++) {

            cout << -1 << " ";

        }

        cout << '\n';

        return 0;

    }

    for(auto x:pass2) {

        if(x.first==pass2.size()) {

            for(long long y:x.second) {

                res[y-1]=res[frnd[y]-1]-1;

            }

        } else {

            for(long long y:x.second) {

                res[y-1]=res[frnd[y]-1]-1;

            }

        }

    }

    for(auto t : res )cout<<t<<" ";

    cout << '\n';

    return 0;

}


#include <bits/stdc++.h>

using namespace std;

int32_t main() {

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    long long n;

    cin >> n;

    vector<long long> a(n);

    for(long long i = 0 ; i < n ; i++) {

        cin >> a[i];

    }

    map<long long,set<long long>> adj;

    map<long long,long long> frnd;

    for(long long i = 0 ; i < n-1 ; i++) {

        long long u;

        cin >> u;

        long long v;

        cin >> v;

        adj[u].insert(v);

        adj[v].insert(u);

    }

    map<long long,set<long long>> adj2=adj;

    set<long long> ghar,noghar;

    queue<long long> Q;

    map<long long,long long> pass;

    for(long long i = 0 ; i < n ; i++) {

        ghar.insert(i+1);

        if(a[i]==0 and adj[i+1].size()==1) {

            Q.push(i+1);

            pass[i+1]=1;

        }

    }

    while(Q.size()>0) {

        long long node = Q.front();

        Q.pop();

        long long par = *adj[node].begin();

        frnd[node]=par;

        ghar.erase(node);

        noghar.insert(node);

        adj[par].erase(node);

        if(adj[par].size()==1 and a[par-1]==0) {

            Q.push(par);

            pass[par]=pass[node]+1;

        }

    }

    map<long long,set<long long>,greater<long long>> pass2;

    for(auto x:pass) {

        pass2[x.second].insert(x.first);

    }

    long long cw=0;

    long long cb=0;

    for(long long x:ghar) {

        if(a[x-1]==0)cb++;

        else cw++;

    }

    vector<long long> res(n);

    for(long long x:ghar) {

        res[x-1]=cw-cb;

    }

    long long tw=count(a.begin(),a.end(),1);

    long long tb=count(a.begin(),a.end(),0);

    if(tw==0) {

        for(long long i = 0 ; i < n ; i++) {

            cout << -1 << " ";

        }

        cout << '\n';

        return 0;

    }

    for(auto x:pass2) {

        if(x.first==pass2.size()) {

            for(long long y:x.second) {

                res[y-1]=res[frnd[y]-1]-1;

            }

        } else {

            for(long long y:x.second) {

                res[y-1]=res[frnd[y]-1]-1;

            }

        }

    }

    for(long long i = 0 ; i < res.size() ; i++) {

        res[i]=max(-1LL,res[i]);

    }

    for(auto t : res )cout<<t<<" ";

    cout << '\n';

    return 0;

}


#include <bits/stdc++.h>

using namespace std;

long long dp[100000];

long long res2[100000];

void dfs(long long src,map<long long,set<long long>> &adj,map<long long,long long> &visit, vector<long long> &a) {

    visit[src]=true;

    long long res=a[src-1]==0?-1:1;

    for(long long x:adj[src]) {

        if(!visit[x]) {

            dfs(x,adj,visit,a);

            if(a[src-1]==0) {

                res+=max(0LL,dp[x]);

            } else {

                res+=max(0LL,dp[x]);

            }

        }

    }

    dp[src]=res;

}

void dfs2(long long src,long long ashw,map<long long,set<long long>> &adj,map<long long,long long> &visit, vector<long long> &a) {

    visit[src]=true;

    res2[src]= max(dp[src],ashw+dp[src]);

    multiset<long long> ms;

    long long res=a[src-1]==0?-1:1;

    for(long long x:adj[src]) {

        if(!visit[x]) {

            if(a[src-1]==0) {

                res+=max(0LL,dp[x]);

            } else {

                res+=max(0LL,dp[x]);

            }

        }

    }

    for(long long x:adj[src]) {

        if(!visit[x]) {

            if(a[src-1]==0) {

                res-=max(0LL,dp[x]);

            } else {

                res-=max(0LL,dp[x]);

            }

            dfs2(x,max(res,res+(ashw>0?ashw:0LL)),adj,visit,a);

            if(a[src-1]==0) {

                res+=max(0LL,dp[x]);

            } else {

                res+=max(0LL,dp[x]);

            }

        }

    }

}

int32_t main() {

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    long long n;

    cin >> n;

    vector<long long> a(n);

    for(long long i = 0 ; i < n ; i++) {

        cin >> a[i];

    }

    map<long long,set<long long>> adj;

    map<long long,long long> frnd;

    for(long long i = 0 ; i < n-1 ; i++) {

        long long u;

        cin >> u;

        long long v;

        cin >> v;

        adj[u].insert(v);

        adj[v].insert(u);

    }

    for(long long i = 0 ; i < a.size() ; i++) {

        if(a[i]==0) {

            dp[i+1]=-1;

        } else {

            dp[i+1]=1;

        }

    }

    map<long long,long long> visit;

    dfs(1,adj,visit,a);

    visit.clear();

    dfs2(1,0,adj,visit,a);

    for(long long i = 0 ; i < n ; i++) {

        cout << res2[i+1] << ' ';

    }

    cout << '\n';

    return 0;

}


#include <bits/stdc++.h>

using namespace std;

long long dp[200002];

long long res2[200002];

void dfs(long long src,map<long long,set<long long>> &adj,map<long long,long long> &visit, vector<long long> &a) {

    visit[src]=true;

    long long res=a[src-1]==0?-1:1;

    for(long long x:adj[src]) {

        if(!visit[x]) {

            dfs(x,adj,visit,a);

            if(a[src-1]==0) {

                res+=max(0LL,dp[x]);

            } else {

                res+=max(0LL,dp[x]);

            }

        }

    }

    dp[src]=res;

}

void dfs2(long long src,long long ashw,map<long long,set<long long>> &adj,map<long long,long long> &visit, vector<long long> &a) {

    visit[src]=true;

    res2[src]= max(dp[src],ashw+dp[src]);

    multiset<long long> ms;

    long long res=a[src-1]==0?-1:1;

    for(long long x:adj[src]) {

        if(!visit[x]) {

            if(a[src-1]==0) {

                res+=max(0LL,dp[x]);

            } else {

                res+=max(0LL,dp[x]);

            }

        }

    }

    for(long long x:adj[src]) {

        if(!visit[x]) {

            if(a[src-1]==0) {

                res-=max(0LL,dp[x]);

            } else {

                res-=max(0LL,dp[x]);

            }

            dfs2(x,max(res,res+(ashw>0?ashw:0LL)),adj,visit,a);

            if(a[src-1]==0) {

                res+=max(0LL,dp[x]);

            } else {

                res+=max(0LL,dp[x]);

            }

        }

    }

}

int32_t main() {

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    long long n;

    cin >> n;

    vector<long long> a(n);

    for(long long i = 0 ; i < n ; i++) {

        cin >> a[i];

    }

    map<long long,set<long long>> adj;

    map<long long,long long> frnd;

    for(long long i = 0 ; i < n-1 ; i++) {

        long long u;

        cin >> u;

        long long v;

        cin >> v;

        adj[u].insert(v);

        adj[v].insert(u);

    }

    for(long long i = 0 ; i < a.size() ; i++) {

        if(a[i]==0) {

            dp[i+1]=-1;

        } else {

            dp[i+1]=1;

        }

    }

    map<long long,long long> visit;

    dfs(1,adj,visit,a);

    visit.clear();

    dfs2(1,0,adj,visit,a);

    for(long long i = 0 ; i < n ; i++) {

        cout << res2[i+1] << ' ';

    }

    cout << '\n';

    return 0;

}


#include <bits/stdc++.h>

using namespace std;

int32_t main() {

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    long long n;

    cin >> n;

    vector<long long> a(n);

    for(long long i = 0 ; i < n ; i++) {

        cin >> a[i];

    }

    map<long long,set<long long>> adj;

    map<long long,long long> frnd;

    for(long long i = 0 ; i < n-1 ; i++) {

        long long u;

        cin >> u;

        long long v;

        cin >> v;

        adj[u].insert(v);

        adj[v].insert(u);

    }

    map<long long,set<long long>> adj2=adj;

    set<long long> ghar,noghar;

    queue<long long> Q;

    map<long long,long long> pass;

    for(long long i = 0 ; i < n ; i++) {

        ghar.insert(i+1);

        if(a[i]==0 and adj[i+1].size()==1) {

            Q.push(i+1);

            pass[i+1]=1;

        }

    }

    while(Q.size()>0) {

        long long node = Q.front();

        Q.pop();

        long long par = *adj[node].begin();

        frnd[node]=par;

        ghar.erase(node);

        noghar.insert(node);

        adj[par].erase(node);

        if(adj[par].size()==1 and a[par-1]==0) {

            Q.push(par);

            pass[par]=pass[node]+1;

        }

    }

    map<long long,set<long long>,greater<long long>> pass2;

    for(auto x:pass) {

        pass2[x.second].insert(x.first);

    }

    long long cw=0;

    long long cb=0;

    for(long long x:ghar) {

        if(a[x-1]==0)cb++;

        else cw++;

    }

    vector<long long> res(n);

    for(long long x:ghar) {

        res[x-1]=cw-cb;

    }

    long long tw=count(a.begin(),a.end(),1);

    long long tb=count(a.begin(),a.end(),0);

    for(auto x:pass2) {

        if(x.first==pass2.size()) {

            for(long long y:x.second) {

                res[y-1]=res[frnd[y]-1]-1;

            }

        } else {

            for(long long y:x.second) {

                res[y-1]=res[frnd[y]-1]-1;

            }

        }

    }

    for(auto t : res )cout<<t<<" ";

    cout << '\n';

    return 0;

}


#include <bits/stdc++.h>

using namespace std;

int32_t main() {

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    long long n;

    cin >> n;

    vector<long long> a(n);

    for(long long i = 0 ; i < n ; i++) {

        cin >> a[i];

    }

    map<long long,set<long long>> adj;

    map<long long,long long> frnd;

    for(long long i = 0 ; i < n-1 ; i++) {

        long long u;

        cin >> u;

        long long v;

        cin >> v;

        adj[u].insert(v);

        adj[v].insert(u);

    }

    map<long long,set<long long>> adj2=adj;

    set<long long> ghar,noghar;

    queue<long long> Q;

    map<long long,long long> pass;

    for(long long i = 0 ; i < n ; i++) {

        ghar.insert(i+1);

        if(a[i]==0 and adj[i+1].size()==1) {

            Q.push(i+1);

            pass[i+1]=1;

        }

    }

    while(Q.size()>0) {

        long long node = Q.front();

        Q.pop();

        long long par = *adj[node].begin();

        frnd[node]=par;

        ghar.erase(node);

        noghar.insert(node);

        adj[par].erase(node);

        if(adj[par].size()==1 and a[par-1]==0) {

            Q.push(par);

            pass[par]=pass[node]+1;

        }

    }

    map<long long,set<long long>,greater<long long>> pass2;

    for(auto x:pass) {

        pass2[x.second].insert(x.first);

    }

    long long cw=0;

    long long cb=0;

    for(long long x:ghar) {

        if(a[x-1]==0)cb++;

        else cw++;

    }

    vector<long long> res(n);

    for(long long x:ghar) {

        res[x-1]=cw-cb;

        if(a[x-1]==1) {

            res[x-1]=max(res[x-1],1LL);

        }

    }

    long long tw=count(a.begin(),a.end(),1);

    long long tb=count(a.begin(),a.end(),0);

    if(tw==0) {

        for(long long i = 0 ; i < n ; i++) {

            cout << -1 << " ";

        }

        cout << '\n';

        return 0;

    }

    for(auto x:pass2) {

        if(x.first==pass2.size()) {

            for(long long y:x.second) {

                res[y-1]=res[frnd[y]-1]-1;

            }

        } else {

            for(long long y:x.second) {

                res[y-1]=res[frnd[y]-1]-1;

            }

        }

    }

    for(long long i = 0 ; i < res.size() ; i++) {

        res[i]=max(-1LL,res[i]);

    }

    for(auto t : res )cout<<t<<" ";

    cout << '\n';

    return 0;

}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 44;
int n;
vector < int > v[N], a;
map < pair < int, int >, pair < int, int > > mp;

void dfs(int i, int pr){
    int cntW = 0, cntB = 0;
    if(a[i]) cntW++;
    else cntB++;
    for(int j = 0; j < v[i].size(); j++){
        int to = v[i][j];
        if(to != pr && mp[{i, to}].first == 0 && mp[{i, to}].second == 0){
            dfs(to, i);
        }
        if(to != pr && mp[{i, to}].first > mp[{i, to}].second){
                cntW += mp[{i, to}].first;
                cntB += mp[{i, to}].second;
        }
    }
    mp[{pr, i}] =  {cntW, cntB};
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    for(int i = 0; i < n; i++){
        int x;
        cin >> x;
        a.push_back(x);
    }
    for(int i = 0; i < n - 1; i++){
        int x, y;
        cin >> x >> y;
        x--; y--;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(0, -1);
    for(int i = 0; i < n; i++){
        int diff = 0;
        if(a[i]) diff++;
        else diff--;
        for(int j = 0; j < v[i].size(); j++){
            int to = v[i][j];
            if(mp[{i, to}].first == 0 && mp[{i, to}].second == 0){
                dfs(i, -1);
            }
           // cout << i + 1 << ' ' << to + 1 << ' ' << mp[{i, to}].first  << ' ' << mp[{i, to}].second << '\n';
            if(mp[{i, to}].first > mp[{i, to}].second){
                diff += mp[{i, to}].first;
                diff -= mp[{i, to}].second;
            }
        }
        //cout << '\n' << " !!!!!!!!!!! " << diff << '\n';
        cout << diff << ' ';
    }
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 44;
int n;
vector < int > v[N], a;
map < pair < int, int >, pair < int, int > > mp;
map < pair < int, int >, bool > used;

void dfs(int i, int pr){
    int cntW = 0, cntB = 0;
    if(a[i]) cntW++;
    else cntB++;
    for(int j = 0; j < v[i].size(); j++){
        int to = v[i][j];
        if(to == pr) continue;
        if(!used[{to, i}]){
            dfs(to, i);
            used[{to, i}] = 1;
        }
        if(mp[{i, to}].first > mp[{i, to}].second){
            cntW += mp[{i, to}].first;
            cntB += mp[{i, to}].second;
        }
    }
    mp[{pr, i}] =  {cntW, cntB};
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    for(int i = 0; i < n; i++){
        int x;
        cin >> x;
        a.push_back(x);
    }
    for(int i = 0; i < n - 1; i++){
        int x, y;
        cin >> x >> y;
        x--; y--;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(0, -1);
    for(int i = 0; i < n; i++){
        int diff = 0;
        if(a[i]) diff++;
        else diff--;
        for(int j = 0; j < v[i].size(); j++){
            int to = v[i][j];
            if(used[{i, to}] && mp[{i, to}].first == 0 && mp[{i, to}].second == 0){
                dfs(i, -1);
                used[{i, to}] = 1;
            }
           // cout << i + 1 << ' ' << to + 1 << ' ' << mp[{i, to}].first  << ' ' << mp[{i, to}].second << '\n';
            if(mp[{i, to}].first > mp[{i, to}].second){
                diff += mp[{i, to}].first;
                diff -= mp[{i, to}].second;
            }
        }
        //cout << '\n' << " !!!!!!!!!!! " << diff << '\n';
        cout << diff << ' ';
    }
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 44;
int n, d[N];
vector < int > v[N], a, ans(N);

void dfs(int i, int pr){
    d[i] = a[i];
    for(auto to:v[i]){
        if(to == pr) continue;
        dfs(to, i);
        d[i] += max(0, d[to]);
    }
}

void dfs2(int i, int pr){
    ans[i] = d[i];
    for(auto to:v[i]){
        if(to == pr) continue;
        d[i] -= max(0, d[to]);
        d[to] += max(0, d[i]);
        dfs2(to, i);
        d[to] -= max(0, d[i]);
        d[i] += max(0, d[to]);
    }

}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
   // freopen("tmp.txt", "r", stdin);
    cin >> n;
    for(int i = 0; i < n; i++){
        int x;
        cin >> x;
        if(!x) x--;
        a.push_back(x);
    }
    for(int i = 0; i < n - 1; i++){
        int x, y;
        cin >> x >> y;
        x--; y--;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(0, -1);
    dfs2(0, -1);
    for(int i = 0; i < n; i++)
        cout << ans[i] << ' ';
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 44;
int n, ss = 0;
vector < int > v[N], a;
map < pair < int, int >, pair < int, int > > mp;

void dfs(int i, int pr){
    int cntW = 0, cntB = 0;
    if(a[i]) cntW++;
    else cntB++;
    for(int j = 0; j < v[i].size(); j++){
        int to = v[i][j];
        if(to != pr && mp[{i, to}].first == 0 && mp[{i, to}].second == 0){
            dfs(to, i);
        }
        if(to != pr && mp[{i, to}].first > mp[{i, to}].second){
                cntW += mp[{i, to}].first;
                cntB += mp[{i, to}].second;
        }
    }
    mp[{pr, i}] =  {cntW, cntB};
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    for(int i = 0; i < n; i++){
        int x;
        cin >> x;
        a.push_back(x);
        ss += x;
    }
    if(!ss){
        for(int i = 0; i < n; i++)
            cout << "-1 ";
        return 0;
    }
    for(int i = 0; i < n - 1; i++){
        int x, y;
        cin >> x >> y;
        x--; y--;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(0, -1);
    for(int i = 0; i < n; i++){
        int diff = 0;
        if(a[i]) diff++;
        else diff--;
        for(int j = 0; j < v[i].size(); j++){
            int to = v[i][j];
            if(mp[{i, to}].first == 0 && mp[{i, to}].second == 0){
                dfs(i, -1);
            }
           // cout << i + 1 << ' ' << to + 1 << ' ' << mp[{i, to}].first  << ' ' << mp[{i, to}].second << '\n';
            if(mp[{i, to}].first > mp[{i, to}].second){
                diff += mp[{i, to}].first;
                diff -= mp[{i, to}].second;
            }
        }
        //cout << '\n' << " !!!!!!!!!!! " << diff << '\n';
        cout << diff << ' ';
    }
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 44;
int n;
vector < int > v[N], a;
map < pair < int, int >, pair < int, int > > mp;
map < pair < int, int >, bool > used;

void dfs(int i, int pr){
    int cntW = 0, cntB = 0;
    if(a[i]) cntW++;
    else cntB++;
    for(int j = 0; j < v[i].size(); j++){
        int to = v[i][j];
        if(to == pr) continue;
        if(!used[{to, i}]){
            dfs(to, i);
            used[{to, i}] = 1;
        }
        if(mp[{i, to}].first > mp[{i, to}].second){
            cntW += mp[{i, to}].first;
            cntB += mp[{i, to}].second;
        }
    }
    mp[{pr, i}] =  {cntW, cntB};
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    for(int i = 0; i < n; i++){
        int x;
        cin >> x;
        a.push_back(x);
    }
    for(int i = 0; i < n - 1; i++){
        int x, y;
        cin >> x >> y;
        x--; y--;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(0, -1);
    for(int i = 0; i < n; i++){
        int diff = 0;
        if(a[i]) diff++;
        else diff--;
        for(int j = 0; j < v[i].size(); j++){
            int to = v[i][j];
            if(!used[{i, to}]){
                dfs(i, -1);
                used[{i, to}] = 1;
            }
           // cout << i + 1 << ' ' << to + 1 << ' ' << mp[{i, to}].first  << ' ' << mp[{i, to}].second << '\n';
            if(mp[{i, to}].first > mp[{i, to}].second){
                diff += mp[{i, to}].first;
                diff -= mp[{i, to}].second;
            }
        }
        //cout << '\n' << " !!!!!!!!!!! " << diff << '\n';
        cout << diff << ' ';
    }
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 44;
int n;
vector < int > v[N], a;
map < pair < int, int >, pair < int, int > > mp;
map < pair < int, int >, bool > used;

void dfs(int i, int pr){
    int cntW = 0, cntB = 0;
    if(a[i]) cntW++;
    else cntB++;
    for(int j = 0; j < v[i].size(); j++){
        int to = v[i][j];
        if(to == pr) continue;
        if(!used[{i, to}]){
            dfs(to, i);
            used[{i, to}] = 1;
        }
        if(mp[{i, to}].first > mp[{i, to}].second){
            cntW += mp[{i, to}].first;
            cntB += mp[{i, to}].second;
        }
    }
    mp[{pr, i}] =  {cntW, cntB};
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
   // freopen("tmp.txt", "r", stdin);
    cin >> n;
    for(int i = 0; i < n; i++){
        int x;
        cin >> x;
        a.push_back(x);
    }
    for(int i = 0; i < n - 1; i++){
        int x, y;
        cin >> x >> y;
        x--; y--;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(0, -1);
    for(int i = 0; i < n; i++){
        int diff = 0;
        if(a[i]) diff++;
        else diff--;
        for(int j = 0; j < v[i].size(); j++){
            int to = v[i][j];
            if(!used[{i, to}]){
                dfs(i, -1);
                used[{i, to}] = 1;
            }
           // cout << i + 1 << ' ' << to + 1 << ' ' << mp[{i, to}].first  << ' ' << mp[{i, to}].second << '\n';
            if(mp[{i, to}].first > mp[{i, to}].second){
                diff += mp[{i, to}].first;
                diff -= mp[{i, to}].second;
            }
        }
        //cout << '\n' << " !!!!!!!!!!! " << diff << '\n';
        cout << diff << ' ';
    }
}


#include <bits/stdc++.h>
#define ENDL '\n'
#define deb(u) cout << #u " : " << (u) << ENDL;
#define deba(alias, u) cout << alias << ": " << (u) << ENDL;
#define debp(u, v) cout << u << " : " << v << ENDL;
#define pb push_back
#define F first
#define S second
#define lli long long
#define pii pair<int, int>
#define pll pair<lli, lli>
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define FORN(i, a, n) for (int i = (a - 1); i >= n; --i)
#define IO                          \
  ios_base::sync_with_stdio(false); \
  cin.tie(0);                       \
  cout.tie(0)
using namespace std;

vector<int> colors;
vector<int> best;
vector<vector<int>> graph;
vector<int> parent;

void dfs(int node, bool update)
{
  if (update)
  {
    if (node != parent[node])
      if (best[parent[node]] > best[node])
      {
        if (best[node] < 0)
        {
          best[node] = best[parent[node]] - 1;
        }
        else
        {
          best[node] = best[parent[node]];
        }
      }
    for (auto v : graph[node])
    {
      dfs(v, true);
    }
  }
  else
  {
    int b;
    if (colors[node] == 1)
      b = 1;
    else
      b = -1;
    for (auto v : graph[node])
    {
      dfs(v, false);
      if (best[v] > 0)
        b += best[v];
    }
    best[node] = b;
  }
}

int main()
{
  IO;
  int n;
  cin >> n;
  vector<int> colorsAux(n), bestAux(n, -1), parentAux(n);
  vector<vector<int>> graphAux(n);
  vector<bool> isParent(n, true);
  colors = colorsAux;
  best = bestAux;
  graph = graphAux;
  parent = parentAux;

  FOR(i, 0, n)
  {
    cin >> colors[i];
  }
  FOR(i, 0, n - 1)
  {
    int from, to;
    cin >> from >> to;
    from--;
    to--;
    graph[from].pb(to);
    parent[to] = from;
    isParent[to] = false;
  }
  int parent;
  FOR(i, 0, n)
  {
    if (isParent[i])
    {
      dfs(i, false);
      dfs(i, true);
    }
  }

  FOR(i, 0, n)
  {
    cout << best[i] << " ";
  }
  return 0;
}


#include <bits/stdc++.h>
#define ENDL '\n'
#define deb(u) cout << #u " : " << (u) << ENDL;
#define deba(alias, u) cout << alias << ": " << (u) << ENDL;
#define debp(u, v) cout << u << " : " << v << ENDL;
#define pb push_back
#define F first
#define S second
#define lli long long
#define pii pair<int, int>
#define pll pair<lli, lli>
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define FORN(i, a, n) for (int i = (a - 1); i >= n; --i)
#define IO                          \
  ios_base::sync_with_stdio(false); \
  cin.tie(0);                       \
  cout.tie(0)
using namespace std;

vector<int> colors;
vector<int> best;
vector<vector<int>> graph;
vector<int> parent;

void dfs(int node, bool update)
{
  if (update)
  {
    if (node != parent[node])
      if (best[parent[node]] > best[node])
      {
        if (best[node] < 0)
        {
          best[node] = best[parent[node]] - 1;
        }
        else
        {
          best[node] = best[parent[node]];
        }
      }
    for (auto v : graph[node])
    {
      dfs(v, true);
    }
  }
  else
  {
    int b;
    if (colors[node] == 1)
      b = 1;
    else
      b = -1;
    for (auto v : graph[node])
    {
      dfs(v, false);
      if (best[v] > 0)
        b += best[v];
    }
    best[node] = b;
  }
}

int main()
{
  IO;
  int n;
  cin >> n;
  vector<int> colorsAux(n), bestAux(n, -1), parentAux(n);
  vector<vector<int>> graphAux(n);
  vector<bool> isParent(n, true);
  colors = colorsAux;
  best = bestAux;
  graph = graphAux;
  parent = parentAux;

  FOR(i, 0, n)
  {
    cin >> colors[i];
  }
  FOR(i, 0, n - 1)
  {
    int from, to;
    cin >> from >> to;
    from--;
    to--;
    graph[from].pb(to);
    parent[to] = from;
    isParent[to] = false;
  }
  int parent;
  FOR(i, 0, n)
  {
    if (isParent[i])
    {
      parent = i;
      break;
    }
  }
  dfs(parent, false);
  dfs(parent, true);
  FOR(i, 0, n)
  {
    cout << best[i] << " ";
  }
  return 0;
}


#include <bits/stdc++.h>
#define ENDL '\n'
#define deb(u) cout << #u " : " << (u) << ENDL;
#define deba(alias, u) cout << alias << ": " << (u) << ENDL;
#define debp(u, v) cout << u << " : " << v << ENDL;
#define pb push_back
#define F first
#define S second
#define lli long long
#define pii pair<int, int>
#define pll pair<lli, lli>
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define FORN(i, a, n) for (int i = (a - 1); i >= n; --i)
#define IO                          \
  ios_base::sync_with_stdio(false); \
  cin.tie(0);                       \
  cout.tie(0)
using namespace std;

vector<int> colors;
vector<int> best;
vector<vector<int>> graph;
vector<bool> visited;
vector<int> parent;

void dfs(int node, bool update)
{
  int b;
  if (colors[node])
    b = 1;
  else
    b = -1;
  if (update)
  {
    if (best[node] > 0)
    {
      if (best[parent[node]] - best[node] > 0)
      {
        best[node] += best[parent[node]] - best[node];
      }
    }
    else
    {
      if (best[parent[node]] > 0)
      {
        best[node] += best[parent[node]];
      }
    }
    for (auto v : graph[node])
    {
      dfs(v, true);
    }
  }
  else
  {
    for (auto v : graph[node])
    {
      dfs(v, false);
      if (best[v] > 0)
        b += best[v];
    }
    best[node] = b;
  }
}

int main()
{
  IO;
  int n;
  cin >> n;
  vector<int> colorsAux(n), bestAux(n, 0), parentAux(n);
  vector<vector<int>> graphAux(n);
  colors = colorsAux;
  best = bestAux;
  graph = graphAux;
  parent = parentAux;
  parent[0] = 0;

  FOR(i, 0, n)
  {
    cin >> colors[i];
  }
  FOR(i, 0, n - 1)
  {
    int from, to;
    cin >> from >> to;
    graph[from - 1].pb(to - 1);
    parent[to - 1] = from - 1;
  }
  dfs(0, false);
  dfs(0, true);
  FOR(i, 0, n)
  {
    cout << best[i] << " ";
  }
  return 0;
}


#include <bits/stdc++.h>
#define ENDL '\n'
#define deb(u) cout << #u " : " << (u) << ENDL;
#define deba(alias, u) cout << alias << ": " << (u) << ENDL;
#define debp(u, v) cout << u << " : " << v << ENDL;
#define pb push_back
#define F first
#define S second
#define lli long long
#define pii pair<int, int>
#define pll pair<lli, lli>
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define FORN(i, a, n) for (int i = (a - 1); i >= n; --i)
#define IO                          \
  ios_base::sync_with_stdio(false); \
  cin.tie(0);                       \
  cout.tie(0)
using namespace std;

vector<int> colors;
vector<int> best;
vector<vector<int>> graph;
vector<bool> visited;

void dfsUp(int node, int parent)
{
  visited[node] = true;

  if (node != parent)
  {
    if (best[parent] > best[node])
    {
      if (best[node] < 0)
      {
        best[node] = best[parent] - 1;
      }
      else
      {
        best[node] = best[parent];
      }
    }
  }

  for (auto v : graph[node])
  {
    if (!visited[v])
    {
      visited[v] = true;
      dfsUp(v, node);
    }
  }
}
void dfsDown(int node)
{
  visited[node] = true;
  int b;
  if (colors[node] == 1)
    b = 1;
  else
    b = -1;
  for (auto v : graph[node])
  {
    if (!visited[v])
    {
      visited[v] = true;
      dfsDown(v);
    }
    if (best[v] > 0)
      b += best[v];
  }
  best[node] = b;
}

int main()
{
  IO;
  int n;
  cin >> n;
  vector<int> colorsAux(n), bestAux(n, -1), parentAux(n);
  vector<vector<int>> graphAux(n);
  colors = colorsAux;
  best = bestAux;
  graph = graphAux;

  FOR(i, 0, n)
  {
    cin >> colors[i];
  }
  FOR(i, 0, n - 1)
  {
    int from, to;
    cin >> from >> to;
    from--;
    to--;
    graph[from].pb(to);
    graph[to].pb(from);
  }
  visited = vector<bool>(n, false);
  for (int i(0); i < n; i++)
  {
    if (graph[i].size() == 1)
    {
      dfsDown(i);
      visited = vector<bool>(n, false);
      dfsUp(i, i);
      break;
    }
  }

  FOR(i, 0, n)
  {
    cout << best[i] << " ";
  }
  return 0;
}


#include <bits/stdc++.h>
#define ENDL '\n'
#define deb(u) cout << #u " : " << (u) << ENDL;
#define deba(alias, u) cout << alias << ": " << (u) << ENDL;
#define debp(u, v) cout << u << " : " << v << ENDL;
#define pb push_back
#define F first
#define S second
#define lli long long
#define pii pair<int, int>
#define pll pair<lli, lli>
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define FORN(i, a, n) for (int i = (a - 1); i >= n; --i)
#define IO                          \
  ios_base::sync_with_stdio(false); \
  cin.tie(0);                       \
  cout.tie(0)
using namespace std;

vector<int> colors;
vector<int> best;
vector<vector<int>> graph;
vector<int> parent;

void dfs(int node, bool update)
{
  if (update)
  {
    if (node != parent[node])
      if (best[node] > 0)
      {
        if (best[parent[node]] > best[node])
        {
          best[node] = best[parent[node]];
        }
      }
      else
      {
        if (best[parent[node]] > 0)
        {
          best[node] += best[parent[node]];
        }
      }
    for (auto v : graph[node])
    {
      dfs(v, true);
    }
  }
  else
  {
    int b;
    if (colors[node])
      b = 1;
    else
      b = -1;
    for (auto v : graph[node])
    {
      dfs(v, false);
      if (best[v] > 0)
        b += best[v];
    }
    best[node] = b;
  }
}

int main()
{
  IO;
  int n;
  cin >> n;
  vector<int> colorsAux(n), bestAux(n, -1), parentAux(n);
  vector<vector<int>> graphAux(n);
  vector<bool> isParent(n, true);
  colors = colorsAux;
  best = bestAux;
  graph = graphAux;
  parent = parentAux;

  FOR(i, 0, n)
  {
    cin >> colors[i];
  }
  FOR(i, 0, n - 1)
  {
    int from, to;
    cin >> from >> to;
    from--;
    to--;
    graph[from].pb(to);
    parent[to] = from;
    isParent[to] = false;
  }
  int parent;
  FOR(i, 0, n)
  {
    if (isParent[i])
    {
      parent = i;
      break;
    }
  }
  dfs(parent, false);
  dfs(parent, true);
  FOR(i, 0, n)
  {
    cout << best[i] << " ";
  }
  return 0;
}


#include <bits/stdc++.h>
#define ENDL '\n'
#define deb(u) cout << #u " : " << (u) << ENDL;
#define deba(alias, u) cout << alias << ": " << (u) << ENDL;
#define debp(u, v) cout << u << " : " << v << ENDL;
#define pb push_back
#define F first
#define S second
#define lli long long
#define pii pair<int, int>
#define pll pair<lli, lli>
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define FORN(i, a, n) for (int i = (a - 1); i >= n; --i)
#define IO                          \
  ios_base::sync_with_stdio(false); \
  cin.tie(0);                       \
  cout.tie(0)
using namespace std;

vector<int> colors;
vector<int> best;
vector<vector<int>> graph;
vector<int> parent;

void dfs(int node, bool update)
{
  if (update)
  {
    if (node != parent[node])
      if (best[node] > 0)
      {
        if (best[parent[node]] - best[node] > 0)
        {
          best[node] += best[parent[node]] - best[node];
        }
      }
      else
      {
        if (best[parent[node]] > 0)
        {
          best[node] += best[parent[node]];
        }
      }
    for (auto v : graph[node])
    {
      dfs(v, true);
    }
  }
  else
  {
    int b;
    if (colors[node])
      b = 1;
    else
      b = -1;
    for (auto v : graph[node])
    {
      dfs(v, false);
      if (best[v] > 0)
        b += best[v];
    }
    best[node] = b;
  }
}

int main()
{
  IO;
  int n;
  cin >> n;
  vector<int> colorsAux(n), bestAux(n, -1), parentAux(n);
  vector<vector<int>> graphAux(n);
  vector<bool> isParent(n, true);
  colors = colorsAux;
  best = bestAux;
  graph = graphAux;
  parent = parentAux;

  FOR(i, 0, n)
  {
    cin >> colors[i];
  }
  FOR(i, 0, n - 1)
  {
    int from, to;
    cin >> from >> to;
    graph[from - 1].pb(to - 1);
    parent[to - 1] = from - 1;
    isParent[to - 1] = false;
  }
  int parent;
  FOR(i, 0, n)
  {
    if (isParent[i])
    {
      parent = i;
      break;
    }
  }
  dfs(parent, false);
  dfs(parent, true);
  FOR(i, 0, n)
  {
    cout << best[i] << " ";
  }
  return 0;
}


#include <bits/stdc++.h>
#define ENDL '\n'
#define deb(u) cout << #u " : " << (u) << ENDL;
#define deba(alias, u) cout << alias << ": " << (u) << ENDL;
#define debp(u, v) cout << u << " : " << v << ENDL;
#define pb push_back
#define F first
#define S second
#define lli long long
#define pii pair<int, int>
#define pll pair<lli, lli>
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define FORN(i, a, n) for (int i = (a - 1); i >= n; --i)
#define IO                          \
  ios_base::sync_with_stdio(false); \
  cin.tie(0);                       \
  cout.tie(0)
using namespace std;

vector<int> colors;
vector<int> best;
vector<vector<int>> graph;
vector<int> parentNode;

void dfs(int node, bool update)
{
  if (update)
  {
    if (node != parentNode[node])
      if (best[parentNode[node]] > best[node])
      {
        if (best[node] < 0)
        {
          best[node] = best[parentNode[node]] - 1;
        }
        else
        {
          best[node] = best[parentNode[node]];
        }
      }
    for (auto v : graph[node])
    {
      dfs(v, true);
    }
  }
  else
  {
    int b;
    if (colors[node] == 1)
      b = 1;
    else
      b = -1;
    for (auto v : graph[node])
    {
      dfs(v, false);
      if (best[v] > 0)
        b += best[v];
    }
    best[node] = max(b, best[node]);
  }
}

int main()
{
  IO;
  int n;
  cin >> n;
  vector<int> colorsAux(n), bestAux(n, -1), parentAux(n);
  vector<vector<int>> graphAux(n);
  vector<bool> isParent(n, true);
  colors = colorsAux;
  best = bestAux;
  graph = graphAux;
  parentNode = parentAux;

  FOR(i, 0, n)
  {
    cin >> colors[i];
  }
  FOR(i, 0, n - 1)
  {
    int from, to;
    cin >> from >> to;
    from--;
    to--;
    graph[from].pb(to);
    parentNode[to] = from;
    isParent[to] = false;
  }
  int parent;
  for (int i(0); i < n; i++)
  {
    if (isParent[i])
    {
      parentNode[i] = i;
      dfs(i, false);
      dfs(i, true);
    }
  }

  FOR(i, 0, n)
  {
    cout << best[i] << " ";
  }
  return 0;
}


#include <bits/stdc++.h>
#define ENDL '\n'
#define deb(u) cout << #u " : " << (u) << ENDL;
#define deba(alias, u) cout << alias << ": " << (u) << ENDL;
#define debp(u, v) cout << u << " : " << v << ENDL;
#define pb push_back
#define F first
#define S second
#define lli long long
#define pii pair<int, int>
#define pll pair<lli, lli>
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define FORN(i, a, n) for (int i = (a - 1); i >= n; --i)
#define IO                          \
  ios_base::sync_with_stdio(false); \
  cin.tie(0);                       \
  cout.tie(0)
using namespace std;

vector<int> colors;
vector<int> best;
vector<vector<int>> graph;
vector<bool> visited;
vector<int> parent;

void dfs(int node, bool update)
{

  if (update)
  {
    if (node != parent[node])
      if (best[node] > 0)
      {
        if (best[parent[node]] - best[node] > 0)
        {
          best[node] += best[parent[node]] - best[node];
        }
      }
      else
      {
        if (best[parent[node]] > 0)
        {
          best[node] += best[parent[node]];
        }
      }
    for (auto v : graph[node])
    {
      dfs(v, true);
    }
  }
  else
  {
    int b;
    if (colors[node])
      b = 1;
    else
      b = -1;
    for (auto v : graph[node])
    {
      dfs(v, false);
      if (best[v] > 0)
        b += best[v];
    }
    best[node] = b;
  }
}

int main()
{
  IO;
  int n;
  cin >> n;
  vector<int> colorsAux(n), bestAux(n, 0), parentAux(n);
  vector<vector<int>> graphAux(n);
  colors = colorsAux;
  best = bestAux;
  graph = graphAux;
  parent = parentAux;
  parent[0] = 0;

  FOR(i, 0, n)
  {
    cin >> colors[i];
  }
  FOR(i, 0, n - 1)
  {
    int from, to;
    cin >> from >> to;
    graph[from - 1].pb(to - 1);
    parent[to - 1] = from - 1;
  }
  dfs(0, false);
  dfs(0, true);
  FOR(i, 0, n)
  {
    cout << best[i] << " ";
  }
  return 0;
}


#include <bits/stdc++.h>
#define ENDL '\n'
#define deb(u) cout << #u " : " << (u) << ENDL;
#define deba(alias, u) cout << alias << ": " << (u) << ENDL;
#define debp(u, v) cout << u << " : " << v << ENDL;
#define pb push_back
#define F first
#define S second
#define lli long long
#define pii pair<int, int>
#define pll pair<lli, lli>
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define FORN(i, a, n) for (int i = (a - 1); i >= n; --i)
#define IO                          \
  ios_base::sync_with_stdio(false); \
  cin.tie(0);                       \
  cout.tie(0)
using namespace std;

vector<int> colors;
vector<int> best;
vector<vector<int>> graph;
vector<int> parent;

void dfs(int node, bool update)
{
  if (update)
  {
    if (node != parent[node])
      if (best[node] > 0)
      {
        if (best[parent[node]] - best[node] > 0)
        {
          best[node] += best[parent[node]] - best[node];
        }
      }
      else
      {
        if (best[parent[node]] > 0)
        {
          best[node] += best[parent[node]];
        }
      }
    for (auto v : graph[node])
    {
      dfs(v, true);
    }
  }
  else
  {
    int b;
    if (colors[node])
      b = 1;
    else
      b = -1;
    for (auto v : graph[node])
    {
      dfs(v, false);
      if (best[v] > 0)
        b += best[v];
    }
    best[node] = b;
  }
}

int main()
{
  IO;
  int n;
  cin >> n;
  vector<int> colorsAux(n), bestAux(n, 0), parentAux(n);
  vector<vector<int>> graphAux(n);
  vector<bool> isParent(n, true);
  colors = colorsAux;
  best = bestAux;
  graph = graphAux;
  parent = parentAux;

  FOR(i, 0, n)
  {
    cin >> colors[i];
  }
  FOR(i, 0, n - 1)
  {
    int from, to;
    cin >> from >> to;
    graph[from - 1].pb(to - 1);
    parent[to - 1] = from - 1;
    isParent[to - 1] = false;
  }
  int parent;
  FOR(i, 0, n)
  {
    if (isParent[i])
    {
      parent = i;
      break;
    }
  }
  dfs(parent, false);
  dfs(parent, true);
  FOR(i, 0, n)
  {
    cout << best[i] << " ";
  }
  return 0;
}


#include <bits/stdc++.h>
#define ENDL '\n'
#define deb(u) cout << #u " : " << (u) << ENDL;
#define deba(alias, u) cout << alias << ": " << (u) << ENDL;
#define debp(u, v) cout << u << " : " << v << ENDL;
#define pb push_back
#define F first
#define S second
#define lli long long
#define pii pair<int, int>
#define pll pair<lli, lli>
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define FORN(i, a, n) for (int i = (a - 1); i >= n; --i)
#define IO                          \
  ios_base::sync_with_stdio(false); \
  cin.tie(0);                       \
  cout.tie(0)
using namespace std;

vector<int> colors;
vector<int> best;
vector<vector<int>> graph;
vector<int> parent;

void dfs(int node, bool update)
{
  if (update)
  {
    if (node != parent[node])
      if (best[node] > 0)
      {
        if (best[parent[node]] - best[node] > 0)
        {
          best[node] += best[parent[node]] - best[node];
        }
      }
      else
      {
        if (best[parent[node]] > 0)
        {
          best[node] += best[parent[node]];
        }
      }
    for (auto v : graph[node])
    {
      dfs(v, true);
    }
  }
  else
  {
    int b;
    if (colors[node])
      b = 1;
    else
      b = -1;
    for (auto v : graph[node])
    {
      dfs(v, false);
      if (best[v] > 0)
        b += best[v];
    }
    best[node] = b;
  }
}

int main()
{
  IO;
  int n;
  cin >> n;
  vector<int> colorsAux(n), bestAux(n, 0), parentAux(n);
  vector<vector<int>> graphAux(n);
  vector<bool> isParent(n, true);
  colors = colorsAux;
  best = bestAux;
  graph = graphAux;
  parent = parentAux;

  FOR(i, 0, n)
  {
    cin >> colors[i];
  }
  FOR(i, 0, n - 1)
  {
    int from, to;
    cin >> from >> to;
    graph[from - 1].pb(to - 1);
    parent[to - 1] = from - 1;
    isParent[to] = false;
  }
  int parent;
  FOR(i, 0, n)
  {
    if (isParent[i])
    {
      parent = i;
      break;
    }
  }
  dfs(parent, false);
  dfs(parent, true);
  FOR(i, 0, n)
  {
    cout << best[i] << " ";
  }
  return 0;
}


#include <bits/stdc++.h>
#define ENDL '\n'
#define deb(u) cout << #u " : " << (u) << ENDL;
#define deba(alias, u) cout << alias << ": " << (u) << ENDL;
#define debp(u, v) cout << u << " : " << v << ENDL;
#define pb push_back
#define F first
#define S second
#define lli long long
#define pii pair<int, int>
#define pll pair<lli, lli>
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define FORN(i, a, n) for (int i = (a - 1); i >= n; --i)
#define IO                          \
  ios_base::sync_with_stdio(false); \
  cin.tie(0);                       \
  cout.tie(0)
using namespace std;

vector<int> colors;
vector<int> best;
vector<vector<int>> graph;
vector<int> parent;

void dfs(int node, bool update)
{
  if (update)
  {
    if (node != parent[node])
      if (best[parent[node]] > best[node])
      {
        if (best[node] < 0)
        {
          best[node] = best[parent[node]] - 1;
        }
        else
        {
          best[node] = best[parent[node]];
        }
      }
    for (auto v : graph[node])
    {
      dfs(v, true);
    }
  }
  else
  {
    int b;
    if (colors[node] == 1)
      b = 1;
    else
      b = -1;
    for (auto v : graph[node])
    {
      dfs(v, false);
      if (best[v] > 0)
        b += best[v];
    }
    best[node] = max(b, best[node]);
  }
}

int main()
{
  IO;
  int n;
  cin >> n;
  vector<int> colorsAux(n), bestAux(n, -1), parentAux(n);
  vector<vector<int>> graphAux(n);
  vector<bool> isParent(n, true);
  colors = colorsAux;
  best = bestAux;
  graph = graphAux;
  parent = parentAux;

  FOR(i, 0, n)
  {
    cin >> colors[i];
  }
  FOR(i, 0, n - 1)
  {
    int from, to;
    cin >> from >> to;
    from--;
    to--;
    graph[from].pb(to);
    parent[to] = from;
    isParent[to] = false;
  }
  int parent;
  FOR(i, 0, n)
  {
    if (isParent[i])
    {
      dfs(i, false);
    }
  }
  FOR(i, 0, n)
  {
    if (isParent[i])
    {
      dfs(i, true);
    }
  }

  FOR(i, 0, n)
  {
    cout << best[i] << " ";
  }
  return 0;
}


#include <bits/stdc++.h>
#define ENDL '\n'
#define deb(u) cout << #u " : " << (u) << ENDL;
#define deba(alias, u) cout << alias << ": " << (u) << ENDL;
#define debp(u, v) cout << u << " : " << v << ENDL;
#define pb push_back
#define F first
#define S second
#define lli long long
#define pii pair<int, int>
#define pll pair<lli, lli>
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define FORN(i, a, n) for (int i = (a - 1); i >= n; --i)
#define IO                          \
  ios_base::sync_with_stdio(false); \
  cin.tie(0);                       \
  cout.tie(0)
using namespace std;

vector<int> colors;
vector<int> best;
vector<vector<int>> graph;
vector<int> parent;

void dfs(int node, bool update)
{
  if (update)
  {
    if (node != parent[node])
      if (best[parent[node]] > best[node])
      {
        if (best[node] < 0)
        {
          best[node] = best[parent[node]] - 1;
        }
        else
        {
          best[node] = best[parent[node]];
        }
      }
    for (auto v : graph[node])
    {
      dfs(v, true);
    }
  }
  else
  {
    int b;
    if (colors[node])
      b = 1;
    else
      b = -1;
    for (auto v : graph[node])
    {
      dfs(v, false);
      if (best[v] > 0)
        b += best[v];
    }
    best[node] = b;
  }
}

int main()
{
  IO;
  int n;
  cin >> n;
  vector<int> colorsAux(n), bestAux(n, -1), parentAux(n);
  vector<vector<int>> graphAux(n);
  vector<bool> isParent(n, true);
  colors = colorsAux;
  best = bestAux;
  graph = graphAux;
  parent = parentAux;

  FOR(i, 0, n)
  {
    cin >> colors[i];
  }
  FOR(i, 0, n - 1)
  {
    int from, to;
    cin >> from >> to;
    from--;
    to--;
    graph[from].pb(to);
    parent[to] = from;
    isParent[to] = false;
  }
  int parent;
  FOR(i, 0, n)
  {
    if (isParent[i])
    {
      parent = i;
      break;
    }
  }
  dfs(parent, false);
  dfs(parent, true);
  FOR(i, 0, n)
  {
    cout << best[i] << " ";
  }
  return 0;
}


#include <bits/stdc++.h>
#define ENDL '\n'
#define deb(u) cout << #u " : " << (u) << ENDL;
#define deba(alias, u) cout << alias << ": " << (u) << ENDL;
#define debp(u, v) cout << u << " : " << v << ENDL;
#define pb push_back
#define F first
#define S second
#define lli long long
#define pii pair<int, int>
#define pll pair<lli, lli>
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define FORN(i, a, n) for (int i = (a - 1); i >= n; --i)
#define IO                          \
  ios_base::sync_with_stdio(false); \
  cin.tie(0);                       \
  cout.tie(0)
using namespace std;

vector<int> colors;
vector<int> best;
vector<vector<int>> graph;
vector<int> parent;

void dfs(int node, bool update)
{
  if (update)
  {
    if (node != parent[node])
      if (best[parent[node]] > best[node])
      {
        if (best[node] < 0)
        {
          best[node] = best[parent[node]] - 1;
        }
        else
        {
          best[node] = best[parent[node]];
        }
      }
    for (auto v : graph[node])
    {
      dfs(v, true);
    }
  }
  else
  {
    int b;
    if (colors[node] == 1)
      b = 1;
    else
      b = -1;
    for (auto v : graph[node])
    {
      dfs(v, false);
      if (best[v] > 0)
        b += best[v];
    }
    best[node] = max(b, best[node]);
  }
}

int main()
{
  IO;
  int n;
  cin >> n;
  vector<int> colorsAux(n), bestAux(n, -1), parentAux(n);
  vector<vector<int>> graphAux(n);
  vector<bool> isParent(n, true);
  colors = colorsAux;
  best = bestAux;
  graph = graphAux;
  parent = parentAux;

  FOR(i, 0, n)
  {
    cin >> colors[i];
  }
  FOR(i, 0, n - 1)
  {
    int from, to;
    cin >> from >> to;
    from--;
    to--;
    graph[from].pb(to);
    parent[to] = from;
    isParent[to] = false;
  }
  int parent;
  FOR(i, 0, n)
  {
    dfs(i, false);
  }
  FOR(i, 0, n)
  {
    if (isParent[i])
      dfs(i, true);
  }

  FOR(i, 0, n)
  {
    cout << best[i] << " ";
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define mxe(v) *max_element(v.begin(),v.end())
#define mne(v) *min_element(v.begin(),v.end())
#define ub upper_bound
#define lb lower_bound
#define sz(a) int((a).size())
#define fill(a) memset(a,0,sizeof(a))
#define present(c,x) (c.find(x) != c.end())
#define removeDuplicates(a) a.resize(unique(all(a))-a.begin())
#define endl '\n'
#define INF 1000000007
#define MOD 998244353
#define INF64 1e18

vector<int>dp1(1000005);
vector<int>dp2(1000005);

void dfs1(vector<vector<int>>edge,vector<int>color,int u,int parent){
	dp1[u]=color[u];
	for(auto child:edge[u]){
		if(child!=parent){
			dfs1(edge,color,child,u);
			dp1[u]+=max(0LL,dp1[child]);
		}
	}
}

void dfs2(vector<vector<int>>edge,int u,int parent){
	dp2[u]=dp1[u];
	if(parent){
		int par=dp2[parent];
		par-=max(0LL,dp1[u]);
		par=max(0LL,par);

		dp2[u]+=par;
	}

	for(auto child:edge[u]){
		if(child!=parent){
			dfs2(edge,child,u);
		}
	}
}

signed main(){

	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);

	int n;
	cin>>n;
	vector<int>color(n+1);
	vector<vector<int>>edge(n+1);
	for(int i=1;i<=n;i++){
		cin>>color[i];
		if(!color[i]) color[i]--;
	}
	for(int i=0;i<n-1;i++){
		int a,b;
		cin>>a>>b;
		edge[a].pb(b);
		edge[b].pb(a);
	}
	dfs1(edge,color,1,0);
	dfs2(edge,1,0);
	for(int i=1;i<=n;i++){
		cout<<dp2[i]<<" ";
	}
}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define mxe(v) *max_element(v.begin(),v.end())
#define mne(v) *min_element(v.begin(),v.end())
#define ub upper_bound
#define lb lower_bound
#define sz(a) int((a).size())
#define fill(a) memset(a,0,sizeof(a))
#define present(c,x) (c.find(x) != c.end())
#define removeDuplicates(a) a.resize(unique(all(a))-a.begin())
#define endl '\n'
#define INF 1000000007
#define MOD 998244353
#define INF64 1e18

vector<int>dp1(1000005);
vector<int>dp2(1000005);
vector<int>edge[1000005];
vector<int>color(1000005);

void dfs1(int u,int parent){
	dp1[u]=color[u];
	for(auto child:edge[u]){
		if(child!=parent){
			dfs1(child,u);
			dp1[u]+=max(0LL,dp1[child]);
		}
	}
}

void dfs2(int u,int parent){
	dp2[u]=dp1[u];
	if(parent){
		int par=dp2[parent];
		par-=max(0LL,dp1[u]);
		par=max(0LL,par);

		dp2[u]+=par;
	}

	for(auto child:edge[u]){
		if(child!=parent){
			dfs2(child,u);
		}
	}
}

signed main(){

	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);

	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>color[i];
		if(!color[i]){
			color[i]--;
		}
	}
	for(int i=0;i<n-1;i++){
		int a,b;
		cin>>a>>b;
		edge[a].pb(b);
		edge[b].pb(a);
	}
	dfs1(1,0);
	dfs2(1,0);
	for(int i=1;i<=n;i++){
		cout<<dp2[i]<<" ";
	}
}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define mxe(v) *max_element(v.begin(),v.end())
#define mne(v) *min_element(v.begin(),v.end())
#define ub upper_bound
#define lb lower_bound
#define sz(a) int((a).size())
#define fill(a) memset(a,0,sizeof(a))
#define present(c,x) (c.find(x) != c.end())
#define removeDuplicates(a) a.resize(unique(all(a))-a.begin())
#define endl '\n'
#define INF 1000000007
#define MOD 998244353
#define INF64 1e18

vector<int>dp1;
vector<int>dp2;

void dfs1(vector<vector<int>>edge,vector<int>color,int u,int parent){
	dp1[u]=color[u];
	for(auto child:edge[u]){
		if(child!=parent){
			dfs1(edge,color,child,u);
			dp1[u]+=max(0LL,dp1[child]);
		}
	}
}

void dfs2(vector<vector<int>>edge,int u,int parent){
	dp2[u]=dp1[u];
	if(parent!=0){
		int par=dp2[parent];
		par-=max(0LL,dp1[u]);
		par=max(0LL,par);

		dp2[u]+=par;
	}

	for(auto child:edge[u]){
		if(child!=parent){
			dfs2(edge,child,u);
		}
	}
}

signed main(){

	ios_base::sync_with_stdio(false);
	cin.tie(0);

	int n;
	cin>>n;
	vector<int>color(n+1);
	vector<vector<int>>edge(n+1);
	dp1.resize(n+1);
	dp2.resize(n+1);
	for(int i=1;i<=n;i++){
		cin>>color[i];
		if(!color[i]) color[i]--;
	}
	for(int i=0;i<n-1;i++){
		int a,b;
		cin>>a>>b;
		edge[a].pb(b);
		edge[b].pb(a);
	}
	dfs1(edge,color,1,0);
	dfs2(edge,1,0);
	for(int i=1;i<=n;i++){
		cout<<dp2[i]<<" ";
	}
}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define mxe(v) *max_element(v.begin(),v.end())
#define mne(v) *min_element(v.begin(),v.end())
#define ub upper_bound
#define lb lower_bound
#define sz(a) int((a).size())
#define fill(a) memset(a,0,sizeof(a))
#define present(c,x) (c.find(x) != c.end())
#define removeDuplicates(a) a.resize(unique(all(a))-a.begin())
#define endl '\n'
#define INF 1000000007
#define MOD 998244353
#define INF64 1e18

vector<int>dp1(1000005);
vector<int>dp2(1000005);
vector<int>edge[1000005];
vector<int>color(1000005);

void dfs1(int u,int parent){
	dp1[u]=color[u];
	for(auto child:edge[u]){
		if(child!=parent){
			dfs1(child,u);
			dp1[u]+=max(0LL,dp1[child]);
		}
	}
}

void dfs2(int u,int parent){
	dp2[u]=dp1[u];
	if(parent){
		int par=dp2[parent];
		par-=max(0LL,dp1[u]);
		par=max(0LL,par);

		dp2[u]+=par;
	}

	for(auto child:edge[u]){
		if(child!=parent){
			dfs2(child,u);
		}
	}
}

signed main(){

	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);

	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>color[i];
		if(!color[i]){
			color[i]--;
		}
	}
	for(int i=0;i<n-1;i++){
		int a,b;
		cin>>a>>b;
		edge[a].pb(b);
		edge[b].pb(a);
	}
	dfs1(edge,color,1,0);
	dfs2(edge,1,0);
	for(int i=1;i<=n;i++){
		cout<<dp2[i]<<" ";
	}
}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define mxe(v) *max_element(v.begin(),v.end())
#define mne(v) *min_element(v.begin(),v.end())
#define ub upper_bound
#define lb lower_bound
#define sz(a) int((a).size())
#define fill(a) memset(a,0,sizeof(a))
#define present(c,x) (c.find(x) != c.end())
#define removeDuplicates(a) a.resize(unique(all(a))-a.begin())
#define endl '\n'
#define INF 1000000007
#define MOD 998244353
#define INF64 1e18

vector<int>dp1(1000005);
vector<int>dp2(1000005);

void dfs1(vector<int>edge[],vector<int>color,int u,int parent){
	dp1[u]=color[u];
	for(auto child:edge[u]){
		if(child!=parent){
			dfs1(edge,color,child,u);
			dp1[u]+=max(0LL,dp1[child]);
		}
	}
}

void dfs2(vector<int>edge[],int u,int parent){
	dp2[u]=dp1[u];
	if(parent){
		int par=dp2[parent];
		par-=max(0LL,dp1[u]);
		par=max(0LL,par);

		dp2[u]+=par;
	}

	for(auto child:edge[u]){
		if(child!=parent){
			dfs2(edge,child,u);
		}
	}
}

signed main(){

	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);

	int n;
	cin>>n;
	vector<int>color(n+1);
	vector<int>edge[n+1];
	for(int i=1;i<=n;i++){
		cin>>color[i];
		if(!color[i]){
			color[i]--;
		}
	}
	for(int i=0;i<n-1;i++){
		int a,b;
		cin>>a>>b;
		edge[a].pb(b);
		edge[b].pb(a);
	}
	dfs1(edge,color,1,0);
	dfs2(edge,1,0);
	for(int i=1;i<=n;i++){
		cout<<dp2[i]<<" ";
	}
}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define mxe(v) *max_element(v.begin(),v.end())
#define mne(v) *min_element(v.begin(),v.end())
#define ub upper_bound
#define lb lower_bound
#define sz(a) int((a).size())
#define fill(a) memset(a,0,sizeof(a))
#define present(c,x) (c.find(x) != c.end())
#define removeDuplicates(a) a.resize(unique(all(a))-a.begin())
#define endl '\n'
#define INF 1000000007
#define MOD 998244353
#define INF64 1e18

vector<int>dp1;
vector<int>dp2;

void dfs1(vector<vector<int>>edge,vector<int>color,int u,int parent){
	dp1[u]=color[u];
	for(auto child:edge[u]){
		if(child!=parent){
			dfs1(edge,color,child,u);
			dp1[u]+=max(0LL,dp1[child]);
		}
	}
}

void dfs2(vector<vector<int>>edge,int u,int parent){
	dp2[u]=dp1[u];
	if(parent){
		int par=dp2[parent];
		par-=max(0LL,dp1[u]);
		par=max(0LL,par);

		dp2[u]+=par;
	}

	for(auto child:edge[u]){
		if(child!=parent){
			dfs2(edge,child,u);
		}
	}
}

signed main(){

	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);

	int n;
	cin>>n;
	vector<int>color(n+1);
	vector<vector<int>>edge(n+1);
	dp1.resize(n+1);
	dp2.resize(n+1);
	for(int i=1;i<=n;i++){
		cin>>color[i];
		if(!color[i]) color[i]--;
	}
	for(int i=0;i<n-1;i++){
		int a,b;
		cin>>a>>b;
		edge[a].pb(b);
		edge[b].pb(a);
	}
	dfs1(edge,color,1,0);
	dfs2(edge,1,0);
	for(int i=1;i<=n;i++){
		cout<<dp2[i]<<" ";
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
#define pci pair<char,int>
#define pib pair<int,bool>

void print(vector<int>& v){for(int i=0;i<v.size();i++){cout<<v[i]<<' ';}cout<<endl;}
void print(vector<char>& v){for(int i=0;i<v.size();i++){cout<<v[i]<<' ';}cout<<endl;}
void print(vector<string>& v){for(int i=0;i<v.size();i++){cout<<v[i]<<' ';}cout<<endl;}
void print(vector<bool>& v){for(int i=0;i<v.size();i++){cout<<v[i]<<' ';}cout<<endl;}
void print(vector<pair<int,int>>& v){for(int i=0;i<v.size();i++){cout<<v[i].F<<','<<v[i].S<<'|';}cout<<endl;}
void print(vector<vector<int>>&v){for(int i=0;i<v.size();i++){print(v[i]);}}
void print(vector<vector<bool>>&v){for(int i=0;i<v.size();i++){print(v[i]);}}

vector<int>v;
vector<vector<int>>adj;
vector<map<int,int>>val;

int dfs(int node, int parent){
	int temp = val[node][parent];
	if(temp>=0){
		return temp;
	}
	int ans = (v[node]==0)?-1:1;
	for(auto u:adj[node]){
		if(u==parent)continue;
		ans+=dfs(u,node);
	}
	val[node][parent]=max(0,ans);
	if(ans<0){
		return 0;
	}
	return ans;
}
void TestCase(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	// n = 5000;
	cin>>n;
	v.resize(n);
	for(int i=0;i<n;i++){
		cin>>v[i];
		// v[i]=0;
	}
	adj.resize(n);
	val.resize(n);
	for(int i=0;i<n-1;i++){
		int a,b;
		cin>>a>>b;
		// a=i+1;b=i+2;
		a--;b--;
		adj[a].pb(b);
		adj[b].pb(a);
		val[a][b]=-1;
		val[b][a]=-1;
	}
	for(int i=0;i<n;i++){
		for(auto u:adj[i]){
			dfs(u,i);
		}
	}
	for(int i=0;i<n;i++){
		int ans = (v[i]==0)?-1:1;
		for(auto u:adj[i]){
			ans+= val[u][i];
		}
		cout<<ans<<' ';
	}
	cout<<'\n';
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int T;
	T = 1;
	// cin>>T;
	for(int tt=1;tt<=T;tt++){
		TestCase();
	}
 }


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
#define pci pair<char,int>
#define pib pair<int,bool>

void print(vector<int>& v){for(int i=0;i<v.size();i++){cout<<v[i]<<' ';}cout<<endl;}
void print(vector<char>& v){for(int i=0;i<v.size();i++){cout<<v[i]<<' ';}cout<<endl;}
void print(vector<string>& v){for(int i=0;i<v.size();i++){cout<<v[i]<<' ';}cout<<endl;}
void print(vector<bool>& v){for(int i=0;i<v.size();i++){cout<<v[i]<<' ';}cout<<endl;}
void print(vector<pair<int,int>>& v){for(int i=0;i<v.size();i++){cout<<v[i].F<<','<<v[i].S<<'|';}cout<<endl;}
void print(vector<vector<int>>&v){for(int i=0;i<v.size();i++){print(v[i]);}}
void print(vector<vector<bool>>&v){for(int i=0;i<v.size();i++){print(v[i]);}}

vector<int>v;
vector<vector<int>>adj;
map<pii,int>val;

int dfs(int node, int parent){
	if(val[{node,parent}]>=0){
		return val[{node,parent}];
	}
	int ans = (v[node]==0)?-1:1;
	for(auto u:adj[node]){
		if(u==parent)continue;
		ans+=dfs(u,node);
	}
	if(ans<0){
		val[{node,parent}]=0;
		return 0;
	}
	val[{node,parent}]=ans;
	return ans;
}
void TestCase(){
	int n;cin>>n;
	v.resize(n);
	for(int i=0;i<n;i++)cin>>v[i];
	adj.resize(n);
	for(int i=0;i<n-1;i++){
		int a,b;
		cin>>a>>b;
		a--;b--;
		adj[a].pb(b);
		adj[b].pb(a);
		val[{a,b}]=-1;
		val[{b,a}]=-1;
	}
	for(int i=0;i<n;i++){
		int ans = (v[i]==0)?-1:1;
		for(auto u:adj[i]){
			ans+= dfs(u,i);
		}
		cout<<ans<<' ';
	}
	cout<<'\n';
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int T;
	T = 1;
	// cin>>T;
	for(int tt=1;tt<=T;tt++){
		TestCase();
	}
 }


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
#define pci pair<char,int>
#define pib pair<int,bool>

void print(vector<int>& v){for(int i=0;i<v.size();i++){cout<<v[i]<<' ';}cout<<endl;}
void print(vector<char>& v){for(int i=0;i<v.size();i++){cout<<v[i]<<' ';}cout<<endl;}
void print(vector<string>& v){for(int i=0;i<v.size();i++){cout<<v[i]<<' ';}cout<<endl;}
void print(vector<bool>& v){for(int i=0;i<v.size();i++){cout<<v[i]<<' ';}cout<<endl;}
void print(vector<pair<int,int>>& v){for(int i=0;i<v.size();i++){cout<<v[i].F<<','<<v[i].S<<'|';}cout<<endl;}
void print(vector<vector<int>>&v){for(int i=0;i<v.size();i++){print(v[i]);}}
void print(vector<vector<bool>>&v){for(int i=0;i<v.size();i++){print(v[i]);}}

vector<int>v;
vector<vector<int>>adj;
vector<map<int,int>>val;

int dfs(int node, int parent){
	if(val[node][parent]>=0){
		return val[node][parent];
	}
	int ans = (v[node]==0)?-1:1;
	for(auto u:adj[node]){
		if(u==parent)continue;
		ans+=dfs(u,node);
	}
	if(ans<0){
		val[node][parent]=0;
		return 0;
	}
	val[node][parent]=ans;
	return ans;
}
void TestCase(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	// n = 5000;
	cin>>n;
	v.resize(n);
	for(int i=0;i<n;i++){
		cin>>v[i];
		// v[i]=0;
	}
	adj.resize(n);
	val.resize(n);
	for(int i=0;i<n-1;i++){
		int a,b;
		cin>>a>>b;
		// a=i+1;b=i+2;
		a--;b--;
		adj[a].pb(b);
		adj[b].pb(a);
		val[a][b]=-1;
		val[b][a]=-1;
	}
	for(int i=0;i<n;i++){
		for(auto u:adj[i]){
			dfs(u,i);
		}
	}
	for(int i=0;i<n;i++){
		int ans = (v[i]==0)?-1:1;
		for(auto u:adj[i]){
			ans+= val[u][i];
		}
		cout<<ans<<' ';
	}
	cout<<'\n';
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int T;
	T = 1;
	// cin>>T;
	for(int tt=1;tt<=T;tt++){
		TestCase();
	}
 }


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
#define pci pair<char,int>
#define pib pair<int,bool>

void print(vector<int>& v){for(int i=0;i<v.size();i++){cout<<v[i]<<' ';}cout<<endl;}
void print(vector<char>& v){for(int i=0;i<v.size();i++){cout<<v[i]<<' ';}cout<<endl;}
void print(vector<string>& v){for(int i=0;i<v.size();i++){cout<<v[i]<<' ';}cout<<endl;}
void print(vector<bool>& v){for(int i=0;i<v.size();i++){cout<<v[i]<<' ';}cout<<endl;}
void print(vector<pair<int,int>>& v){for(int i=0;i<v.size();i++){cout<<v[i].F<<','<<v[i].S<<'|';}cout<<endl;}
void print(vector<vector<int>>&v){for(int i=0;i<v.size();i++){print(v[i]);}}
void print(vector<vector<bool>>&v){for(int i=0;i<v.size();i++){print(v[i]);}}
void print(map<int,int>&v){for(auto i:v){cout<<i.F<<','<<i.S<<' ';}cout<<endl;}

vector<int>v;
vector<vector<int>>adj;
vector<map<int,int>>val;
vector<int>dp;
int count_=0;

int dfs(int node, int parent){
	int temp = val[node][parent];
	if(temp>=-1){
		return temp;
	}
	int ans = (v[node]==0)?-1:1;
	for(auto u:adj[node]){
		if(u==parent)continue;
		ans+=max(dfs(u,node),0);
	}
	val[node][parent]=ans;
	return ans;
}

int dfs2(int node){
	if(dp[node]>-2)return dp[node];
	int parent = -1;
	for(int u:adj[node]){
		if(val[node][u]>=-1){
			parent = u;
			break;
		}
	}
	int pv = dfs2(parent);
	int nv = val[node][parent];
	if(nv==-1){
		dp[node]=max(pv,0)-1;
		return max(pv,0)-1;
	}
	else{
		dp[node] = max(nv,pv);
		return max(pv,nv);
	}
}
void TestCase(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	// n = 200000;
	cin>>n;
	v.resize(n);
	dp.resize(n,-2);
	for(int i=0;i<n;i++){
		cin>>v[i];
		// v[i]=0;
	}
	adj.resize(n);
	val.resize(n);
	for(int i=0;i<n-1;i++){
		int a,b;
		cin>>a>>b;
		// a=i+1;b=i+2;
		a--;b--;
		adj[a].pb(b);
		adj[b].pb(a);
		val[a][b]=-2;
		val[b][a]=-2;
	}

	dp[0] = (v[0]==0)?-1:1;
	for(auto u:adj[0]){
		dp[0] += max(dfs(u,0),0);
	}

	for(int i=0;i<n;i++){
		cout<<dfs2(i)<<' ';
	}
	cout<<'\n';
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int T;
	T = 1;
	// cin>>T;
	for(int tt=1;tt<=T;tt++){
		TestCase();
	}
 }


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
#define pci pair<char,int>
#define pib pair<int,bool>

void print(vector<int>& v){for(int i=0;i<v.size();i++){cout<<v[i]<<' ';}cout<<endl;}
void print(vector<char>& v){for(int i=0;i<v.size();i++){cout<<v[i]<<' ';}cout<<endl;}
void print(vector<string>& v){for(int i=0;i<v.size();i++){cout<<v[i]<<' ';}cout<<endl;}
void print(vector<bool>& v){for(int i=0;i<v.size();i++){cout<<v[i]<<' ';}cout<<endl;}
void print(vector<pair<int,int>>& v){for(int i=0;i<v.size();i++){cout<<v[i].F<<','<<v[i].S<<'|';}cout<<endl;}
void print(vector<vector<int>>&v){for(int i=0;i<v.size();i++){print(v[i]);}}
void print(vector<vector<bool>>&v){for(int i=0;i<v.size();i++){print(v[i]);}}

int dfs(vector<int>&v,int node, int parent, map<pii,int>&val, vector<vector<int>>&adj){
	if(val[{node,parent}]>=0){
		return val[{node,parent}];
	}
	int ans = (v[node]==0)?-1:1;
	for(auto u:adj[node]){
		if(u==parent)continue;
		ans+=dfs(v,u,node,val,adj);
	}
	if(ans<0){
		val[{node,parent}]=0;
		return 0;
	}
	val[{node,parent}]=ans;
	return ans;
}
void TestCase(){
	int n;cin>>n;
	vector<int>v(n);
	for(int i=0;i<n;i++)cin>>v[i];
	vector<vector<int>>adj(n);
	map<pii,int>val;
	for(int i=0;i<n-1;i++){
		int a,b;
		cin>>a>>b;
		a--;b--;
		adj[a].pb(b);
		adj[b].pb(a);
		val[{a,b}]=-1;
		val[{b,a}]=-1;
	}
	for(int i=0;i<n;i++){
		int ans = (v[i]==0)?-1:1;
		for(auto u:adj[i]){
			ans+= dfs(v,u,i,val,adj);
		}
		cout<<ans<<' ';
	}
	cout<<'\n';
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int T;
	T = 1;
	// cin>>T;
	for(int tt=1;tt<=T;tt++){
		TestCase();
	}
 }


import java.util.*;
import java.io.*;
public class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw=new PrintWriter(System.out);
        String str[]=br.readLine().split(" ");
        int t=Integer.parseInt(str[0]);
        int k=Integer.parseInt(str[1]);
        int mx=(int)1e5+1;
        final long mod=(long)1e9+7;
        long arr[]=new long[mx];
        for(int i=0;i<k;i++)
        arr[i]=1;
        for(int i=k;i<mx;i++)
        arr[i]=(arr[i-1]%mod+arr[i-k]%mod)%mod;
        long pref[]=new long[mx];
        pref[0]=arr[0];
        for(int i=1;i<mx;i++)
        {
            pref[i]+=pref[i-1]+arr[i];
            pref[i]%=mod;
        }
        while(t-->0)
        {
            str=br.readLine().split(" ");
            int a=Integer.parseInt(str[0])-1;
            int b=Integer.parseInt(str[1]);
            pw.println(pref[b]-pref[a]);
        }
        pw.flush();
        pw.close();
    }
}


import java.util.*;
import java.io.*;
public class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw=new PrintWriter(System.out);
        String str[]=br.readLine().split(" ");
        int t=Integer.parseInt(str[0]);
        int k=Integer.parseInt(str[1]);
        int mx=(int)1e5+1;
        final long mod=(long)1e9+7;
        long arr[]=new long[mx];
        for(int i=0;i<k;i++)
        arr[i]=1;
        for(int i=k;i<mx;i++)
        arr[i]=(arr[i-1]%mod+arr[i-k]%mod)%mod;
        long pref[]=new long[mx];
        pref[0]=arr[0];
        for(int i=1;i<mx;i++)
        {
            pref[i]+=pref[i-1]+arr[i];
            pref[i]%=mod;
        }
        while(t-->0)
        {
            str=br.readLine().split(" ");
            int a=Integer.parseInt(str[0])-1;
            int b=Integer.parseInt(str[1]);
            pw.println((pref[b]-pref[a]+mod)%mod);
        }
        pw.flush();
        pw.close();
    }
}


import java.io.*;
import java.util.*;

public class Main {

	static long sx = 0, sy = 0, m = (long) (1e9+7);

	static ArrayList<Integer>[] a;
	static long[] dp;
	static int[] size;
	static long[] farr;
	// static boolean b = true;

	// static TreeMap<Integer, Integer> hm = new TreeMap<>();
	public static PrintWriter out;
	// static ArrayList<pair> p = new ArrayList<>();
	static long[] fact = new long[(int) 1e6];
	static boolean b = false;
	static StringBuilder sb = new StringBuilder();
	static boolean cycle = false;
	static long mod = 998244353;
	static long[] no, col;
	static String s;
	static int ans = 0;
	static HashSet<Long>[] hm;
	static int[] dis;

	public static void main(String[] args) throws IOException {

		// Scanner scn = new Scanner(new BufferedReader(new
		// InputStreamReader(System.in)));

		out = new PrintWriter(System.out);
		Reader scn = new Reader();

		dp = new long[(int) 1e6+1];
		int t = scn.nextInt(), k = scn.nextInt();
		dp[0] = 1;

		for(int i=1; i<dp.length; i++)
			 dp[i] = dp[i-1]+ (i-k>=0?dp[i-k]:0) +1;

		 while(t--!=0){

			  int a = scn.nextInt(), b = scn.nextInt();

			  System.out.println(dp[b]-dp[a-1]);

		 }

	}

	// _________________________TEMPLATE_____________________________________________________________

//	public static long lcm(long x, long y) {
//
//		return (x * y) / gcd(x, y);
//	}
//
//	private static long gcd(long x, long y) {
//		if (x == 0)
//			return y;
//
//		return gcd(y % x, x);
//	}

	// static class comp implements Comparator<Integer> {
	//
	// @Override
	// public int compare(Integer o1, Integer o2) {
	// return depth[o2] - depth[o1];
	// }
	//
	// }

	// public static long pow(long a, long b) {
	//
	// if(b<0)return 0;
	// if (b == 0 || b == 1)
	// return (long) Math.pow(a, b);
	//
	// if (b % 2 == 0) {
	//
	// long ret = pow(a, b / 2);
	// ret = (ret % mod * ret % mod) % mod;
	// return ret;
	// }
	//
	// else {
	// return ((pow(a, b - 1) % mod) * a % mod) % mod;
	// }
	// }

	private static class pair implements Comparable<pair> {

		long val, col;

		pair(long a, long b) {

			val = a;
			col = b;
		}

		@Override
		public int compareTo(pair o) {

			if (this.val < o.val)
				return 1;
			else
				return -1;
		}

	}

	private static String reverse(String s) {

		return new StringBuilder(s).reverse().toString();
	}

	public static class Reader {
		final private int BUFFER_SIZE = 1 << 16;
		private DataInputStream din;
		private byte[] buffer;
		private int bufferPointer, bytesRead;

		public Reader() {
			din = new DataInputStream(System.in);
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}

		public Reader(String file_name) throws IOException {
			din = new DataInputStream(new FileInputStream(file_name));
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}

		public String readLine() throws IOException {
			byte[] buf = new byte[1000000 + 1]; // line length
			int cnt = 0, c;
			while ((c = read()) != -1) {
				if (c == '\n')
					break;
				buf[cnt++] = (byte) c;
			}
			return new String(buf, 0, cnt);
		}

		public int nextInt() throws IOException {
			int ret = 0;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();
			do {
				ret = ret * 10 + c - '0';
			} while ((c = read()) >= '0' && c <= '9');

			if (neg)
				return -ret;
			return ret;
		}

		public long nextLong() throws IOException {
			long ret = 0;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();
			do {
				ret = ret * 10 + c - '0';
			} while ((c = read()) >= '0' && c <= '9');
			if (neg)
				return -ret;
			return ret;
		}

		public double nextDouble() throws IOException {
			double ret = 0, div = 1;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();

			do {
				ret = ret * 10 + c - '0';
			} while ((c = read()) >= '0' && c <= '9');

			if (c == '.') {
				while ((c = read()) >= '0' && c <= '9') {
					ret += (c - '0') / (div *= 10);
				}
			}

			if (neg)
				return -ret;
			return ret;
		}

		private void fillBuffer() throws IOException {
			bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
			if (bytesRead == -1)
				buffer[0] = -1;
		}

		private byte read() throws IOException {
			if (bufferPointer == bytesRead)
				fillBuffer();
			return buffer[bufferPointer++];
		}

		public void close() throws IOException {
			if (din == null)
				return;
			din.close();
		}

		public int[] nextIntArray(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = nextInt();
			}
			return arr;
		}

		public long[] nextLongArray(int n) throws IOException {
			long[] arr = new long[n];
			for (int i = 0; i < n; i++) {
				arr[i] = nextLong();
			}
			return arr;
		}

		public int[][] nextInt2DArray(int m, int n) throws IOException {
			int[][] arr = new int[m][n];
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < n; j++)
					arr[i][j] = nextInt();
			}
			return arr;
		}

		public long[][] nextInt2DArrayL(int m, int n) throws IOException {
			long[][] arr = new long[m][n];
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < n; j++)
					arr[i][j] = nextInt();
			}
			return arr;
		}
		// kickstart - Solution
		// atcoder - Main
	}

}


import java.io.*;
import java.util.*;

public class Main {

	static long sx = 0, sy = 0, m = (long) (1e9+7);

	static ArrayList<Integer>[] a;
	static long[] dp;
	static int[] size;
	static long[] farr;
	// static boolean b = true;

	// static TreeMap<Integer, Integer> hm = new TreeMap<>();
	public static PrintWriter out;
	// static ArrayList<pair> p = new ArrayList<>();
	static long[] fact = new long[(int) 1e6];
	static boolean b = false;
	static StringBuilder sb = new StringBuilder();
	static boolean cycle = false;
	static long mod = 998244353;
	static long[] no, col;
	static String s;
	static int ans = 0;
	static HashSet<Long>[] hm;
	static int[] dis;

	public static void main(String[] args) throws IOException {

		// Scanner scn = new Scanner(new BufferedReader(new
		// InputStreamReader(System.in)));

		out = new PrintWriter(System.out);
		Reader scn = new Reader();

		dp = new long[(int) 1e6+1];
		Arrays.fill(dp, -1);
		int t = scn.nextInt(), k = scn.nextInt();
		 f(0,(int)(1e6),k,0);

		 while(t--!=0){

			  int a = scn.nextInt(), b = scn.nextInt();

			  System.out.println(dp[b]-dp[a-1]);

		 }

	}

	// _________________________TEMPLATE_____________________________________________________________

	private static long f(int a, int b, int k, int f) {

		if(f>b)
			 return 0;

		long ans = 0;

		if(dp[f]!=-1)
			 return dp[f];

		if(f>=a && f<=b){

			ans = f(a,b,k,f+1)+f(a,b,k,f+k)+1;
		}

		else{

			ans = f(a,b,k,f+1)+f(a,b,k,f+k);
		}

		return dp[f] = ans%m;
	}

//	public static long lcm(long x, long y) {
//
//		return (x * y) / gcd(x, y);
//	}
//
//	private static long gcd(long x, long y) {
//		if (x == 0)
//			return y;
//
//		return gcd(y % x, x);
//	}

	// static class comp implements Comparator<Integer> {
	//
	// @Override
	// public int compare(Integer o1, Integer o2) {
	// return depth[o2] - depth[o1];
	// }
	//
	// }

	// public static long pow(long a, long b) {
	//
	// if(b<0)return 0;
	// if (b == 0 || b == 1)
	// return (long) Math.pow(a, b);
	//
	// if (b % 2 == 0) {
	//
	// long ret = pow(a, b / 2);
	// ret = (ret % mod * ret % mod) % mod;
	// return ret;
	// }
	//
	// else {
	// return ((pow(a, b - 1) % mod) * a % mod) % mod;
	// }
	// }

	private static class pair implements Comparable<pair> {

		long val, col;

		pair(long a, long b) {

			val = a;
			col = b;
		}

		@Override
		public int compareTo(pair o) {

			if (this.val < o.val)
				return 1;
			else
				return -1;
		}

	}

	private static String reverse(String s) {

		return new StringBuilder(s).reverse().toString();
	}

	public static class Reader {
		final private int BUFFER_SIZE = 1 << 16;
		private DataInputStream din;
		private byte[] buffer;
		private int bufferPointer, bytesRead;

		public Reader() {
			din = new DataInputStream(System.in);
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}

		public Reader(String file_name) throws IOException {
			din = new DataInputStream(new FileInputStream(file_name));
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}

		public String readLine() throws IOException {
			byte[] buf = new byte[1000000 + 1]; // line length
			int cnt = 0, c;
			while ((c = read()) != -1) {
				if (c == '\n')
					break;
				buf[cnt++] = (byte) c;
			}
			return new String(buf, 0, cnt);
		}

		public int nextInt() throws IOException {
			int ret = 0;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();
			do {
				ret = ret * 10 + c - '0';
			} while ((c = read()) >= '0' && c <= '9');

			if (neg)
				return -ret;
			return ret;
		}

		public long nextLong() throws IOException {
			long ret = 0;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();
			do {
				ret = ret * 10 + c - '0';
			} while ((c = read()) >= '0' && c <= '9');
			if (neg)
				return -ret;
			return ret;
		}

		public double nextDouble() throws IOException {
			double ret = 0, div = 1;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();

			do {
				ret = ret * 10 + c - '0';
			} while ((c = read()) >= '0' && c <= '9');

			if (c == '.') {
				while ((c = read()) >= '0' && c <= '9') {
					ret += (c - '0') / (div *= 10);
				}
			}

			if (neg)
				return -ret;
			return ret;
		}

		private void fillBuffer() throws IOException {
			bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
			if (bytesRead == -1)
				buffer[0] = -1;
		}

		private byte read() throws IOException {
			if (bufferPointer == bytesRead)
				fillBuffer();
			return buffer[bufferPointer++];
		}

		public void close() throws IOException {
			if (din == null)
				return;
			din.close();
		}

		public int[] nextIntArray(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = nextInt();
			}
			return arr;
		}

		public long[] nextLongArray(int n) throws IOException {
			long[] arr = new long[n];
			for (int i = 0; i < n; i++) {
				arr[i] = nextLong();
			}
			return arr;
		}

		public int[][] nextInt2DArray(int m, int n) throws IOException {
			int[][] arr = new int[m][n];
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < n; j++)
					arr[i][j] = nextInt();
			}
			return arr;
		}

		public long[][] nextInt2DArrayL(int m, int n) throws IOException {
			long[][] arr = new long[m][n];
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < n; j++)
					arr[i][j] = nextInt();
			}
			return arr;
		}
		// kickstart - Solution
		// atcoder - Main
	}

}