#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  vector<pair<int, int> > f;
  for (int i = 0; i < m; i++) {
    int d, t;
    cin >> d >> t;
    t--;
    f.push_back({d, t});
  }
  sort(f.begin(), f.end());
  auto check = [&](int days) {
    vector<int> kek = a;
    int last = 0;
    long long have = 0;
    vector<int> lst(m, false);
    vector<int> vis(n, false);
    for (int i = m - 1; i >= 0; i--) {
      if (f[i].first <= days && !vis[f[i].second]) {
        vis[f[i].second] = true;
        lst[i] = true;
      }
    }
    for (int i = 0; i < f.size(); i++) {
      int d = f[i].first, t = f[i].second;
      if (d > days) break;
      have += d - last;
      while (lst[i] && have > 0 && kek[t] > 0) {
        have--;
        kek[t]--;
      }
      last = d;
    }
    have += days - last;
    int total = 0;
    for (int x : kek) total += x;
    return total * 2 <= have;
  };
  int l = 1, r = 4e5 + 7;
  int res = 1e9;
  check(20);
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid)) {
      r = mid - 1;
      res = mid;
    } else {
      l = mid + 1;
    }
  }
  cout << res << '\n';
  return 0;
}
