#include <bits/stdc++.h>   using namespace std;   const long long N = 2000 * 100 + 1000;   vector<long long> adj[N];   long long a[N], h[N], t[N];   bool mark[N];   long long f(long long p) {     mark[p] = true;     long long ans = h[p] * a[p];     t[p] = a[p];     for (auto q : adj[p])       if (mark[q] == false) {         h[q] = h[p] + 1;         ans += f(q);         t[p] += t[q];       }     return ans;   }   long long ans = 0, sum = 0;   void calc(long long val, long long p) {     mark[p] = true;     ans = max(ans, val);     for (auto q : adj[p])       if (mark[q] == false) {         long long k = sum - 2 * t[q];         calc(val + k, q);       }   }   int32_t main() {     ios::sync_with_stdio(false);     long long n;     cin >> n;     for (long long i = 0; i < n; i++) {       cin >> a[i];       sum += a[i];     }     for (long long i = 0; i < n - 1; i++) {       long long u, v;       cin >> u >> v;       u--;       v--;       adj[u].push_back(v);       adj[v].push_back(u);     }     long long val = f(0);     for (long long i = 0; i < N; i++) mark[i] = false;     calc(val, 0);     cout << ans <<   n ;     return 0;   }   