#include <bits/stdc++.h>
const int N = 500500;
const int Q = 1 << 21;
const long long mod = 998244353;
using namespace std;
int n;
int m;
int a[N];
int b[N];
vector<int> g[N];
vector<int> v[N];
bool can(int x) {
  for (int i = 0; i < N; i++) {
    v[i].clear();
  }
  for (int i = 1; i <= n; i++) {
    b[i] = a[i];
    int mx = 0;
    for (int y : g[i]) {
      if (y <= x) {
        mx = max(mx, y);
      }
    }
    v[mx].push_back(i);
  }
  int money = 0;
  for (int i = 1; i <= x; i++) {
    money++;
    for (int y : v[i]) {
      int z = min(money, b[y]);
      money -= z;
      b[y] -= z;
    }
  }
  for (int i = 1; i <= n; i++) {
    money -= b[i] * 2;
  }
  return money >= 0;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    g[y].push_back(x);
  }
  int l = 1, r = 4e5 + 100;
  while (l < r) {
    int mid = (l + r) / 2;
    if (can(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l;
}
