#include <bits/stdc++.h>
using namespace std;
struct node {
  int a, b;
  bool operator<(const node& y) const { return a > y.a; }
};
node nd[1050];
int val[1050];
int tmp[1050];
int n, m;
int judge(int x) {
  for (int i = 0; i <= n; i++) {
    tmp[i] = val[i];
  }
  int pt = x, ans = x, delt = 0, sp = 0, pre = x;
  for (int i = 0; i < m; i++) {
    if (nd[i].a > x) continue;
    if (i != 0 && nd[i].a == nd[i - 1].a) {
      int cnt = 0;
      cnt = min(tmp[nd[i].b], ans);
      ans -= cnt;
      tmp[nd[i].b] -= cnt;
      delt += cnt;
      sp += cnt;
    } else {
      delt = max(0, delt - pre + nd[i].a);
      pre = nd[i].a;
      ans = nd[i].a - delt;
      int cnt = 0;
      cnt = min(tmp[nd[i].b], ans);
      ans -= cnt;
      tmp[nd[i].b] -= cnt;
      delt += cnt;
      sp += cnt;
    }
  }
  int su = 0;
  for (int i = 1; i <= n; i++) {
    su += tmp[i] * 2;
  }
  if (su + sp <= x)
    return 1;
  else
    return 0;
}
int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    memset(nd, 0, sizeof(nd));
    memset(val, 0, sizeof(val));
    int su = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &val[i]);
      su += val[i];
    }
    for (int i = 0; i < m; i++) {
      int x, y;
      scanf("%d%d", &x, &y);
      nd[i].a = x;
      nd[i].b = y;
    }
    sort(nd, nd + m);
    int ub = su * 2, lb = 0;
    int ans = -1;
    while (ub >= lb) {
      int md = (ub + lb) >> 1;
      if (judge(md)) {
        ans = md;
        ub = md - 1;
      } else {
        lb = md + 1;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
