#include #include #include #include #include #include #include #include #include #include #define mkp make_pair #define mkt make_tuple #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define all(v) v.begin(), v.end() using namespace std; typedef long long ll; const ll MOD = 1e9 + 7; // const ll MOD = 998244353; template void chmin(T &a, const T &b) { if (a > b) a = b; } template void chmax(T &a, const T &b) { if (a < b) a = b; } const ll INF = 1e18; ll calc(vector H, vector X, vector Y) { const int N = H.size(); const int M = X.size(); vector> g(N); rep(i, M) g[X[i]].push_back(Y[i]); vector> dp(N, vector(2, -INF)); dp[0][0] = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < 2; j++) { const ll value = dp[i][j]; if (value == -INF) continue; for (auto to : g[i]) { if (j == 1 && H[i] < H[to]) continue; int nj = 0; if (H[i] < H[to]) nj = 1; chmax(dp[to][nj], value + max(0ll, H[to] - H[i])); } } } return max(dp[N - 1][0], dp[N - 1][1]); } void solve() { int N, M; cin >> N >> M; vector H(N); rep(i, N) cin >> H[i]; vector X(M), Y(M); rep(i, M) { cin >> X[i] >> Y[i]; X[i]--; Y[i]--; } ll ans1 = calc(H, X, Y); reverse(all(H)); rep(i, M) { X[i] = N - 1 - X[i]; Y[i] = N - 1 - Y[i]; } swap(X, Y); ll ans2 = calc(H, X, Y); if (ans1 == -INF) cout << -1 << endl; else cout << ans1 << endl; if (ans2 == -INF) cout << -1 << endl; else cout << ans2 << endl; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); solve(); return 0; }