結果

問題 No.1221 木 *= 3
ユーザー tktk_snsntktk_snsn
提出日時 2021-02-12 21:03:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 398 ms / 2,000 ms
コード長 796 bytes
コンパイル時間 1,030 ms
コンパイル使用メモリ 87,100 KB
実行使用メモリ 119,960 KB
最終ジャッジ日時 2023-09-27 02:17:02
合計ジャッジ時間 9,389 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
71,180 KB
testcase_01 AC 66 ms
71,328 KB
testcase_02 AC 69 ms
71,216 KB
testcase_03 AC 72 ms
71,116 KB
testcase_04 AC 69 ms
71,440 KB
testcase_05 AC 68 ms
71,256 KB
testcase_06 AC 68 ms
71,172 KB
testcase_07 AC 398 ms
109,248 KB
testcase_08 AC 330 ms
108,584 KB
testcase_09 AC 354 ms
109,360 KB
testcase_10 AC 350 ms
109,348 KB
testcase_11 AC 359 ms
109,436 KB
testcase_12 AC 281 ms
119,888 KB
testcase_13 AC 279 ms
119,080 KB
testcase_14 AC 280 ms
119,744 KB
testcase_15 AC 285 ms
119,776 KB
testcase_16 AC 288 ms
119,960 KB
testcase_17 AC 350 ms
110,016 KB
testcase_18 AC 342 ms
108,968 KB
testcase_19 AC 385 ms
109,972 KB
testcase_20 AC 388 ms
110,076 KB
testcase_21 AC 375 ms
110,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
inf = 10**18

N = 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 -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)

topo = []
par = [-1]*N
que = [0]
while que:
    s = que.pop()
    topo.append(s)
    for t in G[s]:
        if t == par[s]:
            continue
        par[t] = s
        G[t].remove(s)
        que.append(t)

dp_stay = [-inf] * N
dp_remove = [-inf] * N
for 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]))
0