#include // clang-format off using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair; const ll INF=4e18; void print0(){}; template void print0(H h,T... t){cout<void print(H h,T... t){print0(h);if(sizeof...(T)>0)print0(" ");print(t...);} void perr0(){}; template void perr0(H h,T... t){cerr<void perr(H h,T... t){perr0(h);if(sizeof...(T)>0)perr0(" ");perr(t...);} void ioinit() { cout<> edgmap(V); vector height(V); ll N; ll memo1[V][2]; bool done1[V][2]; ll dp1(ll u, ll up) { if (done1[u][up]) return memo1[u][up]; done1[u][up] = true; if (u == N) { return memo1[u][up] = 0; } ll ans = -INF; for (auto v : edgmap[u]) { if (u > v) continue; if (up && height[v] > height[u]) continue; if (height[v] > height[u]) { ll diff = height[v] - height[u]; ans = max(ans, diff + dp1(v, 1)); } else { ans = max(ans, dp1(v, 0)); } } return memo1[u][up] = ans; } ll memo2[V][2]; bool done2[V][2]; ll dp2(ll u, ll up) { if (done2[u][up]) return memo2[u][up]; done2[u][up] = true; if (u == 1) { return memo2[u][up] = 0; } ll ans = -INF; for (auto v : edgmap[u]) { if (u < v) continue; if (up && height[v] > height[u]) continue; if (height[v] > height[u]) { ll diff = height[v] - height[u]; ans = max(ans, diff + dp2(v, 1)); } else { ans = max(ans, dp2(v, 0)); } } return memo2[u][up] = ans; } int main() { ioinit(); ll M; cin >> N >> M; for (ll i = 1; i <= N; i++) { cin >> height[i]; } for (ll i = 0; i < M; i++) { ll x, y; cin >> x >> y; edgmap[x].push_back(y); edgmap[y].push_back(x); } ll a1 = dp1(1, 0); if (a1 < -INF / 2) a1 = -1; print(a1); ll a2 = dp2(N, 0); if (a2 < -INF / 2) a2 = -1; print(a2); return 0; }