#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
typedef long long LL; //可能爆出int
vector<vector<int>> v(100010);
int happy[N];
LL f[N];
bool st[N];
void dfs(int u)
{ //以u为根的子树值
    f[u] = happy[u];
    st[u] = true;
    for (int i = 0; i < v[u].size(); i++)
    {
        int j = v[u][i];
        if (st[j] == false)
        {                           //不能重复搜
            dfs(j);                 //递归求下面的值
            f[u] += max(0ll, f[j]); //f[j]可能为负数
        }
    }
}
int main()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> happy[i];
    for (int i = 0; i < n - 1; i++)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        v[b].push_back(a);
        v[a].push_back(b);
    }
    dfs(1);
    LL res = f[1];
    //注意取个最大值
    for (int i = 2; i <= n; i++)
        res = max(res, f[i]);
    printf("%lld\n", res);
}
