#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int i, n, m, d, t;
  cin >> n >> m;
  vector<int> k(n + 1);
  vector<vector<int> > x(400001);
  vector<int> last(n + 1);
  int sum = 0;
  for (i = 1; i <= n; i++) {
    cin >> k[i];
    sum += 2 * k[i];
  }
  for (i = 0; i < m; i++) {
    cin >> d >> t;
    x[d].push_back(t);
  }
  int l = 1;
  int r = 400000;
  while (l <= r) {
    int mid = (l + r) / 2;
    last.resize(n + 1);
    for (i = 1; i <= mid; i++)
      for (int e = 0; e < x[i].size(); e++) last[x[i][e]] = i;
    int c = 0;
    int ss = sum;
    vector<int> temp(k);
    for (t = 1; t <= mid; t++) {
      c++;
      for (int e = 0; e < x[t].size(); e++)
        if (last[x[t][e]] == t) {
          int a = min(temp[x[t][e]], c);
          c -= a;
          temp[x[t][e]] -= a;
          ss -= 2 * a;
        }
    }
    if (c >= ss)
      r = mid - 1;
    else
      l = mid + 1;
    last.clear();
  }
  cout << l;
  return 0;
}
