#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 10, oo = 1e9;
int a[N], n, m;
vector<int> g[N];
bool check(int x) {
  vector<int> sales[x + 1], need(N);
  for (int i = 1; i <= n; ++i) {
    int last_sale = -1;
    for (int j : g[i]) {
      if (j <= x) {
        last_sale = max(last_sale, j);
      }
    }
    if (last_sale != -1) {
      sales[last_sale].push_back(i);
    }
    need[i] = a[i];
  }
  int money = 0;
  for (int day = 1; day <= x; ++day) {
    ++money;
    for (int type : sales[day]) {
      if (money >= need[type]) {
        money -= need[type];
        need[type] = 0;
      } else {
        need[type] -= money;
        money = 0;
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (need[i] * 2 > money) return 0;
    money -= need[i] * 2;
    need[i] = 0;
  }
  return 1;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
  }
  for (int i = 1; i <= m; ++i) {
    int day, type;
    scanf("%d%d", &day, &type);
    g[type].push_back(day);
  }
  int l = 1, r = 5e5, ans = oo;
  while (r >= l) {
    int m = l + r >> 1;
    if (check(m)) {
      ans = m;
      r = m - 1;
    } else {
      l = m + 1;
    }
  }
  printf("%d\n", ans);
}
