from collections import defaultdict from heapq import heapify, heappop, heappush import sys input = sys.stdin.readline def main(): n, m = map(int, input().split()) H = list(map(int, input().split())) G1 = defaultdict(list) G2 = defaultdict(list) for _ in range(m): x, y = map(int, input().split()) x -= 1 y -= 1 G1[x].append((y, H[y] - H[x])) G2[y].append((x, H[x] - H[y])) DP = [[-1, -1] for _ in range(n)] DP[0][0] = 0 H = [(0, 0, 0)] heapify(H) while H: cnt, cp, up = heappop(H) cnt *= -1 if cnt < DP[cp][up]: continue if cp == n - 1: continue for np, d in G1[cp]: if d > 0: if up == 0: if cnt + d <= DP[np][1]: continue DP[np][1] = cnt + d heappush(H, (-DP[np][1], np, 1)) else: if cnt <= DP[np][0]: continue DP[np][0] = cnt heappush(H, (-DP[np][0], np, 0)) print(max(DP[n - 1])) DP = [[-1, -1] for _ in range(n)] DP[n - 1][0] = 0 H = [(0, n - 1, 0)] heapify(H) while H: cnt, cp, up = heappop(H) cnt *= -1 if cnt < DP[cp][up]: continue if cp == 0: continue for np, d in G2[cp]: if d > 0: if up == 0: if cnt + d <= DP[np][1]: continue DP[np][1] = cnt + d heappush(H, (-DP[np][1], np, 1)) else: if cnt <= DP[np][0]: continue DP[np][0] = cnt heappush(H, (-DP[np][0], np, 0)) print(max(DP[0])) if __name__ == '__main__': main()