#include #include #include using namespace atcoder; using mint = modint998244353; using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf32 1000000001 #define Inf64 2000000000000000001 void solve(vector h,vector x,vector y){ int n = h.size(); vector> E(n); rep(i,x.size()){ E[min(x[i],y[i])].push_back(max(x[i],y[i])); } vector dp(n,vector(2,-Inf64)); dp[0][0] = 0; dp[0][1] = 0; rep(i,n-1){ rep(j,2){ if(dp[i][j]==-Inf64)continue; rep(k,E[i].size()){ int to = E[i][k]; int F; if(h[i] < h[to])F = 1; else F = 0; if(F==j&&j==1)continue; long long C = 0; if(F==1)C = h[to] - h[i]; dp[to][F] = max(dp[to][F],dp[i][j]+C); } } } /* rep(i,n){ cout<>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]--; } solve(h,x,y); rep(i,m){ x[i] = n-1-x[i]; y[i] = n-1-y[i]; } reverse(h.begin(),h.end()); solve(h,x,y); return 0; }