結果

問題 No.1221 木 *= 3
ユーザー Kiri8128Kiri8128
提出日時 2020-07-23 02:15:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 360 ms / 2,000 ms
コード長 741 bytes
コンパイル時間 1,066 ms
コンパイル使用メモリ 87,072 KB
実行使用メモリ 105,628 KB
最終ジャッジ日時 2023-08-09 19:18:10
合計ジャッジ時間 7,022 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,708 KB
testcase_01 AC 94 ms
71,660 KB
testcase_02 AC 92 ms
71,860 KB
testcase_03 AC 91 ms
71,312 KB
testcase_04 AC 91 ms
71,592 KB
testcase_05 AC 92 ms
71,548 KB
testcase_06 AC 90 ms
71,536 KB
testcase_07 AC 266 ms
100,672 KB
testcase_08 AC 259 ms
99,988 KB
testcase_09 AC 265 ms
100,536 KB
testcase_10 AC 268 ms
100,496 KB
testcase_11 AC 267 ms
100,388 KB
testcase_12 AC 255 ms
105,480 KB
testcase_13 AC 259 ms
105,504 KB
testcase_14 AC 268 ms
105,516 KB
testcase_15 AC 270 ms
105,628 KB
testcase_16 AC 271 ms
105,328 KB
testcase_17 AC 335 ms
101,704 KB
testcase_18 AC 337 ms
101,992 KB
testcase_19 AC 360 ms
102,592 KB
testcase_20 AC 359 ms
102,852 KB
testcase_21 AC 338 ms
102,044 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