#include <bits/stdc++.h>
using namespace std;
struct keycompare {
  bool operator()(const pair<long long, long long>& v, const long long& k) {
    return (v.first < k);
  }
  bool operator()(const long long& k, const pair<long long, long long>& v) {
    return (k < v.first);
  }
};
long long mod1 = 998244353, mod2 = 1000000007, limit = 9223372036854775807;
long double pi = 3.1415926535897932;
long long modpow(long long x, long long n, long long m) {
  if (x > m) {
    x %= m;
  }
  if (n == 0) return 1 % m;
  long long u = modpow(x, n / 2, m);
  u = (u * u) % m;
  if (n % 2 == 1) u = (u * x) % m;
  return u;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
bool isprime(long long n) {
  if (n == 2) return true;
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) return false;
  }
  return true;
}
long long power(long long x, long long n) {
  long long x_n = 1;
  for (long long i = 0; i < n; i++) {
    x_n *= x;
  }
  return x_n;
}
vector<long long> v;
set<long long> adj[400005];
set<long long> days[200005];
bool check(long long day) {
  multiset<long long> ms;
  for (auto it : v) {
    ms.insert(it);
  }
  long long burle = 0;
  for (long long i = 1; i <= day; i++) {
    burle++;
    for (auto it : adj[i]) {
      auto it2 = days[it].upper_bound(day);
      if (it2 != days[it].begin()) {
        it2 = prev(it2);
        if (*it2 == i) {
          while (burle > 0 && ms.find(it) != ms.end()) {
            ms.erase(ms.find(it));
            burle--;
          }
        }
      }
    }
  }
  if (2 * ms.size() <= burle) {
    return true;
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m;
  cin >> n >> m;
  for (long long i = 0; i < n; i++) {
    long long temp;
    cin >> temp;
    for (long long j = 0; j < temp; j++) {
      v.push_back(i + 1);
    }
  }
  for (long long i = 0; i < m; i++) {
    long long d, t;
    cin >> d >> t;
    adj[d].insert(t);
    days[t].insert(d);
  }
  long long left = v.size(), right = 2 * v.size();
  while (left < right) {
    long long mid = (left + right) / 2;
    if (check(mid)) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  cout << left;
}
