#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
const int M = 22;
const int mod = 1e9 + 7;
int n, m;
int sum;
int a[N];
int b[N];
vector<int> v[N];
bool can(int x) {
  int res = 0;
  int cnt = x;
  for (int i = 1; i <= n; i++) {
    b[i] = a[i];
  }
  for (int i = x; i >= 1; i--) {
    cnt = min(cnt, i);
    for (auto y : v[i]) {
      if (cnt >= b[y]) {
        res += b[y];
        cnt -= b[y];
        b[y] = 0;
      } else {
        res += cnt;
        b[y] -= cnt;
        cnt = 0;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    b[i] = 0;
  }
  return (sum * 2 - res <= x);
}
void solve1() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
  }
  int l = 0, r = 1e6;
  while (l < r) {
    int m = (l + r) / 2;
    if (!can(m))
      l = m + 1;
    else
      r = m;
  }
  cout << l;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  srand(time(0));
  int cghf = 1;
  while (cghf--) {
    solve1();
  }
}
