#include <bits/stdc++.h>
using namespace std;
int n;
int m;
int k[400005];
vector<int> G[400005];
int sum;
int tmp[400005];
bool check(int ans) {
  int cnt = 0;
  int cur = 0x3f3f3f3f;
  for (int i = 1; i <= n; ++i) tmp[i] = k[i];
  for (int i = ans; i >= 1; --i) {
    for (auto t : G[i]) {
      cur = min(cur, i);
      int buys = min(cur, tmp[t]);
      cur -= buys;
      tmp[t] -= buys;
      cnt += buys;
    }
  }
  return ans >= 2 * sum - cnt;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%d", k + i);
  for (int i = 1; i <= n; ++i) sum += k[i];
  for (int i = 1; i <= m; ++i) {
    int d;
    int t;
    scanf("%d %d", &d, &t);
    G[d].push_back(t);
  }
  int ansl = 0;
  int ansr = 2 * sum;
  while (ansl <= ansr) {
    int ansmid = (ansl + ansr) / 2;
    if (check(ansmid))
      ansr = ansmid - 1;
    else
      ansl = ansmid + 1;
  }
  printf("%d\n", ansl);
  return 0;
}
