#include <bits/stdc++.h>
using namespace std;
vector<int> A, B, O[400000];
int tot;
bool solve(int l) {
  int m = l, sum = 0;
  B.clear();
  B.resize(A.size());
  for (int i = l - 1; i >= 0; i--) {
    if (m > i + 1) m--;
    for (auto o : O[i]) {
      int diff = min(A[o] - B[o], m);
      m -= diff;
      sum += diff;
      B[o] += diff;
    }
  }
  return 2 * (tot - sum) <= l - sum;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int N, M, s = 1, e = 400000;
  cin >> N >> M;
  A.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    tot += A[i];
  }
  while (M--) {
    int a, b;
    cin >> a >> b;
    O[--a].push_back(--b);
  }
  while (s < e) {
    int m = (s + e) >> 1;
    if (solve(m))
      e = m;
    else
      s = m + 1;
  }
  cout << s << '\n';
  return 0;
}
