#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
const int mod = (1e9) + 7, mod2 = 998244353;
const double eps = 1e-10;
const int siz = 4e5 + 5, siz2 = 21, lg = 31, block = 317, block2 = 1000,
          mxv = 1e6;
int n, m, k[siz], last[siz], ktmp[siz];
vector<int> dtot[siz];
bool check(int mid) {
  for (int i = 1; i <= n; i++) {
    ktmp[i] = k[i];
  }
  for (int i = 1; i <= mid; i++) {
    for (int t : dtot[i]) {
      last[t] = i;
    }
  }
  int lft = 0;
  for (int i = 1; i <= mid; i++) {
    lft++;
    for (int t : dtot[i]) {
      if (last[t] != i) {
        continue;
      }
      int b = min(lft, ktmp[t]);
      lft -= b;
      ktmp[t] -= b;
    }
  }
  for (int i = 1; i <= n; i++) {
    lft -= 2 * ktmp[i];
  }
  return lft >= 0;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &k[i]);
  }
  for (int i = 0; i < m; i++) {
    int d, t;
    scanf("%d%d", &d, &t);
    dtot[d].push_back(t);
  }
  int l = 1, r = 400000;
  while (l != r) {
    int mid = (l + r) / 2;
    if (check(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  printf("%d\n", l);
  return 0;
}
