#include <bits/stdc++.h>
using namespace std;
int mod = 998244353;
vector<int> v[200005];
vector<int> onsale[200005];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  int k[200005];
  for (int i = 1; i <= n; i++) {
    scanf("%d", &k[i]);
  }
  for (int i = 1; i <= m; i++) {
    int first, second;
    scanf("%d %d", &first, &second);
    v[second].push_back(first);
  }
  for (int i = 1; i <= n; i++) {
    sort(v[i].begin(), v[i].end());
    v[i].push_back(1e9);
  }
  int Max = 4e5 + 2, Min = 0;
  while (Max > Min + 1) {
    int mid = (Max + Min) / 2;
    for (int i = 1; i <= mid; i++) {
      onsale[i].clear();
    }
    int need[200005];
    int tot = 0;
    for (int i = 1; i <= n; i++) {
      need[i] = k[i];
      tot += k[i];
      if (v[i][0] > mid) {
        continue;
      }
      int first = *(upper_bound(v[i].begin(), v[i].end(), mid) - 1);
      onsale[first].push_back(i);
    }
    int now = 0;
    for (int i = 1; i <= mid; i++) {
      now++;
      for (auto it : onsale[i]) {
        while (need[it] && now) {
          need[it]--;
          now--;
          tot--;
        }
      }
    }
    if (now >= tot * 2) {
      Max = mid;
    } else {
      Min = mid;
    }
  }
  printf("%d\n", Max);
}
