n=int(input())
root=0
happy=[]
graph=[[] for _ in range(n)]
visited=[False for _ in range(n)]
for _ in range(n):
happy.append((int(input())))
for _ in range(n-1):
l, k = map(int,input().split())
graph[k - 1].append(l - 1)
visited[l - 1] = True
for i in range(n):
if not visited[i]:
root = i
def dfs(x):
attend = happy[x]
not_attend = 0
for y in graph[x]:
y_attend, y_not_attend = dfs(y)
not_attend += max(y_not_attend, y_attend)
attend += y_not_attend
return attend,not_attend
print(max(dfs(root)))