#include <bits/stdc++.h>
using namespace std;
int n, m;
int k[200010];
vector<int> p[200010];
bool can(long long d) {
  vector<pair<int, int>> pp;
  for (int i = 0; i < n; i++) {
    if (!k[i]) continue;
    auto it = upper_bound(p[i].begin(), p[i].end(), d);
    if (it == p[i].begin()) continue;
    it--;
    pp.push_back({*it, i});
  }
  sort(pp.begin(), pp.end());
  vector<int> kk;
  for (int i = 0; i < n; i++) kk.push_back(k[i]);
  int lastDay = 0;
  long long money = 0;
  for (int i = 0; i < pp.size(); i++) {
    int curDay = pp[i].first, t = pp[i].second;
    money += curDay - lastDay;
    int amount = min(money, 1LL * kk[t]);
    money -= amount;
    kk[t] -= amount;
    lastDay = curDay;
  }
  money += d - lastDay;
  for (int i = 0; i < n; i++) money -= 2 * kk[i];
  return money >= 0;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d", &k[i]);
  }
  for (int i = 0; i < m; i++) {
    int d, t;
    scanf("%d%d", &d, &t);
    p[t - 1].push_back(d);
  }
  for (int i = 0; i < n; i++) {
    sort(p[i].begin(), p[i].end());
  }
  long long l = 1, r = 1e12;
  while (l + 1 < r) {
    long long mid = l + (r - l) / 2;
    if (can(mid))
      r = mid;
    else
      l = mid;
  }
  printf("%lld", can(l) ? l : r);
}
