#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int inf = 1e9;
const long long llf = 1e18;
const int mod = 1e9 + 7;
const int maxn = 4e5 + 10;
int n, m;
int k[maxn], a[maxn];
vector<int> f[maxn];
bool check(int x) {
  int tot = x;
  for (int i = 1; i <= n; i++) a[i] = k[i];
  int res = 0;
  for (int i = x; i >= 0; i--) {
    res += tot - min(i, tot);
    tot = min(i, tot);
    for (int p : f[i]) {
      if (a[p]) {
        int ret = min(a[p], tot);
        a[p] -= ret;
        tot -= ret;
      }
    }
  }
  int ret = 0;
  for (int i = 1; i <= n; i++) ret += a[i];
  return res >= ret * 2;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> k[i];
  }
  for (int i = 1, a, b; i <= m; i++) {
    cin >> a >> b;
    f[a].push_back(b);
  }
  int l = 0, r = 4e5;
  while (l <= r) {
    int mid = l + r >> 1;
    if (check(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}
