結果

問題 No.1221 木 *= 3
ユーザー qumazakiqumazaki
提出日時 2020-09-05 02:04:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 347 ms / 2,000 ms
コード長 939 bytes
コンパイル時間 324 ms
コンパイル使用メモリ 86,824 KB
実行使用メモリ 143,040 KB
最終ジャッジ日時 2023-08-17 21:14:13
合計ジャッジ時間 7,959 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,136 KB
testcase_01 AC 74 ms
71,240 KB
testcase_02 AC 75 ms
71,064 KB
testcase_03 AC 72 ms
71,240 KB
testcase_04 AC 72 ms
70,884 KB
testcase_05 AC 76 ms
71,064 KB
testcase_06 AC 75 ms
71,064 KB
testcase_07 AC 347 ms
136,116 KB
testcase_08 AC 332 ms
128,284 KB
testcase_09 AC 340 ms
128,076 KB
testcase_10 AC 334 ms
127,988 KB
testcase_11 AC 341 ms
128,152 KB
testcase_12 AC 304 ms
143,040 KB
testcase_13 AC 312 ms
134,372 KB
testcase_14 AC 324 ms
134,024 KB
testcase_15 AC 313 ms
134,512 KB
testcase_16 AC 321 ms
134,832 KB
testcase_17 AC 313 ms
134,156 KB
testcase_18 AC 333 ms
127,840 KB
testcase_19 AC 326 ms
127,728 KB
testcase_20 AC 333 ms
127,936 KB
testcase_21 AC 335 ms
127,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
a = [0] + list(map(int,readline().split()))
b = [0] + list(map(int,readline().split()))
if(n==1):
    print(max(0,a[1]))
    exit()
data = list(map(int,read().split()))

links = [[] for _ in range(n+1)]
it = iter(data)
for u,v in zip(it,it):
    links[u].append(v)
    links[v].append(u)

stack = [1]
done = [0] * (n+1)
done[1] = 1
tp = []
child = [[] for _ in range(n+1)]
while(stack):
    i = stack.pop()
    tp.append(i)
    for j in links[i]:
        if(done[j] == 1):
            continue
        done[j] = 1
        child[i].append(j)
        stack.append(j)

scores = [[0,0] for _ in range(n+1)]

for i in tp[::-1]:
    for j in child[i]:
        scores[i][0] += max(scores[j][0] + b[i] + b[j], scores[j][1])
        scores[i][1] += max(scores[j])
    scores[i][1] += a[i]

print(max(scores[1]))
0