#include <bits/stdc++.h>
using namespace std;
const int N = 200000;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<int> k(n);
  vector<vector<int>> offers(N), days(N);
  for (int& i : k) {
    cin >> i;
  }
  for (int i = 0; i < m; ++i) {
    int d, t;
    cin >> d >> t;
    --d;
    --t;
    offers[d].push_back(t);
    days[t].push_back(d);
  }
  for (int i = 0; i < N; ++i) {
    sort(days[i].begin(), days[i].end());
  }
  auto can = [&offers, &days, k, n](int mid) {
    auto kCopy = k;
    int balance = 0;
    for (int i = 0; i < mid; ++i) {
      ++balance;
      if (i < N) {
        for (auto offer : offers[i]) {
          if (kCopy[offer] > 0) {
            auto u = upper_bound(days[offer].begin(), days[offer].end(), i);
            if (u == days[offer].end() || *u >= mid) {
              while (balance > 0 && kCopy[offer] > 0) {
                --balance;
                --kCopy[offer];
              }
            }
          }
        }
      }
    }
    for (int i = 0; i < n; ++i) balance -= 2 * kCopy[i];
    return balance >= 0;
  };
  int lo = 0, hi = 4e5 + 5;
  while (lo + 1 < hi) {
    int mid = (lo + hi) / 2;
    if (can(mid)) {
      hi = mid;
    } else {
      lo = mid;
    }
  }
  for (int i = lo; i <= hi; ++i) {
    if (can(i)) {
      cout << i << '\n';
      return 0;
    }
  }
}
