#include #include #include #include #include #include #include #include using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) template using vi = vector; template using vii = vector>; template using viii = vector>; void chmax(ll & x, ll y) { x = max(x, y); } ll solve(int n, vi &h, vii &to) { const ll inf = 1e18; vii dp(2, vi(n, -inf)); dp[0][0] = 0; rep(v, n) { for (int nv : to[v]) { if (h[v] < h[nv]) chmax(dp[1][nv], dp[0][v] + h[nv] - h[v]); else { chmax(dp[0][nv], dp[1][v]); chmax(dp[0][nv], dp[0][v]); } } } return max(dp[0][n - 1], dp[1][n - 1]); } int main() { int n, m; cin >> n >> m; vi hc(n), hz(n); rep(i, n) cin >> hc[i]; rep(i, n) hz[i] = hc[n - 1 - i]; vii toc(n), toz(n); rep(i, m) { int x, y; cin >> x >> y; x--, y--; toc[x].push_back(y); toz[n - 1 - y].push_back(n - 1 - x); } ll resc = solve(n, hc, toc); ll resz = solve(n, hz, toz); cout << (resc < 0 ? -1 : resc) << endl; cout << (resz < 0 ? -1 : resz) << endl; return 0; }