/*************************************************************************
	> File Name: 1.cpp
	> Author: Knowledge_llz
	> Mail: 925538513@qq.com 
	> Blog: https://blog.csdn.net/Pig_cfbsl 
	> Created Time: 2020/10/11 22:45:26
 ************************************************************************/
#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;
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]));
//	cout<<dis[3]<<endl;
	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;
		}
	int ans=1e9;
	do{
		ans=min(ans,solve());
//		return 0;
	}while(next_permutation(p+1,p+n+1));
	printf("%d\n",ans);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define zep(i,m,n) for(ll i = (m); i < (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(int i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;

vector<vector<P>> g;
ll a[100 * 100], b[100 * 100];
bool use[100 * 100], ans[100 * 100], usev[100];

void dfs(ll at){
    usev[at] = true;
    
    zep(i, 0, g[at].size()){
        auto nx = g[at][i];
        if(!usev[nx.first]){
            if(!use[nx.second]){
                use[nx.second] = true;
                ans[nx.second] = (a[nx.second] == at);
            }
            dfs(nx.first);
        }else{
            if(!use[nx.second]){
                use[nx.second] = true;
                ans[nx.second] = (a[nx.second] == at);
            }
        }
    }
}

int main(){
    cin.tie(0); ios::sync_with_stdio(false);
    
    ll n, m; cin >> n >> m;
    zep(j, 0, m){cin >> a[j] >> b[j]; a[j]--; b[j]--;}
    ll c[n]; zep(i, 0, n)cin >> c[i];
    
    g.resize(n);
    zep(j, 0, m){
        if(c[a[j]] > c[b[j]]){
            ans[j] = true;
        }else if(c[a[j]] < c[b[j]]){
            ans[j] = false;
        }else{
            g[a[j]].push_back(P(b[j], j));
            g[b[j]].push_back(P(a[j], j));
        }
    }

    zep(i, 0, n)dfs(i);

    zep(j, 0, m){
        print(ans[j]? "->" : "<-")
    }

    return 0;
}