結果
問題 | No.1221 木 *= 3 |
ユーザー |
![]() |
提出日時 | 2020-09-04 21:47:27 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 163 ms / 2,000 ms |
コード長 | 1,582 bytes |
コンパイル時間 | 2,563 ms |
コンパイル使用メモリ | 204,860 KB |
最終ジャッジ日時 | 2025-01-14 05:29:33 |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 18 |
ソースコード
#include <bits/stdc++.h>using namespace std;using Int = long long;const char newl = '\n';template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}template<typename T=Int>vector<T> read(size_t n){vector<T> ts(n);for(size_t i=0;i<n;i++) cin>>ts[i];return ts;}template<typename F>struct FixPoint : F{FixPoint(F&& f):F(forward<F>(f)){}template<typename... Args>decltype(auto) operator()(Args&&... args) const{return F::operator()(*this,forward<Args>(args)...);}};template<typename F>inline decltype(auto) MFP(F&& f){return FixPoint<F>{forward<F>(f)};}//INSERT ABOVE HEREsigned main(){cin.tie(0);ios::sync_with_stdio(0);Int n;cin>>n;auto as=read(n);auto bs=read(n);vector<vector<Int>> G(n);for(Int i=1;i<n;i++){Int u,v;cin>>u>>v;u--;v--;G[u].emplace_back(v);G[v].emplace_back(u);}const Int INF = 1e18;vector<Int> dp0(n,-INF),dp1(n,-INF);MFP([&](auto dfs,Int v,Int p)->void{dp0[v]=as[v];Int sum=0;vector<Int> vs;for(Int u:G[v]){if(u==p) continue;dfs(u,v);dp0[v]+=max(dp0[u],dp1[u]);sum+=dp0[u];vs.emplace_back((dp1[u]+bs[u])-dp0[u]);}sort(vs.rbegin(),vs.rend());chmax(dp1[v],sum);for(Int i=0;i<(Int)vs.size();i++){sum+=vs[i];chmax(dp1[v],sum+(i+1)*bs[v]);}})(0,-1);cout<<max(dp0[0],dp1[0])<<newl;return 0;}