結果
問題 | No.1221 木 *= 3 |
ユーザー |
![]() |
提出日時 | 2021-02-12 21:03:17 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 395 ms / 2,000 ms |
コード長 | 796 bytes |
コンパイル時間 | 198 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 118,016 KB |
最終ジャッジ日時 | 2024-07-19 19:48:31 |
合計ジャッジ時間 | 7,842 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 18 |
ソースコード
import sysinput = sys.stdin.buffer.readlinesys.setrecursionlimit(10 ** 7)inf = 10**18N = int(input())A = tuple(map(int, input().split()))B = tuple(map(int, input().split()))G = [[] for _ in range(N)]for _ in range(N - 1):a, b = map(int, input().split())a -= 1b -= 1G[a].append(b)G[b].append(a)topo = []par = [-1]*Nque = [0]while que:s = que.pop()topo.append(s)for t in G[s]:if t == par[s]:continuepar[t] = sG[t].remove(s)que.append(t)dp_stay = [-inf] * Ndp_remove = [-inf] * Nfor s in topo[::-1]:dp_remove[s] = A[s] + sum(max(dp_stay[c], dp_remove[c]) for c in G[s])dp_stay[s] = sum(max(dp_stay[c] + B[s] + B[c], dp_remove[c]) for c in G[s])print(max(dp_stay[0], dp_remove[0]))