#include <bits/stdc++.h>
using namespace std;
const int mxn = 2e5 + 5, N = 8e1 + 5, mod = 1e9 + 7, mod2 = 998244353,
          inf = 0x3f;
void sfsf() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.tie(nullptr);
}
int n, m;
int arr[mxn], curr[mxn];
vector<int> vi1[mxn], vi2[mxn];
bool solve(int x) {
  for (int i = 1; i <= n; i++) curr[i] = arr[i];
  int c1 = x, c2 = x;
  for (int i = x; i >= 1; i--) {
    c2 = min(c2, i);
    if (i <= 200000) {
      while (c2 && !vi1[i].empty()) {
        int pp = vi1[i].back();
        int p = min(c2, curr[pp]);
        curr[pp] -= p;
        c2 -= p;
        c1 -= p;
        if (curr[pp] == 0) vi1[i].pop_back();
      }
    }
  }
  for (int i = 1; i <= n; i++) c1 -= curr[i] * 2;
  return (c1 >= 0);
}
int main() {
  sfsf();
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> arr[i];
  for (int i = 0; i < m; i++) {
    int d, t;
    cin >> d >> t;
    vi1[d].push_back(t);
    vi2[d].push_back(t);
  }
  int st = 1, en = mxn * 2, mid, ans = -1;
  while (st <= en) {
    mid = (st + en) / 2;
    for (int i = 1; i <= min(mid, mxn - 2); i++) vi1[i] = vi2[i];
    if (solve(mid)) {
      en = mid - 1;
      ans = mid;
    } else
      st = mid + 1;
  }
  cout << ans;
  return 0;
}
