結果
| 問題 |
No.1221 木 *= 3
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-09-04 22:18:55 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 103 ms / 2,000 ms |
| コード長 | 1,296 bytes |
| コンパイル時間 | 1,798 ms |
| コンパイル使用メモリ | 174,468 KB |
| 実行使用メモリ | 23,552 KB |
| 最終ジャッジ日時 | 2024-11-26 14:06:33 |
| 合計ジャッジ時間 | 4,627 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 18 |
ソースコード
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i ++)
#define irep(i,n) for (int i = (int)(n) - 1;i >= 0;--i)
using namespace std;
using ll = long long;
using PL = pair<ll,ll>;
using P = pair<int,int>;
constexpr int INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007;// = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;
int N = 100010;
vector<ll> A(N),B(N);
vector<vector<int>> G(N);
vector<vector<ll>> dp(N,vector<ll>(2,-1));
void dfs(int v,int p) {
for (int e:G[v]) {
if (p == e) continue;
dfs(e,v);
}
// v消す
ll res = A[v];
for (int e:G[v]) {
if (p == e) continue;
res += max(dp[e][0],dp[e][1]);
}
dp[v][0] = res;
// v消さない
res = 0;
for (int e:G[v]) {
if (p == e) continue;
res += max(dp[e][0],dp[e][1] + B[v] + B[e]);
}
dp[v][1] = res;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin >> N;
rep(i,N) cin >> A[i];
rep(i,N) cin >> B[i];
rep(i,N - 1) {
int a,b; cin >> a >> b;
--a; --b;
G[a].push_back(b);
G[b].push_back(a);
}
dfs(0,-1);
cout << max(dp[0][0],dp[0][1]) << '\n';
return 0;
}