#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long llinf = (1LL << 62);
const int inf = (1 << 30);
const long long nmax = 5e5 + 50;
const int mod = 1e9 + 7;
using namespace std;
int n, m, d, t, l, r, ans, a[nmax], nr[nmax], i, mid;
vector<int> vc[nmax];
int ok(int x) {
  int v, i, j, mx = 0, k = 0;
  for (i = 1; i <= x; i++) nr[i] = 0;
  for (i = 1; i <= n; i++) {
    v = 0;
    for (j = 0; j < vc[i].size(); j++)
      if (vc[i][j] <= x) mx = vc[i][j], v = 1;
    if (!v)
      nr[x] += 2 * a[i];
    else
      nr[mx] += a[i];
  }
  for (i = 1; i <= x; i++) {
    k++;
    if (nr[i] <= k) {
      k -= nr[i];
      if (i == x) return 1;
    } else
      nr[i] -= k, k = 0, nr[x] += 2 * nr[i];
  }
  return 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cerr.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (i = 1; i <= n; i++) cin >> a[i];
  while (m--) {
    cin >> d >> t;
    vc[t].push_back(d);
  }
  for (i = 1; i <= n; i++) sort(vc[i].begin(), vc[i].end());
  l = 1, r = 5e5, ans = 5e5;
  while (l <= r) {
    mid = (l + r) / 2;
    if (ok(mid))
      r = mid - 1, ans = min(ans, mid);
    else
      l = mid + 1;
  }
  cout << ans << endl;
  return 0;
}
