#include namespace { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic pop using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; VI h(n); rep(i, n) cin >> h[i]; VVI to1(n), to2(n); rep(_, m) { int u, v; cin >> u >> v; u--, v--; to1[u].emplace_back(v); to2[n - 1 - v].emplace_back(n - 1 - u); } constexpr long long INF = 1002003004005006007; rep(_, 2) { vector> dp(n, {-INF, -INF}); dp[0][0] = 0; rep(i, n - 1) { ll v0 = dp[i][0], v1 = max(dp[i][0], dp[i][1]); for(int j: to1[i]) { if (h[i] < h[j]) chmax(dp[j][1], v0 + h[j] - h[i]); else chmax(dp[j][0], v1); } } ll ans = max(dp[n-1][0], dp[n-1][1]); chmax(ans, -1LL); cout << ans << '\n'; reverse(all(h)); swap(to1, to2); } }