From 8353d5b6d9e7db1d86245e11ad54288c8df5d2c6 Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Fri, 15 Nov 2024 13:36:39 +0800 Subject: [PATCH] update --- src/P1352/P1352.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/P1352/P1352.cpp b/src/P1352/P1352.cpp index 4c4f291..1649b80 100644 --- a/src/P1352/P1352.cpp +++ b/src/P1352/P1352.cpp @@ -1,16 +1,27 @@ +#include #include #include +#include +#include #include 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 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<