#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
inline int gi(){
	char tmp=getchar();int ans=0;
	int flag=1;
	while(!isdigit(tmp)){
		if(tmp=='-'){
			tmp=getchar();
			flag=-1;
			break;
		}
		tmp=getchar();
	}
	while(isdigit(tmp)){
		ans=ans*10+tmp-'0';
		tmp=getchar();
	}
	return ans*flag;
}
const int N = 201;
int S[N],R[N],F[N],Mark[N],L[N];
inline int Check(int l,int r){ // check if [l,r] is a "loop section".
	int range = ((l+r)>>1)-l+1;
	for(int i=l;i<l+range;++i){
		if(R[S[i]]) return 0;
		if(S[i]==0){
			if(R[S[i+range]])
				continue;
			if(!S[i+range])
				continue;
		}
		if(S[i]){
			if(L[S[i]])
				if(S[i+range]==0 || S[i+range] == S[i])
					continue;
			if(Mark[S[i]])
				if(S[i+range]==S[i])
					continue;
		}
		return 0;
	}
	return 1;
}
signed main()
{
#ifdef TSUKIAKIOI
	freopen("data.in","r",stdin);
#endif
	int n=gi();
	for(int i=1;i<=n;++i){
		int l,r;l=gi(),r=gi();
		if(S[l] || S[r]) return puts("No"),0;
		if(l>r && r!=-1) return puts("No"),0;
		if(~l&&~r){
			S[l]=i;S[r]=i;Mark[i]=1;
			continue;
		}
		if(l==-1 && r==-1)
			continue;
		if(l==-1){
			if(S[r]) return puts("No"),0;
			S[r]=i;R[i]=1;
		}
		if(r==-1){
			if(S[l]) return puts("No"),0;
			S[l]=i;L[i]=1;
		}
	}
	F[0]=1;
	Check(5,6);
	for(int i=2;i<=n<<1;i+=2){
		for(int j=0;j<i && !F[i];j+=2){
			if(!F[j]) continue;
			if(Check(j+1,i)){
				F[i] |= F[j];
			}
		}
	}
	if(F[n<<1])
		return puts("Yes"),0;
	return puts("No"),0;
}

#include<bits/stdc++.h>
using namespace std;
#include <numeric> 
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl "\n"
#define int long long int
#define MOD 1000000007 
#define INT_BITS 16
#define INF 1e18  
#define pi 3.141592
#define watch(x) cout<<(#x)<<"="<<(x)<<"\n" 
#define pb push_back
#define se second
#define fi first
#define pii pair<int,,int>
#define MAXN   1001
#define all(x) (x).begin(), (x).end()

#ifndef ONLINE_JUDGE
#define deb(x) cout<<#x<<" : "<<x<<"\n";
#define debug(x,y) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<"\n";
#define deb_arr(a,n) {cout<<#a<<":";for(int i=0;i<n;i++) {cout<<a[i]<<" ";} cout<<'\n';}
#define deb_mat(dist,n,m) {cout<<#dist<<"\n";for(int i=0;i<n;i++){for(int j=0;j<m;j++){cout<<dist[i][j]<<" ";}cout<<'\n';}}
#else
#define deb(x) ;
#define debug(x,y) ;
#define deb_arr(a,n) ;
#define deb_mat(dist,n,m) ;
#endif


//__builtin_popcount

int dx[] = { 1, 0, 0, -1};
int dy[] = {0, -1, 1, 0};

//=========================================================================================
int fact[300000], numinv[300000], factinv[300000];

int find_nCr(int n, int r)
{
    int ans = fact[n];
    ans = (ans * factinv[r]) % MOD;
    ans = (ans * factinv[n - r]) % MOD;
    return ans;
}

void factorial()
{
    fact[0] = 1;
      for (int i = 1; i < 300000; i++)
      {
          fact[i] = (fact[i - 1] * i) % MOD;
      }
      numinv[0] = 1;
      numinv[1] = 1;
      for (int i = 2; i < 300000; i++)
          numinv[i] = (MOD - (MOD / i) * numinv[MOD % i] % MOD) % MOD;
   
      factinv[0] = factinv[1] = 1;
      for (int i = 2; i < 300000; i++)
          factinv[i] = (numinv[i] * factinv[i - 1]) % MOD;
}


//=======================================================================================

const int N = 200004;
int incoming[N];
vector<int>v[N];
int n,m;
vector<int>temp;
int a[N];
int b[N];



void topo()
{
    for(int i=0;i<n;i++)
    {
      if(incoming[i]==0)
      {
        //q.push(i);
        temp.push_back(i);
      }
    }
}

int32_t main()
{
   #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    #endif
   
    
    cin>>n>>m;

    
    for(int i=0;i<n;i++)
    {
      b[i]=INF;
      cin>>a[i];
    }
    

    for(int i=0;i<m;i++)
    {
      int x,y;
      cin>>x>>y;
      x--;
      y--;
      v[x].pb(y);
      incoming[y]++;
    }
    topo();
    set<int>q;
    int vis[n]={0};
   for(auto it:temp)
   {
      b[it]=INF;
      q.insert(it);
      vis[it]=1;
      //watch(it);
   }

   int ans=INT_MIN;

   while(!q.empty())
   {
      int x = *(q.begin());
      q.erase(q.begin());
      //vis[x]=0;
      for(auto it:v[x])
      {

        ans=max(a[it]-min(a[x],b[x]),ans);
        b[it]=min(min(a[x],b[x]),b[it]);
        if(vis[it]==0)
          q.insert(it);
        vis[it]=1;
      }
   }

   cout<<ans<<endl;
   

return 0;


}