#include using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; template using vc = vector; template using vvc = vc>; using pi = pair; using pl = pair; using vi = vc; using vvi = vvc; using vl = vc; using vvl = vvc; #define rep(i,a,b) for (int i = (int)(a); i < (int)(b); i++) #define irep(i,a,b) for (int i = (int)(a); i > (int)(b); i--) #define all(a) a.begin(),a.end() #define print(n) cout << n << '\n' #define pritn(n) print(n) #define printv(n,a) {copy(all(n),ostream_iterator(cout," ")); cout<<"\n";} #define printvv(n,a) {for(auto itr:n) printv(itr,a);} #define rup(a,b) (a+b-1)/b #define input(A,N) rep(i,0,N) cin>>A[i] #define chmax(a,b) a = max(a,b) #define chmin(a,b) a = min(a,b) template< typename T > struct edge{ int from,to; T cost; /* *to:繋がってる先 *cost:辺のコスト */ edge(int from,int to, T cost):from(from),to(to),cost(cost){}; edge &operator=(const int& x){ to = x; return *this; } }; template< typename T > struct graph{ int n; vector>> e; graph(int n):n(n),e(n){} void addedge(int from,int to,T cost){ e[from].emplace_back(from,to,cost); } vector> &operator[](int i) { return e[i]; } }; int main(){ cout << fixed << setprecision(15); int n,m; cin>>n>>m; vl h(n); input(h,n); graph g(n); rep(i,0,m){ int u,v; cin>>u>>v; u--;v--; g.addedge(u,v,1); g.addedge(v,u,1); } vvc dp(n,vc(2,-1)); dp[0][0] = 0; dp[0][1] = 0; for(int i = 0;i=e.to) continue; int ni = e.to; ll d = h[ni] - h[i]; if(d<0){ if(dp[i][0]==-1) continue; chmax(dp[ni][1],dp[i][0]); }else{ if(dp[i][1] == -1) continue; chmax(dp[ni][0],dp[i][1]+d); } } } ll a = max(dp[n-1][0],dp[n-1][1]); for(int i = 0;i=1;i--){ for(auto e:g[i]){ if(i<=e.to) continue; int ni = e.to; ll d = h[ni] - h[i]; if(d<0){ if(dp[i][0]!=-1) chmax(dp[ni][1],dp[i][0]); if(dp[i][1]!=-1) chmax(dp[ni][1],dp[i][1]); }else{ if(dp[i][1] == -1) continue; chmax(dp[ni][0],dp[i][1]+d); } } } ll b = max(dp[0][0],dp[0][1]); //printvv(dp,ll); print(a); print(b); //system("pause"); return 0; }