結果

問題 No.1221 木 *= 3
ユーザー 👑 rin204rin204
提出日時 2022-04-12 10:26:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 700 ms / 2,000 ms
コード長 617 bytes
コンパイル時間 404 ms
コンパイル使用メモリ 82,796 KB
実行使用メモリ 212,756 KB
最終ジャッジ日時 2024-12-17 15:50:38
合計ジャッジ時間 8,969 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,860 KB
testcase_01 AC 39 ms
53,104 KB
testcase_02 AC 39 ms
53,100 KB
testcase_03 AC 38 ms
53,240 KB
testcase_04 AC 38 ms
53,688 KB
testcase_05 AC 38 ms
53,336 KB
testcase_06 AC 38 ms
52,928 KB
testcase_07 AC 647 ms
207,656 KB
testcase_08 AC 695 ms
211,788 KB
testcase_09 AC 605 ms
206,212 KB
testcase_10 AC 700 ms
211,428 KB
testcase_11 AC 699 ms
212,756 KB
testcase_12 AC 235 ms
100,916 KB
testcase_13 AC 231 ms
101,076 KB
testcase_14 AC 248 ms
100,732 KB
testcase_15 AC 245 ms
100,072 KB
testcase_16 AC 240 ms
100,164 KB
testcase_17 AC 287 ms
99,288 KB
testcase_18 AC 282 ms
99,736 KB
testcase_19 AC 287 ms
98,656 KB
testcase_20 AC 290 ms
99,060 KB
testcase_21 AC 296 ms
99,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10 ** 9)

n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
edges = [[] for _ in range(n)]
for _ in range(n - 1):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    edges[u].append(v)
    edges[v].append(u)
    
dp0 = [0] * n
dp1 = [0] * n

def dfs(pos, bpos):
    dp0[pos] = A[pos]
    for npos in edges[pos]:
        if npos == bpos:
            continue
        dfs(npos, pos)
        dp0[pos] += max(dp0[npos], dp1[npos])
        dp1[pos] += max(dp0[npos], dp1[npos] + B[pos] + B[npos])
    
dfs(0, -1)
print(max(dp0[0], dp1[0]))
0