from collections import deque N,M = map(int,input().split()) H = [0]+list(map(int,input().split())) G0 = {i:[] for i in range(1,N+1)} Indeg0 = [0]*(N+1) G1 = {i:[] for i in range(1,N+1)} Indeg1 = [0]*(N+1) for _ in range(M): a,b = map(int,input().split()) if b>a: G0[a].append((b,max(H[b]-H[a],0))) Indeg0[b] += 1 G1[b].append((a,max(H[a]-H[b],0))) Indeg1[a] += 1 else: G0[b].append((a,max(H[a]-H[b],0))) Indeg0[a] += 1 G1[a].append((b,max(H[b]-H[a],0))) Indeg1[b] += 1 INFTY = 10**15 dist0 = [[-INFTY,-INFTY] for _ in range(N+1)] que0 = deque([1]) dist0[1] = [0,0] for i in range(2,N+1): if Indeg0[i]==0: que0.append(i) while que0: u = que0.popleft() d0,d1 = dist0[u] for v,d in G0[u]: if d>0: dist0[v][1] = max(dist0[v][1],d0+d) else: dist0[v][0] = max(dist0[v][0],d0,d1) Indeg0[v] -= 1 if Indeg0[v]==0: que0.append(v) dist1 = [[-INFTY,-INFTY] for _ in range(N+1)] que1 = deque([N]) dist1[N] = [0,0] for i in range(1,N): if Indeg1[i]==0: que1.append(i) while que1: u = que1.popleft() d0,d1 = dist1[u] for v,d in G1[u]: if d>0: dist1[v][1] = max(dist1[v][1],d0+d) else: dist1[v][0] = max(dist1[v][0],d0,d1) Indeg1[v] -= 1 if Indeg1[v]==0: que1.append(v) print(max(max(dist0[N]),-1)) print(max(max(dist1[1]),-1))