1220 生命之树
作者:
几毫克的光
,
2024-04-10 20:34:44
,
所有人可见
,
阅读 9
# # 1220 生命之树
# 状态表示数组和深度优先搜索,把节点的空间开到合适n*2,把节点的index搞清楚
n = int(input())
w = list(map(int, input().split()))
N = n
e = [0] * (2 * n)
ne = [0] * (2 * n)
cnt = 0
h = [-1] * (2 * n)
def add(a, b):
global cnt
e[cnt] = b
ne[cnt] = h[a]
h[a] = cnt
cnt += 1
while(N - 1):
a, b = map(int, input().split())
add(a - 1, b - 1)
add(b - 1, a - 1)
N -= 1
q = [0] * (n + 10)
def dfs(a, father):
q[a] = w[a]
buf = h[a]
while(buf != -1):
if e[buf] != father:
dfs(e[buf], a)
q[a] += max(0, q[e[buf]])
buf = ne[buf]
dfs(0, -1)
ans = q[0]
for i in range(1, n):
ans = max(ans, q[i])
print(ans)