#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
int n, m, req[maxn];
vector<int> offer[maxn];
bool can(int target) {
  vector<pair<int, int> > dlist;
  int taken[maxn], prev = 0, have = 0;
  memset(taken, 0, sizeof(taken));
  for (int i = 1; i <= n; i++) {
    if (req[i] == 0) continue;
    int ptr = upper_bound(offer[i].begin(), offer[i].end(), target) -
              offer[i].begin();
    ptr--;
    if (ptr >= 0) dlist.push_back({offer[i][ptr], i});
  }
  dlist.push_back({target, 0});
  sort(dlist.begin(), dlist.end());
  for (auto u : dlist) {
    have += (u.first - prev);
    prev = u.first;
    int cost = min(have, req[u.second] - taken[u.second]);
    taken[u.second] += cost;
    have -= cost;
  }
  for (int i = 1; i <= n; i++) {
    have -= 2 * (req[i] - taken[i]);
  }
  return have >= 0;
}
int bsrch(int l, int r) {
  if (l == r) return l;
  int mid = (l + r) / 2;
  if (can(mid))
    return bsrch(l, mid);
  else
    return bsrch(mid + 1, r);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> req[i];
  for (int i = 1; i <= m; i++) {
    int d, t;
    cin >> d >> t;
    offer[t].push_back(d);
  }
  for (int i = 1; i <= n; i++) sort(offer[i].begin(), offer[i].end());
  cout << bsrch(1, 5e5);
  return 0;
}
