結果

問題 No.1221 木 *= 3
ユーザー kyoprounokyoprouno
提出日時 2020-09-06 00:16:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 169 ms / 2,000 ms
コード長 1,211 bytes
コンパイル時間 1,958 ms
コンパイル使用メモリ 182,368 KB
実行使用メモリ 18,304 KB
最終ジャッジ日時 2024-05-06 20:26:50
合計ジャッジ時間 5,512 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 169 ms
18,304 KB
testcase_08 AC 160 ms
18,304 KB
testcase_09 AC 161 ms
18,272 KB
testcase_10 AC 167 ms
18,304 KB
testcase_11 AC 163 ms
18,304 KB
testcase_12 AC 152 ms
12,184 KB
testcase_13 AC 150 ms
12,184 KB
testcase_14 AC 151 ms
12,184 KB
testcase_15 AC 150 ms
12,196 KB
testcase_16 AC 150 ms
12,184 KB
testcase_17 AC 163 ms
12,032 KB
testcase_18 AC 157 ms
12,032 KB
testcase_19 AC 158 ms
11,904 KB
testcase_20 AC 158 ms
12,032 KB
testcase_21 AC 160 ms
11,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll i=0;i<(n);i++)
#define pll pair<ll,ll>
#define pii pair<int,int>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define endl '\n'
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))

using namespace std;

const ll mod=1e9+7;

ll n;

vector<vector<int>> to;
vector<ll> a,b,dp0,dp1;

void dfs(int v,int p=-1){
    dp0[v]=a[v];
    for(auto x:to[v]){
        if(x==p) continue;
        dfs(x,v);
        dp0[v]+=max(dp0[x],dp1[x]);
        dp1[v]+=max(dp0[x],dp1[x]+b[x]+b[v]);
    }
    return ;
}

int main(){
    cin >> n;
    to=vector<vector<int>>(n);
    a=vector<ll>(n);
    b=vector<ll>(n);
    rep(i,n){
        cin >> a[i];
    }
    rep(i,n){
        cin >> b[i];
    }
    rep(i,n-1){
        ll u,v;
        cin >> u >> v;
        u--; v--;
        to[u].push_back(v);
        to[v].push_back(u);
    }
    dp0=vector<ll>(n);
    dp1=vector<ll>(n);
    dfs(0);
    cout << max(dp0[0],dp1[0]) << endl;
    return 0;
}
0