#include <bits/stdc++.h>
using namespace std;
int req[1000006], tot;
vector<int> day[1000006 + 100];
int used[1000006 + 100];
vector<int> lastDay[1000006 + 100];
int n, m;
bool solve(int dd) {
  memset(used, -1, sizeof(used));
  for (int i = int(dd); i >= int(0); i--) {
    lastDay[i].clear();
    for (auto& x : day[i]) {
      if (used[x] == -1) lastDay[i].push_back(x), used[x] = 0;
    }
  }
  int coin = 0, cnt = 0;
  for (int i = int(1); i < int(dd + 1); i++) {
    coin++;
    for (auto& x : lastDay[i]) {
      while (coin and used[x] < req[x]) coin--, cnt++, used[x]++;
    }
  }
  return coin >= (tot - cnt) * 2;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = int(0); i < int(n); i++) scanf("%d", &req[i]), tot += req[i];
  while (m--) {
    int d, x;
    scanf("%d %d", &d, &x);
    day[d].push_back(x - 1);
  }
  int lo = 0, hi = 1000006 * 2, ans;
  while (lo <= hi) {
    int mid = (lo + hi) >> 1;
    if (solve(mid))
      ans = mid, hi = mid - 1;
    else
      lo = mid + 1;
  }
  printf("%d\n", ans);
  return 0;
}
