#include #include using namespace std; using namespace atcoder; using ll = long long; using ld = long double; using mint = modint998244353; int N, M, H[101010]; vector G[101010]; void bfs(int start, int goal, int s) { ll ans1 = 0; queue> que1; que1.push({start, 0, false}); map, bool> vis1; bool flag1 = false; while (!que1.empty()) { auto &[u, c, f] = que1.front(); que1.pop(); if (u == goal) { flag1 = true; ans1 = max(ans1, c); continue; } for (auto v: G[u]) { if (s == 0 && u > v) continue; if (s == 1 && u < v) continue; if (f && H[u] < H[v]) continue; ll c1 = c; bool f1 = f; if(vis1[{min(u, v), max(u, v)}]) continue; vis1[{min(u, v), max(u, v)}] = true; if (H[u] < H[v]) { c1 += abs(H[v] - H[u]); f1 = true; } else { f1 = false; } que1.push({v, c1, f1}); } } cout << (flag1 ? ans1: -1) << endl; } int main() { cin >> N >> M; for (int i = 1; i <= N; i++) cin >> H[i]; for (int i = 1; i <= M; i++) { int x, y; cin >> x >> y; G[x].push_back(y); G[y].push_back(x); } bfs(1, N, 0); bfs(N, 1, 1); return 0; }