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