#include <bits/stdc++.h>
using LL = long long;
using LD = long double;
using namespace std;
vector<std::pair<int, int>> offers;
bool is_possible(vector<int> items, int days) {
  vector<int> last_offer(static_cast<int>((items).size()), -1);
  for (auto& o : offers) {
    if (o.first < days) {
      last_offer[o.second] = max(last_offer[o.second], o.first);
    }
  }
  vector<std::pair<int, int>> sorted_offers;
  for (auto t = decltype(static_cast<int>((items).size())){0};
       t < (static_cast<int>((items).size())); t++) {
    if (last_offer[t] != -1) sorted_offers.push_back({last_offer[t], t});
  }
  sort(std::begin(sorted_offers), std::end(sorted_offers));
  int money = 0;
  auto it = begin(sorted_offers);
  for (auto d = decltype(days){0}; d < (days); d++) {
    money++;
    while (it != end(sorted_offers) && it->first < d) ++it;
    while (it != end(sorted_offers) && it->first == d) {
      if (money > items[it->second])
        money -= items[it->second], items[it->second] = 0;
      else
        items[it->second] -= money, money = 0;
      ++it;
    }
  }
  return 2 * accumulate(std::begin(items), std::end(items), 0) <= money;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<int> items(n);
  for (auto i = decltype(n){0}; i < (n); i++) cin >> items[i];
  offers.resize(m);
  for (auto i = decltype(m){0}; i < (m); i++)
    cin >> offers[i].first >> offers[i].second;
  for (auto i = decltype(m){0}; i < (m); i++)
    offers[i].first--, offers[i].second--;
  int max_days = 2 * accumulate(std::begin(items), std::end(items), 0);
  int l = 0, r = max_days;
  while (l < r) {
    int mid = (l + r) / 2;
    if (!is_possible(items, mid))
      l = mid + 1;
    else
      r = mid;
  }
  cout << l << endl;
}
