#include <bits/stdc++.h>
using namespace std;
ifstream fin;
vector<vector<long long>> primes;
vector<vector<long long>> facts;
vector<long long> factorialNumInverse;
vector<long long> naturalNumInverse;
vector<long long> fact;
long long big = 1e9 + 7;
void InverseofNumber(long long N, long long p = big) {
  naturalNumInverse[0] = naturalNumInverse[1] = 1;
  for (long long i = 2; i <= N; i++)
    naturalNumInverse[i] = naturalNumInverse[p % i] * (p - p / i) % p;
}
void InverseofFactorial(long long N, long long p = big) {
  factorialNumInverse[0] = factorialNumInverse[1] = 1;
  for (long long i = 2; i <= N; i++)
    factorialNumInverse[i] =
        (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p;
}
void factorial(long long N, long long p = big) {
  fact[0] = 1;
  for (long long i = 1; i <= N; i++) {
    fact[i] = (fact[i - 1] * i) % p;
  }
}
void setncr(long long size = 1e5 + 1, long long p = big) {
  factorialNumInverse.resize(size);
  naturalNumInverse.resize(size);
  fact.resize(size);
  InverseofNumber(size, p);
  factorial(size, p);
  InverseofFactorial(size, p);
}
long long ncr(long long N, long long R, long long p = big) {
  long long ans =
      ((fact[N] * factorialNumInverse[R]) % p * factorialNumInverse[N - R]) % p;
  return ans;
}
long long power(long long a, long long b, long long m = big) {
  if (b == 0) return 1;
  long long ans = power(a, b / 2, m);
  if (b % 2 == 0) return ans * ans % m;
  return ((a * ans) % m * ans) % m;
}
long long inverse(long long a, long long m = big) { return power(a, m - 2, m); }
long long ceil(long long a, long long b) { return (a - 1) / b + 1; }
void init_prime(long long size = 1e6 + 1) {
  primes.resize(size);
  for (long long i = 2; i < size; i++) {
    if (primes[i].size() == 0) {
      for (long long j = i; j < size; j += i) primes[j].push_back(i);
    }
  }
}
void init_facts(long long size = 2e5 + 1) {
  facts.resize(size);
  for (long long i = 1; i < size; i++) {
    for (long long j = i; j < size; j += i) facts[j].push_back(i);
  }
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <typename T>
vector<long long> argsort(const vector<T> &v) {
  vector<long long> idx(v.size());
  iota(idx.begin(), idx.end(), 0);
  stable_sort(idx.begin(), idx.end(),
              [&v](long long i1, long long i2) { return v[i1] < v[i2]; });
  return idx;
}
void solve();
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t = 1;
  while (t--) solve();
}
long long n, m;
vector<long long> arr;
vector<vector<long long>> sales;
bool check(long long d) {
  vector<long long> days(n, d + 1);
  long long temp;
  for (long long i = 0; i < n; i++) {
    if (sales[i].size() == 0 || sales[i][0] > d) continue;
    if (sales[i].back() <= d) {
      days[i] = sales[i].back();
      continue;
    }
    temp = upper_bound(sales[i].begin(), sales[i].end(), d) - sales[i].begin();
    days[i] = sales[i][temp - 1];
  }
  vector<vector<long long>> cro(d);
  long long rem = 0;
  for (long long i = 0; i < n; i++) {
    if (days[i] <= d) {
      cro[days[i] - 1].push_back(i);
    } else {
      rem += arr[i];
    }
  }
  long long c = 0;
  for (long long i = 0; i < d; i++) {
    c++;
    for (long long item : cro[i]) {
      temp = min(arr[item], c);
      c -= temp;
      rem += arr[item] - temp;
    }
  }
  return c >= 2 * rem;
}
void solve() {
  cin >> n >> m;
  arr.resize(n);
  sales.resize(n);
  long long sum = 0;
  for (long long i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  long long d, t;
  for (long long i = 0; i < m; i++) {
    cin >> d >> t;
    sales[t - 1].push_back(d);
  }
  for (long long i = 0; i < n; i++) sort(sales[i].begin(), sales[i].end());
  long long l = 0, r = 2 * sum;
  long long mid;
  while (l < r) {
    mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << r << endl;
}
