噗
莫得感情
打着打着交一下居然ac了
# include <bits/stdc++.h>
using namespace std;
const int N = 6010;
int n, p[N], h[N], f[2][N], boss, l, k;
vector<int> s[N];
void renew(int t)
{
if (!s[t].size()) f[0][t] = 0, f[1][t] = max(0, h[t]);
else
{
for(auto x : s[t]) renew(x), f[1][t] += f[0][x], f[0][t] += f[1][x];
f[1][t] += max(0, h[t]);
}
}
int main()
{
ios::sync_with_stdio(false);
cin >> n;
for(int i = 1; i <= n; i ++) cin >> h[i];
for(int i = 1; i < n; i ++) cin >> l >> k, p[l] = k, s[k].push_back(l);
for(int i = 1; i <= n; i ++) if(!p[i]) boss = i;
renew(boss);
cout << max(f[0][boss], f[1][boss]);
}