#include <bits/stdc++.h>
using namespace std;
long long final[200005], a[200005], d[200005], t[200005], n, m, aasli[200005];
vector<long long> sale[1000005];
bool can(long long day) {
  for (long long i = 1; i <= 1000000; i++) sale[i].clear();
  memset(final, 0, sizeof(final));
  for (long long i = 1; i <= m; i++) {
    if (d[i] <= day) final[t[i]] = max(final[t[i]], d[i]);
  }
  for (long long i = 1; i <= n; i++) {
    a[i] = aasli[i];
    if (final[i] == 0) continue;
    sale[final[i]].push_back(i);
  }
  long long money = 0;
  for (long long i = 1; i <= day; i++) {
    money++;
    for (int j = 0; j < sale[i].size(); j++) {
      long long x = sale[i][j];
      if (money <= a[x]) {
        a[x] -= money;
        money = 0;
      } else {
        money -= a[x];
        a[x] = 0;
      }
    }
  }
  long long cnt = 0;
  for (long long i = 1; i <= n; i++) cnt += a[i];
  if (money >= 2 * cnt) return true;
  return false;
}
int main() {
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) cin >> aasli[i];
  for (long long i = 1; i <= m; i++) cin >> d[i] >> t[i];
  long long ki = 1, ka = 1000000, res;
  while (ki <= ka) {
    long long mid = (ki + ka) / 2;
    if (can(mid)) {
      res = mid;
      ka = mid - 1;
    } else
      ki = mid + 1;
  }
  cout << res << endl;
  return 0;
}
