結果

問題 No.1221 木 *= 3
ユーザー anagohirameanagohirame
提出日時 2020-07-18 16:45:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 286 ms / 2,000 ms
コード長 733 bytes
コンパイル時間 128 ms
コンパイル使用メモリ 81,904 KB
実行使用メモリ 111,736 KB
最終ジャッジ日時 2024-04-27 13:55:16
合計ジャッジ時間 5,542 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
51,968 KB
testcase_01 AC 35 ms
52,608 KB
testcase_02 AC 34 ms
52,100 KB
testcase_03 AC 34 ms
52,096 KB
testcase_04 AC 34 ms
52,480 KB
testcase_05 AC 33 ms
52,208 KB
testcase_06 AC 33 ms
51,968 KB
testcase_07 AC 253 ms
109,456 KB
testcase_08 AC 249 ms
109,092 KB
testcase_09 AC 266 ms
109,888 KB
testcase_10 AC 261 ms
108,636 KB
testcase_11 AC 262 ms
109,144 KB
testcase_12 AC 240 ms
111,736 KB
testcase_13 AC 240 ms
111,140 KB
testcase_14 AC 255 ms
110,940 KB
testcase_15 AC 258 ms
111,448 KB
testcase_16 AC 259 ms
110,936 KB
testcase_17 AC 256 ms
106,228 KB
testcase_18 AC 270 ms
106,304 KB
testcase_19 AC 274 ms
106,364 KB
testcase_20 AC 273 ms
106,292 KB
testcase_21 AC 286 ms
106,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
def input():
	return sys.stdin.buffer.readline()[:-1]

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
adj = [[] for _ in range(n)]
for i in range(n-1):
	u, v = map(int, input().split())
	adj[u-1].append((v-1, b[u-1] + b[v-1]))
	adj[v-1].append((u-1, b[u-1] + b[v-1]))

dp = [[0 for _ in range(2)] for _ in range(n)]
visited = [False for _ in range(n)]

stack = [(0, -1)]
while stack:
	i, p = stack[-1]
	if not visited[i]:
		visited[i] = True
		for j, e in adj[i]:
			if j != p:
				stack.append((j, i))
	else:
		stack.pop()
		dp[i][0] = a[i]
		for j, e in adj[i]:
			if j != p:
				dp[i][0] += max(dp[j][0], dp[j][1])
				dp[i][1] += max(dp[j][0], dp[j][1] + e)

print(max(dp[0]))
0