#include #include #include using namespace std; using ll = long long; void chmax(ll &x, ll y) { x = max(x, y); } ll solve(const vector &h, const vector> &g) { vector dp0(h.size(), -1), dp1(h.size(), -1); dp0[0] = 0; for(size_t i = 0; i< h.size(); i++) { for(auto &j : g[i]) { if(i < j) { if(h[i] < h[j]) { if(dp0[i] >= 0) chmax(dp1[j], dp0[i] + (h[j] - h[i])); } else { chmax(dp0[j], max(dp0[i], dp1[i])); } } } } return max(dp0.back(), dp1.back()); } int main() { int n, m; cin >> n >> m; vector h(n); for(int i = 0; i < n; i++) cin >> h[i]; vector> g(n); for(int i = 0; i < m; i++) { size_t x, y; cin >> x >> y; --x; --y; g[x].emplace_back(y); g[y].emplace_back(x); } cout << solve(h, g) << endl; // reverse reverse(begin(h), end(h)); reverse(begin(g), end(g)); for(auto &i : g) for(auto &j :i) j = n - j - 1; cout << solve(h, g) << endl; }