結果
問題 | No.1221 木 *= 3 |
ユーザー | snow39 |
提出日時 | 2020-09-04 21:45:05 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 84 ms / 2,000 ms |
コード長 | 1,459 bytes |
コンパイル時間 | 1,191 ms |
コンパイル使用メモリ | 104,984 KB |
実行使用メモリ | 27,392 KB |
最終ジャッジ日時 | 2024-11-26 12:25:39 |
合計ジャッジ時間 | 3,556 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 18 |
ソースコード
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <cmath> #include <map> #include <queue> #include <iomanip> #include <set> #include <tuple> #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) #define all(v) v.begin(),v.end() using namespace std; typedef long long ll; const ll MOD=1e9+7; template<class T> void chmin(T &a,const T &b){if(a>b) a=b;} template<class T> void chmax(T &a,const T &b){if(a<b) a=b;} int N; vector<ll> A,B; vector<vector<int>> g; ll dp[100010][2]; const ll INF=1e18; void dfs(int now,int par){ dp[now][0]=A[now]; ll allzero=0; for(auto nex:g[now]) if(nex!=par){ dfs(nex,now); dp[now][0]+=max(dp[nex][0],dp[nex][1]); allzero+=dp[nex][0]; } dp[now][1]=allzero; vector<ll> v; for(auto nex:g[now]) if(nex!=par){ v.push_back(dp[nex][1]-dp[nex][0]+B[nex]); } sort(all(v)); reverse(all(v)); ll sum=allzero; rep(i,v.size()){ sum+=v[i]+B[now]; chmax(dp[now][1],sum); } } int main(){ cin.tie(0); ios::sync_with_stdio(false); cin>>N; A.resize(N); B.resize(N); rep(i,N) cin>>A[i]; rep(i,N) cin>>B[i]; g.resize(N); rep(i,N-1){ int a,b; cin>>a>>b; a--;b--; g[a].push_back(b); g[b].push_back(a); } int root=0; rep(i,N) if(g[i].size()==1) root=i; rep(i,N) rep(j,2) dp[i][j]=-INF; dfs(root,-1); ll ans=max(dp[root][0],dp[root][1]); cout<<ans<<endl; return 0; }