#include using namespace std; using ll = long long; using P = pair; #define fix(x) fixed << setprecision(x) #define asc(x) x, vector, greater #define rep(i, n) for(ll i = 0; i < n; i++) #define all(x) (x).begin(),(x).end() templatebool chmin(T&a, const T&b){if(a>b){a=b;return 1;}return 0;} templatebool chmax(T&a, const T&b){if(a> n >> m; vector h(n); rep(i,n) cin >> h[i]; vector> to(n); rep(i,m){ int x,y; cin >> x >> y; x--; y--; to[x].push_back(y); to[y].push_back(x); } vector> dp(2,vector(n,-INF)), dp2(2,vector(n,-INF)); dp[0][0] = dp[1][0] = dp2[0][n-1] = dp2[1][n-1] = 0; rep(i,n){ for(int x:to[i]){ if(i=0;i--){ for(int x:to[i]){ if(i>x){ if(h[i]({-1,dp[0][n-1],dp[1][n-1]}) << "\n" << max({-1,dp2[0][0],dp2[1][0]}) << '\n'; return 0; }