#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
template <class T>
istream &operator>>(istream &, vector<T> &);
template <class T>
ostream &operator<<(ostream &, const vector<T> &);
using ll = long long;
using vll = vector<long long>;
constexpr int INF = 0x3f3f3f3f;
constexpr ll BINF = 0x3f3f3f3f3f3f3f3fLL;
int solve() {
  int n, m;
  cin >> n >> m;
  vi k(n);
  cin >> k;
  const int MX = 400000;
  vector<vi> sale(MX);
  for (int i = 0; i < m; i++) {
    int d, t;
    cin >> d >> t;
    d--, t--;
    sale[d].push_back(t);
  }
  int tot = accumulate(k.begin(), k.end(), 0);
  auto check = [&](int mxd) {
    vi lst(n, -1);
    vector<vi> sale2(MX);
    for (int i = 0; i < mxd; i++)
      for (int item : sale[i]) lst[item] = i;
    for (int i = 0; i < n; i++)
      if (lst[i] != -1) sale2[lst[i]].push_back(i);
    int money = 0, nobuyyet = tot;
    vi nobuyk{k};
    for (int d = 0; d < mxd; d++) {
      money++;
      for (int saleItem : sale2[d]) {
        while (money > 0 && nobuyk[saleItem] > 0)
          money--, nobuyk[saleItem]--, nobuyyet--;
      }
    }
    return money >= 2 * nobuyyet;
  };
  int lo = 0, hi = MX;
  while (lo < hi) {
    int mi = (lo + hi) / 2;
    if (check(mi))
      hi = mi;
    else
      lo = mi + 1;
  }
  cout << lo << endl;
  return 0;
}
int main() {
  ios::sync_with_stdio(0);
  solve();
  return 0;
}
template <class T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto it = v.begin(); it != v.end(); ++it) is >> *it;
  return is;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto it = v.begin(); it != v.end();)
    os << *it, os << " \n"[++it == v.end()];
  return os;
}
