#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define bs binary_search
#define fi first
#define se second

#define int long long

using pii = pair<int,int>;
using ll = long long;
const int maxn = 2e5;
const int inf = 1e18;
const int mod = 1e9+7;

void open_io() { freopen("in","r",stdin); freopen("out","w",stdout); }
void fast_io() { ios::sync_with_stdio(0); cout.tie(0);  cin.tie(0);  }

int n,m;
int cost[maxn+5];
int ans[maxn+5];
int maxval[maxn+5];
vector<int> adj[maxn+5];
bool vis[maxn+5];

void dfs(int u)
{
	vis[u] = 1;
	ans[u] = maxval[u] = -inf;
	for(int v : adj[u])
	{
		if(not vis[v]) dfs(v);
		maxval[u] = max({maxval[u],cost[v],maxval[v]});
	}
	ans[u] = maxval[u] - cost[u];
}

int solve()
{
	int ret = -inf;
	for(int i=1;i<=n;i++)
		if(not vis[i])
			dfs(i);
	for(int i=1;i<=n;i++)
		ret = max(ret,ans[i]);
	return ret;
}

int32_t main () 
{
	//open_io();
	fast_io();
	cin >> n >> m;
	for(int i=1;i<=n;i++) cin >> cost[i];
	while(m--)
	{
		int a,b;
		cin >> a >> b;
		adj[a].pb(b);
	}
	cout << solve() << endl;
}
#include<bits/stdc++.h>
#define For(i,a,b) for(register int i=(a);i<=(b);++i)
#define pb push_back
#define pr pair<int,int>
#define fi first
#define se second
#define LL long long
#define mk(a,b) make_pair(a,b)
using namespace std;
int read(){
	char x=getchar(); int u=0,fg=0;
	while(!isdigit(x)){ if(x=='-') fg=1; x=getchar(); }
	while(isdigit(x)){ u=(u<<3)+(u<<1)+(x^48); x=getchar(); }
	return fg?-u:u;
}
const int maxx=1e5+10;
int n,m,sum=0,cnt=0,mx,ans=1e9;
int w[10],tmp[10],s[10],dis[10],v[maxx],l[maxx];
int p[10]={0,1,2,3,4,5,6,7,8};
pr a[maxx];
bool cmp(pr x,pr y){
	if(x.fi!=y.fi) return x.fi<y.fi;
	return x.se>y.se;
}
int iso(int x){
	x=lower_bound(v+1,v+cnt+1,x)-v-1;
	return l[x];
}
int solve(){
	s[0]=0;
	For(i,1,n){ tmp[i]=w[p[i]]; s[i]=s[i-1]+tmp[i];  dis[i]=0;}
	For(i,2,n) For(j,1,i-1){
		dis[i]=max(dis[i],dis[j]+iso(s[i]-s[j-1]));
		if(dis[i]>=ans) return dis[i];
	}
	return dis[n];
}
	
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif
	int u1,u2;
	n=read(); m=read();
	For(i,1,n){ w[i]=read(); mx=max(w[i],mx);}
	For(i,1,m){
		u1=read(); u2=read();
		a[i]=mk(u2,u1);
		if(u2<mx){ printf("-1\n"); return 0; }
	}
	sort(a+1,a+m+1,cmp);
	v[++cnt]=a[1].fi; l[cnt]=a[1].se;
	For(i,2,m)
		if(a[i].se>l[cnt]){
			v[++cnt]=a[i].fi;
			l[cnt]=a[i].se;
		}
	do{
		ans=min(ans,solve());
	}while(next_permutation(p+1,p+n+1));
	printf("%d\n",ans);
	return 0;
}

