結果

問題 No.1221 木 *= 3
ユーザー anagohirameanagohirame
提出日時 2020-07-18 16:45:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 374 ms / 2,000 ms
コード長 733 bytes
コンパイル時間 237 ms
コンパイル使用メモリ 82,324 KB
実行使用メモリ 111,872 KB
最終ジャッジ日時 2024-11-15 16:16:34
合計ジャッジ時間 7,190 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
52,096 KB
testcase_01 AC 42 ms
52,352 KB
testcase_02 AC 42 ms
51,840 KB
testcase_03 AC 41 ms
52,224 KB
testcase_04 AC 41 ms
52,224 KB
testcase_05 AC 41 ms
51,840 KB
testcase_06 AC 41 ms
52,096 KB
testcase_07 AC 331 ms
109,756 KB
testcase_08 AC 329 ms
108,996 KB
testcase_09 AC 346 ms
110,348 KB
testcase_10 AC 343 ms
109,204 KB
testcase_11 AC 348 ms
109,084 KB
testcase_12 AC 327 ms
111,844 KB
testcase_13 AC 332 ms
111,464 KB
testcase_14 AC 350 ms
111,384 KB
testcase_15 AC 340 ms
111,872 KB
testcase_16 AC 360 ms
111,372 KB
testcase_17 AC 354 ms
106,240 KB
testcase_18 AC 372 ms
106,368 KB
testcase_19 AC 372 ms
106,368 KB
testcase_20 AC 363 ms
106,368 KB
testcase_21 AC 374 ms
106,496 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