#include <bits/stdc++.h>
inline long long sbt(long long x) { return __builtin_popcount(x); }
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
long long k[200003], k1[200003];
set<long long> adj[400013];
vector<long long> typ[400013];
long long n, m;
bool vis[200003];
bool f(long long days) {
  long long bache = 0;
  long long paise = 0;
  memset(vis, 0, sizeof vis);
  for (long long i = 1; i <= n; i++) k1[i] = k[i];
  for (long long i = 1; i <= days; i++) {
    typ[i].clear();
  }
  for (long long i = days; i > 0; i--) {
    for (auto ele : adj[i]) {
      if (vis[ele]) continue;
      vis[ele] = 1;
      typ[i].push_back(ele);
    }
  }
  for (long long i = 1; i <= days; i++) {
    paise++;
    if (!typ[i].size()) continue;
    for (auto ele : typ[i]) {
      long long prev = k1[ele];
      k1[ele] -= min(paise, prev);
      paise -= min(paise, prev);
    }
  }
  for (long long i = 1; i <= n; i++) bache += k1[i];
  return (2 * bache) <= paise;
}
int32_t main() {
  cin.sync_with_stdio(false);
  cout.sync_with_stdio(false);
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) cin >> k[i];
  for (long long i = 0; i < m; i++) {
    long long d, t;
    cin >> d >> t;
    adj[d].insert(t);
  }
  long long lo = 0;
  long long hi = 400013;
  long long res = -1;
  while (lo <= hi) {
    long long mid = (lo + hi) >> 1;
    if (f(mid)) {
      hi = mid - 1;
      res = mid;
    } else {
      lo = mid + 1;
    }
  }
  cout << res;
  return 0;
}
