結果
問題 | No.1221 木 *= 3 |
ユーザー |
|
提出日時 | 2020-09-04 22:16:35 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 170 ms / 2,000 ms |
コード長 | 1,000 bytes |
コンパイル時間 | 1,600 ms |
コンパイル使用メモリ | 171,040 KB |
実行使用メモリ | 18,560 KB |
最終ジャッジ日時 | 2024-11-26 13:23:56 |
合計ジャッジ時間 | 5,205 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 18 |
ソースコード
#include <bits/stdc++.h>#define rep(i,n) for(int i=0;i<(int)(n);i++)using namespace std;using ll = long long ;using P = pair<int,int> ;using pll = pair<long long,long long>;constexpr int INF = 1e9;constexpr long long LINF = 1e17;constexpr int MOD = 1000000007;constexpr double PI = 3.14159265358979323846;int n;vector<int> tree[100005];ll a[100005];ll b[100005];ll dp[100005][2];pll dfs(int i,int before){ll one = a[i];ll two = 0;for(int j:tree[i]){if(j==before) continue;pll now = dfs(j,i);one += max(now.first,now.second);two += max(now.first,now.second+b[j]+b[i]);}dp[i][0] = one;dp[i][1] = two;return pll(one,two);}int main(){cin >> n;rep(i,n) cin >> a[i];rep(i,n) cin >> b[i];rep(i,n-1){int u,v;cin >> u >> v;--u;--v;tree[u].push_back(v);tree[v].push_back(u);}dfs(0,-1);cout << max(dp[0][0],dp[0][1]) << endl;return 0;}