#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long powmod(long long a, long long b, long long MOD) {
  long long ans = 1;
  while (b) {
    if (b % 2) ans = ans * a % MOD;
    a = a * a % MOD;
    b /= 2;
  }
  return ans;
}
const int N = 5e5 + 11;
int n, m, a[N];
struct uzi {
  int t, d;
} p[N];
int b[N], sum, c[N];
vector<int> v[N];
int C(int x) {
  sum = 0;
  int now = 0;
  for (int i = 1; i <= n; i++) b[i] = a[i], sum += a[i], c[i] = 0, v[i].clear();
  for (int i = 1; i <= m; i++)
    if (p[i].t <= x) c[p[i].d] = max(c[p[i].d], p[i].t);
  for (int i = 1; i <= m; i++)
    if (p[i].t <= x && c[p[i].d] == p[i].t) v[p[i].t].push_back(p[i].d);
  for (int i = 1; i <= x; i++) {
    now++;
    for (auto k : v[i]) {
      int d = min(now, b[k]);
      now -= d;
      b[k] -= d;
      sum -= d;
    }
  }
  return now >= 2 * sum;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= m; i++) cin >> p[i].t >> p[i].d;
  int l = 0, r = 400000 + 4, ans = 0;
  while (l <= r) {
    int mid = l + r >> 1;
    if (C(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  cout << ans << endl;
  return 0;
}
