#include <bits/stdc++.h>
using namespace std;
int m, n, ne[200005], tmp[200005], mid, sum, l(0), r(400005), z[200005];
bool b[200005];
char ch;
string s;
struct DZ {
  int day, stf;
  const bool operator<(const DZ &x) const { return this->day < x.day; }
} D[200005];
inline int RD() {
  char ich = getchar();
  int intmp = 0, insign = 1;
  while (((ich < '0') || (ich > '9')) && ((ich != '-'))) {
    ich = getchar();
  }
  if (ich == '-') {
    insign = -1;
    ich = getchar();
  }
  while ((ich <= '9') && (ich >= '0')) {
    intmp *= 10;
    intmp += ich - '0';
    ich = getchar();
  }
  return intmp * insign;
}
bool Check(const int &x) {
  for (register int i(1); i <= n; ++i) {
    tmp[i] = ne[i];
  }
  for (register int i(1); D[i].day <= x && i <= m; ++i) {
    z[D[i].stf] = D[i].day;
  }
  int Spt(0);
  for (register int i(1); D[i].day <= x && i <= m; ++i) {
    if (D[i].day <= x) {
      if (z[D[i].stf] == D[i].day) {
        if (tmp[D[i].stf]) {
          tmp[0] = min(tmp[D[i].stf], D[i].day - Spt);
          tmp[D[i].stf] -= tmp[0];
          Spt += tmp[0];
        }
      }
    }
  }
  for (register int i(1); i <= n; ++i) {
    Spt += (tmp[i]) << 1;
  }
  if (Spt > x) {
    return 0;
  }
  return 1;
}
int main() {
  n = RD();
  m = RD();
  memset(b, 1, sizeof(b));
  for (register int i(1); i <= n; ++i) {
    ne[i] = RD();
    if (ne[i] == 0) {
      b[i] = 0;
    }
    sum += ne[i];
  }
  for (register int i(1); i <= m; ++i) {
    D[i].day = RD();
    D[i].stf = RD();
    if (!b[D[i].stf]) {
      --i;
      --m;
    }
  }
  sort(D + 1, D + m + 1);
  while (l < r) {
    mid = (l + r) >> 1;
    if (Check(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  printf("%d\n", l);
  return 0;
}
