from collections import defaultdict import sys sys.setrecursionlimit(10**6) try: import pypyjit pypyjit.set_param('max_unroll_recursion=-1') except ModuleNotFoundError: pass N = int(input()) S = list(map(int, input().split())) adj = defaultdict(list) adj2 = defaultdict(list) for _ in range(N-1): a, b = map(lambda x: int(x)-1, input().split()) adj[a].append(b) adj[b].append(a) if S[a] > S[b]: adj2[a].append(b) if S[a] < S[b]: adj2[b].append(a) def dfs(v): s = S[v] res = s for to in adj2[v]: res = max(res, s + dfs(to)) return res ans = 0 for i in range(N): b = True small = False for to in adj[i]: if S[to] > S[i]: b = False break if S[to] < S[i]: small = True if b and small: ans = max(ans, dfs(i)) print(ans)