結果

問題 No.1221 木 *= 3
ユーザー snow39snow39
提出日時 2020-09-04 21:45:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 81 ms / 2,000 ms
コード長 1,459 bytes
コンパイル時間 998 ms
コンパイル使用メモリ 104,072 KB
実行使用メモリ 25,768 KB
最終ジャッジ日時 2023-08-17 15:21:18
合計ジャッジ時間 3,441 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 78 ms
25,636 KB
testcase_08 AC 76 ms
25,704 KB
testcase_09 AC 79 ms
25,636 KB
testcase_10 AC 81 ms
25,680 KB
testcase_11 AC 81 ms
25,768 KB
testcase_12 AC 63 ms
13,116 KB
testcase_13 AC 65 ms
13,148 KB
testcase_14 AC 68 ms
13,012 KB
testcase_15 AC 68 ms
13,128 KB
testcase_16 AC 68 ms
13,124 KB
testcase_17 AC 72 ms
11,724 KB
testcase_18 AC 70 ms
11,712 KB
testcase_19 AC 72 ms
11,716 KB
testcase_20 AC 73 ms
11,740 KB
testcase_21 AC 74 ms
11,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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