#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;

int main(){
	int n, m;
	cin>>n>>m;
	vector<int> w(n);
	rep(i, n) cin>>w[i];
	vector<P> lv;
	rep(i, m){
		int l, v;
		cin>>l>>v;
		lv.push_back(make_pair(l, v));
	}

	bool ng=false;

	int mnv=INT_MAX;
	for(P p : lv) mnv=min(mnv, p.second);
	rep(i, n) if(w[i]>mnv) ng=true;
	if(ng){
		cout<<(-1)<<endl;
		return 0;
	}


	sort(lv.begin(), lv.end());

	map<int, int> ma;
	for(P p : lv){
		int l=p.first, v=p.second;
		auto it=ma.lower_bound(v);
		vector<map<int, int>::iterator> dl;
		for(; it!=ma.end(); it++){
			dl.push_back(it);
		}
		for(auto it : dl) ma.erase(it);
		ma[v]=l;
	}

	// for(P p : ma) cout<<"ma"<< (p.first)<<" "<<(p.second)<<endl;
	auto mnl=[&](int w){
		auto it=ma.lower_bound(w);
		if(it==ma.begin()) return 0;
		else{
			it--;
			return it->second;
		}
	};

	vector<int> inds(n);
	rep(i, n) inds[i]=i;
	ll mn=LLONG_MAX;
	do{
		vector<ll> s(n+1);
		rep(i, n) s[i+1]=s[i]+w[inds[i]];
		// cout<<"w "; rep(i, n) cout<<w[inds[i]]<<" "; cout<<endl;
		// cout<<"s "; rep(i, n+1) cout<<s[i]<<" "; cout<<endl;
		vector<ll> dp(n);
		rep(i, n){
			rep(j, i){
				ll sw=s[i+1]-s[j];
				dp[i]=max(dp[i], dp[j]+mnl(sw));
				// cout<<"mn "<<i<<" "<<j<<" "<<mnl(sw)<<endl;
			}
		}
		mn=min(mn, dp[n-1]);
	} while(next_permutation(inds.begin(), inds.end()));

	cout<<mn<<endl;
	return 0;
}

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <bitset>
#include <string>
#include <cmath>
#include <complex>
#include <numeric>
#include <cassert>

#include <vector>
#include <array>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <utility>

#define PI 3.14159265358979323846
#define int64 long long
#define uint64 unsigned long long
#define coutfix(i) cout << fixed << setprecision(i)		//coutの浮動小数出力の桁数設定
using namespace std;

int main()
{
	int64 ss, pp, ans = 0;
	cin >> ss >> pp;
	for(int64 ii = 1;ii * ii <= pp;ii++)
	{
		int64 jj = ss - ii;
		if(ii * jj == pp)
		{
			if(ii == jj)
			{
				ans--;
			}
			ans += 2;
		}
	}
	if(ans == 0)
	{
		cout << "No" << endl;
	}
	else
	{
		cout << "Yes" << endl;
	}
	return 0;
}