#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 10;
int K[MAX], Last[MAX], Day[MAX], Type[MAX], N, M, max_ans = 0;
int kenajabe(int type, int tk) {
  int quo = 0, am = K[type];
  if (tk >= am)
    quo = am;
  else
    quo = tk;
  return quo;
}
bool ok(int day) {
  memset(Last, 0, sizeof Last);
  vector<int> adj[day + 5];
  for (int i = 1; i <= M; i++) {
    if (Day[i] <= day) {
      Last[Type[i]] = max(Last[Type[i]], Day[i]);
    }
  }
  for (int i = 1; i <= N; i++) adj[Last[i]].push_back(i);
  int curr = 0, rem = max_ans;
  for (int i = 1; i <= day; i++) {
    curr++;
    for (int j = 0; j < adj[i].size(); j++) {
      int quo = kenajabe(adj[i][j], curr);
      curr -= quo;
      rem -= quo;
    }
  }
  if (curr > 1 && rem) {
    int q = curr / 2;
    curr = curr - 2 * q;
    rem -= q;
  }
  return (rem <= 0);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M;
  for (int i = 1; i <= N; i++) {
    cin >> K[i];
    max_ans += K[i];
  }
  for (int i = 1; i <= M; i++) {
    cin >> Day[i] >> Type[i];
  }
  int lo = 1, hi = 2 * max_ans;
  while (hi - lo > 5) {
    int mid = (hi + lo) / 2;
    if (ok(mid))
      hi = mid;
    else
      lo = mid + 1;
  }
  for (int i = max(1, lo - 10); i <= min(2 * max_ans, hi + 10); i++) {
    if (ok(i)) {
      cout << i << endl;
      return 0;
    }
  }
  return 0;
}
