P,U=print,max def J(): return map(int,input().split()) N,M=J() H=list(J()) R=range(M) Z,W=[[] for j in R],[[] for j in R] for j in R: X,Y=J() X-=1 Y-=1 Z[j],W[j]=[X,Y],[Y,X] Z.sort() W.sort(reverse=1) N-=1 C,D,E,F=[-1]*N,[-1]*N,[-1]*N,[-1]*N C[0]=D[N]=0 for c,e,z in [C,E,Z],[D,F,W]: for x in z: X,Y=x[0],x[1] h=H[Y]-H[X] if c[X]>=0: if h>0:e[Y]=U(e[Y],c[X]+h) else:c[Y]=U(c[Y],c[X]) if e[X]>=0 and h<0:c[Y]=U(c[Y],e[X]) P(U(C[N],E[N])) P(U(D[0],F[0]))