#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream in("asmax.in");
ofstream out("asmax.out");
const int N = 16001;
int n, x, y, v[N];
bool viz[N];
vector <int> a[N];
void dfs(int x)
{
viz[x] = true;
for(auto y: a[x])
{
if(!viz[y])
{
dfs(y);
if(v[y] > 0)
v[x] += v[y];
}
}
}
int main()
{
in>>n;
for(int i=1; i<=n; i++)
in>>v[i];
for(int i=1; i<n; i++)
{
in>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
dfs(1);
int ans = -1001;
for(int i=1; i<=n; i++)
if(v[i] > ans) ans = v[i];
out<<ans;
return 0;
}