53 lines
983 B
C++
53 lines
983 B
C++
|
#include <algorithm>
|
||
|
#include <cassert>
|
||
|
#include <iostream>
|
||
|
#include <istream>
|
||
|
#include <vector>
|
||
|
|
||
|
const int MAX_N=6e3+5;
|
||
|
|
||
|
struct Node{
|
||
|
std::vector<int> sons;
|
||
|
int r;
|
||
|
}nodes[MAX_N];
|
||
|
|
||
|
bool had_father[MAX_N];
|
||
|
|
||
|
int dp[MAX_N][2];
|
||
|
|
||
|
void dfs(const int n){
|
||
|
dp[n][1]=nodes[n].r;
|
||
|
for(auto &i:nodes[n].sons){
|
||
|
dfs(i);
|
||
|
dp[n][0]+=std::max(dp[i][1],dp[i][0]);
|
||
|
dp[n][1]+=dp[i][0];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main(){
|
||
|
std::iostream::sync_with_stdio(false),std::cin.tie(0),std::cout.tie(0);
|
||
|
|
||
|
int n;
|
||
|
std::cin>>n;
|
||
|
|
||
|
for(int i=1;i<=n;i++){
|
||
|
int _r;
|
||
|
std::cin>>_r;
|
||
|
nodes[i].r=_r;
|
||
|
}
|
||
|
for(int i=1;i<n;i++){
|
||
|
int s,f;
|
||
|
std::cin>>s>>f;
|
||
|
nodes[f].sons.push_back(s);
|
||
|
had_father[s]=true;
|
||
|
}
|
||
|
int master{-1};
|
||
|
for(int i=1;i<=n;i++){
|
||
|
if(had_father[i]==false){
|
||
|
master=i;
|
||
|
}
|
||
|
}
|
||
|
assert(master!=-1);
|
||
|
dfs(master);
|
||
|
std::cout<<std::max(dp[master][0],dp[master][1])<<"\n";
|
||
|
}
|