#include <bits/stdc++.h>
using namespace std;
long long int a[200005];
long long int b[200005];
vector<long long int> day[400005] = {};
int main() {
  long long int n, m, i, sum = 0, sum1 = 0, d, t, l, u, f, g, tot, mid, ans,
                         pos, prev;
  vector<long long int>::iterator it;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    cin >> b[i];
    sum1 += b[i];
  }
  for (i = 1; i <= m; i++) {
    cin >> d >> t;
    day[d].push_back(t);
  }
  l = 1;
  u = 400000;
  while (l <= u) {
    sum = sum1;
    f = 0;
    mid = (l + u) / 2;
    prev = mid;
    for (i = 1; i <= n; i++) a[i] = b[i];
    for (i = mid; i >= 1; i--) {
      prev = min(prev, i);
      for (it = day[i].begin(); it != day[i].end(); it++) {
        g = min(prev, a[*it]);
        sum = sum - g;
        a[*it] -= g;
        f += g;
        prev -= g;
      }
    }
    ans = 2 * sum + f;
    if (ans <= mid) {
      pos = mid;
      u = mid - 1;
    } else
      l = mid + 1;
  }
  cout << pos << endl;
}
