結果
問題 | No.1221 木 *= 3 |
ユーザー |
|
提出日時 | 2020-09-05 00:56:27 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 222 ms / 2,000 ms |
コード長 | 798 bytes |
コンパイル時間 | 2,411 ms |
コンパイル使用メモリ | 201,848 KB |
最終ジャッジ日時 | 2025-01-14 06:35:03 |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 18 |
ソースコード
#include <bits/stdc++.h>using namespace std;#define rep(i,n) for(int i = 0; i < (int)n; i++)using ll = long long;const ll INF = 1e17;int main(){int n;cin >> n;vector<ll> a(n), b(n);rep(i,n) cin >> a[i];rep(i,n) cin >> b[i];vector<vector<int>> g(n);rep(i,n-1) {int u, v;cin >> u >> v;u--; v--;g[u].push_back(v);g[v].push_back(u);}vector<ll> dp1(n,-INF);vector<ll> dp2(n,-INF);auto dfs = [&](auto self, int u, int p) -> void {ll del = a[u], res = 0;for(int to : g[u]) {if(to != p) {self(self,to,u);del += max(dp1[to],dp2[to]);res += max(dp1[to]+b[u]+b[to],dp2[to]);}}dp1[u] = res;dp2[u] = del;};dfs(dfs,0,-1);cout << max(dp1[0],dp2[0]) << endl;return 0;}