#include <bits/stdc++.h>
using namespace std;
int n, m, a[((int)1e6 + 10)], b[((int)1e6 + 10)], c[((int)1e6 + 10)];
vector<int> d[((int)1e6 + 10)];
bool vis[((int)1e6 + 10)];
vector<pair<int, int> > v;
bool check(int x) {
  for (int i = 0; i <= n; i++) {
    b[i] = a[i];
  }
  for (int i = 0; i <= 200000; i++) {
    d[i].clear();
    vis[i] = false;
  }
  for (int i = 0; i < v.size(); i++) {
    if (!vis[v[i].second] && v[i].first <= x) {
      vis[v[i].second] = true;
      d[v[i].first].push_back(v[i].second);
    }
  }
  int cnt = 0;
  for (int i = 1; i <= x; i++) {
    cnt++;
    for (int j = 0; j < d[i].size(); j++) {
      int mn = min(cnt, b[d[i][j]]);
      b[d[i][j]] -= mn;
      cnt -= mn;
    }
  }
  for (int i = 0; i <= n; i++) {
    cnt -= b[i] * 2;
  }
  return cnt >= 0;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    v.push_back(make_pair(x, y));
  }
  sort(v.begin(), v.end(), greater<pair<int, int> >());
  int s = 1, e = 400000;
  int ans = -1;
  while (s <= e) {
    int mid = s + (e - s) / 2;
    if (check(mid)) {
      ans = mid;
      e = mid - 1;
    } else {
      s = mid + 1;
    }
  }
  cout << ans << endl;
  return 0;
}
