#include <bits/stdc++.h>
using namespace std;
const int maxn = 500500;
int n, m, a[maxn], lst[maxn], tmp[maxn], sum;
;
struct node {
  int d, t;
} e[maxn];
vector<int> v[maxn];
int can(int x) {
  memset(lst, 0, sizeof(lst));
  memset(tmp, 0, sizeof(tmp));
  int cost = 0, cnt = sum;
  for (int i = 1; i <= x; i++) {
    for (int j = 0; j < (int)v[i].size(); j++) lst[v[i][j]] = i;
  }
  for (int i = 1; i <= x; i++) {
    cost++;
    for (int j = 0; j < (int)v[i].size(); j++)
      while (tmp[v[i][j]] < a[v[i][j]] && cost && lst[v[i][j]] == i)
        --cost, tmp[v[i][j]]++, cnt--;
  }
  return cost >= cnt * 2;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), sum += a[i];
  for (int i = 1; i <= m; i++)
    scanf("%d%d", &e[i].d, &e[i].t), v[e[i].d].push_back(e[i].t);
  int L = 0, R = sum * 2, ans = 0;
  while (L <= R) {
    long long mid = (L + R) >> 1;
    if (can(mid))
      R = mid - 1, ans = mid;
    else
      L = mid + 1;
  }
  cout << ans << endl;
}
