import sys def solve(H, g): n = len(H) dp = [[-1] * 2 for _ in range(n)] dp[0] = [0, 0] for x in range(n): hx = H[x] for y in g[x]: hy = H[y] up = 1 if hy > hx else 0 d = max(0, hy - hx) for i in range(2): if dp[x][i] == -1: continue if i == 1 and up: continue dp[y][up] = max(dp[y][up], dp[x][i] + d) return max(dp[-1]) def main(): input = sys.stdin.readline n, m = map(int, input().split()) H = list(map(int, input().split())) g1 = [[] for _ in range(n)] g2 = [[] for _ in range(n)] n2 = n - 1 for _ in range(m): x, y = map(lambda x: int(x) - 1, input().split()) g1[x].append(y) g2[n2 - y].append(n2 - x) print(solve(H, g1)) print(solve(H[::-1], g1)) if __name__ == '__main__': main()