#include <bits/stdc++.h>
using namespace std;
vector<int> sale_days[400005];
int k[400005];
int last_day[400005];
vector<int> to_buy[400005];
int cnt;
int temp_cnt;
int n, m;
bool can(int day) {
  for (int i = 1; i <= n; i++) {
    if (k[i] == 0) continue;
    for (int j = 0; j < sale_days[i].size(); j++) {
      if (sale_days[i][j] <= day && sale_days[i][j] > last_day[i])
        last_day[i] = sale_days[i][j];
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!k[i]) continue;
    to_buy[last_day[i]].push_back(i);
  }
  int burles = 0;
  for (int i = 1; i <= day; i++) {
    burles++;
    for (int j = 0; j < to_buy[i].size(); j++) {
      int buy = min(burles, k[to_buy[i][j]]);
      temp_cnt -= buy;
      burles -= buy;
    }
  }
  if (burles < temp_cnt * 2)
    return false;
  else
    return true;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> k[i];
    cnt += k[i];
  }
  for (int i = 1; i <= m; i++) {
    int day, type;
    cin >> day >> type;
    sale_days[type].push_back(day);
  }
  int l = 0;
  int r = 400005;
  while (l != r) {
    int mid = (l + r) / 2;
    temp_cnt = cnt;
    for (int i = 0; i < 200005; i++) to_buy[i].clear();
    for (int i = 0; i < 200005; i++) last_day[i] = 0;
    if (can(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  cout << l << endl;
  return 0;
}
