#include <bits/stdc++.h>
using namespace std;
const int MX = 200000;
const long long mod = 1e9 + 7;
const long long inf = 1e9;
vector<pair<int, int> > sale;
int a[MX + 5], fr[MX + 5];
bool ck(int md, int n) {
  int lo = lower_bound(sale.begin(), sale.end(), make_pair(md + 1, 0)) -
           sale.begin();
  lo--;
  for (int i = 1; i <= n; i++) fr[i] = a[i];
  int cost = md;
  int cnt = 0;
  for (int i = lo; i >= 0; i--) {
    int x = sale[i].first;
    int y = sale[i].second;
    cost = min(cost, x);
    if (fr[y] && cost) {
      int need = min(fr[y], cost);
      fr[y] -= need;
      cost -= need;
      cnt += need;
    }
  }
  cost = cnt;
  for (int i = 1; i <= n; i++) {
    if (fr[i]) cost += 2 * fr[i];
  }
  return cost <= md;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    sale.push_back({x, y});
  }
  sort(sale.begin(), sale.end());
  int res = -1;
  int l, h, md;
  l = 0;
  h = inf;
  while (l <= h) {
    md = (l + h) / 2;
    if (ck(md, n)) {
      res = md;
      h = md - 1;
    } else {
      l = md + 1;
    }
  }
  cout << res << endl;
  return 0;
}
