#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m, i, sum = 0, money, mi, x, y, j, bal, Sum;
  cin >> n >> m;
  long long arr[200001], temp[200001], l, k, r, mid;
  vector<long long> offer[200001];
  for (i = 1; i <= n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  for (i = 0; i < m; i++) {
    cin >> x >> y;
    offer[x].push_back(y);
  }
  l = 1;
  r = sum * 2;
  while (r >= l) {
    mid = (l + r) / 2;
    for (i = 1; i <= n; i++) temp[i] = arr[i];
    bal = 2 * sum;
    mi = mid;
    for (j = min(mid, (long long)200000); j > 0; j--) {
      mi = min(mi, j);
      for (k = 0; k < offer[j].size(); k++) {
        money = min(mi, temp[offer[j][k]]);
        bal -= money;
        temp[offer[j][k]] -= money;
        mi -= money;
      }
    }
    if (bal <= mid) {
      r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}
