#include <bits/stdc++.h>
using namespace std;
const long long maxn = 5e5 + 7;
long long n, m, d, t;
vector<long long> adj[maxn], k, curr;
void solve() {
  cin >> n >> m;
  k.resize(n);
  for (long long i = 0; i < n; ++i) {
    cin >> k[i];
  }
  for (long long i = 0; i < m; ++i) {
    cin >> d >> t;
    t--;
    adj[d].push_back(t);
  }
  long long l = 0, r = maxn, m;
  while (r - l > 1) {
    m = (l + r) / 2;
    curr = k;
    long long left = m, val = 0;
    for (long long i = m; i >= 0; --i) {
      if (left > i) {
        val += left - i;
        left = i;
      }
      for (long long v : adj[i]) {
        while (curr[v] && left) {
          curr[v]--, left--;
        }
      }
    }
    for (long long i : curr) {
      val -= 2 * i;
    }
    if (val < 0) {
      l = m;
    } else {
      r = m;
    }
  }
  cout << l + 1 << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
