#include <bits/stdc++.h>
#include <math.h>
#include <chrono>
using namespace std;
#pragma GCC optimize("-O3")
#define endl "\n"
#define mp make_pair
#define st first
#define nd second
#define pii pair<int, int>
#define pb push_back
#define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0);
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FWD(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fwd(i, a, b) for (int i = (a); i < (b); ++i)
#define all(c) (c).begin(), (c).end()
#define sz(X) (int)((X).size())
#define what(x) cerr << #x << " is " << x << endl;

ostream &operator<<(ostream &out, string str)
{
    for (char c : str)
        out << c;
    return out;
}
template <class L, class R>
ostream &operator<<(ostream &out, pair<L, R> p)
{
    return out << "(" << p.st << ", " << p.nd << ")";
}
template <class T>
auto operator<<(ostream &out, T a) -> decltype(a.begin(), out)
{
    out << '{';
    for (auto it = a.begin(); it != a.end(); it = next(it))
        out << (it != a.begin() ? ", " : "") << *it;
    return out << '}';
}
void dump() { cerr << "\n"; }
template <class T, class... Ts>
void dump(T a, Ts... x)
{
    cerr << a << ", ";
    dump(x...);
}
#define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)

const int MAXM = 1e8 + 99;
const int silnia = 40320;
int n, m;

int V[10];
int maks[MAXM + 22];

int get(vector<int> &X, int x, int y)
{
    int s = 0;
    for (int i = x; i <= y; i++)
        s += V[X[i]];
    return maks[min(s, MAXM)];
}

int solve(vector<int> X)
{
    vector<int> dp(10);
    for (int i = n - 1; i >= 0; i--)
        for (int j = i + 1; j < n; j++)
            dp[i] = max(dp[j] + get(X, i, j), dp[i]);
    // debug(X, dp);
    return dp[0];
}

int32_t main()
{
    _upgrade;

    cin >> n >> m;
    rep(i, n) cin >> V[i];

    rep(i, m)
    {
        int l, v;
        cin >> l >> v;
        maks[v + 1] = max(l, maks[v + 1]);
    }

    rep(i, MAXM + 10) maks[i + 1] = max(maks[i], maks[i + 1]);

    vector<int> perm;
    rep(i, n) perm.push_back(i);

    rep(i, n) if (maks[V[i]] > 0)
    {
        //   debug(i, V[i], maks[V[i]]);
        cout << -1 << endl;
        exit(0);
    }
    int res = 1e9;

    do
    {
        res = min(res, solve(perm));
        // debug(perm);
    } while (next_permutation(all(perm)));

    cout << res << endl;
}
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;

const int M = 1e5+5;
int n,m;
int w[10];
int sums[M],mw[M];
int main(){
	cin >> n >> m;
	int maxw = 0;
	for(int i = 0;i < n;i++){
		cin >> w[i];
		maxw = max(maxw,w[i]);
	}
	int minv = 0x3f3f3f3f;
	for(int i = 0;i < 1<<n;i++){
		int x = 0;
		for(int j = 0;j < n;j++)
			if(i >> j & 1)x+=w[j];
		sums[i] = x;
	}
	for(int i = 0;i < m;i++){
		int a,b;
		cin >> a >> b;
		minv = min(minv,b);
		for(int s = 0;s < 1 << n;s++)
			if(sums[s] > b)
				mw[s] = max(mw[s],a);
	}
	
	if(maxw > minv){
		cout << "-1"<<endl;
		return 0;
	}
	int ans = 0x3f3f3f3f;
	vector<int>perm(n);
	iota(perm.begin(),perm.end(),0);
	do{
		vector<int>pos(n);
		for(int i = 0;i < n;i++){
			int x = 0;
			for(int j = i;j >= 0;j--){
				x|=1 << perm[j];
				pos[i] = max(pos[i],pos[j]+mw[x]);
			}
		}
		ans = min(ans,pos[n-1]);
	}while(next_permutation(perm.begin(),perm.end()));
	cout << ans << endl;
	return 0;
}