#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
pair<int, int> p[maxn];
int a[maxn], sum = 0, n, m, ma, mi, mid, sto[maxn];
bool last[maxn];
bool srt(pair<int, int> lhs, pair<int, int> rhs) {
  return lhs.first < rhs.first;
}
int fi() {
  int midd, maa = m - 1, mii = 0;
  while (maa > mii) {
    midd = maa + mii >> 1;
    if (mii == midd) midd++;
    if (p[midd].first <= mid)
      mii = midd;
    else
      maa = midd - 1;
  }
  return mii;
}
bool ch() {
  int k = fi();
  stack<pair<int, int> > disc;
  for (int i = 1; i <= n; i++) {
    sto[i] = 0;
    last[i] = false;
  }
  for (int i = k; i >= 0; i--) {
    if (!(last[p[i].second])) {
      last[p[i].second] = true;
      disc.push(p[i]);
    }
  }
  int cnt = 0, day = 0, bit = 0, x;
  while (!disc.empty()) {
    cnt += disc.top().first - day;
    day = disc.top().first;
    x = min(a[disc.top().second] - sto[disc.top().second], cnt);
    bit += x;
    cnt -= x;
    sto[disc.top().second] += x;
    disc.pop();
  }
  cnt += mid - day;
  if (bit + cnt / 2 >= sum)
    return true;
  else
    return false;
}
int bs() {
  ma = 2 * sum, mi = sum;
  while (ma > mi) {
    mid = ma + mi >> 1;
    if (ch()) {
      ma = mid;
    } else {
      mi = mid + 1;
    }
  }
  return mi;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    sum += a[i];
  }
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &(p[i].first), &(p[i].second));
  }
  sort(p, p + m, srt);
  printf("%d", bs());
}
