#include <bits/stdc++.h>
int dx4[] = {0, 0, -1, 1};
int dy4[] = {-1, 1, 0, 0};
bool valid(int r, int c, int x, int y) {
  if (x >= 1 && x <= r && y >= 1 && y <= c) return 1;
  return 0;
}
using namespace std;
long long int a[1000005];
long long int n, m;
long long int tot;
long long int b[200005];
vector<long long int> val[500005];
long long int BS(long long int mid) {
  long long int taka;
  long long int on = 0;
  for (int i = 1; i <= n; i++) b[i] = a[i];
  taka = mid;
  for (long long int i = mid; i >= 1; i--) {
    if (val[i].size() == 0) {
      if (taka >= i) taka--;
      continue;
    }
    for (int j = 0; j < val[i].size(); j++) {
      long long int p = min(taka, b[val[i][j]]);
      taka -= p;
      on += p;
      b[val[i][j]] -= p;
    }
    if (taka >= i) taka--;
  }
  return ((tot - on) * 2LL) + (on);
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    tot += a[i];
  }
  long long int x, y;
  for (int i = 1; i <= m; i++) {
    cin >> x >> y;
    val[x].push_back(y);
  }
  long long int low = 0;
  long long int mid;
  long long int high = 2 * tot;
  long long int ans = 0;
  while (low <= high) {
    mid = ((high - low) / 2) + low;
    if (BS(mid) <= mid) {
      ans = mid;
      high = mid - 1;
    } else
      low = mid + 1;
  }
  cout << ans << endl;
  return 0;
}
