#include <bits/stdc++.h>
using namespace std;
long long k[200005];
long long d[200005];
long long n, m;
struct NODE {
  long long d, t;
};
NODE a[200005];
vector<long long> day[200005];
vector<long long> D[200005];
long long c[200005];
bool judge(long long mid) {
  for (long long i = 1; i <= n; i++) d[i] = k[i];
  for (long long i = 1; i <= mid; i++) day[i].clear();
  memset(c, 0, sizeof(c));
  for (long long i = 1; i <= min(mid, 200000ll); i++) {
    for (long long k : D[i]) c[k] = i;
  }
  for (long long i = 1; i <= n; i++) {
    if (c[i] == 0) continue;
    day[c[i]].push_back(i);
  }
  long long tmp = 0;
  for (long long i = 1; i <= mid; i++) {
    tmp++;
    if (i <= 200000) {
      for (long long k : day[i]) {
        long long v = min(d[k], tmp);
        d[k] -= v;
        tmp -= v;
      }
    } else {
      tmp += (mid - 200001);
      break;
    }
  }
  long long sum = 0;
  for (long long i = 1; i <= n; i++) sum += 2 * d[i];
  return sum <= tmp;
}
long long binary(long long l, long long r) {
  long long ans = 0;
  while (l <= r) {
    long long mid = (l + r) / 2;
    if (judge(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  return ans;
}
void solve() {
  scanf("%lld%lld", &n, &m);
  for (long long i = 1; i <= m; i++) D[i].clear();
  for (long long i = 1; i <= n; i++) scanf("%lld", &k[i]);
  for (long long i = 0; i < m; i++) {
    long long d, t;
    scanf("%lld%lld", &d, &t);
    D[d].push_back(t);
  }
  long long ans = binary(1, 400000);
  printf("%lld\n", ans);
}
int main() {
  solve();
  return 0;
}
