結果

問題 No.1221 木 *= 3
ユーザー bachoppibachoppi
提出日時 2020-09-05 16:30:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 180 ms / 2,000 ms
コード長 1,321 bytes
コンパイル時間 1,862 ms
コンパイル使用メモリ 126,656 KB
実行使用メモリ 22,440 KB
最終ジャッジ日時 2023-08-19 06:52:54
合計ジャッジ時間 6,912 ms
ジャッジサーバーID
(参考情報)
judge13 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,912 KB
testcase_01 AC 3 ms
6,844 KB
testcase_02 AC 3 ms
6,896 KB
testcase_03 AC 3 ms
6,968 KB
testcase_04 AC 3 ms
6,840 KB
testcase_05 AC 3 ms
6,848 KB
testcase_06 AC 3 ms
6,980 KB
testcase_07 AC 176 ms
22,440 KB
testcase_08 AC 173 ms
22,060 KB
testcase_09 AC 171 ms
22,400 KB
testcase_10 AC 180 ms
22,136 KB
testcase_11 AC 179 ms
22,216 KB
testcase_12 AC 174 ms
14,128 KB
testcase_13 AC 162 ms
14,152 KB
testcase_14 AC 167 ms
14,168 KB
testcase_15 AC 166 ms
14,144 KB
testcase_16 AC 166 ms
14,084 KB
testcase_17 AC 173 ms
12,232 KB
testcase_18 AC 166 ms
12,152 KB
testcase_19 AC 176 ms
12,164 KB
testcase_20 AC 173 ms
12,096 KB
testcase_21 AC 171 ms
12,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;
#define PI 3.141592653589793

vector<int> to[101010];
ll dp[101010][2];
ll c[101010], b[101010];

void DFS(int a, int p){
  vector<pair<ll, int>> kurabe;
  dp[a][0] = c[a];
  for(auto k: to[a]){
    if(k==p) continue;
    DFS(k, a);
    kurabe.push_back({dp[k][1]+b[a]+b[k]-dp[k][0], k});
    if(dp[k][1]>dp[k][0]) dp[a][0]+=dp[k][1];
    else dp[a][0]+=dp[k][0];
    dp[a][1]+=dp[k][0];
  }
  sort(kurabe.begin(), kurabe.end(), greater<>());
  ll nyan = dp[a][1];
  rep(i, kurabe.size()){
    if(kurabe[i].first>0) dp[a][1]+=kurabe[i].first;
  }
}


int main(){
  int n; cin >> n; 
  rep(i, n) cin >> c[i];
  rep(i, n) cin >> b[i];
  rep(i, n-1){
    int u, v; cin >> u >> v; u--; v--;
    to[u].push_back(v); to[v].push_back(u);
  }
  DFS(0, -1);
  cout << max(dp[0][0], dp[0][1]) << endl;
  //cout << dp[0][1] << endl;
}
0