#include using namespace std; using ll = long long; using pll = pair; #define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i) #define rep(i, n) drep(i, 0, n - 1) #define all(a) (a).begin(), (a).end() #define pb push_back #define fi first #define se second const ll MOD = 1000000007; const ll MOD2 = 998244353; const ll INF = 1LL << 60; const ll MAX_N = 2e5; int main(){ ll n, m; cin >> n >> m; vector h(n); rep(i, n) cin >> h[i]; vector> G(n); rep(i, m){ ll u, v; cin >> u >> v; u--; v--; G[u].pb(v); G[v].pb(u); } // cherry vector> cost1(n, vector(2, -INF)); vector> check1(n, vector(2, false)); cost1[0][0] = 0; priority_queue> PQ1; PQ1.push({0, 0, 0}); while(!PQ1.empty()){ ll u = get<1>(PQ1.top()); ll isUp = get<2>(PQ1.top()); PQ1.pop(); if(check1[u][isUp]) continue; check1[u][isUp] = true; for(ll v : G[u]){ if(v < u) continue; if(isUp == 0){ if(h[u] < h[v]){ if(cost1[v][1] < cost1[u][0]+(h[v]-h[u])){ cost1[v][1] = cost1[u][0]+(h[v]-h[u]); PQ1.push({cost1[v][1], v, 1}); } }else{ if(cost1[v][0] < cost1[u][0]){ cost1[v][0] = cost1[u][0]; PQ1.push({cost1[v][0], v, 0}); } } }else{ if(h[u] > h[v]){ if(cost1[v][0] < cost1[u][1]){ cost1[v][0] = cost1[u][1]; PQ1.push({cost1[v][0], v, 0}); } } } } } // zerukoba vector> cost2(n, vector(2, -INF)); vector> check2(n, vector(2, false)); cost2[n-1][0] = 0; priority_queue> PQ2; PQ2.push({0, n-1, 0}); while(!PQ2.empty()){ ll u = get<1>(PQ2.top()); ll isUp = get<2>(PQ2.top()); PQ2.pop(); if(check2[u][isUp]) continue; check2[u][isUp] = true; for(ll v : G[u]){ if(u < v) continue; if(isUp == 0){ if(h[u] < h[v]){ if(cost2[v][1] < cost2[u][0]+(h[v]-h[u])){ cost2[v][1] = cost2[u][0]+(h[v]-h[u]); PQ2.push({cost2[v][1], v, 1}); } }else{ if(cost2[v][0] < cost2[u][0]){ cost2[v][0] = cost2[u][0]; PQ2.push({cost2[v][0], v, 0}); } } }else{ if(h[u] > h[v]){ if(cost2[v][0] < cost2[u][1]){ cost2[v][0] = cost2[u][1]; PQ2.push({cost2[v][0], v, 0}); } } } } } ll che = max(cost1[n-1][0], cost1[n-1][1]); ll zer = max(cost2[0][0], cost2[0][1]); if(che == -INF) cout << -1 << endl; else cout << che << endl; if(zer == -INF) cout << -1 << endl; else cout << zer << endl; }