#include <bits/stdc++.h>
using namespace std;
template <typename T>
string tos(T a) {
  stringstream ss;
  string ret;
  ss << a;
  ss >> ret;
  return ret;
}
void read(int &_a) { int temp = scanf("%d", &_a); }
void read(int &_a, int &_b) { int temp = scanf("%d %d", &_a, &_b); }
void read(int &_a, int &_b, int &_c) {
  int temp = scanf("%d %d %d", &_a, &_b, &_c);
}
const int SZ = 4e5 + 7;
int K[SZ], KS[SZ], N, SL[SZ];
vector<int> DS[SZ];
bool poss(int days) {
  for (int(x) = (0); (x) < (N); (x)++) KS[x] = K[x];
  for (int(x) = (0); (x) < (N); (x)++) SL[x] = 2147483647;
  for (int(d) = (1); (d) < (days + 1); (d)++) {
    int l = DS[d].size();
    for (int(x) = (0); (x) < (l); (x)++) {
      int t = DS[d][x];
      SL[t] = d;
    }
  }
  int coin = 0;
  for (int(d) = (1); (d) < (days + 1); (d)++) {
    coin += 1;
    int l = DS[d].size();
    for (int(x) = (0); (x) < (l); (x)++) {
      if (!coin) break;
      int t = DS[d][x];
      if (SL[t] != d) continue;
      int cc = min(coin, KS[t]);
      KS[t] -= cc;
      coin -= cc;
    }
  }
  for (int(x) = (0); (x) < (N); (x)++) {
    if (2 * KS[x] > coin) return false;
    coin -= 2 * KS[x];
  }
  return true;
}
int main() {
  int m;
  while (cin >> N >> m) {
    for (int(x) = (0); (x) < (SZ); (x)++) DS[x].clear();
    for (int(x) = (0); (x) < (N); (x)++) read(K[x]);
    int di, ti;
    for (int(x) = (0); (x) < (m); (x)++) {
      read(di, ti);
      ti--;
      DS[di].push_back(ti);
    }
    int lo = 1;
    int hi = SZ;
    int ans = 0;
    while (true) {
      if (hi - lo < 6) {
        for (int x = lo; x <= hi; x++) {
          if (poss(x)) {
            ans = x;
            break;
          }
        }
        break;
      }
      int m = (lo + hi) / 2;
      if (poss(m))
        hi = m;
      else
        lo = m;
    }
    cout << ans << endl;
  }
  return 0;
}
