#include <bits/stdc++.h>
using namespace std;
void read(long long &a) {
  a = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    a = (a << 1) + (a << 3) + c - '0';
    c = getchar();
  }
}
long long Set(long long n, long long pos) { return n = n | (1LL << pos); }
long long reset(long long n, long long pos) { return n = n & ~(1LL << pos); }
bool check(long long n, long long pos) { return (bool)(n & (1LL << pos)); }
long long msb(long long mask) { return (mask & (-mask)); }
long long cnt_on(long long mask) { return __builtin_popcountll(mask); }
long long mul(long long a, long long b) {
  long long c;
  c = (a % 1000000007 * b % 1000000007) % 1000000007;
  return c;
}
long long add(long long a, long long b) {
  long long c;
  c = (a % 1000000007 + b % 1000000007) % 1000000007;
  return c;
}
long long sub(long long a, long long b) {
  long long c;
  c = ((a % 1000000007 - b % 1000000007) % 1000000007 + 1000000007) %
      1000000007;
  return c;
}
long long power(long long x, long long y) {
  long long res = 1;
  x = x;
  while (y > 0) {
    if (y & 1) res = (res * x);
    y = y >> 1;
    x = (x * x);
  }
  return res;
}
long long exgcd(long long x, long long y, long long &a, long long &b) {
  int flag = 0;
  long long t, la = 1, lb = 0, ra = 0, rb = 1;
  while (x % y) {
    if (flag == 0)
      la -= x / y * ra, lb -= x / y * rb;
    else
      ra -= x / y * la, rb -= x / y * lb;
    t = x, x = y, y = t % y;
    flag = 1 - flag;
  }
  if (flag == 0)
    a = ra, b = rb;
  else
    a = la, b = lb;
  return y;
}
long long n, m;
long long arr[200005], arr2[200005], last[200005];
vector<long long> days;
vector<long long> day[200005];
bool can(long long t) {
  for (long long i = 1; i <= n; i++) arr2[i] = arr[i];
  memset((last), (0), sizeof(last));
  for (long long i = 0; i < days.size(); i++) {
    long long u = days[i];
    if (u > t) break;
    for (long long j = 0; j < day[u].size(); j++) {
      long long now_type = day[u][j];
      last[now_type] = max(last[now_type], u);
    }
  }
  long long prev = 0;
  long long rem = 0;
  for (long long i = 0; i < days.size(); i++) {
    long long u = days[i];
    if (u > t) break;
    rem += (u - prev);
    for (long long j = 0; j < day[u].size(); j++) {
      long long now_type = day[u][j];
      if (last[now_type] > u) continue;
      if (rem >= arr2[now_type]) {
        rem -= arr2[now_type];
        arr2[now_type] = 0;
      } else {
        arr2[now_type] -= rem;
        rem = 0;
      }
      if (rem == 0) break;
    }
    prev = u;
  }
  rem += (t - prev);
  for (long long i = 1; i <= n; i++) {
    rem -= (arr2[i] * 2);
  }
  if (rem >= 0) return 1;
  return 0;
}
int main() {
  scanf("%lld %lld", &n, &m);
  for (long long i = 1; i <= n; i++) scanf("%lld", &arr[i]);
  for (long long i = 1; i <= m; i++) {
    long long d, t;
    scanf("%lld %lld", &d, &t);
    if (arr[t] == 0) continue;
    days.push_back(d);
    day[d].push_back(t);
  }
  sort(days.begin(), days.end());
  days.erase(unique(days.begin(), days.end()), days.end());
  for (long long i = 0; i < days.size(); i++) {
    long long now_day = days[i];
    sort(day[now_day].begin(), day[now_day].end());
    day[now_day].erase(unique(day[now_day].begin(), day[now_day].end()),
                       day[now_day].end());
  }
  long long low = 1, high = 500000, mid, ans = -1;
  while (low <= high) {
    mid = (low + high) / 2;
    if (can(mid)) {
      ans = mid;
      high = mid - 1;
    } else
      low = mid + 1;
  }
  cout << ans << endl;
  return 0;
}
