#include <bits/stdc++.h>
using namespace std;
int n, m;
pair<int, int> offers[200013];
int req[200013];
vector<int> dayOffers[200013 * 2];
vector<int> dayOffersTemp[200013 * 2];
bool seen[200013];
bool check(int d) {
  int totalCost = 0;
  for (int i = 0; i < n; i++) {
    totalCost += req[i] * 2;
  }
  for (int i = 0; i < d; i++) {
    dayOffersTemp[i].clear();
  }
  for (int i = 0; i < n; i++) {
    seen[i] = false;
  }
  for (int i = d - 1; i >= 0; i--) {
    for (int j : dayOffers[i]) {
      if (seen[j]) continue;
      seen[j] = true;
      dayOffersTemp[i].push_back(j);
    }
  }
  int currentBalance = 0;
  for (int i = 0; i < d; i++) {
    currentBalance++;
    for (int j : dayOffersTemp[i]) {
      int bestSaved = min(req[j], currentBalance);
      totalCost -= bestSaved;
      currentBalance -= bestSaved;
    }
  }
  return totalCost <= d;
}
int main() {
  if (fopen("FILENAME.in", "r")) {
    freopen("FILENAME.in", "r", stdin);
    freopen("FILENAME.out", "w", stdout);
  }
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> req[i];
  }
  for (int i = 0; i < m; i++) {
    int d, t;
    cin >> d >> t;
    d--;
    t--;
    offers[i] = make_pair(d, t);
    dayOffers[d].push_back(t);
  }
  sort(offers, offers + m);
  int start = 0;
  int end = 200013 * 2;
  while (start <= end) {
    int mid = (start + end) / 2;
    if (check(mid)) {
      end = mid - 1;
    } else {
      start = mid + 1;
    }
  }
  cout << start << endl;
}
