# https://yukicoder.me/problems/no/3660 from collections import deque def main(): N = int(input()) S = list(map(int, input().split())) next_nodes = [[] for _ in range(N)] for _ in range(N - 1): a, b = map(int ,input().split()) next_nodes[a - 1].append(b - 1) next_nodes[b - 1].append(a - 1) # 全方位木dp stack = deque() parents = [-2] * N nexts = [{} for _ in range(N)] stack.append((0, 0)) parents[0] = -1 while len(stack) > 0: v, index = stack.pop() while index < len(next_nodes[v]): w = next_nodes[v][index] if w == parents[v]: index += 1 continue parents[w] = v stack.append((v, index + 1)) stack.append((w, 0)) break if index == len(next_nodes[v]): p = parents[v] if p != -1: s = S[v] max_v = 0 for c, v0 in nexts[v].items(): s0 = S[c] if s < s0: max_v = max(max_v, v0) nexts[p][v] = max_v + s queue = deque() queue.append((0, 0)) while len(queue) > 0: v, value = queue.popleft() if parents[v] != -1: p = parents[v] nexts[v][p] = value max_v = 0 for c, value in nexts[v].items(): s = S[c] if s > S[v]: max_v = max(max_v, value) for c in next_nodes[v]: if c == parents[v]: continue if S[c] < S[v]: queue.append((c, S[v] + max_v)) else: queue.append((c, 0)) answer = 0 for i in range(N): for v, value in nexts[i].items(): if S[v] > S[i]: answer = max(answer, value + S[i]) print(answer) if __name__ == "__main__": main()