#include <bits/stdc++.h>
using namespace std;
#if 1
#define int LL
#endif
using LL = long long;
using DB = double;
using PI = pair<int, int>;
using PL = pair<LL, LL>;
template<typename T> using v = vector<T>;
constexpr auto INF = 0X3F3F3F3F;
template<typename T1, typename T2> using umap = unordered_map<T1, T2>;
#define ic std::ios::sync_with_stdio(false);std::cin.tie(nullptr)
template <typename ConTainermap> void dbgumap(ConTainermap c);
template <typename _Ty> void dbg(_Ty arr[], int n, int m = -1);
inline int read();
inline void write(int x);
int n, m;
const int N = 1e5 + 10;
int nums[N], wsum[N], ans;
v<int> vec[N];
bool vis[N];
bool top[N];
void dfs(int root, int fa)
{
if (!vis[fa])
{
wsum[root] = nums[root];
}
for (int i = 0; i < vec[root].size(); i++)
{
int son = vec[root][i];
if (son != fa)
{
int sumL = wsum[root], sumR = wsum[root];
if (!vis[fa])
{
vis[root] = true;
dfs(son, root);
if (wsum[son] > 0) sumL += wsum[son];
}
else
{
dfs(son, root);
if (wsum[son] > 0) sumR += wsum[son];
}
wsum[root] = max(sumL, sumR);
}
}
if (wsum[root] > ans) ans = wsum[root];
}
signed main()
{
cin >> n;
for (int i = 1; i <= n; i++) cin >> nums[i];
for (int i = 1; i <= n - 1; i++)
{
int u, v;
cin >> u >> v;
vec[u].push_back(v);
vec[v].push_back(u);
top[u] = true;
}
for (int i = 1; i <= n; i++)
{
memset(vis,false,sizeof(vis));
if (!top[i]) dfs(i, 0);
}
cout << ans;
#define one 1
return 0;
}
template <typename _Ty>
void dbg(_Ty arr[], int n, int m)
{
#if one
for (int i = 1; i <= n; i++)
{
cout << arr[i] << ' ';
}
#else
if (m == -1) { cout << "please input m! "; return; }
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
cout << *(arr[i] + j) << ' ';
}
cout << endl;
}
#endif
cout << endl;
}
template <typename ConTainermap>
void dbgumap(ConTainermap c)
{
for (auto& x : c)
{
cout << "key:" << x.first << " val:" << x.second << endl;
}
}
inline int read()
{
int x = 0, w = 1;
char ch = 0;
while (ch < '0' || ch > '9')
{
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = x * 10 + (ch - '0');
ch = getchar();
}
return x * w;
}
inline void write(int x)
{
static int sta[35];
int top = 0;
do {
sta[top++] = x % 10, x /= 10;
} while (x);
while (top) putchar(sta[--top] + 48);
}