#include <bits/stdc++.h>
using namespace std;
int N, M, K[200000], D[200000], T[200000], KS;
bool check(int X) {
  vector<int> last(N, -1);
  for (int i = 0; i < M; i++)
    if (D[i] < X) last[T[i]] = max(last[T[i]], D[i]);
  vector<vector<int>> l2i(X);
  for (int i = 0; i < N; i++)
    if (last[i] != -1) l2i[last[i]].push_back(i);
  int buy = 0, now = 0;
  for (int i = 0; i < X; i++) {
    now++;
    for (int j : l2i[i]) {
      int s = min(now, K[j]);
      now -= s;
      buy += s;
    }
  }
  buy += now / 2;
  return (buy >= KS);
}
int main() {
  scanf("%d %d", &N, &M);
  for (int i = 0; i < N; i++) scanf("%d", &K[i]);
  for (int i = 0; i < M; i++) {
    scanf("%d %d", &D[i], &T[i]);
    D[i]--;
    T[i]--;
  }
  KS = accumulate(K, K + N, 0);
  int ok = 2 * KS, ng = KS - 1;
  while (ok - ng > 1) {
    int mid = (ok + ng) / 2;
    (check(mid) ? ok : ng) = mid;
  }
  cout << ok << endl;
  return 0;
}
