結果
問題 | No.1221 木 *= 3 |
ユーザー |
|
提出日時 | 2020-09-05 12:58:47 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 309 ms / 2,000 ms |
コード長 | 879 bytes |
コンパイル時間 | 492 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 108,948 KB |
最終ジャッジ日時 | 2024-11-28 03:21:59 |
合計ジャッジ時間 | 6,026 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 18 |
ソースコード
import sysfrom collections import dequeN = int(input())a = list(map(int,input().split()))b = list(map(int,input().split()))edge = [[] for i in range(N)]for _ in range(N-1):u,v = map(int,input().split())edge[u-1].append(v-1)edge[v-1].append(u-1)deq = deque([0])parent = [-1] * Nres = []while deq:v = deq.popleft()res.append(v)for nv in edge[v]:if nv!=parent[v]:parent[nv] = vdeq.append(nv)res = res[::-1]exist_dp = [0] * Nnot_exist_dp = [0] * Nfor v in res:#not_exist_dpnot_exist_dp[v] = a[v]for nv in edge[v]:if nv!=parent[v]:not_exist_dp[v] += max(exist_dp[nv],not_exist_dp[nv])#exist_dpfor nv in edge[v]:if nv!=parent[v]:exist_dp[v] += max(b[v]+b[nv]+exist_dp[nv],not_exist_dp[nv])print(max(exist_dp[0],not_exist_dp[0]))