#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long LL;
LL n, h[10000],maxx=-10000,d[10000][3], l, k, vis[10000];
vector<int> p[10000];
void dfs(long long u)
{
d[u][1] = h[u];
for (int i = 0; i < p[u].size(); i++)
{
long long v = p[u][i];
dfs(v);
d[u][2] += max(d[v][1], d[v][2]);
d[u][1] += d[v][2];
}
maxx = max(d[u][1], max(d[u][2], maxx));
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++)
cin >> h[i];
for (int i = 1; i <= n - 1; i++)
{
cin >> l >> k;
vis[l] = 1;
p[k].push_back(l);
}
for (int i = 1; i <= n; i++)
if (!vis[i])
l = i;
dfs(l);
cout << maxx << endl;
return 0;
}
树形dp模板(acwing285)
最新推荐文章于 2024-09-10 15:28:21 发布