結果

問題 No.1221 木 *= 3
ユーザー anagohirameanagohirame
提出日時 2020-08-22 01:38:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 862 ms / 2,000 ms
コード長 699 bytes
コンパイル時間 93 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 56,348 KB
最終ジャッジ日時 2024-04-27 13:57:07
合計ジャッジ時間 14,079 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
10,752 KB
testcase_01 AC 27 ms
10,880 KB
testcase_02 AC 27 ms
11,008 KB
testcase_03 AC 28 ms
10,880 KB
testcase_04 AC 28 ms
11,008 KB
testcase_05 AC 30 ms
11,008 KB
testcase_06 AC 27 ms
11,008 KB
testcase_07 AC 821 ms
54,700 KB
testcase_08 AC 842 ms
56,348 KB
testcase_09 AC 825 ms
55,680 KB
testcase_10 AC 840 ms
55,420 KB
testcase_11 AC 840 ms
55,676 KB
testcase_12 AC 794 ms
53,896 KB
testcase_13 AC 809 ms
54,024 KB
testcase_14 AC 799 ms
53,916 KB
testcase_15 AC 787 ms
54,040 KB
testcase_16 AC 816 ms
54,016 KB
testcase_17 AC 843 ms
49,936 KB
testcase_18 AC 839 ms
50,844 KB
testcase_19 AC 845 ms
49,944 KB
testcase_20 AC 843 ms
49,948 KB
testcase_21 AC 862 ms
50,044 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)
	adj[v-1].append(u-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 in adj[i]:
			if j != p:
				stack.append((j, i))
	else:
		stack.pop()
		dp[i][0] = a[i]
		for j 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] + b[i] + b[j])

print(max(dp[0]))
0