#include <bits/stdc++.h>
using namespace std;
long long tmp, k, arr[1000005], h, brr[1000005], l, r, n, m, mid, x, y, ans,
    sum;
vector<long long> v[1000005];
bool cek(long long x) {
  for (long long i = 1; i <= n; i++) brr[i] = 0;
  long long last = x, need = 0;
  for (long long i = x; i > 0; i--) {
    last = min(last, i);
    for (long long j = 0; j < v[i].size(); j++) {
      h = v[i][j];
      tmp = min(arr[h] - brr[h], last);
      last -= tmp;
      brr[h] += tmp;
      need += tmp;
    }
  }
  return ((sum - need) * 2) <= x - need;
}
signed main() {
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  for (long long i = 1; i <= m; i++) {
    cin >> x >> y;
    v[x].push_back(y);
  }
  l = 1;
  r = sum * 2;
  while (l <= r) {
    long long mid = (l + r) / 2;
    if (cek(mid)) {
      r = mid - 1;
      ans = mid;
    } else {
      l = mid + 1;
    }
  }
  if (ans == 0)
    cout << "-1\n";
  else
    cout << ans << endl;
}
