結果

問題 No.1221 木 *= 3
ユーザー kenskens
提出日時 2020-09-05 00:56:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 182 ms / 2,000 ms
コード長 798 bytes
コンパイル時間 1,954 ms
コンパイル使用メモリ 208,376 KB
実行使用メモリ 18,176 KB
最終ジャッジ日時 2024-05-05 03:57:04
合計ジャッジ時間 5,423 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 159 ms
18,176 KB
testcase_08 AC 153 ms
17,920 KB
testcase_09 AC 182 ms
18,176 KB
testcase_10 AC 152 ms
18,048 KB
testcase_11 AC 158 ms
18,048 KB
testcase_12 AC 144 ms
12,188 KB
testcase_13 AC 137 ms
12,188 KB
testcase_14 AC 146 ms
12,316 KB
testcase_15 AC 145 ms
12,180 KB
testcase_16 AC 149 ms
12,180 KB
testcase_17 AC 155 ms
11,904 KB
testcase_18 AC 148 ms
12,032 KB
testcase_19 AC 150 ms
11,904 KB
testcase_20 AC 152 ms
11,948 KB
testcase_21 AC 149 ms
12,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0