This commit is contained in:
Zengtudor 2024-11-15 13:36:39 +08:00
parent 8e805915d6
commit 8353d5b6d9

View File

@ -1,16 +1,27 @@
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <istream>
#include <stdexcept>
#include <vector>
using std::cin, std::cout;
using ll = int64_t;
const ll max_n = 6e3+5;
ll n, r[max_n];
ll n, r[max_n], dp[max_n][2], ind[max_n];
std::vector<ll> sons[max_n];
void dfs(const ll &now){
dp[now][1]=r[now];
for(const ll &next: sons[now]){
dfs(next);
dp[now][1] += dp[next][0];
dp[now][0] += std::max(dp[next][0], dp[next][1]);
}
}
int main(){
std::iostream::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
cin>>n;
for(ll i{1};i<=n;i++){
cin>>r[i];
@ -19,5 +30,16 @@ int main(){
ll l, k;
cin>>l>>k;
sons[k].emplace_back(l);
ind[l]++;
}
const ll mst = [&]()->ll{
for(ll i{1}; i<=n; i++){
if(ind[i]==0){
return i;
}
}
throw std::runtime_error("unreachable");
}();
dfs(mst);
cout<<std::max(dp[mst][0], dp[mst][1])<<'\n';
}