#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const long long N = 2e5 + 5;
const long long mod = 1000000007;
long long n, m;
long long K[N];
long long a[N];
vector<long long> adj[N];
long long mpp[N], mp[N];
bool solve(long long mid) {
  for (long long i = 1; i <= n; i++) {
    a[i] = K[i];
    mpp[i] = 0;
    mp[i] = 0;
  }
  long long total = 0;
  for (long long i = 1; i <= min(mid, N - 1); i++) {
    for (auto it : adj[i]) {
      mpp[it]++;
    }
  }
  for (long long i = 1; i <= min(mid, N - 1); i++) {
    total++;
    for (auto it : adj[i]) {
      mp[it]++;
      if ((mp[it] == mpp[it]) && (total > 0) && (a[it] > 0)) {
        long long zx = min(a[it], total);
        total -= zx;
        a[it] -= zx;
      }
    }
  }
  total += max(mid - N + 1, 0LL);
  long long rem = 0;
  for (long long i = 1; i <= n; i++) {
    rem += a[i];
  }
  if (rem * 2 <= total) {
    return true;
  } else {
    return false;
  }
}
void testCase() {
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) {
    cin >> K[i];
  }
  for (long long i = 0; i < m; i++) {
    long long d, t;
    cin >> d >> t;
    adj[d].push_back(t);
  }
  long long left = 1, right = 2 * N + 5, ans = -1;
  while (left <= right) {
    long long mid = (left + right) / 2;
    if (solve(mid)) {
      ans = mid;
      right = mid - 1;
    } else {
      left = mid + 1;
    }
  }
  cout << ans << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) testCase();
  return 0;
}
