結果

問題 No.1221 木 *= 3
ユーザー beetbeet
提出日時 2020-09-04 21:47:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 1,582 bytes
コンパイル時間 2,125 ms
コンパイル使用メモリ 209,148 KB
実行使用メモリ 27,404 KB
最終ジャッジ日時 2023-08-17 15:22:57
合計ジャッジ時間 4,622 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 84 ms
27,292 KB
testcase_08 AC 82 ms
26,796 KB
testcase_09 AC 85 ms
27,404 KB
testcase_10 AC 90 ms
26,772 KB
testcase_11 AC 88 ms
27,040 KB
testcase_12 AC 68 ms
14,148 KB
testcase_13 AC 70 ms
14,140 KB
testcase_14 AC 73 ms
13,972 KB
testcase_15 AC 70 ms
14,020 KB
testcase_16 AC 71 ms
13,972 KB
testcase_17 AC 77 ms
12,348 KB
testcase_18 AC 75 ms
12,364 KB
testcase_19 AC 81 ms
12,492 KB
testcase_20 AC 80 ms
12,364 KB
testcase_21 AC 82 ms
12,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 HERE
signed 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;
}
0