#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7, N = 2e5 + 7;
int n, m, need[N], take[N], buy[2 * N];
pair<int, int> offer[N];
bool ok(int day) {
  memset(take, 0, sizeof take);
  memset(buy, 0, sizeof buy);
  int req = 0;
  for (int i = 1; i <= n; i++) req += need[i] * 2;
  if (day >= req) return true;
  int offer_need = req - day;
  for (int i = m - 1; i >= 0; i--) {
    if (offer[i].first > day) continue;
    if (take[offer[i].second] < need[offer[i].second]) {
      int ttake =
          min(need[offer[i].second] - take[offer[i].second], offer_need);
      offer_need -= ttake;
      take[offer[i].second] += ttake;
      buy[offer[i].first] += ttake;
    }
  }
  if (offer_need) return false;
  int money = 0;
  for (int i = 1; i <= day; i++) {
    money++;
    money -= buy[i];
    if (money < 0) return false;
  }
  return true;
}
signed main() {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);
  ;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> need[i];
  for (int i = 0; i < m; i++) cin >> offer[i].first >> offer[i].second;
  sort(offer, offer + m);
  int l = 0, r = 4e5 + 7;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (ok(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  cout << l << endl;
}
