結果

問題 No.1221 木 *= 3
ユーザー Kiri8128Kiri8128
提出日時 2020-07-23 02:15:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 246 ms / 2,000 ms
コード長 741 bytes
コンパイル時間 128 ms
コンパイル使用メモリ 81,964 KB
実行使用メモリ 108,056 KB
最終ジャッジ日時 2024-04-27 13:55:38
合計ジャッジ時間 4,754 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,504 KB
testcase_01 AC 37 ms
54,016 KB
testcase_02 AC 43 ms
54,272 KB
testcase_03 AC 45 ms
53,844 KB
testcase_04 AC 39 ms
54,144 KB
testcase_05 AC 38 ms
54,144 KB
testcase_06 AC 37 ms
53,888 KB
testcase_07 AC 177 ms
104,276 KB
testcase_08 AC 169 ms
104,080 KB
testcase_09 AC 193 ms
104,392 KB
testcase_10 AC 185 ms
103,992 KB
testcase_11 AC 187 ms
104,048 KB
testcase_12 AC 178 ms
107,448 KB
testcase_13 AC 184 ms
108,056 KB
testcase_14 AC 192 ms
107,976 KB
testcase_15 AC 193 ms
108,004 KB
testcase_16 AC 197 ms
107,872 KB
testcase_17 AC 225 ms
105,040 KB
testcase_18 AC 219 ms
105,264 KB
testcase_19 AC 241 ms
105,812 KB
testcase_20 AC 246 ms
105,952 KB
testcase_21 AC 239 ms
105,436 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda: sys.stdin.readline().rstrip()
from collections import deque

N = int(input())
A = [int(a) for a in input().split()]
B = [int(a) for a in input().split()]
X = [[] for i in range(N)]
for i in range(N-1):
    x, y = map(int, input().split())
    X[x-1].append(y-1)
    X[y-1].append(x-1)

P = [-1] * N
Q = deque([0])
R = []
while Q:
    i = deque.popleft(Q)
    R.append(i)
    for a in X[i]:
        if a != P[i]:
            P[a] = i
            X[a].remove(i)
            deque.append(Q, a)

SA = [0] * N
SB = [0] * N

for i in R[::-1]:
    aa = A[i]
    bb = 0
    for j in X[i]:
        aa += max(SA[j], SB[j])
        bb += max(SA[j], SB[j] + B[i] + B[j])
    SA[i] = aa
    SB[i] = bb

print(max(SA[0], SB[0]))
0