#include <bits/stdc++.h>
#pragma GCC optimize(3, "Ofast", "inline")
using namespace std;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const long long maxn = (long long)3e5 + 5;
const long long mod = 998244353;
const long long inf = 0x3f3f3f3f;
long long T = 1;
long long a[maxn];
vector<long long> d[maxn];
long long n, m;
bool check(long long x) {
  vector<pair<long long, long long> > v;
  long long tmp = 0;
  for (long long i = 1; i <= n; ++i) {
    if (d[i].empty() || d[i][0] > x) {
      tmp += a[i] * 2;
      continue;
    }
    long long nowt =
        upper_bound(d[i].begin(), d[i].end(), x) - d[i].begin() - 1;
    v.push_back(pair<long long, long long>(d[i][nowt], i));
  }
  if (v.empty()) {
    for (long long i = 1; i <= n; ++i) x -= a[i] * 2;
    return x >= 0;
  }
  sort(v.begin(), v.end());
  long long now = v[0].first;
  if (a[v[0].second] > now) {
    x -= (a[v[0].second] - now) * 2;
    now = 0;
  } else
    now -= a[v[0].second];
  for (long long i = 1; i < v.size(); ++i) {
    now += v[i].first - v[i - 1].first;
    if (a[v[i].second] > now) {
      x -= (a[v[i].second] - now) * 2;
      now = 0;
    } else
      now -= a[v[i].second];
  }
  return now + x - v.back().first >= tmp;
}
void solve() {
  cin >> n >> m;
  for (long long i = 1; i <= n; ++i) cin >> a[i];
  for (long long i = 1; i <= m; ++i) {
    long long x, y;
    cin >> x >> y;
    d[y].push_back(x);
  }
  for (long long i = 1; i <= n; ++i) sort(d[i].begin(), d[i].end());
  long long l = 1, r = 1e6, ans = 0;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    if (check(mid))
      r = mid - 1, ans = mid;
    else
      l = mid + 1;
  }
  cout << ans;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  while (T--) solve();
  return 0;
}
