#include <bits/stdc++.h>
using namespace std;
long long int a[4000005], b[4000005], dp[500005];
vector<long long int> adj[500005], v[500005];
long long int check(long long int k, long long int tot, long long int n) {
  memset(dp, 0, sizeof(dp));
  for (long long int i = k; i > 0; i--) {
    for (long long int j = 0; j < (long long int)adj[i].size(); j++)
      dp[adj[i][j]] = max(dp[adj[i][j]], i);
  }
  for (long long int i = 1; i <= n; i++) v[dp[i]].push_back(i);
  long long int cnt = 0;
  for (long long int i = 1; i <= k; i++) {
    cnt++;
    for (long long int j = 0; j < (long long int)v[i].size(); j++) {
      long long int x = v[i][j];
      tot -= min(cnt, a[x]);
      cnt -= min(cnt, a[x]);
    }
  }
  for (long long int i = 0; i < 500005; i++) v[i].clear();
  if (cnt >= 2 * tot) return 1;
  return 0;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int n, m;
  cin >> n >> m;
  long long int tot = 0;
  for (long long int i = 1; i <= n; i++) cin >> a[i], tot += a[i];
  for (long long int i = 0; i < m; i++) {
    long long int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
  }
  long long int ans;
  long long int lo = 1, hi = 5e5;
  while (lo <= hi) {
    long long int mid = (lo + hi) / 2;
    if (check(mid, tot, n))
      hi = mid - 1, ans = mid;
    else
      lo = mid + 1;
  }
  cout << ans;
  cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
}
