#include <bits/stdc++.h>
#define rep(i, a, b)   for(int i = a; i <= b; ++i)
#define repr(i,a,b)    for(int i = a ; i >= b; --i)
#define NAME           "CAMEL"
#define pb             push_back
#define mp             make_pair
#define EL             cout << '\n'
#define cqtshadow      int main
#define fillchar(a,x)  memset(a, x, sizeof (a))
#define faster         ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;
typedef int64_t        i64;
typedef vector<int>    vi;
typedef vector<i64>    vl;
typedef pair<int, int> ii;
typedef pair<i64, i64> iil;
const i64 oo  =  1e18;
const int inf =  1e9;
const int mod =  1e9 + 7;
const int N   =  1e5 + 7;
const int M   =  1e6 + 7;
void file() {freopen ( NAME ".inp", "r", stdin ); freopen ( NAME ".out", "w", stdout ); freopen ( NAME ".err", "w", stderr);}

int n, m, w[N], l[N], v[N], p[N], H[1 << 8], s[1 << 8];
i64 ans = oo, dp[8];

bool checkbit(int mask, int x) {
        return ((mask >> x) & 1);
}

void onbit(int &mask, int x) {
        mask = (mask | (1 << x));
}

cqtshadow () {
//        file();
        faster
        cin >> n >> m;
        rep(i, 0, n - 1) cin >> w[i], p[i] = i;
        rep(i, 0, m - 1) cin >> l[i] >> v[i];

        if(*min_element(v, v + m) < *max_element(w, w + n)) return cout << -1, 0;

        int limit = (1 << n) - 1;

        // weight of every subset
        rep(mask, 1, limit)
        rep(i, 0, n - 1)
                if(checkbit(mask, i)) s[mask] += w[i];

        //calculate the at least separate length of every subset
        rep(mask, 1, limit)
        rep(i, 0, m - 1) {
                if(s[mask] > v[i])
                        H[mask] = max(H[mask], l[i]);
        }

        // go through all permutation
        //dp[i] is the "have to separate length" from 1 -> i

        do {
                memset(dp, 0, sizeof dp);
                rep(i, 0, n - 1) {
                        int mask = 0;
                        repr(j, i, 0) {
                                onbit(mask, p[j]);
                                dp[i] = max(dp[i], dp[j] + H[mask]);
                        }
                }
                ans = min(ans, dp[n - 1]);
        } while(next_permutation(p, p + n));

        cout << ans;
}

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

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

  int n, m;
  cin >> n >> m;
  
  vector<int> w(n);
  vector<pair<int, int>> p(m);
  
  int mn = INT_MAX;
  for (int i = 0; i < n; ++i) cin >> w[i];
  for (int i = 0; i < m; ++i) {
    cin >> p[i].second >> p[i].first;
    mn = min(mn, p[i].first);
  }
  
  if (*max_element(w.begin(), w.end()) > mn) {
    cout << -1 << '\n';
    return 0;
  }
  
  p.emplace_back(0, 0);
  p.emplace_back(1e9, 0);

  sort(p.begin(), p.end());
  sort(w.begin(), w.end());

  for (int i = 1; i < (int)p.size(); ++i) {
    p[i].second = max(p[i].second, p[i - 1].second);
  }
  
  auto getlen = [&](int wei) {
    int j = lower_bound(p.begin(), p.end(), make_pair(wei, 0)) - p.begin() - 1;
    return p[j].second;
  };

  auto solve = [&]() {
    vector<int> dist(n, 0);
    for (int i = 1; i < n; ++i) {
      int sum = w[i];
      for (int j = i - 1; j >= 0; --j) {
        sum += w[j];
        dist[i] = max(dist[i], getlen(sum) + dist[j]);
      }
    }

    return dist[n - 1];
  };
  
  int ans = solve();
  while (next_permutation(w.begin(), w.end())) {
    ans = min(ans, solve());
  }

  cout << ans << '\n';
}
