#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
const int MX = 1e9;
int n, m;
int k[N + 2], tmp[N + 2];
bool fou[N + 2];
pair<int, int> p[N + 2];
bool can(int x) {
  memset(tmp, 0, sizeof(tmp));
  memset(fou, 0, sizeof(fou));
  vector<pair<int, int> > v;
  for (int i = m; i >= 1; i--) {
    int d = p[i].first;
    int t = p[i].second;
    if (d > x || fou[t]) continue;
    v.push_back({d, t});
    fou[t] = 1;
  }
  sort(v.begin(), v.end());
  int ls = 0;
  int cur = 0;
  for (auto i : v) {
    int d = i.first;
    int t = i.second;
    cur += d - ls;
    int q = min(k[t] - tmp[t], cur);
    tmp[t] += q;
    cur -= q;
    ls = d;
  }
  cur += x - ls;
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    cnt += k[i] - tmp[i];
  }
  return cur >= 2 * cnt;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> k[i];
  }
  for (int i = 1; i <= m; i++) {
    int d, t;
    cin >> d >> t;
    p[i] = {d, t};
  }
  sort(p + 1, p + m + 1);
  int ans;
  for (int l = 0, r = MX; l <= r;) {
    int mid = (l + r) / 2;
    if (can(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans << '\n';
  return 0;
}
