#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
using lld = long double;
const ll oo = 1e15;
const ll MOD = 1000000007;
const ll N = 1e6 + 2;
vector<pair<ll, ll> > offers;
ll req[N];
ll n, m;
bool can(ll day) {
  vector<ll> need;
  need.emplace_back(0);
  for (ll i = 1; i <= n; i++) need.emplace_back(req[i]);
  vector<ll> best(n + 1);
  for (auto [d, t] : offers) {
    if (d > day) continue;
    best[t] = max(best[t], d);
  }
  vector<vector<ll> > op(day + 1);
  for (ll i = 1; i <= n; i++) op[best[i]].emplace_back(i);
  ll cur_money = 0;
  for (ll i = 1; i <= day; i++) {
    cur_money++;
    for (ll j : op[i]) {
      if (cur_money <= need[j]) {
        need[j] -= cur_money;
        cur_money = 0;
        break;
      } else {
        cur_money -= need[j];
        need[j] = 0;
      }
    }
  }
  ll sum = 0;
  for (ll i = 1; i <= n; i++) sum += need[i];
  return sum * 2 <= cur_money;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (ll i = 1; i <= n; ++i) cin >> req[i];
  for (ll i = 0; i < m; i++) {
    ll d, t;
    cin >> d >> t;
    offers.emplace_back(d, t);
  }
  ll h = 1e6;
  ll l = 0;
  while (l + 1 < h) {
    ll mid = (l + h) >> 1;
    if (can(mid))
      h = mid;
    else
      l = mid;
  }
  cout << h << '\n';
  return 0;
}
