#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int n, m, a[200005], tmp[200005], lt[200005];
struct dat {
  int x, y, nxt;
  bool operator<(const dat& T) const { return x < T.x; }
} sale[200005];
bool check(int v) {
  for (int i = 0; i < n; i++) tmp[i] = a[i];
  int money = 0;
  int p = 0;
  for (int i = 1; i <= v; i++) {
    money++;
    while (p < m && sale[p].x <= i) {
      if (sale[p].nxt > v) {
        int buy = min(money, tmp[sale[p].y]);
        tmp[sale[p].y] -= buy;
        money -= buy;
      }
      p++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (money < 2 * tmp[i]) return 0;
    money -= 2 * tmp[i];
  }
  return 1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    y--;
    sale[i].x = x;
    sale[i].y = y;
  }
  sort(sale, sale + m);
  memset(lt, -1, sizeof(lt));
  for (int i = m - 1; i >= 0; i--) {
    if (lt[sale[i].y] == -1)
      sale[i].nxt = INF;
    else
      sale[i].nxt = lt[sale[i].y];
    lt[sale[i].y] = sale[i].x;
  }
  int lb = 0;
  int ub = 400000;
  while (ub - lb > 1) {
    int mid = (lb + ub) / 2;
    if (check(mid))
      ub = mid;
    else
      lb = mid;
  }
  printf("%d\n", ub);
}
