#include <bits/stdc++.h>
using namespace std;
vector<long long> arr;
vector<vector<long long> > offer(2e5 + 5);
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m, inp, typ, sum = 0;
  cin >> n >> m;
  arr.push_back(0);
  for (long long i = 0; i < n; i++) {
    cin >> inp;
    arr.push_back(inp);
    sum += inp;
  }
  for (long long i = 0; i < m; i++) {
    cin >> inp >> typ;
    offer[inp].push_back(typ);
  }
  long long lb = 1, ub = 2 * sum, mid, ans = 0;
  while (lb <= ub) {
    mid = (lb + ub) / 2;
    long long bal = mid, temSum = sum;
    vector<long long> temp = arr;
    long long mi = mid;
    for (long long i = min(mid, (long long)200000); i > 0; i--) {
      if (temSum == 0) break;
      mi = min(mi, i);
      for (long long j = 0; j < offer[i].size(); j++) {
        long long money = min(mi, temp[offer[i][j]]);
        bal -= money;
        temSum -= money;
        temp[offer[i][j]] -= money;
        mi -= money;
      }
    }
    if (bal >= 2 * temSum)
      ub = mid - 1;
    else
      lb = mid + 1;
  }
  cout << lb;
  return 0;
}
