結果

問題 No.1221 木 *= 3
ユーザー 👑 rin204rin204
提出日時 2022-04-12 10:26:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 725 ms / 2,000 ms
コード長 617 bytes
コンパイル時間 439 ms
コンパイル使用メモリ 87,032 KB
実行使用メモリ 215,328 KB
最終ジャッジ日時 2023-08-22 15:57:37
合計ジャッジ時間 9,942 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,108 KB
testcase_01 AC 74 ms
71,104 KB
testcase_02 AC 72 ms
71,200 KB
testcase_03 AC 73 ms
71,148 KB
testcase_04 AC 73 ms
71,140 KB
testcase_05 AC 73 ms
71,368 KB
testcase_06 AC 73 ms
71,216 KB
testcase_07 AC 637 ms
215,328 KB
testcase_08 AC 710 ms
209,960 KB
testcase_09 AC 637 ms
213,988 KB
testcase_10 AC 718 ms
209,824 KB
testcase_11 AC 725 ms
210,508 KB
testcase_12 AC 250 ms
101,972 KB
testcase_13 AC 257 ms
102,572 KB
testcase_14 AC 269 ms
102,800 KB
testcase_15 AC 271 ms
101,964 KB
testcase_16 AC 274 ms
101,912 KB
testcase_17 AC 310 ms
100,584 KB
testcase_18 AC 304 ms
100,624 KB
testcase_19 AC 314 ms
100,852 KB
testcase_20 AC 317 ms
100,808 KB
testcase_21 AC 338 ms
101,044 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