#include <bits/stdc++.h>
using namespace std;
constexpr int maxn = 2e5 + 10;
int a[maxn], b[maxn], t, d;
int n, m, sum = 0;
struct node {
  int t, id;
  bool operator<(const node &x) const { return t > x.t; }
} A[maxn];
bool ok(int T) {
  int limid = T, k = T;
  for (int i = 1; i <= n; i++) b[i] = a[i];
  for (int i = 1; i <= m; i++) {
    while (b[A[i].id] && A[i].t <= limid && k) {
      if (A[i].t < k) k = A[i].t;
      b[A[i].id]--;
      T--;
      k--;
    }
  }
  for (int i = 1; i <= n; i++) T -= b[i] * 2;
  if (T >= 0) return true;
  return false;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), sum += a[i];
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", &t, &d);
    A[i].t = t;
    A[i].id = d;
  }
  sort(A + 1, A + 1 + m);
  int l = 1, r = sum * 2, mid;
  while (l < r) {
    mid = (r + l) / 2;
    if (ok(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  printf("%d\n", l);
  return 0;
}
