#include <bits/stdc++.h>
using namespace std;
int n, m, k[200100], d[200100], t[200100];
int last[200100];
int sched[400100];
bool check(int x) {
  for (int i = (int)(0); i < (int)(200100); i++) last[i] = -1;
  for (int i = (int)(0); i < (int)(m); i++)
    if (d[i] <= x) last[t[i]] = max(last[t[i]], d[i]);
  int ksum = 0;
  for (int i = (int)(0); i < (int)(n); i++) ksum += k[i];
  for (int i = (int)(0); i < (int)(400100); i++) sched[i] = 0;
  for (int i = (int)(1); i < (int)(n + 1); i++)
    if (last[i] >= 0) sched[last[i]] += k[i - 1];
  int nec = 0;
  int has = 0;
  for (int i = (int)(1); i < (int)(x + 1); i++) {
    has++;
    int y = min(sched[i], has);
    has -= y;
    nec += y;
    ksum -= y;
  }
  nec += 2 * ksum;
  return nec <= x;
}
int main(void) {
  scanf("%d%d", &n, &m);
  for (int i = (int)(0); i < (int)(n); i++) scanf("%d", k + i);
  for (int i = (int)(0); i < (int)(m); i++) scanf("%d%d", d + i, t + i);
  int ps = 400010, fl = 0;
  while (ps - fl > 1) {
    int mid = (ps + fl) / 2;
    if (check(mid))
      ps = mid;
    else
      fl = mid;
  }
  printf("%d\n", ps);
}
