#include <bits/stdc++.h>
using namespace std;
int n, m, tot;
int need[400010], tmp[400010], last[400010];
vector<int> vec[400010];
inline bool check(int x) {
  memset(last, 0, sizeof(last));
  memcpy(tmp, need, sizeof(need));
  for (int i = 1; i <= x; i++) {
    for (int j = 0; j < vec[i].size(); j++) {
      int now = vec[i][j];
      last[now] = i;
    }
  }
  int money = 0, cur_ans = 0;
  for (int i = 1; i <= x; i++) {
    money++;
    for (int j = 0; j < vec[i].size(); j++) {
      int now = vec[i][j];
      while (last[now] == i && money && tmp[now])
        tmp[now]--, cur_ans++, money--;
    }
  }
  cur_ans += money / 2;
  if (cur_ans >= tot)
    return true;
  else
    return false;
}
int main() {
  scanf("%d%d", &n, &m);
  int l = 0, r = 0, ans;
  for (int i = 1; i <= n; i++) scanf("%d", &need[i]), tot += need[i];
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    vec[x].push_back(y);
  }
  r = tot * 2;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\n", ans);
  return 0;
}
