#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,mmx,avx")
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
#define PB push_back
#define MP make_pair
#define endl "\n"
#define fst first
#define snd second
const int UNDEF = -1;
const int INF=1<<30;
template<typename T> inline bool chkmax(T &aa, T bb) { return aa < bb ? aa = bb, true : false; }
template<typename T> inline bool chkmin(T &aa, T bb) { return aa > bb ? aa = bb, true : false; }
typedef pair<ll,ll> pll;typedef vector<ll> vll;typedef pair<int,int> pii;typedef vector<int> vi;typedef vector<vi> vvi;typedef vector<pii> vpii;typedef vector<pll> vpll;
template<typename T> void makeunique(vector<T> &vx) {sort(vx.begin(),vx.end());auto it=unique(vx.begin(),vx.end());vx.resize(std::distance(vx.begin(),it));}

//If w[L,R] >= v[i], then dist[L,R] >= len[i]

int f[1<<8];
#define FORSSI(sub, full) for(int sub = 0 ;sub!=(full)  ; sub = (sub - (full)) & (full))

int hquery(map<int,int> &h, int key) {
  //Queries the largest (k,v) pair where k<=key
  auto it=h.upper_bound(key);
  if (it==h.begin()) {
    return 0;
  }
  --it;
  return it->snd;
}

void hins(map<int,int> &h, int key, ll val) {
  //Inserts (key,val) into h.
  //Maintains the invariant that if key1<key2, then val1<val2.
  auto it=h.upper_bound(key);
  if (it==h.begin()) {
    it=h.insert(MP(key,val)).fst;
  }
  else {
    --it;
    if (it->snd >= val) return;
    if (it->fst==key) it->snd = val;
    else it=h.insert(MP(key,val)).fst;
  }
  ++it;
  while(it!=h.end()) {
    if (it->snd <= val) it=h.erase(it);
    else return;
  }
}

ll solve(long long N, long long M, std::vector<long long> w, std::vector<long long> vlen, std::vector<long long> v){
  {
    ll wmax=0; for (auto &x:w) chkmax(wmax,x);
    ll vmin=LLONG_MAX; for (auto &x:v) chkmin(vmin,x);
    if (wmax>vmin) return -1;
  }
  map<int,int> h;
  for (int i=0;i<M;i++) hins(h,v[i],vlen[i]);
  vi perm; for (int i=0;i<N;i++) perm.PB(i);
  int final=INT_MAX;
  do {
    memset(f,0,sizeof f);
    for (int l=0;l<N;l++) {
      int sumw=w[perm[l]];
      int sig=0;
      for (int r=l+1;r<N;r++) {
        sumw+=w[perm[r]];
        int dist=hquery(h,sumw-1);
        sig|=1<<(r-1);
        //if(dist) printf("l:%d r:%d sig:%d dist:%d\n",l,r,sig,dist);
        f[sig]=dist;
      }
    }
    int lim=1<<N;
    for (int x=3;x<lim;x++) {
      int best=f[x];
      FORSSI(y,x) {
        chkmax(best,f[y]+f[x^y]);
      }
      //if(best) printf("x:%d best:%d\n",x,best);
      f[x]=best;
    }
    chkmin(final,f[lim-1]);
  } while(next_permutation(perm.begin(),perm.end()));
  return final;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main(){
    long long N;
    scanf("%lld",&N);
    long long M;
    scanf("%lld",&M);
    std::vector<long long> w(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&w[i]);
    }
    std::vector<long long> l(M);
    std::vector<long long> v(M);
    for(int i = 0 ; i < M ; i++){
        scanf("%lld",&l[i]);
        scanf("%lld",&v[i]);
    }
    ll ans=solve(N, M, std::move(w), std::move(l), std::move(v));
    printf("%lld\n",ans);
    return 0;
}

// RioTian 21/03/24
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
    ios_base::sync_with_stdio(false), cin.tie(0);
    int n;
    cin >> n;
    vector<pair<ll, ll>> towns;
    ll sa = 0, sb = 0;  // T,A
    for (int i = 0; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        towns.emplace_back(a, b);
        sa += a;
    }
    sort(towns.begin(), towns.end(), [](pair<ll, ll>& p, pair<ll, ll>& q) {
        return p.first * 2 + p.second > q.first * 2 + q.second;
    });
    for (int i = 0; i < n; ++i) {
        if (sa < sb) {
            cout << i << "\n";
            return 0;
        }
        sa -= towns[i].first, sb += towns[i].first + towns[i].second;
    }
    cout << n << "\n";
    return 0;
}