#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll inf = 1e6;
const int N = 2 * 1e6 + 10;
ll n, m;
ll a[N], b[N];
vector<ll> d[N];
bool ok(ll k) {
  ll money = k, extra = 0;
  memset(b, 0, sizeof b);
  for (int i = k; i > 0; --i) {
    if (money > i) {
      extra += (money - i);
      money = i;
    }
    for (auto j : d[i]) {
      if (!b[j]) {
        if ((a[j] - b[j]) >= money) {
          b[j] = money;
          money = 0;
          break;
        }
        money -= a[j];
        b[j] = a[j];
      }
    }
  }
  extra += money;
  for (int i = 1; i <= n; ++i) {
    extra -= 2 * (a[i] - b[i]);
  }
  return money >= 0 && extra >= 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ll x, y;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < m; ++i) {
    cin >> x >> y;
    d[x].push_back(y);
  }
  ll lo = 0, hi = inf;
  while (lo + 1 < hi) {
    ll m = lo + hi >> 1;
    if (ok(m))
      hi = m;
    else
      lo = m;
  }
  cout << hi;
}
