#include <bits/stdc++.h>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
int n;
int cost[16005];
vector<int> graf[16005];
int Max = INT_MIN;
int dp[16001];
bitset<16001> vis;
void dfs(int x) {
vis[x] = true;
for (int i : graf[x]) {
if (!vis[i]) {
dfs(i);
if (dp[i] > 0)
dp[x] += dp[i];
}
}
Max = max(Max, dp[x]);
}
int main() {
fin>>n;
for (int i = 1; i <= n; i++) {
fin>>cost[i];
dp[i] = cost[i];
}
for (int i = 1; i < n; i++) {
int x, y;
fin>>x>>y;
graf[x].push_back(y);
graf[y].push_back(x);
}
dfs(1);
fout<<Max;
return 0;
}