#include <bits/stdc++.h>
using namespace std;
long long n, m;
vector<long long> a;
long long sum = 0;
vector<pair<long long, long long>> sp;
bool F(long long x) {
  long long burl = 1e9 + 7;
  vector<long long> h(n);
  long long kol = 0;
  for (long long i = m - 1; i >= 0; i--) {
    burl = min(sp[i].first, burl);
    if (h[sp[i].second] < a[sp[i].second] && burl && sp[i].first <= x) {
      long long t = min(burl, a[sp[i].second] - h[sp[i].second]);
      burl -= t;
      kol += t;
      h[sp[i].second] += t;
    }
  }
  return (kol + (x - kol) / 2 >= sum);
}
int32_t main() {
  cin >> n >> m;
  a.resize(n);
  sp.resize(m);
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  for (long long i = 0; i < m; i++) {
    cin >> sp[i].first >> sp[i].second;
    sp[i].second--;
  }
  sort(sp.begin(), sp.end());
  F(13);
  long long l = 0, r = 1e9 + 7;
  while (r - l > 1) {
    long long m = (r + l) / 2;
    if (F(m))
      r = m;
    else
      l = m;
  }
  cout << r;
}
