結果

問題 No.1221 木 *= 3
ユーザー milanis48663220
提出日時 2020-09-04 22:24:26
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 74 ms / 2,000 ms
コード長 999 bytes
コンパイル時間 801 ms
コンパイル使用メモリ 85,700 KB
実行使用メモリ 17,352 KB
最終ジャッジ日時 2024-11-26 14:28:00
合計ジャッジ時間 3,155 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;

int N;
ll a[100000];
ll b[100000];
bool used[100000];

vector<int> G[100000];
// dp1残す、dp2残さない
ll dp1[100000], dp2[100000];

const ll INF = 1e18;

void dfs(int v){
    used[v] = true;
    dp2[v] = a[v];
    ll max1 = -INF, max2 = -INF;
    for(int to : G[v]){
        if(!used[to]){
            dfs(to);
            dp1[v] += max(dp1[to]+b[to]+b[v], dp2[to]);
            dp2[v] += max(dp1[to], dp2[to]);
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> N;
    for(int i = 0;i < N; i++) cin >> a[i];
    for(int i = 0;i < N; i++) cin >> b[i];
    for(int i = 0; i < N-1; i++){
        int u, v; cin >> u >> v; u--; v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(0);
    cout << max(dp1[0], dp2[0]) << endl;
}
0