#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll days = 2e5 + 10;
bool check(vector<int>& k, vector<vector<pair<int, int>>>& mp, ll cur,
           vector<vector<int>>& type) {
  vector<int> kcopy(k.size());
  ll s1 = 0;
  for (int i = 0; i < k.size(); i++) {
    s1 += k[i];
    kcopy[i] = k[i];
  }
  int no = 0;
  vector<int> d(days + 4, 0);
  for (int i = 0; i < k.size(); i++) {
    if (kcopy[i] != 0) {
      int m = type[i].size();
      if (m == 0) {
        no += kcopy[i];
        continue;
      }
      int last = type[i][0];
      for (int j = 0; j < m; j++) {
        if (type[i][j] <= cur) {
          last = type[i][j];
        }
      }
      if (last <= cur) {
        d[last] += kcopy[i];
      } else {
        no += kcopy[i];
      }
    }
  }
  int cnt = 0;
  for (int i = 1; i <= min(cur, days); i++) {
    cnt += 1;
    if (d[i] != 0) {
      if (cnt > d[i]) {
        cnt -= d[i];
        d[i] = 0;
      } else {
        d[i] -= cnt;
        cnt = 0;
        no += d[i];
      }
    }
  }
  ll rem = cur - min(cur, days);
  cnt += rem;
  if (cnt >= 2 * no * 1LL) {
    return true;
  }
  return false;
}
int main(int argc, const char* argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  vector<int> k(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> k[i];
  }
  vector<int> lastday(n + 1, 0);
  vector<vector<int>> type(n + 1);
  vector<pair<int, int>> q;
  for (int i = 0; i < m; i++) {
    int d, t;
    cin >> d >> t;
    q.push_back({d, t});
    type[t].push_back(d);
    lastday[t] = max(d, lastday[t]);
  }
  for (int i = 0; i <= n; i++) {
    sort(type[i].begin(), type[i].end());
  }
  vector<vector<pair<int, int>>> mp(days + 1);
  for (int i = 0; i < m; i++) {
    int d = q[i].first, t = q[i].second;
    mp[d].push_back({lastday[t], t});
  }
  for (int i = 0; i <= days; i++) {
    sort(mp[i].begin(), mp[i].end());
  }
  ll low = 0, high = 1e9, ans = -1;
  while (low <= high) {
    ll mid = (low + high) / 2;
    if (check(k, mp, mid, type)) {
      ans = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  cout << ans << endl;
  return 0;
}
