#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> k;
vector<pair<int, int>> offers;
bool ok(int days) {
  vector<int> last(n, -1);
  for (auto offer : offers)
    if (offer.first <= days)
      last[offer.second] = max(last[offer.second], offer.first);
  vector<vector<int>> last_by_day(days + 1);
  for (int i = 0; i < n; i++)
    if (last[i] != -1) last_by_day[last[i]].push_back(i);
  vector<int> k2(k.begin(), k.end());
  int money = 0;
  for (int i = 1; i <= days; i++) {
    money++;
    for (int t : last_by_day[i]) {
      int hm = min(k2[t], money);
      k2[t] -= hm;
      money -= hm;
    }
  }
  assert(money >= 0);
  int left = 0;
  for (int x : k2) left += x;
  return money >= left * 2;
}
int main() {
  {
    cin >> n >> m;
    k.resize(n);
    for (int i = 0; i < n; i++) cin >> k[i];
    offers.resize(m);
    for (int i = 0; i < m; i++) {
      cin >> offers[i].first >> offers[i].second;
      offers[i].second--;
    }
    int low = 0, high = 2 * (int)(1e5 + 20), ans = 2 * (int)(1e5 + 20);
    while (low <= high) {
      int mid = low + (high - low) / 2;
      if (ok(mid)) {
        ans = min(ans, mid);
        high = mid - 1;
      } else
        low = mid + 1;
    }
    cout << ans << endl;
  }
}
