// Enjoy your stay. Code by evima

#include <bits/stdc++.h>
using namespace std;

using ld = long double;
using ll = long long;
using lll = __int128;
using vl = vector<ll>;
using LOOPVAR_TYPE = ll;

#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))
#define sz(x) ll((x).size())
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define rep1(i, n) rep2(i, 0, n)
#define rep2(i, a, b) for(LOOPVAR_TYPE i = LOOPVAR_TYPE(a); i < LOOPVAR_TYPE(b); i++)
#define rep(...) GET_MACRO(__VA_ARGS__, rep2, rep1)(__VA_ARGS__)

#define eb emplace_back
#define fir first
#define sec second
#define mp make_pair
#define mt make_tuple

const ld EPS = 1e-9;
const ld PI = 3.14159265358979323846L;
const ll INF = 1070000000LL;
const ll MOD = 998244353LL;// 1000000007LL;

void fast_io() { cin.tie(NULL); ios_base::sync_with_stdio(false); }
ll lin() { ll x; cin >> x; return x; }
string input() { string s; cin >> s; return s; }
vl vin(int n) { vector<ll> v(n); rep(i, n) cin >> v[i]; return v; }
template<class T> bool chmin(T& a, const T& b) { return (b < a) ? (a = b, true) : false; }
template<class T> bool chmax(T& a, const T& b) { return (a < b) ? (a = b, true) : false; }
template<class T> map<T, ll> freq(const vector<T>& v){ map<T, ll> ret; for(T x: v) ++ret[x]; return ret; }
template<class T> vector<T> reversed(vector<T> v) { reverse(all(v)); return v; }
template<class T> vector<T> sorted(vector<T> v) { sort(all(v)); return v; }
template<class T> vector<T> sub(const vector<T>& v, int from, int to) { vector<T> ret; copy(&v[from], &v[to], back_inserter(ret)); return ret; }
template<class T> string str(const T& x, int prec = 20) { stringstream ss; ss << setprecision(prec) << x; return ss.str(); }
template<class T> string str(const vector<T>& v, int prec = 20) { stringstream ss; ss << setprecision(prec); rep(i, sz(v)) ss << v[i] << (i < sz(v) - 1 ? " " : ""); return ss.str(); }
template<class T> void print1(T&& x, const string& end) { cout << str(x) << end; }
void print() { print1("", "\n"); }
template<class T, class... U> void print(T&& head, U&&... tail) { print1(head, " "); print(forward<U>(tail)...); }
template<class T> void eprint1(T&& x, const string& end) { cout << str(x) << end; }
void eprint() { eprint1("", "\n"); }
template<class T, class... U> void eprint(T&& head, U&&... tail) { eprint1(head, " "); eprint(forward<U>(tail)...); }
template<class... T> void quit(T&&... x) { print(forward<T>(x)...); exit(0); }
void yn(bool cnd, const string& yes = "Yes", const string& no = "No") { if(cnd) print(yes); else print(no); }
void zip(vl& v) { vl w = v; sort(all(w)); int n = unique(all(w)) - w.begin(); for(ll& x: v) x = lower_bound(&w[0], &w[n], x) - &w[0]; }
vl zipped(vl v) { zip(v); return v; }
map<ll, ll> zipmap(vl v) { map<ll, ll> ret; sort(all(v)); v.erase(unique(all(v)), v.end()); rep(i, sz(v)) ret[v[i]] = i; return ret; }

ll N, M, w[8], l[100010], v[100010];
pair<ll, ll> p[100010];

ll dp[8];
ll solve(vl u){
    fill(dp, dp + N, 0);
    rep(i, N){
        ll sum = u[i];
        rep(j, i + 1, N){
            sum += u[j];
            int pos = lower_bound(v, v + M + 1, sum) - v - 1;
            chmax(dp[j], dp[i] + l[pos]);
        }
    }
    return dp[N - 1];
}

void solveOne(){
    cin >> N >> M;
    rep(i, N) cin >> w[i];
    ll maxw = *max_element(w, w + N);
    rep(i, M){
        cin >> l[i] >> v[i];
        if(v[i] < maxw) quit(-1);
    }

    rep(i, M) p[i] = {v[i], l[i]};
    sort(p, p + M + 1);
    rep(i, M) chmax(p[i + 1].sec, p[i].sec);
    rep(i, M + 1) v[i] = p[i].fir, l[i] = p[i].sec;
    ll ans = INF;
    vl perm(N);
    rep(i, N) perm[i] = i;
    do{
        vl u(N);
        rep(i, N) u[i] = w[perm[i]];
        chmin(ans, solve(u));
    }while(next_permutation(all(perm)));
    print(ans);
}

int main(){
    fast_io();
    
    int num_tc = 1;
    // cin >> num_tc;
    
    
    
    rep(tc, 1, num_tc + 1){
        // cout << "Case #" << tc << ": " ;// << endl;
        solveOne();
    }
}
#include <random>
#include "bits/stdc++.h"
//#include <atcoder/all>

using namespace std;
// using namespace atcoder;

using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld eps = 1e-12;
constexpr int inf = numeric_limits<int>::max() / 2;
constexpr ll mod = 1e9 + 7;
mt19937_64 rnd{random_device()()};

template <typename T>
void printv(const vector<T> &v) {
  int sz = v.size();
  for (int i = 0; i < sz; i++) {
    cout << v[i] << " \n"[i == sz - 1];
  }
}

const int MAX_L = 100000010;
vector<int> cap(MAX_L, inf);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;
  vector<int> w(n);
  for (int i = 0; i < n; i++) {
    cin >> w[i];
  }
  sort(w.begin(), w.end());
  for (int i = 0; i < m; i++) {
    int l, v;
    cin >> l >> v;
    cap[l - 1] = min(cap[l - 1], v);
  }
  for (int i = MAX_L - 2; i >= 0; i--) {
    cap[i] = min(cap[i], cap[i + 1]);
  }
  {
    auto ma = *max_element(w.begin(), w.end());
    if (cap[0] < ma) {
      cout << -1 << endl;
      return 0;
    }
  }
  ll ans = 1e15;
  do {
    vector<vector<ll>> sum(n, vector<ll>(n));
    for (int i = 0; i < n; i++) {
      for (int j = i; j < n; j++) {
        for (int k = i; k <= j; k++) sum[i][j] += w[k];
      }
    }
    vector<vector<ll>> dp(n, vector<ll>(n, 0));
    for (int len = 1; len <= n - 1; len++) {
      for (int x = 0; x + len < n; x++) {
        int y = x + len;
        ll mi = 0;
        for (int k = x + 1; k < y; k++) {
          mi = max(mi, dp[x][k] + dp[k][y]);
        }
        auto itr = lower_bound(cap.begin(), cap.end(), sum[x][y]);
        int pos = distance(cap.begin(), itr);
        dp[x][y] = max(mi, ll(pos));
      }
    }
    ans = min(ans, dp[0][n - 1]);
  } while (next_permutation(w.begin(), w.end()));
  cout << ans << endl;
}
