#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m, k, type;
  cin >> n >> m;
  vector<int> a(n);
  for (auto& i : a) cin >> i;
  int sm = accumulate((a).begin(), (a).end(), 0);
  vector<vector<int> > s(n);
  for (int i = 0; i < (int)(m); i++) {
    cin >> k >> type;
    s[type - 1].push_back(k - 1);
  }
  int lo = 0, hi = 2 * sm;
  while (lo < hi) {
    int mid = (lo + hi) / 2;
    vector<int> last(mid);
    for (int type = 0; type < n; type++) {
      int mx = -1;
      for (auto i : s[type]) {
        if (i < mid) {
          mx = max(mx, i);
        }
      }
      if (mx != -1) {
        last[mx] += a[type];
      }
    }
    int paisa = 0, remain = 0;
    for (int i = 0; i < mid; i++) {
      paisa++;
      int x = min(paisa, last[i]);
      paisa -= x;
      remain += x;
    }
    int req = 2 * sm - remain;
    if (req <= mid) {
      hi = mid;
    } else
      lo = mid + 1;
  }
  cout << lo << '\n';
}
