#include <bits/stdc++.h>
using namespace std;
int mx[500010], n, m, am[500010], rem[500010];
vector<int> lst[500010];
vector<pair<int, int> > of;
bool can(int d) {
  memset(mx, -1, sizeof(mx));
  for (int i = 0, ThxDem = n; i < ThxDem; ++i) rem[i] = am[i];
  for (int i = 0, ThxDem = d; i < ThxDem; ++i) lst[i].clear();
  for (auto x : of)
    if (x.first <= d) mx[x.second] = max(mx[x.second], x.first);
  for (int i = 0, ThxDem = n; i < ThxDem; ++i)
    if (mx[i] >= 0) lst[mx[i]].push_back(i);
  int have = 0;
  for (int i = 1, ThxDem = d + 1; i < ThxDem; ++i) {
    have++;
    for (auto x : lst[i]) {
      int v = min(rem[x], have);
      have -= v;
      rem[x] -= v;
    }
  }
  for (int i = 0, ThxDem = n; i < ThxDem; ++i) have -= 2 * rem[i];
  return have >= 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0, ThxDem = n; i < ThxDem; ++i) cin >> am[i];
  for (int i = 0, ThxDem = m; i < ThxDem; ++i) {
    int x, y;
    cin >> x >> y;
    of.push_back({x, y - 1});
  }
  int l = 1, r = 4e5 + 10;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (can(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  cout << l << endl;
}
