#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 9;
int n, m, k[N];
pair<int, int> a[N];
int tmp[N];
bool check(int x) {
  memcpy(tmp, k, sizeof(k));
  int xx = x;
  int used = 0, now = x;
  for (int i = m; i >= 1; i--) {
    if (a[i].first > xx) continue;
    now = min(now, a[i].first);
    if (tmp[a[i].second]) {
      used = min(now, tmp[a[i].second]);
      tmp[a[i].second] -= used;
      x -= used;
      now -= used;
    }
  }
  int sum = 0;
  for (int i = 1; i <= n; i++) sum += tmp[i];
  return x >= 2 * sum;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> k[i];
  for (int i = 1; i <= m; i++) cin >> a[i].first >> a[i].second;
  sort(a + 1, a + m + 1);
  int l = 0, r = N * 2, mid, ans;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  cout << ans << endl;
  return 0;
}
