結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
10,624 KB
testcase_01 AC 33 ms
10,752 KB
testcase_02 AC 32 ms
10,752 KB
testcase_03 AC 31 ms
10,752 KB
testcase_04 AC 37 ms
10,752 KB
testcase_05 AC 32 ms
10,752 KB
testcase_06 AC 32 ms
10,624 KB
testcase_07 AC 825 ms
63,856 KB
testcase_08 AC 834 ms
64,980 KB
testcase_09 AC 823 ms
63,692 KB
testcase_10 AC 814 ms
64,756 KB
testcase_11 AC 815 ms
65,004 KB
testcase_12 AC 657 ms
45,948 KB
testcase_13 AC 645 ms
46,048 KB
testcase_14 AC 650 ms
46,080 KB
testcase_15 AC 647 ms
46,048 KB
testcase_16 AC 653 ms
46,044 KB
testcase_17 AC 757 ms
49,004 KB
testcase_18 AC 739 ms
49,892 KB
testcase_19 AC 740 ms
49,152 KB
testcase_20 AC 747 ms
49,124 KB
testcase_21 AC 747 ms
49,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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)]

def dfs(i, p):
	dp[i][0] = a[i]
	for j in adj[i]:
		if j == p:
			continue
		dfs(j, i)
		dp[i][0] += max(dp[j][0], dp[j][1])
		dp[i][1] += max(dp[j][0], dp[j][1] + b[i] + b[j])
	return

dfs(0, -1)
print(max(dp[0]))
0