#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int M = 1e5 + 10;
const int INF = 2147483647;
const int MOD = 1e9 + 7;
int T, n, m;
int sum;
int a[N];
pair<int, int> b[N];
long long gcd(long long x, long long y) { return y == 0 ? x : gcd(y, x % y); }
long long qpow(long long base, long long x) {
  long long res = 1;
  while (x) {
    if (x & 1) res *= base;
    base *= base;
    base %= MOD;
    res %= MOD;
    x >>= 1;
  }
  return res;
}
long long qmul(long long a, long long b, long long mod) {
  long long res = 0;
  while (b) {
    if (b & 1) res = (res + a) % mod;
    a = (a + a) % mod;
    b >>= 1;
  }
  return res;
}
void exgcd(long long a, long long b, long long &x, long long &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return;
  }
  exgcd(b, a % b, y, x);
  y -= (a / b) * x;
}
long long getinv(long long x, long long mod) {
  long long x_0, y_0;
  exgcd(x, mod, x_0, y_0);
  return x_0;
}
bool check(int x) {
  if (sum > x) return false;
  vector<int> day(n + 1, 0);
  for (int i = 1; i <= m; i++) {
    int d = b[i].first, item = b[i].second;
    if (d <= x) {
      day[item] = max(day[item], d);
    }
  }
  vector<vector<int>> tmp(200005);
  for (int i = 1; i <= n; i++) {
    if (day[i]) {
      tmp[day[i]].emplace_back(i);
    }
  }
  vector<int> use(n + 1, 0);
  for (int i = 1; i <= n; i++) use[i] = a[i];
  int cur = 0;
  for (int i = 1; i <= x; i++) {
    ++cur;
    if (i > 200000) continue;
    for (auto &t : tmp[i]) {
      if (cur >= a[t]) {
        cur -= a[t];
        use[t] = 0;
      } else {
        use[t] -= cur;
        cur = 0;
        break;
      }
    }
  }
  int ss = 0;
  for (int i = 1; i <= n; i++) ss += use[i];
  return cur >= ss * 2;
}
void solve() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> a[i], sum += a[i];
  for (int i = 1; i <= m; i++) cin >> b[i].first >> b[i].second;
  int l = 1, r = 1e9;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
  T = 1;
  while (T--) solve();
  return 0;
}
