#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U>
ostream& operator<<(ostream& o, const pair<T, U>& p) {
  o << "(" << p.first << "," << p.second << ")";
  return o;
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}
using pi = pair<int, int>;
int main() {
  int n, m;
  scanf(" %d %d", &n, &m);
  vector<int> k(n);
  int sumK = 0;
  for (int i = 0; i < (int)(n); ++i) {
    scanf(" %d", &k[i]);
    sumK += k[i];
  }
  vector<pi> v(m);
  for (int i = 0; i < (int)(m); ++i) {
    int d, t;
    scanf(" %d %d", &d, &t);
    v[i] = {d, t - 1};
  }
  auto check = [&](int x) {
    vector<int> last_sale(n);
    for (const auto& p : v)
      if (p.first <= x) last_sale[p.second] = max(last_sale[p.second], p.first);
    vector<vector<int>> sale(x + 1);
    for (int i = 0; i < (int)(n); ++i)
      if (last_sale[i] > 0) sale[last_sale[i]].push_back(i);
    int now = 0;
    vector<int> rem(k);
    for (int i = 1; i <= x; ++i) {
      ++now;
      for (int j : sale[i]) {
        int buy = min(now, rem[j]);
        rem[j] -= buy;
        now -= buy;
      }
    }
    for (int i = 0; i < (int)(n); ++i) {
      int cost = rem[i] * 2;
      if (now >= cost) {
        now -= cost;
        rem[i] = 0;
      }
      if (rem[i] > 0) return false;
    }
    return true;
  };
  int ng = 0, ok = 2 * sumK;
  while (ok - ng > 1) {
    int mid = (ok + ng) / 2;
    if (check(mid))
      ok = mid;
    else
      ng = mid;
  }
  printf("%d\n", ok);
  return 0;
}
