#include <bits/stdc++.h>
constexpr static int ___maxN = 200010;
char ch;
template <typename Num>
void read(Num& x) {
  for (ch = getchar(); !isdigit(ch); ch = getchar())
    ;
  for (x = 0; isdigit(ch); x = x * 10 + (ch ^ 48), ch = getchar())
    ;
}
template <typename Num, typename... List>
void read(Num& x, List&... y) {
  read(x), read(y...);
}
struct Data {
  int d, t;
  Data(int d = 0, int t = 0) : d(d), t(t) {}
};
int cmp(const Data& x, const Data& y) { return x.d > y.d; }
int n, m, tot;
int k[___maxN], b[___maxN];
Data a[___maxN];
int judge(long long x) {
  memcpy(b, k, sizeof b);
  int calc = 0, curMoney = x;
  for (int i = 1; i <= m && curMoney; ++i)
    if (a[i].d <= x) {
      calc += std::min(b[a[i].t], std::min(curMoney, a[i].d));
      int lMoney = curMoney;
      curMoney = std::max(0, std::min(curMoney, a[i].d) - b[a[i].t]);
      b[a[i].t] = std::max(b[a[i].t] - std::min(lMoney, a[i].d), 0);
    }
  for (int i = 1; i <= n; ++i) calc += 2 * b[i];
  return calc <= x;
}
int main() {
  read(n, m);
  for (int i = 1; i <= n; ++i) read(k[i]), tot += k[i];
  for (int i = 1; i <= m; ++i) read(a[i].d, a[i].t);
  std::sort(a + 1, a + m + 1, cmp);
  int l = 1, r = tot * 2;
  while (l <= r) {
    long long mid = l + r >> 1;
    if (judge(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  std::cout << l << std::endl;
  return 0;
}
