#include <bits/stdc++.h>
using namespace std;
template <typename T>
void dbg(T t) {
  cerr << t << endl;
}
template <typename T, typename... Args>
void dbg(T t, Args... args) {
  cerr << t << " ";
  dbg(args...);
}
int t[200000], cp[200000];
set<int> sl[200001];
map<int, vector<int>> mm;
int n, m, sm = 0;
bool calc(int day) {
  int s = sm;
  int pv = 0;
  int curr = 0;
  auto it = mm.begin();
  while (it != mm.end() && it->first <= day) {
    curr += it->first - pv;
    pv = it->first;
    vector<int> q;
    for (int aa : it->second) {
      auto it2 = sl[aa].upper_bound(day);
      it2 = prev(it2);
      if (*it2 == it->first) q.push_back(aa);
    }
    for (int bb : q) {
      int poss = min(cp[bb], curr);
      curr -= poss;
      cp[bb] -= poss;
      s -= poss;
    }
    it++;
  }
  curr += day - pv;
  return s * 2 <= curr;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ;
  int a, b;
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> t[i], sm += t[i];
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    mm[a].push_back(b - 1);
    sl[b - 1].insert(a);
  }
  int l = 1, r = sm << 1, mid, ans;
  while (l <= r) {
    memcpy(cp, t, n * sizeof(int));
    mid = (l + r) >> 1;
    if (calc(mid)) {
      r = mid - 1;
      ans = mid;
    } else {
      l = mid + 1;
    }
  }
  cout << ans << endl;
  return 0;
}
