#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, a[N], mx[N], b[N];
int x[N], y[N];
vector<int> g[2 * N];
inline bool check(int d) {
  for (int i = 1; i <= n; i++) {
    mx[i] = -1;
    b[i] = a[i];
  }
  for (int i = 0; i <= d; i++) {
    g[i].clear();
  }
  for (int i = 1; i <= m; i++) {
    if (x[i] <= d) {
      mx[y[i]] = max(mx[y[i]], x[i]);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (mx[i] != -1) {
      g[mx[i]].push_back(i);
    }
  }
  int cur = 0;
  for (int i = 1; i <= d; i++) {
    cur += 1;
    for (auto it : g[i]) {
      if (cur >= b[it]) {
        cur -= b[it];
        b[it] = 0;
      } else {
        b[it] -= cur;
        cur = 0;
        break;
      }
    }
  }
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    sum += b[i];
  }
  return sum * 2 <= cur;
}
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 >> a[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> x[i] >> y[i];
  }
  int l = 0, r = 4e5;
  while (r - l > 1) {
    int mid = (r + l) >> 1;
    if (check(mid)) {
      r = mid;
    } else {
      l = mid;
    }
  }
  if (check(l)) {
    cout << l;
  } else {
    cout << r;
  }
}
