#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int n, m;
int k[200010];
int rem[200010];
int idx[200010];
vector<int> special[200010];
vector<int> days[200010];
bool pos(const long long M) {
  long long coins = 0;
  memset(idx, 0, sizeof(idx));
  for (int i = 0; i < n; i++) rem[i] = k[i];
  for (int i = 1; i <= min((long long)200000, M); i++) {
    coins++;
    for (int j = 0; j < int(special[i].size()) && coins; j++) {
      int item = special[i][j];
      auto it = upper_bound(days[item].begin(), days[item].end(), i);
      if (it == days[item].end() || *it > M) {
        long long temp = rem[item];
        rem[item] -= min((long long)rem[item], coins);
        coins -= min(temp, coins);
      }
    }
  }
  if (M > 200000) coins += M - 200000;
  for (int i = 0; i < n && coins >= 0; i++) {
    if (rem[i]) coins -= 2 * rem[i];
  }
  return coins >= 0;
}
int main() {
  scanf("%d %d", &n, &m);
  long long sum = 0;
  for (int i = 0; i < n; i++) scanf("%d", &k[i]), sum += k[i];
  long long L = sum, M, R = 2 * sum, res;
  int d, t;
  while (m--) {
    scanf("%d %d", &d, &t);
    special[d].push_back(t - 1), days[t - 1].push_back(d);
  }
  for (int i = 0; i < n; i++) sort(days[i].begin(), days[i].end());
  while (L <= R) {
    M = (L + R) / 2;
    if (pos(M))
      res = M, R = M - 1;
    else
      L = M + 1;
  }
  printf("%I64d\n", res);
  return 0;
}
